Ackley.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Convex quadratic benchmark function with single dominant axis
5 
6  *
7  *
8  * \author -
9  * \date -
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_ACKLEY_H
33 #define SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_ACKLEY_H
34 
36 #include <shark/Core/Random.h>
37 
38 namespace shark {
39 /**
40  * \brief Convex quadratic benchmark function with single dominant axis
41  */
43  Ackley(std::size_t numberOfVariables = 5) {
45  m_numberOfVariables = numberOfVariables;
46  }
47 
48  /// \brief From INameable: return the class name.
49  std::string name() const
50  { return "Ackley"; }
51 
52  std::size_t numberOfVariables()const{
53  return m_numberOfVariables;
54  }
55 
57  return true;
58  }
59 
60  /// \brief Adjusts the number of variables if the function is scalable.
61  /// \param [in] numberOfVariables The new dimension.
63  m_numberOfVariables = numberOfVariables;
64  }
65 
68  x.resize(m_numberOfVariables);
69 
70  for (std::size_t i = 0; i < x.size(); i++) {
71  x(i) = random::uni(*mep_rng, -10, 10);
72  }
73  return x;
74  }
75 
76  double eval(const SearchPointType &p) const {
78 
79  const double A = 20.;
80  const double B = 0.2;
81  const double C = 2* M_PI;
82 
83  std::size_t n = p.size();
84  double a = 0., b = 0.;
85 
86  for (std::size_t i = 0; i < n; ++i) {
87  a += p(i) * p(i);
88  b += cos(C * p(i));
89  }
90 
91  return -A * std::exp(-B * std::sqrt(a / n)) - std::exp(b / n) + A + M_E;
92  }
93 private:
94  std::size_t m_numberOfVariables;
95 };
96 
97 }
98 
99 #endif