Added examples from the EvA2Doc
This commit is contained in:
parent
ee7ac6efc6
commit
98ece7d783
@ -1,11 +1,12 @@
|
|||||||
clear all
|
%% Usage examples for the EvA2 to Matlab interface.
|
||||||
clear classes
|
% Author: Marcel Kronfeld, Chair for Cognitive Systems, University of Tuebingen, Germany
|
||||||
|
% URL: http://www.ra.cs.uni-tuebingen.de/software/EvA2/
|
||||||
|
|
||||||
% adapt the path settings!
|
% adapt the path settings!
|
||||||
addpath '/home/mkron/workspace/JE2Base/resources/MatlabInterface'
|
addpath '/home/user/workspace/MatlabInterface' % .. directory containing @JEInterface
|
||||||
javaaddpath '/home/mkron/workspace/JE2Base/build'
|
javaaddpath '/home/user/workspace/EvA2Base.jar' % .. the EvA2 base package
|
||||||
addpath 'C:\Dokumente und Einstellungen\mkron\workspace\JE2Base\resources\MatlabInterface'
|
% addpath 'C:\Dokumente und Einstellungen\user\workspace\MatlabInterface' % Windows will look differently
|
||||||
javaaddpath 'C:\Dokumente und Einstellungen\mkron\workspace\JE2Base\build'
|
% javaaddpath 'C:\Dokumente und Einstellungen\user\workspace\EvA2Base.jar' % Windows will look differently
|
||||||
|
|
||||||
% real valued case
|
% real valued case
|
||||||
R=[-5 -5 -5; 5 5 5];
|
R=[-5 -5 -5; 5 5 5];
|
||||||
@ -28,4 +29,3 @@ JI=JEInterface(@testfun, 'int', R, initR, 5, 'Display', 'iter');
|
|||||||
JI=optimize(JI, 3);
|
JI=optimize(JI, 3);
|
||||||
[sol, fit]=getResult(JI);
|
[sol, fit]=getResult(JI);
|
||||||
finalPop=getMultipleResults(JI);
|
finalPop=getMultipleResults(JI);
|
||||||
|
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
|
%% Usage examples for the EvA2 to Matlab interface.
|
||||||
|
% Author: Marcel Kronfeld, Chair for Cognitive Systems, University of Tuebingen, Germany
|
||||||
|
% URL: http://www.ra.cs.uni-tuebingen.de/software/EvA2/
|
||||||
|
|
||||||
function z = testfun(x, y)
|
function z = testfun(x, y)
|
||||||
switch y
|
switch y
|
||||||
case 1 % modulated parabola
|
case 1 % modulated parabola
|
||||||
|
31
src/eva2/examples/TerminatorExample.java
Normal file
31
src/eva2/examples/TerminatorExample.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package eva2.examples;
|
||||||
|
import eva2.OptimizerFactory;
|
||||||
|
import eva2.server.go.problems.F1Problem;
|
||||||
|
import eva2.server.go.operators.terminators.*;
|
||||||
|
import eva2.server.go.operators.terminators.PopulationMeasureTerminator.*;
|
||||||
|
|
||||||
|
public class TerminatorExample {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
F1Problem f1 = new F1Problem();
|
||||||
|
double[] sol;
|
||||||
|
// A combined terminator for fitness and phenotype convergence
|
||||||
|
CombinedTerminator convT = new CombinedTerminator(
|
||||||
|
// fitness-based stagnation period, absolute threshold, consider stagnation
|
||||||
|
// in both direction (per dim.) or w.r.t. minimization only
|
||||||
|
new FitnessConvergenceTerminator(0.0001, 1000, StagnationTypeEnum.fitnessCallBased, ChangeTypeEnum.absoluteChange, DirectionTypeEnum.decrease),
|
||||||
|
new PhenotypeConvergenceTerminator(0.0001, 1000, StagnationTypeEnum.fitnessCallBased, ChangeTypeEnum.absoluteChange, DirectionTypeEnum.bidirectional),
|
||||||
|
CombinedTerminator.AND);
|
||||||
|
// Adding an evaluation terminator with OR to the convergence criterion
|
||||||
|
OptimizerFactory.setTerminator(new CombinedTerminator(
|
||||||
|
new EvaluationTerminator(20000),
|
||||||
|
convT,
|
||||||
|
CombinedTerminator.OR));
|
||||||
|
sol = OptimizerFactory.optimizeToDouble(OptimizerFactory.PSO, f1, null);
|
||||||
|
System.out.println(OptimizerFactory.lastEvalsPerformed()
|
||||||
|
+ " evals performed. "
|
||||||
|
+ OptimizerFactory.terminatedBecause()
|
||||||
|
+ " Found solution: ");
|
||||||
|
for (int i=0; i<f1.getProblemDimension(); i++) System.out.print(sol[i] + " ");
|
||||||
|
System.out.println();
|
||||||
|
};
|
||||||
|
}
|
39
src/eva2/examples/TestingCbnPostProc.java
Normal file
39
src/eva2/examples/TestingCbnPostProc.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package eva2.examples;
|
||||||
|
import java.util.Vector;
|
||||||
|
import eva2.OptimizerFactory;
|
||||||
|
import eva2.server.go.individuals.AbstractEAIndividual;
|
||||||
|
import eva2.server.go.operators.postprocess.PostProcessParams;
|
||||||
|
import eva2.server.go.operators.terminators.EvaluationTerminator;
|
||||||
|
import eva2.server.go.problems.FM0Problem;
|
||||||
|
import eva2.server.modules.GOParameters;
|
||||||
|
|
||||||
|
public class TestingCbnPostProc {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// a simple bimodal target function, two optima near (1.7,0) and (-1.44/0)
|
||||||
|
FM0Problem fm0 = new FM0Problem();
|
||||||
|
AbstractEAIndividual best;
|
||||||
|
Vector<AbstractEAIndividual> ppSols;
|
||||||
|
|
||||||
|
GOParameters esParams = OptimizerFactory.standardCbnES(fm0);
|
||||||
|
esParams.setTerminator(new EvaluationTerminator(2000));
|
||||||
|
esParams.setSeed(0);
|
||||||
|
best = (AbstractEAIndividual)OptimizerFactory.optimizeToInd(esParams, null);
|
||||||
|
|
||||||
|
System.out.println(esParams.getTerminator().lastTerminationMessage() + "\nFound solution: "
|
||||||
|
+ AbstractEAIndividual.getDefaultDataString(best));
|
||||||
|
|
||||||
|
// post-process with clustering only
|
||||||
|
ppSols = OptimizerFactory.postProcessIndVec(new PostProcessParams(0, 0.1, 5));
|
||||||
|
System.out.println("After clustering: ");
|
||||||
|
for (AbstractEAIndividual indy : ppSols) {
|
||||||
|
System.out.println(AbstractEAIndividual.getDefaultDataString(indy));
|
||||||
|
}
|
||||||
|
|
||||||
|
// post-process with clustering and hill climbing
|
||||||
|
ppSols = OptimizerFactory.postProcessIndVec(new PostProcessParams(1000, 0.1, 5));
|
||||||
|
System.out.println("After clustering and local refinement: ");
|
||||||
|
for (AbstractEAIndividual indy : ppSols) {
|
||||||
|
System.out.println(AbstractEAIndividual.getDefaultDataString(indy));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
15
src/eva2/examples/TestingF1PSO.java
Normal file
15
src/eva2/examples/TestingF1PSO.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package eva2.examples;
|
||||||
|
import eva2.OptimizerFactory;
|
||||||
|
import eva2.server.go.problems.F1Problem;
|
||||||
|
|
||||||
|
public class TestingF1PSO {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
F1Problem f1 = new F1Problem();
|
||||||
|
// start a PSO with a runtime of 50000 evaluations
|
||||||
|
OptimizerFactory.setEvaluationTerminator(50000);
|
||||||
|
double[] sol = OptimizerFactory.optimizeToDouble(OptimizerFactory.PSO, f1, null);
|
||||||
|
System.out.println(OptimizerFactory.terminatedBecause() + "\nFound solution: ");
|
||||||
|
for (int i=0; i<f1.getProblemDimension(); i++) System.out.print(sol[i] + " ");
|
||||||
|
System.out.println();
|
||||||
|
};
|
||||||
|
}
|
35
src/eva2/examples/TestingGAB1.java
Normal file
35
src/eva2/examples/TestingGAB1.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package eva2.examples;
|
||||||
|
import java.util.BitSet;
|
||||||
|
import eva2.OptimizerFactory;
|
||||||
|
import eva2.server.go.operators.selection.SelectXProbRouletteWheel;
|
||||||
|
import eva2.server.go.operators.terminators.EvaluationTerminator;
|
||||||
|
import eva2.server.go.populations.Population;
|
||||||
|
import eva2.server.go.problems.B1Problem;
|
||||||
|
import eva2.server.go.strategies.GeneticAlgorithm;
|
||||||
|
import eva2.server.modules.GOParameters;
|
||||||
|
|
||||||
|
public class TestingGAB1 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
B1Problem b1 = new B1Problem();
|
||||||
|
BitSet sol;
|
||||||
|
// default go-parameter instance with a GA
|
||||||
|
GOParameters gaParams = OptimizerFactory.standardGA(b1);
|
||||||
|
// add an evaluation terminator
|
||||||
|
gaParams.setTerminator(new EvaluationTerminator(1000));
|
||||||
|
// set a specific random seed
|
||||||
|
gaParams.setSeed(2342);
|
||||||
|
|
||||||
|
// access the GA
|
||||||
|
GeneticAlgorithm ga = (GeneticAlgorithm)gaParams.getOptimizer();
|
||||||
|
ga.setElitism(false);
|
||||||
|
ga.setParentSelection(new SelectXProbRouletteWheel()); // roulette wheel selection
|
||||||
|
ga.setPopulation(new Population(150)); // population size 150
|
||||||
|
|
||||||
|
// run optimization and print intermediate results to a file with given prefix
|
||||||
|
sol = OptimizerFactory.optimizeToBinary(gaParams, "ga-opt-results");
|
||||||
|
System.out.println(OptimizerFactory.terminatedBecause() + "\nFound solution: ");
|
||||||
|
for (int i=0; i<b1.getProblemDimension(); i++) System.out.print(sol.get(i)+" ");
|
||||||
|
System.out.println();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
40
src/eva2/examples/TestingPlusCmaEs.java
Normal file
40
src/eva2/examples/TestingPlusCmaEs.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package eva2.examples;
|
||||||
|
import eva2.OptimizerFactory;
|
||||||
|
import eva2.server.go.individuals.AbstractEAIndividual;
|
||||||
|
import eva2.server.go.operators.crossover.CrossoverESDefault;
|
||||||
|
import eva2.server.go.operators.mutation.MutateESCovarianceMatrixAdaption;
|
||||||
|
import eva2.server.go.operators.terminators.EvaluationTerminator;
|
||||||
|
import eva2.server.go.problems.FM0Problem;
|
||||||
|
import eva2.server.go.strategies.EvolutionStrategies;
|
||||||
|
import eva2.server.modules.GOParameters;
|
||||||
|
|
||||||
|
public class TestingPlusCmaEs {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// a simple bimodal target function, two optima near (1.7,0) and (-1.44/0)
|
||||||
|
FM0Problem fm0 = new FM0Problem();
|
||||||
|
AbstractEAIndividual bestIndy;
|
||||||
|
// create standard ES parameters
|
||||||
|
GOParameters esParams = OptimizerFactory.standardES(fm0);
|
||||||
|
esParams.setTerminator(new EvaluationTerminator(2000));
|
||||||
|
// set a random seed based on system time
|
||||||
|
esParams.setSeed(0);
|
||||||
|
|
||||||
|
// set evolutionary operators and probabilities
|
||||||
|
AbstractEAIndividual.setOperators(
|
||||||
|
fm0.getIndividualTemplate(),
|
||||||
|
new MutateESCovarianceMatrixAdaption(true), 0.9,
|
||||||
|
new CrossoverESDefault(), 0.1);
|
||||||
|
|
||||||
|
// access the ES
|
||||||
|
EvolutionStrategies es = (EvolutionStrategies)esParams.getOptimizer();
|
||||||
|
// set a (1+5) selection strategy
|
||||||
|
es.setMu(1);
|
||||||
|
es.setLambda(5);
|
||||||
|
es.setPlusStrategy(true);
|
||||||
|
|
||||||
|
// run optimization and retrieve winner individual
|
||||||
|
bestIndy = (AbstractEAIndividual)OptimizerFactory.optimizeToInd(esParams, null);
|
||||||
|
System.out.println(esParams.getTerminator().lastTerminationMessage() + "\nFound solution: "
|
||||||
|
+ AbstractEAIndividual.getDefaultDataString(bestIndy));
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user