MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
ArtificialLandscapes.hpp
Go to the documentation of this file.
1 
25 #ifndef ARTIFICIALLANDSCAPES_HPP_
26 #define ARTIFICIALLANDSCAPES_HPP_
27 
28 #include <types/MatrixTypes.hpp>
29 
30 namespace mic {
31 namespace neural_nets {
32 namespace optimization {
33 namespace artificial_landscapes {
34 
39 template <typename eT=float>
41 public:
43  DifferentiableFunction (size_t dims_) : dims(dims_) {
44  assert (dims > 0);
45  }
46 
48  virtual ~DifferentiableFunction () { }
49 
50  // Abstract method responsible for calculation of the function value.
51  virtual eT calculateValue(mic::types::MatrixPtr<eT> x_) = 0;
52 
53  // Abstract method responsible for calculation of the a gradient in a given point.
54  virtual mic::types::MatrixPtr<eT> calculateGradient(mic::types::MatrixPtr<eT> x_) = 0;
55 
57  mic::types::MatrixPtr<eT> minArguments () { return min_arguments; }
58 
60  eT minValue() { return min_value; }
61 
62 protected:
64  size_t dims;
65 
67  mic::types::MatrixPtr<eT> min_arguments;
68 
71 };
72 
73 
74 
79 template <typename eT=float>
81 public:
82 
84  SphereFunction(size_t dims_) : DifferentiableFunction<eT>(dims_) {
85  // Set minimum.
86  this->min_arguments = MAKE_MATRIX_PTR(eT, this->dims, 1);
87  this->min_arguments->zeros();
88  this->min_value = 0;
89  }
90 
94  eT calculateValue(mic::types::MatrixPtr<eT> x_) {
95  assert((size_t)x_->size() == this->dims);
96  // Calculate sum of x^2.
97  eT val = 0;
98  for (size_t i=0; i<this->dims; i++)
99  val += (*x_)[i] * (*x_)[i];
100  return val;
101  }
102 
106  mic::types::MatrixPtr<eT> calculateGradient(mic::types::MatrixPtr<eT> x_) {
107  assert((size_t)x_->size() == this->dims);
108 
109  // Calculate gradients.
110  mic::types::MatrixPtr<eT> dx = MAKE_MATRIX_PTR(eT, this->dims, 1);
111  for (size_t i=0; i<this->dims; i++)
112  (*dx)[i] = 2 * (*x_)[i];
113 
114  return dx;
115  }
116 };
117 
118 
123 template <typename eT=float>
125 public:
126 
129  // Set minimum.
130  this->min_arguments = MAKE_MATRIX_PTR(eT, this->dims, 1);
131  (*this->min_arguments)[0] = 3;
132  (*this->min_arguments)[1] = 0.5;
133  this->min_value = 0.0;
134  }
135 
139  eT calculateValue(mic::types::MatrixPtr<eT> x_) {
140  assert((size_t)x_->size() == this->dims);
141 
142  // Calculate value.
143  eT x = (*x_)[0];
144  eT y = (*x_)[1];
145 
146  eT a = (1.5 - x + x*y);
147  eT b = (2.25 - x + x * y * y);
148  eT c = (2.625 - x + x * y * y * y);
149  return a*a + b*b + c*c;
150  }
151 
155  mic::types::MatrixPtr<eT> calculateGradient(mic::types::MatrixPtr<eT> x_) {
156  assert((size_t)x_->size() == this->dims);
157 
158  mic::types::MatrixPtr<double> dx = MAKE_MATRIX_PTR(eT, this->dims, 1);
159 
160  // Calculate gradients.
161  eT x = (*x_)[0];
162  eT y = (*x_)[1];
163 
164  eT ax = 2*(1.5 - x + x*y) * (-1 + y);
165  eT bx = 2*(2.25 - x + x * y * y) * ( - 1 + y * y);
166  eT cx = 2*(2.625 - x + x * y * y * y) * ( - 1 + y * y * y);
167 
168  eT ay = 2*(1.5 - x + x*y) * (x);
169  eT by = 2*(2.25 - x + x * y * y) * (2*x*y);
170  eT cy = 2*(2.625 - x + x * y * y * y) * (3*x*y*y);
171 
172  (*dx)[0] = ax + bx +cx;
173  (*dx)[1] = ay + by +cy;
174 
175  return dx;
176  }
177 };
178 
179 
180 
181 
186 template <typename eT=float>
188 public:
189 
191  Rosenbrock2DFunction(eT a_ = 1, eT b_ = 100) : DifferentiableFunction<eT>(2), a(a_), b(b_) {
192  // Set minimum.
193  this->min_arguments = MAKE_MATRIX_PTR(eT, this->dims, 1);
194  (*this->min_arguments)[0] = a;
195  (*this->min_arguments)[1] = a*a;
196  this->min_value = 0.0;
197  }
198 
202  eT calculateValue(mic::types::MatrixPtr<eT> x_) {
203  assert((size_t)x_->size() == this->dims);
204 
205  // Calculate value.
206  eT x = (*x_)[0];
207  eT y = (*x_)[1];
208 
209  eT p1 = (a - x) * (a - x);
210  eT p2 = b * (y - x * x) * (y - x * x);
211 // std::cout << "p1 = " << p1 << " p2 = " << p2 << std::endl;
212  return p1 + p2;
213  }
214 
218  mic::types::MatrixPtr<eT> calculateGradient(mic::types::MatrixPtr<eT> x_) {
219  assert((size_t)x_->size() == this->dims);
220 
221  mic::types::MatrixPtr<eT> dx = MAKE_MATRIX_PTR(eT, this->dims, 1);
222 
223  // Calculate gradients.
224  eT x = (*x_)[0];
225  eT y = (*x_)[1];
226 
227  eT p1x = -2 * (a - x);
228  eT p2x = 2 * b * (y - x * x) * (-2 * x) ;
229 // std::cout << "p1x = " << p1x << " p2x = " << p2x << std::endl;
230 
231  eT p1y = 0;
232  eT p2y = 2 * b * (y - x * x) ;
233 // std::cout << "p1y = " << p1y << " p2y = " << p2y << std::endl;
234 
235  (*dx)[0] = p1x + p2x;
236  (*dx)[1] = p1y + p2y;
237 
238  return dx;
239  }
240 
241 private:
243  eT a, b;
244 };
245 
246 
247 } //: artificial_landscapes
248 } //: optimization
249 } //: neural_nets
250 } //: mic
251 
252 
253 #endif /* ARTIFICIALLANDSCAPES_HPP_ */
mic::types::MatrixPtr< eT > min_arguments
vector of arguments for which the function has a minimum.
mic::types::MatrixPtr< eT > calculateGradient(mic::types::MatrixPtr< eT > x_)
A sphere function - square function generalized to n dimensions.
mic::types::MatrixPtr< eT > calculateGradient(mic::types::MatrixPtr< eT > x_)
mic::types::MatrixPtr< eT > calculateGradient(mic::types::MatrixPtr< eT > x_)
DifferentiableFunction(size_t dims_)
Constructor. Asserts whether dimensions must be > 0.
Abstract class representing interface to a differentiable function.
virtual mic::types::MatrixPtr< eT > calculateGradient(mic::types::MatrixPtr< eT > x_)=0
mic::types::MatrixPtr< eT > minArguments()
Returns the vector of arguments being the function minimum.