Ellipsoid.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Convex quadratic benchmark function.
5  *
6  *
7  * \author T.Voss
8  * \date 2010-2011
9  *
10  *
11  * \par Copyright 1995-2017 Shark Development Team
12  *
13  * <BR><HR>
14  * This file is part of Shark.
15  * <http://shark-ml.org/>
16  *
17  * Shark is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Lesser General Public License as published
19  * by the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * Shark is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31 #ifndef SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_ELLIPSOID_H
32 #define SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_ELLIPSOID_H
33 
35 #include <shark/Core/Random.h>
36 
37 namespace shark {
38 /**
39 * \brief Convex quadratic benchmark function
40 *
41 * The eigenvalues of the Hessian of this convex quadratic benchmark
42 * function are equally distributed on logarithmic scale.
43 */
45  Ellipsoid(size_t numberOfVariables = 5, double alpha=1E-3) : m_alpha(alpha) {
49  m_numberOfVariables = numberOfVariables;
50  }
51 
52  /// \brief From INameable: return the class name.
53  std::string name() const
54  { return "Ellipsoid"; }
55 
56  std::size_t numberOfVariables()const{
57  return m_numberOfVariables;
58  }
59 
61  return true;
62  }
63 
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  double sum = 0;
80  double sizeMinusOne = p.size() - 1.;
81  for( std::size_t i = 0; i < p.size(); i++ ){
82  sum += ::pow( m_alpha, i / sizeMinusOne ) * sqr(p( i ) );
83  }
84 
85  return sum;
86  }
87 
88  double evalDerivative( const SearchPointType & p, FirstOrderDerivative & derivative ) const {
89  double sizeMinusOne=p.size() - 1.;
90  derivative.resize(p.size());
91  for (std::size_t i = 0; i < p.size(); i++) {
92  derivative(i) = 2 * ::pow(m_alpha, i / sizeMinusOne) * p(i);
93  }
94  return eval(p);
95  }
96  double evalDerivative(const SearchPointType &p, SecondOrderDerivative &derivative)const {
97  std::size_t size=p.size();
98  double sizeMinusOne=p.size() - 1.;
99  derivative.gradient.resize(size);
100  derivative.hessian.resize(size,size);
101  derivative.hessian.clear();
102  for (std::size_t i = 0; i < size; i++) {
103  derivative.gradient(i) = 2 * std::pow(m_alpha, i / sizeMinusOne ) * p(i);
104  derivative.hessian(i,i) = 2 * std::pow(m_alpha, i /sizeMinusOne );
105  }
106  return eval(p);
107  }
108 private:
109  std::size_t m_numberOfVariables;
110  double m_alpha;
111 };
112 
113 }
114 
115 #endif