CigarDiscus.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Convex quadratic benchmark function.
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_BENCHMARK_CIGARDISCUS_H
33 #define SHARK_OBJECTIVEFUNCTIONS_BENCHMARK_CIGARDISCUS_H
34 
36 #include <shark/Core/Random.h>
37 
38 namespace shark {
39 /**
40  * \brief Convex quadratic benchmark function.
41  */
43 public:
44 
45  CigarDiscus(std::size_t numberOfVariables = 5,double alpha=1E-3) : m_alpha(alpha) {
47  m_numberOfVariables = numberOfVariables;
48  }
49 
50  /// \brief From INameable: return the class name.
51  std::string name() const
52  { return "CigarDiscus"; }
53 
54  std::size_t numberOfVariables()const{
55  return m_numberOfVariables;
56  }
57 
59  return true;
60  }
61 
62  /// \brief Adjusts the number of variables if the function is scalable.
63  /// \param [in] numberOfVariables The new dimension.
65  m_numberOfVariables = numberOfVariables;
66  }
67 
69  RealVector x(numberOfVariables());
70 
71  for (std::size_t i = 0; i < x.size(); i++) {
72  x(i) = random::uni(*mep_rng, 0, 1);
73  }
74  return x;
75  }
76 
77  double eval(const SearchPointType &p) const {
79 
80  double sum = m_alpha * sqr(p(0)) + sqr(p(p.size() - 1));
81  double alpha = ::sqrt(m_alpha);
82  for (std::size_t i = 1; i < p.size()-1; i++)
83  sum += alpha * sqr(p(i));
84 
85  return sum;
86  }
87 
88  double alpha() const {
89  return m_alpha;
90  }
91 
92  void setAlpha(double alpha) {
93  m_alpha = alpha;
94  }
95 
96 private:
97  double m_alpha;
98  std::size_t m_numberOfVariables;
99 };
100 }
101 
102 #endif