Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
ObjectiveFunctions
Benchmarks
ConstrainedSphere.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_CONSTRAINEDSPHERE_H
32
#define SHARK_OBJECTIVEFUNCTIONS_BENCHMARK_CONSTRAINEDSPHERE_H
33
34
#include <
shark/ObjectiveFunctions/AbstractObjectiveFunction.h
>
35
#include <
shark/Core/Random.h
>
36
37
namespace
shark
{
namespace
benchmarks{
38
/**
39
* \brief Constrained Sphere function
40
*
41
* This is a simple sphere function minimizing \f$ f(x) = \sum_i^N x_i^2-m \f$ under the constraints that
42
* \f$ x_i \geq 1\f$ for \f$ i = 1,\dots,m \f$. The minimum is at \f$ x_1=\dots = x_m = 1\f$ and
43
* \f$ x_{m+1}=\dots = x_N = 0 \f$ with function value 0.
44
*
45
* This is a simple benchmark for evolutionary algorithms as, the closer the algorithm is to the optimum
46
* \ingroup benchmarks
47
*/
48
struct
ConstrainedSphere
:
public
SingleObjectiveFunction
{
49
50
ConstrainedSphere
(std::size_t
numberOfVariables
= 5, std::size_t m = 1)
51
:m_numberOfVariables(
numberOfVariables
), m_constraints(m) {
52
m_features
|=
CAN_PROPOSE_STARTING_POINT
;
53
m_features
|=
IS_CONSTRAINED_FEATURE
;
54
m_features
|=
IS_THREAD_SAFE
;
55
}
56
57
/// \brief From INameable: return the class name.
58
std::string
name
()
const
59
{
return
"ConstrainedSphere"
; }
60
61
std::size_t
numberOfVariables
()
const
{
62
return
m_numberOfVariables;
63
}
64
65
bool
hasScalableDimensionality
()
const
{
66
return
true
;
67
}
68
69
void
setNumberOfVariables
( std::size_t
numberOfVariables
){
70
m_numberOfVariables =
numberOfVariables
;
71
}
72
73
SearchPointType
proposeStartingPoint
()
const
{
74
RealVector x(
numberOfVariables
());
75
76
for
(std::size_t i = 0; i < m_constraints; i++) {
77
x(i) = std::abs(
random::gauss
(*
mep_rng
, 0, 1))+1;
78
}
79
for
(std::size_t i = m_constraints; i < x.size(); i++) {
80
x(i) =
random::gauss
(*
mep_rng
,0, 1);
81
}
82
return
x;
83
}
84
85
bool
isFeasible
(
SearchPointType
const
& input)
const
{
86
for
(std::size_t i = 0; i < m_constraints; i++) {
87
if
(input(i) < 1)
return
false
;
88
}
89
return
true
;
90
}
91
92
double
eval
(
const
SearchPointType
&p)
const
{
93
m_evaluationCounter
++;
94
return
norm_sqr(p)-m_constraints;
95
}
96
private
:
97
std::size_t m_numberOfVariables;
98
std::size_t m_constraints;
99
};
100
101
}}
102
103
#endif