Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
ObjectiveFunctions
Benchmarks
Sphere.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_BENCHMARK_SPHERE_H
32
#define SHARK_OBJECTIVEFUNCTIONS_BENCHMARK_SPHERE_H
33
34
#include <
shark/ObjectiveFunctions/AbstractObjectiveFunction.h
>
35
#include <
shark/Core/Random.h
>
36
37
namespace
shark
{
namespace
benchmarks{
38
/**
39
* \brief Convex quadratic benchmark function.
40
* \ingroup benchmarks
41
*/
42
struct
Sphere
:
public
SingleObjectiveFunction
{
43
44
Sphere
(std::size_t
numberOfVariables
= 5):m_numberOfVariables(
numberOfVariables
) {
45
m_features
|=
CAN_PROPOSE_STARTING_POINT
;
46
m_features
|=
HAS_FIRST_DERIVATIVE
;
47
}
48
49
/// \brief From INameable: return the class name.
50
std::string
name
()
const
51
{
return
"Sphere"
; }
52
53
std::size_t
numberOfVariables
()
const
{
54
return
m_numberOfVariables;
55
}
56
57
bool
hasScalableDimensionality
()
const
{
58
return
true
;
59
}
60
61
void
setNumberOfVariables
( std::size_t
numberOfVariables
){
62
m_numberOfVariables =
numberOfVariables
;
63
}
64
65
SearchPointType
proposeStartingPoint
()
const
{
66
RealVector x(
numberOfVariables
());
67
68
for
(std::size_t i = 0; i < x.size(); i++) {
69
x(i) =
random::gauss
(*
mep_rng
, 0,1);
70
}
71
return
x;
72
}
73
74
double
eval
(
SearchPointType
const
& x)
const
{
75
SIZE_CHECK
(x.size() ==
numberOfVariables
());
76
m_evaluationCounter
++;
77
return
norm_sqr(x);
78
}
79
80
double
evalDerivative
(
SearchPointType
const
& x,
FirstOrderDerivative
& derivative)
const
{
81
SIZE_CHECK
(x.size() ==
numberOfVariables
());
82
m_evaluationCounter
++;
83
derivative.resize(x.size());
84
noalias(derivative) = 2*x;
85
return
norm_sqr(x);
86
}
87
private
:
88
std::size_t m_numberOfVariables;
89
};
90
91
}}
92
93
#endif