Line data Source code
1 : /*
2 : * File: TestElasticNet.cpp
3 : * Author: Pantelis Sopasakis
4 : *
5 : * Created on Oct 29, 2015, 6:51:55 PM
6 : *
7 : * ForBES is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU Lesser General Public License as published by
9 : * the Free Software Foundation, either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * ForBES is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU Lesser General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU Lesser General Public License
18 : * along with ForBES. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "TestElasticNet.h"
22 : #include "ElasticNet.h"
23 : #include "ForBES.h"
24 :
25 :
26 1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestElasticNet);
27 :
28 3 : TestElasticNet::TestElasticNet() {
29 3 : }
30 :
31 6 : TestElasticNet::~TestElasticNet() {
32 6 : }
33 :
34 3 : void TestElasticNet::setUp() {
35 3 : }
36 :
37 3 : void TestElasticNet::tearDown() {
38 3 : }
39 :
40 1 : void TestElasticNet::testCall() {
41 1 : Function * elastic = new ElasticNet(2.5, 1.3);
42 1 : const size_t n = 9;
43 1 : double xdata[n] = {-1.0, -3.0, 7.5, 2.0, -1.0, -1.0, 5.0, 2.0, -5.0};
44 :
45 1 : Matrix x(n, 1, xdata);
46 :
47 : double f;
48 1 : const double f_expected = 193.562500;
49 1 : const double tol = 1e-12;
50 1 : _ASSERT(elastic->category().defines_f());
51 1 : int status = elastic->call(x, f);
52 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
53 1 : _ASSERT_NUM_EQ(f_expected, f, tol);
54 1 : delete elastic;
55 1 : }
56 :
57 1 : void TestElasticNet::testCallProx() {
58 1 : Function * elastic = new ElasticNet(2.5, 1.3);
59 1 : const size_t n = 9;
60 1 : double xdata[n] = {-1.0, -3.0, 7.5, 2.0, -1.0, -1.0, 5.0, 2.0, -5.0};
61 1 : const double gamma = 1.6;
62 1 : const double prox_expected_data[n] = {0.0, -0.1840, 1.0840, 0.0, 0.0, 0.0, 0.5840, 0.0, -0.5840};
63 :
64 1 : Matrix x(n, 1, xdata);
65 2 : Matrix prox_expected(n, 1, prox_expected_data);
66 2 : Matrix prox(n, 1);
67 :
68 : double f_at_prox;
69 1 : const double f_at_prox_expected = 5.5305800;
70 1 : const double tol = 1e-12;
71 1 : _ASSERT(elastic->category().defines_prox());
72 1 : int status = elastic->callProx(x, gamma, prox, f_at_prox);
73 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
74 1 : _ASSERT_NUM_EQ(f_at_prox_expected, f_at_prox, tol);
75 1 : _ASSERT_EQ(prox_expected, prox);
76 :
77 1 : status = elastic->callProx(x, gamma, prox);
78 1 : _ASSERT_EQ(prox_expected, prox);
79 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
80 :
81 2 : delete elastic;
82 1 : }
83 :
84 1 : void TestElasticNet::testOther() {
85 1 : Function * elastic = new ElasticNet(2.5, 1.3);
86 1 : Matrix x;
87 2 : Matrix grad;
88 : double f_star;
89 1 : _ASSERT_NOT(elastic->category().defines_conjugate());
90 1 : int status = elastic->callConj(x, f_star);
91 1 : _ASSERT_EQ(ForBESUtils::STATUS_UNDEFINED_FUNCTION, status);
92 :
93 1 : _ASSERT_NOT(elastic->category().defines_conjugate_grad());
94 1 : status = elastic->callConj(x, f_star, grad);
95 1 : _ASSERT_EQ(ForBESUtils::STATUS_UNDEFINED_FUNCTION, status);
96 2 : delete elastic;
97 4 : }
|