Unfit  3.1.1
Data fitting and optimization software
LevenbergMarquardtTestFunctions.hpp
1 // Unfit: Data fitting and optimization software
2 //
3 // Copyright (C) 2012- Dr Martin Buist & Dr Alberto Corrias
4 // Contacts: martin.buist _at_ nus.edu.sg; alberto _at_ nus.edu.sg
5 //
6 // See the 'Contributors' file for a list of those who have contributed
7 // to this work.
8 //
9 // This program is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 #ifndef UNFIT_UNITTESTS_LEVENBERGMARQUARDTTESTFUNCTIONS_HPP_
23 #define UNFIT_UNITTESTS_LEVENBERGMARQUARDTTESTFUNCTIONS_HPP_
24 
25 #include <limits>
26 #include <vector>
27 #include "GenericCostFunction.hpp"
28 
29 namespace Unfit
30 {
31 namespace Examples
32 {
42 {
43  public:
59  std::vector<double> operator()(const std::vector<double> &variable)
60  {
61  std::vector<double> result {0};
62  result[0] = variable[0]*variable[0] + 2*variable[0] + 1;
63  return result;
64  }
65 };
66 
76 {
77  public:
93  std::vector<double> operator()(const std::vector<double> &variable)
94  {
95  std::vector<double> result(3, 0.0);
96  result[0] = variable[0]*variable[0]*variable[0]*variable[0];
97  result[1] = 2*variable[0]*variable[0] * variable[1]*variable[1];
98  result[2] = variable[1]*variable[1]*variable[1]*variable[1];
99  return result;
100  }
101 };
102 
112 {
113  public:
129  std::vector<double> operator()(const std::vector<double> &variable)
130  {
131  std::vector<double> result(2, 0.0);
132  result[0] = (variable[0]-1)*(variable[0]-1);
133  result[1] = (variable[1]-1)*(variable[1]-1);
134  return result;
135  }
136 };
137 
147 {
148  public:
164  std::vector<double> operator()(const std::vector<double> &variable)
165  {
166  std::vector<double> result {0};
167  result[0] = 4*variable[0]*variable[0]*variable[0]*variable[0];
168  return result;
169  }
170 };
171 
177 {
178  public:
186  std::vector<double> operator()(const std::vector<double> &variable)
187  {
188  std::vector<double> residuals {0};
189  if (fabs(variable[0]) < 0.1) {
190  residuals[0] = std::numeric_limits<double>::quiet_NaN();
191  }
192  else {
193  residuals[0] = variable[0]*variable[0];
194  }
195  return residuals;
196  }
197 };
198 
199 /* The equations below are commented out because LM cannot find minimum that
200  is negative
201  */
202 
203 // /**
204 // * \brief Equation5 is defined as x^2 + 2y + 1
205 // *
206 // * Number of dimensions = 2
207 // * The global minimum is: 0 at (-1)T
208 // * Initial guess: ???
209 // *
210 // */
211 // class Equation5 : public GenericCostFunction
212 // {
213 // public:
214 // /**
215 // * We overload the operator as is required in GenericCostFunction to
216 // * calculate the cost of the function.
217 // *
218 // * Behaviour:
219 // * cost = x^2 + 2y + 1
220 // *
221 // * Intended use :
222 // * Equation5 Func;
223 // * cost = Func(const std::vector<double> variable);
224 // *
225 // * Parameters:
226 // * \param variable (input) vector containing coordinates of x and y
227 // * \return cost
228 // */
229 // std::vector<double> operator()(const std::vector<double> &variable)
230 // {
231 // std::vector<double> result {0};
232 // result[0] = variable[0]*variable[0] + 2*variable[1] + 1;
233 // return result;
234 // // doesnt work as the cost here is -ve inf for x or y
235 // // since LM will square it, it will find min at (0.0, 0.0) instead
236 // }
237 // };
238 //
239 // /**
240 // * \brief Equation6 is defined as x^3 + y^3
241 // *
242 // * Number of dimensions = 2
243 // * The global minimum is: negative infinity
244 // * Initial guess: ???
245 // *
246 // */
247 // class Equation6 : public GenericCostFunction
248 // {
249 // public:
250 // /**
251 // * We overload the operator as is required in GenericCostFunction to
252 // * calculate the cost of the function.
253 // *
254 // * Behaviour:
255 // * cost = x^3 + y^3
256 // *
257 // * Intended use :
258 // * Equation6 Func;
259 // * cost = Func(const std::vector<double> variable);
260 // *
261 // * Parameters:
262 // * \param variable (input) vector containing coordinates of x and y
263 // * \return cost
264 // */
265 // std::vector <double> operator()(const std::vector <double> &variable)
266 // {
267 // std::vector <double> result {0};
268 // result[0] = variable[0]*variable[0]*variable[0] + variable[1]*variable[1]*
269 // variable[1];
270 // return result;
271 // }
272 // };
273 //
274 // /**
275 // * \brief Equation7 is defined as x^3 + y^3 - 2
276 // *
277 // * Number of dimensions = 2
278 // * The global minimum is: negative infinity
279 // * Initial guess: ???
280 // *
281 // */
282 // class Equation7 : public GenericCostFunction
283 // {
284 // public:
285 // /**
286 // * We overload the operator as is required in GenericCostFunction to
287 // * calculate the cost of the function.
288 // *
289 // * Behaviour:
290 // * cost = x^3 + y^3 - 2
291 // *
292 // * Intended use :
293 // * Equation7 Func;
294 // * cost = Func(const std::vector<double> variable);
295 // *
296 // * Parameters:
297 // * \param variable (input) vector containing coordinates of x and y
298 // * \return cost
299 // */
300 // std::vector <double> operator()(const std::vector <double> &variable)
301 // {
302 // std::vector <double> result {0};
303 // result[0] = variable[0]*variable[0]*variable[0] + variable[1]*variable[1]*
304 // variable[1] - 2;
305 // return result;
306 // }
307 // };
308 //
309 // /**
310 // * \brief Equation8 is defined as 2*x^2 + 3*y + z
311 // *
312 // * Number of dimensions = 3
313 // * The global minimum is: 0 at (0)T
314 // * Initial guess: ???
315 // *
316 // */
317 // class Equation8 : public GenericCostFunction
318 // {
319 // public:
320 // /**
321 // * We overload the operator as is required in GenericCostFunction to
322 // * calculate the cost of the function.
323 // *
324 // * Behaviour:
325 // * cost = 2*x^2 + 3*y + z
326 // *
327 // * Intended use :
328 // * Equation8 Func;
329 // * cost = Func(const std::vector<double> variable);
330 // *
331 // * Parameters:
332 // * \param variable (input) vector containing coordinates of x, y and z
333 // * \return cost
334 // */
335 // std::vector<double> operator()(const std::vector<double> &variable)
336 // {
337 // std::vector<double> result(3, 0.0);
338 // result[0] = 2*variable[0]*variable[0];
339 // result[1] = 3*variable[1];
340 // result[2] = variable[2];
341 // return result;
342 // }
343 // };
344 //
345 // /**
346 // * \brief Equation9 is defined as x*y
347 // *
348 // * Number of dimensions = 2
349 // * The global minimum is: ???
350 // * Initial guess: (10, 10)
351 // *
352 // */
353 // class Equation9 : public GenericCostFunction
354 // {
355 // public:
356 // /**
357 // * We overload the operator as is required in GenericCostFunction to
358 // * calculate the cost of the function.
359 // *
360 // * Behaviour:
361 // * cost = x*y
362 // *
363 // * Intended use :
364 // * Equation9 Func;
365 // * cost = Func(const std::vector<double> variable);
366 // *
367 // * Parameters:
368 // * \param variable (input) vector containing coordinates of x and y
369 // * \return cost
370 // */
371 // std::vector<double> operator()(const std::vector<double> &variable)
372 // {
373 // std::vector<double> result(2, 0.0);
374 // result[0] = variable[0];
375 // result[1] = variable[1];
376 // return result;
377 // // doesnt work as the cost here is -ve inf for x or y
378 // // since LM will square it, it will find min at (0.0, 0.0) instead
379 // }
380 // };
381 
382 } // namespace Examples
383 } // namespace Unfit
384 
385 #endif
std::vector< double > operator()(const std::vector< double > &variable)
Definition: LevenbergMarquardtTestFunctions.hpp:129
std::vector< double > operator()(const std::vector< double > &variable)
Definition: LevenbergMarquardtTestFunctions.hpp:186
Definition: Bounds.hpp:27
std::vector< double > operator()(const std::vector< double > &variable)
Definition: LevenbergMarquardtTestFunctions.hpp:93
Definition: LevenbergMarquardtTestFunctions.hpp:176
Definition: GenericCostFunction.hpp:36
std::vector< double > operator()(const std::vector< double > &variable)
Definition: LevenbergMarquardtTestFunctions.hpp:164
std::vector< double > operator()(const std::vector< double > &variable)
Definition: LevenbergMarquardtTestFunctions.hpp:59
Equation3 is defined as (x - 1)^2 + (y - 1)^2.
Definition: LevenbergMarquardtTestFunctions.hpp:111
Equation1 is defined as x^2 + 2x + 1.
Definition: LevenbergMarquardtTestFunctions.hpp:41
Equation4 is defined as 4x^4.
Definition: LevenbergMarquardtTestFunctions.hpp:146
Equation2 is defined as x^4 + 2x^2*y^2 + y^4.
Definition: LevenbergMarquardtTestFunctions.hpp:75