-
Notifications
You must be signed in to change notification settings - Fork 79
/
sample-code-bounds.cc
55 lines (51 loc) · 1.97 KB
/
sample-code-bounds.cc
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
/**
* CMA-ES, Covariance Matrix Adaptation Evolution Strategy
* Copyright (c) 2014 Inria
* Author: Emmanuel Benazera <emmanuel.benazera@lri.fr>
*
* This file is part of libcmaes.
*
* libcmaes 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.
*
* libcmaes 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 libcmaes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libcmaes/cmaes.h>
#include <iostream>
using namespace libcmaes;
FitFunc fsphere = [](const double *x, const int N)
{
double val = 0.0;
for (int i=0;i<N;i++)
val += x[i]*x[i];
return val;
};
int main(int argc, char *argv[])
{
const int dim = 10; // problem dimensions.
double sigma = 0.1;
double lbounds[dim],ubounds[dim]; // arrays for lower and upper parameter bounds, respectively
for (int i=0;i<dim;i++)
{
lbounds[i] = -2.0;
ubounds[i] = 2.0;
}
std::vector<double> x0(dim,1.0); // beware that x0 is within bounds.
GenoPheno<pwqBoundStrategy> gp(lbounds,ubounds,dim); // genotype / phenotype transform associated to bounds.
CMAParameters<GenoPheno<pwqBoundStrategy>> cmaparams(x0,sigma,-1,0,gp); // -1 for automatically decided lambda, 0 is for random seeding of the internal generator.
cmaparams.set_algo(aCMAES);
CMASolutions cmasols = cmaes<GenoPheno<pwqBoundStrategy>>(fsphere,cmaparams);
std::cout << "best solution: ";
cmasols.print(std::cout,0,gp);
std::cout << std::endl;
std::cout << "optimization took " << cmasols.elapsed_time() / 1000.0 << " seconds\n";
return cmasols.run_status();
}