Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Algorithms
DirectSearch
CrossEntropyMethod.h
Go to the documentation of this file.
1
//===========================================================================
2
/*!
3
*
4
* \brief Implements the Cross Entropy Algorithm.
5
*
6
* Christophe Thiery, Bruno Scherrer. Improvements on Learning Tetris with Cross Entropy.
7
* International Computer Games Association Journal, ICGA, 2009, 32. <inria-00418930>
8
*
9
*
10
* \author Jens Holm, Mathias Petræus and Mark Wulff
11
* \date January 2016
12
*
13
* \par Copyright 1995-2017 Shark Development Team
14
*
15
* <BR><HR>
16
* This file is part of Shark.
17
* <https://shark-ml.github.io/Shark/>
18
*
19
* Shark is free software: you can redistribute it and/or modify
20
* it under the terms of the GNU Lesser General Public License as published
21
* by the Free Software Foundation, either version 3 of the License, or
22
* (at your option) any later version.
23
*
24
* Shark is distributed in the hope that it will be useful,
25
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
* GNU Lesser General Public License for more details.
28
*
29
* You should have received a copy of the GNU Lesser General Public License
30
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
31
*
32
*/
33
//===========================================================================
34
35
36
#ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_CROSSENTROPY_H
37
#define SHARK_ALGORITHMS_DIRECT_SEARCH_CROSSENTROPY_H
38
39
#include <
shark/Core/DLLSupport.h
>
40
#include <
shark/Algorithms/AbstractSingleObjectiveOptimizer.h
>
41
#include <
shark/Core/Random.h
>
42
#include <
shark/Algorithms/DirectSearch/Individual.h
>
43
44
#include <boost/shared_ptr.hpp>
45
46
namespace
shark
{
47
48
/// \brief Implements the Cross Entropy Method.
49
///
50
/// This class implements the noisy cross entropy method
51
/// as descibed in the following article.
52
///
53
/// Christophe Thiery, Bruno Scherrer. Improvements on Learning Tetris with Cross Entropy.
54
/// International Computer Games Association Journal, ICGA, 2009, 32. <inria-00418930>
55
///
56
/// The algorithm aims to minimize an objective function through stochastic search.
57
/// It works iteratively until a certain stopping criteria is met. At each
58
/// iteration, it samples a number of vectors from a Gaussian distribution
59
/// and evaluates each of these against the supplied objective function.
60
/// Based on the return value from the objective function, a subset of the
61
/// the best ranked vectors are chosen to update the search parameters
62
/// of the next generation.
63
///
64
/// The mean of the Gaussian distribution is set to the centroid of the best ranked
65
/// vectors, and the variance is set to the variance of the best ranked
66
/// vectors in each individual dimension.
67
/// \ingroup singledirect
68
class
CrossEntropyMethod
:
public
AbstractSingleObjectiveOptimizer
<RealVector >
69
{
70
public
:
71
72
/// \brief Interface class for noise type.
73
class
INoiseType
{
74
public
:
75
virtual
double
noiseValue
(
int
t)
const
{
return
0.0; };
76
virtual
std::string
name
()
const
{
return
std::string(
"Default noise of 0"
); }
77
};
78
79
/// \brief Smart pointer for noise type.
80
typedef
boost::shared_ptr<INoiseType>
StrongNoisePtr
;
81
82
/// \brief Constant noise term z_t = noise.
83
class
ConstantNoise
:
public
INoiseType
{
84
public
:
85
ConstantNoise
(
double
noise
) { m_noise =
noise
; };
86
virtual
double
noiseValue
(
int
t)
const
{
return
std::max(m_noise, 0.0); }
87
virtual
std::string
name
()
const
{
88
std::stringstream ss;
89
ss <<
"z(t) = "
<< m_noise;
90
return
std::string(ss.str());
91
}
92
private
:
93
double
m_noise;
94
};
95
96
/// \brief Linear noise term z_t = a + t / b.
97
class
LinearNoise
:
public
INoiseType
{
98
public
:
99
LinearNoise
(
double
a,
double
b ) { m_a = a; m_b = b; };
100
virtual
double
noiseValue
(
int
t)
const
{
return
std::max(m_a + (t * m_b), 0.0); }
101
virtual
std::string
name
()
const
{
102
std::stringstream ss;
103
std::string sign = (m_b < 0.0 ?
" - "
:
" + "
);
104
ss <<
"z(t) = "
<< m_a << sign <<
"t * "
<< std::abs(m_b);
105
return
std::string(ss.str());
106
}
107
private
:
108
double
m_a, m_b;
109
};
110
111
112
/// \brief Default c'tor.
113
SHARK_EXPORT_SYMBOL
CrossEntropyMethod
();
114
115
/// \brief From INameable: return the class name.
116
std::string
name
()
const
117
{
return
"Cross Entropy Method"
; }
118
119
/// \brief Sets default value for Population size.
120
SHARK_EXPORT_SYMBOL
static
unsigned
suggestPopulationSize
( ) ;
121
122
/// \brief Calculates selection size for the supplied population size.
123
SHARK_EXPORT_SYMBOL
static
unsigned
suggestSelectionSize
(
unsigned
int
populationSize
) ;
124
125
void
read
(
InArchive
& archive );
126
void
write
(
OutArchive
& archive )
const
;
127
128
using
AbstractSingleObjectiveOptimizer
<RealVector >
::init
;
129
130
/// \brief Initializes the algorithm for the supplied objective function and the initial mean p.
131
SHARK_EXPORT_SYMBOL
void
init
(
ObjectiveFunctionType
const
& function,
SearchPointType
const
& p);
132
133
/// \brief Initializes the algorithm for the supplied objective function.
134
SHARK_EXPORT_SYMBOL
void
init
(
135
ObjectiveFunctionType
const
& function,
136
SearchPointType
const
& initialSearchPoint,
137
unsigned
int
populationSize
,
138
unsigned
int
selectionSize
,
139
RealVector initialSigma
140
);
141
142
/// \brief Executes one iteration of the algorithm.
143
SHARK_EXPORT_SYMBOL
void
step
(
ObjectiveFunctionType
const
& function);
144
145
/// \brief Access the current variance.
146
RealVector
const
&
variance
()
const
{
147
return
m_variance
;
148
}
149
150
/// \brief Set the variance to a vector.
151
void
setVariance
(RealVector
variance
) {
152
assert(
variance
.size() ==
m_variance
.size());
153
m_variance
=
variance
;
154
}
155
156
/// \brief Set all variance values.
157
void
setVariance
(
double
variance
){
158
m_variance
= blas::repeat(
variance
,
m_variance
.size());
159
}
160
161
/// \brief Access the current population mean.
162
RealVector
const
&
mean
()
const
{
163
return
m_mean
;
164
}
165
166
/// \brief Returns the size of the parent population.
167
unsigned
int
selectionSize
()
const
{
168
return
m_selectionSize
;
169
}
170
171
/// \brief Returns a mutable reference to the size of the parent population.
172
unsigned
int
&
selectionSize
(){
173
return
m_selectionSize
;
174
}
175
176
/// \brief Returns a immutable reference to the size of the population.
177
unsigned
int
populationSize
()
const
{
178
return
m_populationSize
;
179
}
180
181
/// \brief Returns a mutable reference to the size of the population.
182
unsigned
int
&
populationSize
(){
183
return
m_populationSize
;
184
}
185
186
/// \brief Set the noise type from a raw pointer.
187
void
setNoiseType
(
INoiseType
* noiseType ) {
188
m_noise
.reset();
189
m_noise
= boost::shared_ptr<INoiseType> (noiseType);
190
}
191
192
/// \brief Get an immutable reference to Noise Type.
193
const
INoiseType
&
getNoiseType
( )
const
{
194
return
*
m_noise
.get();
195
}
196
197
198
protected
:
199
typedef
Individual<RealVector, double, RealVector>
IndividualType
;
200
/// \brief Updates the strategy parameters based on the supplied parent population.
201
SHARK_EXPORT_SYMBOL
void
updateStrategyParameters
( std::vector< IndividualType >
const
& parents ) ;
202
203
std::size_t
m_numberOfVariables
;
///< Stores the dimensionality of the search space.
204
unsigned
int
m_selectionSize
;
///< Number of vectors chosen when updating distribution parameters.
205
unsigned
int
m_populationSize
;
///< Number of vectors sampled in a generation.
206
207
RealVector
m_variance
;
///< Variance for sample parameters.
208
209
210
RealVector
m_mean
;
///< The mean of the population.
211
212
unsigned
m_counter
;
///< Counter for generations.
213
214
StrongNoisePtr
m_noise
;
///< Noise type to apply in the update of distribution parameters.
215
};
216
}
217
218
#endif