Unfit  3.1.1
Data fitting and optimization software
NelderMeadTestFunctions.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_NELDERMEADTESTFUNCTIONS_HPP_
23 #define UNFIT_UNITTESTS_NELDERMEADTESTFUNCTIONS_HPP_
24 
25 #include <cmath>
26 #include <limits>
27 #include <vector>
28 #include "GenericCostFunction.hpp"
29 
30 namespace Unfit
31 {
32 namespace UnitTests
33 {
69 {
70  public:
89  std::vector<double> operator()(const std::vector<double> &x)
90  {
91  std::vector<double> ret {0};
92  if (x[0] > 3.0) {
93  ret[0] = sqrt(fabs(x[0]));
94  }
95  else {
96  ret[0] = std::numeric_limits<double>::infinity();
97  }
98  return ret;
99  }
100 };
101 
107 {
108  public:
127  std::vector<double> operator()(const std::vector<double> &x)
128  {
129  std::vector<double> ret {0};
130  if (x[0] > 1.0) {
131  ret[0] = x[0];
132  }
133  else if (x[0] < -1.0) {
134  ret[0] = x[0];
135  }
136  else {
137  ret[0] = std::numeric_limits<double>::infinity();
138  }
139  return ret;
140  }
141 };
142 
147 {
148  public:
160  std::vector<double> operator()(const std::vector<double> &x)
161  {
162  std::vector<double> f_i(3, 0.0);
163  if ((x[0] < 1.26 && x[0] > 1.24) && (x[1] < 0.51 && x[1] > 0.49)) {
164  f_i[0] = std::numeric_limits<double>::infinity();
165  f_i[1] = std::numeric_limits<double>::infinity();
166  f_i[2] = std::numeric_limits<double>::infinity();
167  }
168  else if (x[0] < 0.9 || x[0] > 2.1 || x[1] < 0.9 || x[1] > 2.1) {
169  f_i[0] = 3.5;
170  f_i[1] = 3.5;
171  f_i[2] = 3.5;
172  }
173  else {
174  f_i[0] = x[0] + x[1];
175  f_i[1] = x[0] + x[1];
176  f_i[2] = x[0] + x[1];
177  }
178  return f_i;
179  }
180 };
181 
199 {
200  public:
219  std::vector<double> operator()(const std::vector<double> &x)
220  {
221  std::vector<double> ret {0};
222  ret[0] = sqrt(fabs(x[0]*x[0] - 4*x[0] + x[1]*x[1] - x[1] - x[0]*x[1]));
223  return ret;
224  }
225 };
226 
227 // /**
228 // * The Sample function 2 is defined as
229 // *
230 // * (y^2 + 4xy +x^3 - 2x).
231 // *
232 // * Number of dimensions = 2
233 // * The global minimum is: (2.896805253, -5.793610507)
234 // * Initial guess: (0, 0.8)
235 // *
236 // * Reference: http://www.wolframalpha.com/input/
237 // * ?i=%28y^2+%2B+4xy+%2Bx^3+-+2x%29
238 // */
239 // class SampleCostFunction2 : public GenericCostFunction
240 // {
241 // public:
242 // /**
243 // * We overload the operator as is required in GenericCostFunction to
244 // * calculate the cost of the function.
245 // *
246 // * Behaviour:
247 // * cost = y^2 + 4xy +x^3 - 2x
248 // *
249 // * Intended use :
250 // * SampleCostFunction2 Func;
251 // * cost = Func(const std::vector<double> x);
252 // *
253 // * NOTE that the returned cost is the sqare root of the evaluation
254 // * due to the fact the Nelder Mead class will square the cost.
255 // *
256 // * Parameter:
257 // * \param x (input) vector containing coordinates of x and y
258 // * \return cost
259 // */
260 // std::vector<double> operator()(const std::vector<double> &x)
261 // {
262 // std::vector<double> ret {0};
263 // ret[0] = sqrt(fabs(x[1]*x[1] + 4*x[1]*x[0] + x[0]*x[0]*x[0]- 2*x[0]));
264 // return ret;
265 // }
266 // };
267 
268 
281 {
282  public:
301  std::vector<double> operator()(const std::vector<double> &x)
302  {
303  std::vector<double> ret {0};
304  ret[0] = sqrt(sqrt(x[1]*x[1] + x[0]*x[0]));
305  return ret;
306  }
307 };
308 
309 // /**
310 // * The Sample function 4 is defined as
311 // *
312 // * (x^2 + (y - 11)^2 + (y^2 - 7 + x)^2).
313 // *
314 // * Number of dimensions = 2
315 // * The global minimum is: (3, 2)
316 // * Initial guess: (0, 0)
317 // *
318 // * Reference:
319 // */
320 // class SampleCostFunction4 : public GenericCostFunction
321 // {
322 // public:
323 // /**
324 // * We overload the operator as is required in GenericCostFunction to
325 // * calculate the cost of the function.
326 // *
327 // * Behaviour:
328 // * cost = x^2 + (y - 11)^2 + (y^2 - 7 + x)^2
329 // *
330 // * Intended use :
331 // * SampleCostFunction4 Func;
332 // * cost = Func(const std::vector<double> x);
333 // *
334 // * NOTE that the returned cost is the sqare root of the evaluation
335 // * due to the fact the Nelder Mead class will square the cost.
336 // *
337 // * Parameters:
338 // * \param x (input) vector containing coordinates of x and y
339 // * \return cost
340 // */
341 // std::vector<double> operator()(const std::vector<double> &x)
342 // {
343 // std::vector<double> ret {0};
344 // ret[0] = sqrt(fabs(x[0]*x[0] + x[1] - 11)*(x[0]*x[0] + x[1] - 11)
345 // + (x[1]*x[1] - 7 + x[0])*(x[1]*x[1] - 7 + x[0]));
346 // return ret;
347 // }
348 // };
349 
362 {
363  public:
382  std::vector<double> operator()(const std::vector<double> &x)
383  {
384  std::vector<double> ret {0};
385  ret[0] = sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);
386  return ret;
387  }
388 };
389 
405 {
406  public:
425  std::vector<double> operator()(const std::vector<double> &x)
426  {
427  std::vector<double> ret {0};
428  ret[0] = sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2] + x[3]*x[3] + x[4]*x[4]);
429  return ret;
430  }
431 };
432 
445 {
446  public:
465  std::vector<double> operator()(const std::vector<double> &x)
466  {
467  std::vector<double> ret {0};
468  ret[0] = sqrt(fabs(1/((x[0] - 1.05)*x[1]*x[2])));
469  return ret;
470  }
471 };
472 
485 {
486  public:
505  std::vector<double> operator()(const std::vector<double> &x)
506  {
507  std::vector<double> ret {0};
508  ret[0] = sqrt(1/(x[0]*x[0]) + 1/(x[1]*x[1]));
509  return ret;
510  }
511 };
512 
525 {
526  public:
545  std::vector<double> operator()(const std::vector<double> &x)
546  {
547  std::vector<double> ret {0};
548  ret[0] = sqrt(fabs(1/((x[0] - 0.95)*x[1]*x[2])));
549  return ret;
550  }
551 };
552 
565 {
566  public:
582  std::vector<double> operator()(const std::vector<double> &x)
583  {
584  std::vector<double> ret {0};
585  ret[0] = (fabs(-1/(x[0]*x[1] + x[0]*(x[0] - 3))));
586  return ret;
587  }
588 };
589 
602 {
603  public:
623  std::vector<double> operator()(const std::vector<double> &x)
624  {
625  std::vector<double> ret {0};
626  if (x[0] < 1 && x[1]>1.025) {
627  ret[0] = 1/(x[0]-x[0]);
628  }
629  else {
630  ret[0] = 5*x[0]*x[0] + x[1]*x[1];
631  }
632  ret[0] = sqrt(ret[0]);
633  return ret;
634  }
635 };
636 
649 {
650  public:
670  std::vector<double> operator()(const std::vector<double> &x)
671  {
672  std::vector<double> ret {0};
673  ret[0] = (x[0]-90)*(x[0]-90) + (x[1]+50)*(x[1]+50) + 1/(x[0]-102);
674  if (x[0] < 105) {
675  ret[0] += 1/(1 - (0.5*x[0]+x[1]+48.5));
676  }
677 
678  ret[0] = sqrt(fabs(ret[0]));
679  return ret;
680  }
681 };
682 
695 {
696  public:
713  std::vector<double> operator()(const std::vector<double> &x)
714  {
715  std::vector<double> ret {0};
716  ret[0] = ((x[0] + 10*x[1])*(x[0] + 10*x[1]) + 5*(x[2] -
717  x[3])*(x[2] - x[3]) + (x[1] - 2*x[2])*(x[1] - 2*x[2])*(x[1] -
718  2*x[2])*(x[1] - 2*x[2]) + 10*(x[0] - x[3])*(x[0] -
719  x[3])*(x[0] - x[3])*(x[0] - x[3]));
720 
721  ret[0] = sqrt(fabs(ret[0]));
722  return ret;
723  }
724 };
725 
730 {
731  public:
747  std::vector<double> operator()(const std::vector<double> &x)
748  {
749  const double temp = (x[0] >= 1.0) ? x[0] : 1.0;
750  std::vector<double> ret {0};
751  ret[0] = (std::numeric_limits<double>::max()*1000.0*temp);
752  return ret;
753  }
754 };
755 } // UnitTests namespace
756 } // Unfit namespace
757 #endif
758 
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:670
Definition: NelderMeadTestFunctions.hpp:564
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:127
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:545
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:89
Definition: NelderMeadTestFunctions.hpp:524
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:219
Definition: NelderMeadTestFunctions.hpp:146
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:505
Definition: Bounds.hpp:27
Definition: NelderMeadTestFunctions.hpp:68
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:747
Definition: NelderMeadTestFunctions.hpp:361
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:160
Definition: NelderMeadTestFunctions.hpp:198
Definition: GenericCostFunction.hpp:36
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:623
Definition: NelderMeadTestFunctions.hpp:729
Definition: NelderMeadTestFunctions.hpp:106
Definition: NelderMeadTestFunctions.hpp:404
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:713
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:465
Definition: NelderMeadTestFunctions.hpp:694
Definition: NelderMeadTestFunctions.hpp:648
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:425
Definition: NelderMeadTestFunctions.hpp:444
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:301
Definition: NelderMeadTestFunctions.hpp:601
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:582
std::vector< double > operator()(const std::vector< double > &x)
Definition: NelderMeadTestFunctions.hpp:382
Definition: NelderMeadTestFunctions.hpp:280
Definition: NelderMeadTestFunctions.hpp:484