Pallas Solver  0.1
C++ Global Optimization Algorithms
test_functions.h
1 // Pallas Solver
2 // Copyright 2015. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
17 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 // POSSIBILITY OF SUCH DAMAGE.
24 //
25 // Author: ryan.latture@gmail.com (Ryan Latture)
26 
27 #ifndef PALLAS_TEST_FUNCTIONS_H
28 #define PALLAS_TEST_FUNCTIONS_H
29 
30 #include "pallas/types.h"
31 
32 #include <cmath>
33 
34 #define PI 3.14159265358979323846
35 
36 namespace pallas {
37  namespace internal {
38  using std::cos;
39  using std::sin;
40  using std::sqrt;
41  using std::exp;
42  using std::abs;
43 
44  static const double pow_LUT[2] = { 1.0, -1.0 };
45 
61  double schwefel(const Vector &x)
62  {
63  unsigned int num = x.size();
64  double s = 0.0;
65  for (unsigned int i = 0; i < num; ++i)
66  {
67  s += -x(i) * sin(sqrt(abs(x(i))));
68  }
69  return 418.9829 * num + s;
70  }
71 
88  double rosenbrock(const Vector &x)
89  {
90  unsigned int num = x.size();
91  double s = 0.0;
92  for (unsigned int i = 0; i < (num - 1); ++i)
93  {
94  s += (x(i+1) - x(i)*x(i))*(x(i+1) - x(i)*x(i)) + (1-x(i)) * (1-x(i));
95  }
96  return 100.0 * s;
97  }
98 
113  double easom(const Vector &x)
114  {
115  unsigned int num = x.size();
116  double sm = 0.0;
117  double tm = 1.0;
118  for (unsigned int i = 0; i < num; ++i)
119  {
120  sm += (x(i) - PI)*(x(i) - PI);
121  tm *= cos(x(i))*cos(x(i));
122  }
123  return -(pow_LUT[num % 2]) * tm * exp(-sm) + 1.0;
124  }
125 
141  double xsy02(const Vector &x)
142  {
143  unsigned int num = x.size();
144  double sm1 = 0.0;
145  double sm2 = 0.0;
146  for (unsigned int i = 0; i < num; ++i)
147  {
148  sm1 += abs(x(i));
149  sm2 += sin(x(i)*x(i));
150  }
151  return sm1 / exp(sm2);
152  }
153  }
154 }
155 
156 #endif // PALLAS_TEST_FUNCTIONS_H
Definition: basinhopping.h:51