-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
202 lines (177 loc) · 6.58 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*!
*
*
* \brief Example of an experiment using the CMA-ES on several benchmark functions
*
* \author O.Krause
* \date 2014
*
* \par Copyright 1995-2014 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Implementations of the xNES
#include "fCMA.h"
#include "fCMA_NNH.h"
#include "pcCMSA.h"
#include "pcfCMA.h"
#include "CSA.h"
//benchmark functions
#include <shark/ObjectiveFunctions/Benchmarks/Sphere.h>
#include <shark/ObjectiveFunctions/Benchmarks/Ellipsoid.h>
#include <shark/ObjectiveFunctions/Benchmarks/Cigar.h>
#include <shark/ObjectiveFunctions/Benchmarks/Discus.h>
//misc
#include <fstream>
#include <boost/filesystem.hpp>
using namespace shark;
struct NoisySphere : public SingleObjectiveFunction {
NoisySphere(std::size_t numberOfVariables):m_numberOfVariables(numberOfVariables){
m_features |= CAN_PROPOSE_STARTING_POINT;
m_features |= IS_NOISY;
}
/// \brief From INameable: return the class name.
std::string name() const
{ return "NoisySphere"; }
std::size_t numberOfVariables()const{
return m_numberOfVariables;
}
bool hasScalableDimensionality()const{
return true;
}
void setNumberOfVariables( std::size_t numberOfVariables ){
m_numberOfVariables = numberOfVariables;
}
SearchPointType proposeStartingPoint() const {
RealVector x(numberOfVariables());
for (std::size_t i = 0; i < x.size(); i++) {
x(i) = random::gauss(random::globalRng(), 0,1);
}
return x;
}
double eval(SearchPointType const& x) const {
SIZE_CHECK(x.size() == numberOfVariables());
m_evaluationCounter++;
double var = 0.0001;
return norm_sqr(x)+random::gauss(random::globalRng(),0.0, var);
}
private:
std::size_t m_numberOfVariables;
};
struct NoisyEllipsoid : public benchmarks::Ellipsoid{
NoisyEllipsoid(std::size_t numberOfVariables):benchmarks::Ellipsoid(numberOfVariables, 0.01){
m_features |= IS_NOISY;
}
/// \brief From INameable: return the class name.
std::string name() const
{ return "NoisyEllipsoid"; }
double eval(SearchPointType const& x) const {
double var = 0.0001;
return benchmarks::Ellipsoid::eval(x)+random::gauss(random::globalRng(),0.0, var);
}
};
typedef boost::shared_ptr<SingleObjectiveFunction > Function;
template<class Optimizer>
void evaluate(std::vector<Function >const& functions, std::vector<Function >const& functionsEval, std::size_t m){
Optimizer cma;
for(std::size_t f = 0; f != functions.size(); ++f){
//init function
random::globalRng().seed(142+f);
functions[f]->init();
std::size_t d = functions[f]->numberOfVariables();
unsigned int lambda = Optimizer::suggestLambda(d);
std::size_t budget = Optimizer::suggestLambda(1000)*m*d+1;
//initialize the optimizer
cma.init( *functions[f] );
std::string directory ="./results/"+cma.name()+"/";
boost::filesystem::create_directory(directory);
std::string fileName=directory+functions[f]->name() + "-" + std::to_string(d)+".txt";
std::ofstream str(fileName.c_str());
str<<"#iteration value sigma \n";
std::cout<<functions[f]->name()<<" "<<d<<std::endl;
//optimize
std::size_t lastPrint = 0;
while(functions[f]->evaluationCounter() < budget){
str.precision( 7 );
str<<functions[f]->evaluationCounter()<<" "<<(*functionsEval[f])(cma.solution().point)<<" "<<cma.sigma()<<" "<<cma.lambda()<<"\n";
cma.step(*functions[f]);
if(functions[f]->evaluationCounter() - lastPrint >= 1000*lambda){
std::cout<<functions[f]->evaluationCounter()<<" "<<(*functionsEval[f])(cma.solution().point)<<" "<<cma.sigma()<<" "<<cma.lambda()<<std::endl;
lastPrint = functions[f]->evaluationCounter();
}
}
std::cout<<functions[f]->evaluationCounter()<<" "<<(*functionsEval[f])(cma.solution().point)<<" "<<cma.sigma()<<" "<<cma.lambda()<<std::endl;
}
}
int main( int argc, char ** argv ) {
using namespace shark::benchmarks;
boost::filesystem::create_directory("./results");
{
std::size_t dims=1000;
std::size_t epochs=10;
Function sphere = Function(new Sphere(dims));
Function elli = Function(new Ellipsoid(dims, 1.e-2));
std::vector<Function > functions;
functions.push_back(sphere);
functions.push_back(elli);
evaluate<fCMA>(functions, functions, epochs);
evaluate<pcCMSA>(functions, functions, epochs);
evaluate<CSA>(functions, functions, epochs);
}
{
std::size_t dims=8000;
std::size_t epochs=10;
Function sphere = Function(new Sphere(dims));
Function elli = Function(new Ellipsoid(dims, 1.e-2));
std::vector<Function > functions;
functions.push_back(sphere);
functions.push_back(elli);
evaluate<fCMA>(functions, functions, epochs);
evaluate<pcCMSA>(functions, functions, epochs);
evaluate<CSA>(functions, functions, epochs);
}
{
std::size_t dims=1000;
std::size_t epochs=1000;
Function sphere = Function(new Sphere(dims));
std::vector<Function > functions;
std::vector<Function > functionsEval;
functions.push_back(Function(new NoisySphere(dims))); functionsEval.push_back(sphere);
functions.push_back(Function(new NoisyEllipsoid(dims))); functionsEval.push_back(Function(new Ellipsoid(dims, 1.e-2)));
evaluate<fCMA>(functions, functionsEval, epochs);
evaluate<pcfCMA>(functions, functionsEval, epochs);
evaluate<fCMA_NNH>(functions, functionsEval, epochs);
evaluate<pcCMSA>(functions, functionsEval, epochs);
evaluate<CSA>(functions, functionsEval, epochs);
}
{
std::size_t dims=8000;
std::size_t epochs=1000;
Function sphere = Function(new Sphere(dims));
std::vector<Function > functions;
std::vector<Function > functionsEval;
functions.push_back(Function(new NoisySphere(dims))); functionsEval.push_back(sphere);
functions.push_back(Function(new NoisyEllipsoid(dims))); functionsEval.push_back(Function(new Ellipsoid(dims, 1.e-2)));
evaluate<pcfCMA>(functions, functionsEval, epochs);
evaluate<fCMA>(functions, functionsEval, epochs);
evaluate<pcCMSA>(functions, functionsEval, epochs);
evaluate<fCMA_NNH>(functions, functionsEval, epochs);
evaluate<CSA>(functions, functionsEval, epochs);
}
}