-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMAExample1.java
141 lines (120 loc) · 6.09 KB
/
CMAExample1.java
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
package fr.inria.optimization.cmaes.examples;
import fr.inria.optimization.cmaes.CMAEvolutionStrategy;
import fr.inria.optimization.cmaes.fitness.IObjectiveFunction;
/** The very well-known Rosenbrock objective function to be minimized.
*/
class Rosenbrock implements IObjectiveFunction { // meaning implements methods valueOf and isFeasible
public double valueOf (double[] x) {
double res = 0;
for (int i = 0; i < x.length-1; ++i)
res += 100 * (x[i]*x[i] - x[i+1]) * (x[i]*x[i] - x[i+1]) +
(x[i] - 1.) * (x[i] - 1.);
return res;
}
public boolean isFeasible(double[] x) {return true; } // entire R^n is feasible
}
/** A very short example program how to use the class CMAEvolutionStrategy. The code is given below, see also the code snippet in the documentation of class {@link CMAEvolutionStrategy}.
* For implementation of restarts see {@link CMAExample2}.
<pre>
public class CMAExample1 {
public static void main(String[] args) {
IObjectiveFunction fitfun = new Rosenbrock();
// new a CMA-ES and set some initial values
CMAEvolutionStrategy cma = new CMAEvolutionStrategy();
cma.readProperties(); // read options, see file CMAEvolutionStrategy.properties
cma.setDimension(22); // overwrite some loaded properties
cma.setInitialX(0.5); // in each dimension, also setTypicalX can be used
cma.setInitialStandardDeviation(0.2); // also a mandatory setting
cma.options.stopFitness = 1e-9; // optional setting
// initialize cma and get fitness array to fill in later
double[] fitness = cma.init(); // new double[cma.parameters.getPopulationSize()];
// initial output to files
cma.writeToDefaultFilesHeaders(0); // 0 == overwrites old files
// iteration loop
while(cma.stopConditions.getNumber() == 0) {
// core iteration step
double[][] pop = cma.samplePopulation(); // get a new population of solutions
for(int i = 0; i < pop.length; ++i) { // for each candidate solution i
while (!fitfun.isFeasible(pop[i])) // test whether solution is feasible,
pop[i] = cma.resampleSingle(i); // re-sample solution until it is feasible
fitness[i] = fitfun.valueOf(pop[i]); // compute fitness value, where fitfun
} // is the function to be minimized
cma.updateDistribution(fitness); // pass fitness array to update search distribution
// output to console and files
cma.writeToDefaultFiles();
int outmod = 150;
if (cma.getCountIter() % (15*outmod) == 1)
cma.printlnAnnotation(); // might write file as well
if (cma.getCountIter() % outmod == 1)
cma.println();
}
// evaluate mean value as it is the best estimator for the optimum
cma.setFitnessOfMeanX(fitfun.valueOf(cma.getMeanX())); // updates the best ever solution
// final output
cma.writeToDefaultFiles(1);
cma.println();
cma.println("Terminated due to");
for (String s : cma.stopConditions.getMessages())
cma.println(" " + s);
cma.println("best function value " + cma.getBestFunctionValue()
+ " at evaluation " + cma.getBestEvaluationNumber());
// we might return cma.getBestSolution() or cma.getBestX()
} // main
} // class
</pre>
*
* @see CMAEvolutionStrategy
*
* @author Nikolaus Hansen, released into public domain.
*/
public class CMAExample1 {
public static void main(String[] args) {
IObjectiveFunction fitfun = new Rosenbrock();
// new a CMA-ES and set some initial values
CMAEvolutionStrategy cma = new CMAEvolutionStrategy();
cma.readProperties(); // read options, see file CMAEvolutionStrategy.properties
cma.setDimension(10); // overwrite some loaded properties
cma.setInitialX(0.05); // in each dimension, also setTypicalX can be used
cma.setInitialStandardDeviation(0.2); // also a mandatory setting
cma.options.stopFitness = 1e-14; // optional setting
// initialize cma and get fitness array to fill in later
double[] fitness = cma.init(); // new double[cma.parameters.getPopulationSize()];
// initial output to files
cma.writeToDefaultFilesHeaders(0); // 0 == overwrites old files
// iteration loop
while(cma.stopConditions.getNumber() == 0) {
// --- core iteration step ---
double[][] pop = cma.samplePopulation(); // get a new population of solutions
for(int i = 0; i < pop.length; ++i) { // for each candidate solution i
// a simple way to handle constraints that define a convex feasible domain
// (like box constraints, i.e. variable boundaries) via "blind re-sampling"
// assumes that the feasible domain is convex, the optimum is
while (!fitfun.isFeasible(pop[i])) // not located on (or very close to) the domain boundary,
pop[i] = cma.resampleSingle(i); // initialX is feasible and initialStandardDeviations are
// sufficiently small to prevent quasi-infinite looping here
// compute fitness/objective value
fitness[i] = fitfun.valueOf(pop[i]); // fitfun.valueOf() is to be minimized
}
cma.updateDistribution(fitness); // pass fitness array to update search distribution
// --- end core iteration step ---
// output to files and console
cma.writeToDefaultFiles();
int outmod = 150;
if (cma.getCountIter() % (15*outmod) == 1)
cma.printlnAnnotation(); // might write file as well
if (cma.getCountIter() % outmod == 1)
cma.println();
}
// evaluate mean value as it is the best estimator for the optimum
cma.setFitnessOfMeanX(fitfun.valueOf(cma.getMeanX())); // updates the best ever solution
// final output
cma.writeToDefaultFiles(1);
cma.println();
cma.println("Terminated due to");
for (String s : cma.stopConditions.getMessages())
cma.println(" " + s);
cma.println("best function value " + cma.getBestFunctionValue()
+ " at evaluation " + cma.getBestEvaluationNumber());
// we might return cma.getBestSolution() or cma.getBestX()
} // main
} // class