MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
ArtificialLandscapesTests.cpp
Go to the documentation of this file.
1 
26 
27 
33  (*x)[0] = 0.0;
34  ASSERT_LE(std::abs(fun.calculateValue(x) - 0.0), eps);
35  (*x)[0] = 1.0;
36  ASSERT_LE(std::abs(fun.calculateValue(x) - 1.0), eps);
37  (*x)[0] = 2.0;
38  ASSERT_LE(std::abs(fun.calculateValue(x) - 4.0), eps);
39 }
40 
41 
47  (*x)[0] = 0.0;
48  ASSERT_LE(std::abs((*fun.calculateGradient(x))[0] - 0.0), eps);
49  (*x)[0] = 1.0;
50  ASSERT_LE(std::abs((*fun.calculateGradient(x))[0] - 2.0), eps);
51  (*x)[0] = 2.0;
52  ASSERT_LE(std::abs((*fun.calculateGradient(x))[0] - 4.0), eps);
53 }
54 
55 
60 TEST(Sphere3DLandscape, Gradient) {
62  mic::types::MatrixPtr<double> x = MAKE_MATRIX_PTR(double, 3,1);
63  mic::types::MatrixPtr<double> dx;
65  double eps = 1e-10;
66 
67  // 0.0
68  for (size_t i=0; i<3; i++)
69  (*x)[i] = 0.0;
70  dx = fun.calculateGradient(x);
71  for (size_t i=0; i<3; i++)
72  ASSERT_LE(std::abs((*dx)[i] - 0.0), eps);
73 
74  // 2.0
75  for (size_t i=0; i<3; i++)
76  (*x)[i] = 2.0;
77  dx = fun.calculateGradient(x);
78  for (size_t i=0; i<3; i++)
79  ASSERT_LE(std::abs((*dx)[i] - 4.0), eps);
80 
81  // 0,1,2
82  for (size_t i=0; i<3; i++)
83  (*x)[i] = i;
84  dx = fun.calculateGradient(x);
85  for (size_t i=0; i<3; i++)
86  ASSERT_LE(std::abs((*dx)[i] - 2*i), eps);
87 }
88 
89 
95  for (size_t i=0; i<20; i++)
96  (*x)[i] = 0.0;
97  ASSERT_LE(std::abs(fun.calculateValue(x) - 0.0), eps);
98  for (size_t i=0; i<20; i++)
99  (*x)[i] = 1.0;
100  ASSERT_LE(std::abs(fun.calculateValue(x) - 20.0), eps);
101  for (size_t i=0; i<20; i++)
102  (*x)[i] = 2.0;
103  ASSERT_LE(std::abs(fun.calculateValue(x) - 80.0), eps);
104 
105  for (size_t i=0; i<20; i++)
106  (*x)[i] = i;
107  ASSERT_LE(std::abs(fun.calculateValue(x) - 2470.0), eps);
108 }
109 
110 
116  mic::types::MatrixPtr<double> dx;
117 
118  // 0.0
119  for (size_t i=0; i<20; i++)
120  (*x)[i] = 0.0;
121  dx = fun.calculateGradient(x);
122  for (size_t i=0; i<20; i++)
123  ASSERT_LE(std::abs((*dx)[i] - 0.0), eps);
124 
125  // 2.0
126  for (size_t i=0; i<20; i++)
127  (*x)[i] = 2.0;
128  dx = fun.calculateGradient(x);
129  for (size_t i=0; i<20; i++)
130  ASSERT_LE(std::abs((*dx)[i] - 4.0), eps);
131 
132  // 0,1,2,...
133  for (size_t i=0; i<20; i++)
134  (*x)[i] = i;
135  dx = fun.calculateGradient(x);
136  for (size_t i=0; i<3; i++)
137  ASSERT_LE(std::abs((*dx)[i] - 2*i), eps);
138 }
139 
144 TEST_F(Beale2DLandscape, GradientInMin) {
145  (*x)[0] = 3.0;
146  (*x)[1] = 0.5;
147  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
148  ASSERT_LE(std::abs((*dx)[0]), eps);
149  ASSERT_LE(std::abs((*dx)[1]), eps);
150 }
151 
152 
158  (*x)[0] = 0.0;
159  (*x)[1] = 0.0;
160  ASSERT_LE(std::abs(fun.calculateValue(x) - 1.0), eps);
161  (*x)[0] = 1.0;
162  (*x)[1] = 1.0;
163  ASSERT_LE(std::abs(fun.calculateValue(x) - 0.0), eps);
164  (*x)[0] = 2.0;
165  (*x)[1] = 2.0;
166  ASSERT_LE(std::abs(fun.calculateValue(x) - 401.0), eps);
167 }
168 
173 TEST_F(Rosenbrock2DLandscape, GradientInMin) {
174  (*x)[0] = 1;
175  (*x)[1] = 1;
176  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
177  ASSERT_LE(std::abs((*dx)[0]), eps);
178  ASSERT_LE(std::abs((*dx)[1]), eps);
179 }
180 
186  (*x)[0] = 2;
187  (*x)[1] = 0;
188  mic::types::MatrixPtr<double> dx = fun.calculateGradient(x);
189  ASSERT_LE(std::abs((*dx)[0] - 3202.0), eps);
190  ASSERT_LE(std::abs((*dx)[1] + 800.0), eps);
191  (*x)[0] = 2;
192  (*x)[1] = 2;
193  dx = fun.calculateGradient(x);
194  ASSERT_LE(std::abs((*dx)[0] - 1602.0), eps);
195  ASSERT_LE(std::abs((*dx)[1] + 400.0), eps);
196 }
197 
198 
199 
200 int main(int argc, char **argv) {
201  testing::InitGoogleTest(&argc, argv);
202  return RUN_ALL_TESTS();
203 }
204 
205 
Test fixture - artificial landscape - sphere function 20D (square function).
mic::types::MatrixPtr< eT > calculateGradient(mic::types::MatrixPtr< eT > x_)
Test fixture - artificial landscape - Rosenbrock function 2D.
int main(int argc, char **argv)
Test fixture - artificial landscape - sphere function 1D (square function).
TEST_F(Sphere1DLandscape, Value)
TEST(Sphere3DLandscape, Gradient)
Test fixture - artificial landscape - Beale's function 2D.