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 * <https://shark-ml.github.io/Shark/>
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
37namespace shark {namespace benchmarks{
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* \ingroup benchmarks
44*/
46 Ellipsoid(size_t numberOfVariables = 5, double alpha=1E-3) : m_alpha(alpha) {
50 m_numberOfVariables = numberOfVariables;
51 }
52
53 /// \brief From INameable: return the class name.
54 std::string name() const
55 { return "Ellipsoid"; }
56
57 std::size_t numberOfVariables()const{
58 return m_numberOfVariables;
59 }
60
62 return true;
63 }
64
66 m_numberOfVariables = numberOfVariables;
67 }
68
70 RealVector x(numberOfVariables());
71
72 for (std::size_t i = 0; i < x.size(); i++) {
73 x(i) = random::uni(*mep_rng, 0,1);
74 }
75 return x;
76 }
77
78 double eval( const SearchPointType & p ) const {
80 double sum = 0;
81 double sizeMinusOne = p.size() - 1.;
82 for( std::size_t i = 0; i < p.size(); i++ ){
83 sum += ::pow( m_alpha, i / sizeMinusOne ) * sqr(p( i ) );
84 }
85
86 return sum;
87 }
88
89 double evalDerivative( const SearchPointType & p, FirstOrderDerivative & derivative ) const {
90 double sizeMinusOne=p.size() - 1.;
91 derivative.resize(p.size());
92 for (std::size_t i = 0; i < p.size(); i++) {
93 derivative(i) = 2 * ::pow(m_alpha, i / sizeMinusOne) * p(i);
94 }
95 return eval(p);
96 }
97 double evalDerivative(const SearchPointType &p, SecondOrderDerivative &derivative)const {
98 std::size_t size=p.size();
99 double sizeMinusOne=p.size() - 1.;
100 derivative.gradient.resize(size);
101 derivative.hessian.resize(size,size);
102 derivative.hessian.clear();
103 for (std::size_t i = 0; i < size; i++) {
104 derivative.gradient(i) = 2 * std::pow(m_alpha, i / sizeMinusOne ) * p(i);
105 derivative.hessian(i,i) = 2 * std::pow(m_alpha, i /sizeMinusOne );
106 }
107 return eval(p);
108 }
109private:
110 std::size_t m_numberOfVariables;
111 double m_alpha;
112};
113
114}}
115
116#endif