Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
ObjectiveFunctions
Benchmarks
Discus.h
Go to the documentation of this file.
1
/*!
2
*
3
*
4
* \brief Convex quadratic benchmark function.
5
*
6
*
7
* \author -
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_DISCUS_H
32
#define SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_DISCUS_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
Discus
:
public
SingleObjectiveFunction
{
43
44
Discus
(std::size_t
numberOfVariables
= 5,
double
alpha
= 1.E-3) : m_alpha(
alpha
) {
45
m_features
|=
CAN_PROPOSE_STARTING_POINT
;
46
m_features
|=
HAS_FIRST_DERIVATIVE
;
47
m_numberOfVariables =
numberOfVariables
;
48
}
49
50
/// \brief From INameable: return the class name.
51
std::string
name
()
const
52
{
return
"Discus"
; }
53
54
std::size_t
numberOfVariables
()
const
{
55
return
m_numberOfVariables;
56
}
57
58
bool
hasScalableDimensionality
()
const
{
59
return
true
;
60
}
61
62
/// \brief Adjusts the number of variables if the function is scalable.
63
/// \param [in] numberOfVariables The new dimension.
64
void
setNumberOfVariables
( std::size_t
numberOfVariables
){
65
m_numberOfVariables =
numberOfVariables
;
66
}
67
68
SearchPointType
proposeStartingPoint
()
const
{
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
(
SearchPointType
const
& p)
const
{
78
m_evaluationCounter
++;
79
double
sum =
sqr
(p(0));
80
for
(std::size_t i = 1; i < p.size(); i++)
81
sum += m_alpha *
sqr
(p(i));
82
83
return
sum;
84
}
85
double
evalDerivative
(
SearchPointType
const
& p,
FirstOrderDerivative
& derivative )
const
{
86
derivative.resize(p.size());
87
noalias(derivative) = (2 * m_alpha) * p;
88
derivative(0) = 2 * p(0);
89
return
eval
(p);
90
}
91
92
double
alpha
()
const
{
93
return
m_alpha;
94
}
95
96
void
setAlpha
(
double
alpha
) {
97
m_alpha =
alpha
;
98
}
99
100
private
:
101
double
m_alpha;
102
std::size_t m_numberOfVariables;
103
};
104
}}
105
106
#endif