Cigar.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_CIGAR_H
33 #define SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_CIGAR_H
34 
36 #include <shark/Core/Random.h>
37 
38 namespace shark {
39 /**
40  * \brief Convex quadratic benchmark function with single dominant axis
41  */
42 struct Cigar : public SingleObjectiveFunction {
43 
44  Cigar(std::size_t numberOfVariables = 5, double alpha=1.E-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 "Cigar"; }
53 
54  std::size_t numberOfVariables()const{
55  return m_numberOfVariables;
56  }
57 
59  return true;
60  }
61 
63  m_numberOfVariables = numberOfVariables;
64  }
65 
67  RealVector x(numberOfVariables());
68 
69  for (std::size_t i = 0; i < x.size(); i++) {
70  x(i) = random::uni(*mep_rng, 0, 1);
71  }
72  return x;
73  }
74 
75  double eval(const SearchPointType &p) const {
77 
78  double sum = m_alpha * sqr(p(0));
79  for (std::size_t i = 1; i < p.size(); i++)
80  sum += sqr(p(i));
81 
82  return sum;
83  }
84  double evalDerivative(SearchPointType const& p, FirstOrderDerivative & derivative ) const {
85  derivative.resize(p.size());
86  noalias(derivative) = 2* p;
87  derivative(0) = 2 * m_alpha * p(0);
88  return eval(p);
89  }
90 
91  double alpha() const {
92  return m_alpha;
93  }
94 
95  void setAlpha(double alpha) {
96  m_alpha = alpha;
97  }
98 
99 private:
100  double m_alpha;
101  std::size_t m_numberOfVariables;
102 };
103 }
104 
105 #endif // SHARK_EA_CIGAR_H