fr.inria.optimization.cmaes.examples
Class CMAExample1
java.lang.Object
fr.inria.optimization.cmaes.examples.CMAExample1
public class CMAExample1
- extends java.lang.Object
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 CMAEvolutionStrategy.
For implementation of restarts see CMAExample2.
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
- Author:
- Nikolaus Hansen, released into public domain.
- See Also:
CMAEvolutionStrategy
|
Method Summary |
static void |
main(java.lang.String[] args)
|
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
CMAExample1
public CMAExample1()
main
public static void main(java.lang.String[] args)