Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
ObjectiveFunctions
Benchmarks
Ackley.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
* <https://shark-ml.github.io/Shark/>
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_ACKLEY_H
33
#define SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_ACKLEY_H
34
35
#include <
shark/ObjectiveFunctions/AbstractObjectiveFunction.h
>
36
#include <
shark/Core/Random.h
>
37
38
namespace
shark
{
namespace
benchmarks{
39
/**
40
* \brief Convex quadratic benchmark function with single dominant axis
41
* \ingroup benchmarks
42
*/
43
struct
Ackley
:
public
SingleObjectiveFunction
{
44
Ackley
(std::size_t
numberOfVariables
= 5) {
45
m_features
|=
CAN_PROPOSE_STARTING_POINT
;
46
m_numberOfVariables =
numberOfVariables
;
47
}
48
49
/// \brief From INameable: return the class name.
50
std::string
name
()
const
51
{
return
"Ackley"
; }
52
53
std::size_t
numberOfVariables
()
const
{
54
return
m_numberOfVariables;
55
}
56
57
bool
hasScalableDimensionality
()
const
{
58
return
true
;
59
}
60
61
/// \brief Adjusts the number of variables if the function is scalable.
62
/// \param [in] numberOfVariables The new dimension.
63
void
setNumberOfVariables
( std::size_t
numberOfVariables
){
64
m_numberOfVariables =
numberOfVariables
;
65
}
66
67
SearchPointType
proposeStartingPoint
()
const
{
68
SearchPointType
x;
69
x.resize(m_numberOfVariables);
70
71
for
(std::size_t i = 0; i < x.size(); i++) {
72
x(i) =
random::uni
(*
mep_rng
, -10, 10);
73
}
74
return
x;
75
}
76
77
double
eval
(
const
SearchPointType
&p)
const
{
78
m_evaluationCounter
++;
79
80
const
double
A = 20.;
81
const
double
B = 0.2;
82
const
double
C = 2* M_PI;
83
84
std::size_t n = p.size();
85
double
a = 0., b = 0.;
86
87
for
(std::size_t i = 0; i < n; ++i) {
88
a += p(i) * p(i);
89
b += cos(C * p(i));
90
}
91
92
return
-A * std::exp(-B * std::sqrt(a / n)) - std::exp(b / n) + A + M_E;
93
}
94
private
:
95
std::size_t m_numberOfVariables;
96
};
97
98
}}
99
100
#endif