Improved the OptimizerFactory. However, the optimizer.init() methods have to be called after all settings were applied to the certain optimizer.
This commit is contained in:
parent
fe490f4b22
commit
2db50903f8
@ -1,3 +1,6 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) ZBiT, University of Tübingen, Germany
|
||||||
|
*/
|
||||||
package javaeva;
|
package javaeva;
|
||||||
|
|
||||||
import java.util.BitSet;
|
import java.util.BitSet;
|
||||||
@ -64,25 +67,35 @@ import javaeva.server.modules.GOParameters;
|
|||||||
* @since 2.0
|
* @since 2.0
|
||||||
* @author mkron
|
* @author mkron
|
||||||
* @author Andreas Dräger <andreas.draeger@uni-tuebingen.de>
|
* @author Andreas Dräger <andreas.draeger@uni-tuebingen.de>
|
||||||
* @date 17.04.2007 Copyright (c) ZBiT, University of Tübingen, Germany
|
* @date 17.04.2007
|
||||||
* Compiler: JDK 1.5.0_10
|
|
||||||
*/
|
*/
|
||||||
public class OptimizerFactory {
|
public class OptimizerFactory {
|
||||||
private static InterfaceTerminator term = null;
|
private static InterfaceTerminator term = null;
|
||||||
|
|
||||||
public final static int STD_ES = 1;
|
public final static int STD_ES = 1;
|
||||||
|
|
||||||
public final static int CMA_ES = 2;
|
public final static int CMA_ES = 2;
|
||||||
|
|
||||||
public final static int STD_GA = 3;
|
public final static int STD_GA = 3;
|
||||||
|
|
||||||
public final static int PSO = 4;
|
public final static int PSO = 4;
|
||||||
|
|
||||||
public final static int DE = 5;
|
public final static int DE = 5;
|
||||||
|
|
||||||
public final static int TRIBES = 6;
|
public final static int TRIBES = 6;
|
||||||
|
|
||||||
public final static int RANDOM = 7;
|
public final static int RANDOM = 7;
|
||||||
|
|
||||||
public final static int HILLCL = 8;
|
public final static int HILLCL = 8;
|
||||||
|
|
||||||
public final static int CBN_ES = 9;
|
public final static int CBN_ES = 9;
|
||||||
|
|
||||||
public final static int CL_HILLCL = 10;
|
public final static int CL_HILLCL = 10;
|
||||||
|
|
||||||
public final static int defaultFitCalls = 10000;
|
public final static int defaultFitCalls = 10000;
|
||||||
|
|
||||||
public final static int randSeed = 0;
|
public final static int randSeed = 0;
|
||||||
|
|
||||||
private static OptimizerRunnable lastRunnable = null;
|
private static OptimizerRunnable lastRunnable = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,7 +115,7 @@ public class OptimizerFactory {
|
|||||||
bAnd));
|
bAnd));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters cbnES(AbstractOptimizationProblem problem) {
|
public static final GOParameters cbnES(AbstractOptimizationProblem problem) {
|
||||||
ClusterBasedNichingEA cbn = new ClusterBasedNichingEA();
|
ClusterBasedNichingEA cbn = new ClusterBasedNichingEA();
|
||||||
EvolutionStrategies es = new EvolutionStrategies();
|
EvolutionStrategies es = new EvolutionStrategies();
|
||||||
es.setMu(15);
|
es.setMu(15);
|
||||||
@ -112,7 +125,7 @@ public class OptimizerFactory {
|
|||||||
ClusteringDensityBased clustering = new ClusteringDensityBased(0.1);
|
ClusteringDensityBased clustering = new ClusteringDensityBased(0.1);
|
||||||
cbn.setConvergenceCA((ClusteringDensityBased) clustering.clone());
|
cbn.setConvergenceCA((ClusteringDensityBased) clustering.clone());
|
||||||
cbn.setDifferentationCA(clustering);
|
cbn.setDifferentationCA(clustering);
|
||||||
cbn.setShowCycle(0); // dont do graphical output
|
cbn.setShowCycle(0); // don't do graphical output
|
||||||
|
|
||||||
Population pop = new Population();
|
Population pop = new Population();
|
||||||
pop.setPopulationSize(100);
|
pop.setPopulationSize(100);
|
||||||
@ -121,7 +134,7 @@ public class OptimizerFactory {
|
|||||||
return makeParams(cbn, pop, problem, randSeed, defaultTerminator());
|
return makeParams(cbn, pop, problem, randSeed, defaultTerminator());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters clusteringHillClimbing(
|
public static final GOParameters clusteringHillClimbing(
|
||||||
AbstractOptimizationProblem problem) {
|
AbstractOptimizationProblem problem) {
|
||||||
ClusteringHillClimbing chc = new ClusteringHillClimbing();
|
ClusteringHillClimbing chc = new ClusteringHillClimbing();
|
||||||
chc.SetProblem(problem);
|
chc.SetProblem(problem);
|
||||||
@ -138,7 +151,7 @@ public class OptimizerFactory {
|
|||||||
return makeParams(chc, pop, problem, randSeed, defaultTerminator());
|
return makeParams(chc, pop, problem, randSeed, defaultTerminator());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters cmaES(AbstractOptimizationProblem problem) {
|
public static final GOParameters cmaES(AbstractOptimizationProblem problem) {
|
||||||
EvolutionStrategies es = new EvolutionStrategies();
|
EvolutionStrategies es = new EvolutionStrategies();
|
||||||
es.setMu(15);
|
es.setMu(15);
|
||||||
es.setLambda(50);
|
es.setLambda(50);
|
||||||
@ -146,7 +159,8 @@ public class OptimizerFactory {
|
|||||||
|
|
||||||
// TODO improve this by adding getEAIndividual to AbstractEAIndividual?
|
// TODO improve this by adding getEAIndividual to AbstractEAIndividual?
|
||||||
AbstractEAIndividual indyTemplate = problem.getIndividualTemplate();
|
AbstractEAIndividual indyTemplate = problem.getIndividualTemplate();
|
||||||
if ((indyTemplate != null) && (indyTemplate instanceof InterfaceESIndividual)) {
|
if ((indyTemplate != null)
|
||||||
|
&& (indyTemplate instanceof InterfaceESIndividual)) {
|
||||||
// Set CMA operator for mutation
|
// Set CMA operator for mutation
|
||||||
AbstractEAIndividual indy = (AbstractEAIndividual) indyTemplate;
|
AbstractEAIndividual indy = (AbstractEAIndividual) indyTemplate;
|
||||||
MutateESCovarianceMartixAdaption cmaMut = new MutateESCovarianceMartixAdaption();
|
MutateESCovarianceMartixAdaption cmaMut = new MutateESCovarianceMartixAdaption();
|
||||||
@ -154,7 +168,8 @@ public class OptimizerFactory {
|
|||||||
indy.setMutationOperator(cmaMut);
|
indy.setMutationOperator(cmaMut);
|
||||||
indy.setCrossoverOperator(new CrossoverESDefault());
|
indy.setCrossoverOperator(new CrossoverESDefault());
|
||||||
} else {
|
} else {
|
||||||
System.err.println("Error, CMA-ES is implemented for ES individuals only (requires double data types)");
|
System.err
|
||||||
|
.println("Error, CMA-ES is implemented for ES individuals only (requires double data types)");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -335,7 +350,6 @@ public class OptimizerFactory {
|
|||||||
tmpIndi.setCrossoverProbability(0);
|
tmpIndi.setCrossoverProbability(0);
|
||||||
|
|
||||||
HillClimbing hc = new HillClimbing();
|
HillClimbing hc = new HillClimbing();
|
||||||
// System.out.println(hc.getStringRepresentation());
|
|
||||||
hc.getPopulation().setPopulationSize(pop);
|
hc.getPopulation().setPopulationSize(pop);
|
||||||
hc.addPopulationChangedEventListener(listener);
|
hc.addPopulationChangedEventListener(listener);
|
||||||
hc.SetProblem(problem);
|
hc.SetProblem(problem);
|
||||||
@ -470,13 +484,14 @@ public class OptimizerFactory {
|
|||||||
*
|
*
|
||||||
* @return the default number of fitness call done before termination
|
* @return the default number of fitness call done before termination
|
||||||
*/
|
*/
|
||||||
public static int getDefaultFitCalls() {
|
public static final int getDefaultFitCalls() {
|
||||||
return defaultFitCalls;
|
return defaultFitCalls;
|
||||||
}
|
}
|
||||||
|
|
||||||
// /////////////////////////// constructing a default OptimizerRunnable
|
// /////////////////////////// constructing a default OptimizerRunnable
|
||||||
|
|
||||||
public static GOParameters getParams(final int optType, AbstractOptimizationProblem problem) {
|
public static GOParameters getParams(final int optType,
|
||||||
|
AbstractOptimizationProblem problem) {
|
||||||
switch (optType) {
|
switch (optType) {
|
||||||
case STD_ES:
|
case STD_ES:
|
||||||
return standardES(problem);
|
return standardES(problem);
|
||||||
@ -504,12 +519,14 @@ public class OptimizerFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OptimizerRunnable getOptRunnable(final int optType, AbstractOptimizationProblem problem, int fitCalls, String outputFilePrefix) {
|
public static OptimizerRunnable getOptRunnable(final int optType,
|
||||||
|
AbstractOptimizationProblem problem, int fitCalls, String outputFilePrefix) {
|
||||||
OptimizerRunnable opt = null;
|
OptimizerRunnable opt = null;
|
||||||
GOParameters params = getParams(optType, problem);
|
GOParameters params = getParams(optType, problem);
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
opt = new OptimizerRunnable(params, outputFilePrefix);
|
opt = new OptimizerRunnable(params, outputFilePrefix);
|
||||||
if (fitCalls != defaultFitCalls) opt.getGOParams().setTerminator(new EvaluationTerminator(fitCalls));
|
if (fitCalls != defaultFitCalls)
|
||||||
|
opt.getGOParams().setTerminator(new EvaluationTerminator(fitCalls));
|
||||||
}
|
}
|
||||||
return opt;
|
return opt;
|
||||||
}
|
}
|
||||||
@ -524,7 +541,8 @@ public class OptimizerFactory {
|
|||||||
return OptimizerFactory.term;
|
return OptimizerFactory.term;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters hillClimbing(AbstractOptimizationProblem problem) {
|
public static final GOParameters hillClimbing(
|
||||||
|
AbstractOptimizationProblem problem) {
|
||||||
HillClimbing hc = new HillClimbing();
|
HillClimbing hc = new HillClimbing();
|
||||||
hc.SetProblem(problem);
|
hc.SetProblem(problem);
|
||||||
Population pop = new Population();
|
Population pop = new Population();
|
||||||
@ -534,9 +552,7 @@ public class OptimizerFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static int lastEvalsPerformed() {
|
public static int lastEvalsPerformed() {
|
||||||
if (lastRunnable != null)
|
return (lastRunnable != null) ? lastRunnable.getProgress() : -1;
|
||||||
return lastRunnable.getProgress();
|
|
||||||
else return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /////////////////////// Creating default strategies
|
// /////////////////////// Creating default strategies
|
||||||
@ -552,7 +568,8 @@ public class OptimizerFactory {
|
|||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters monteCarlo(AbstractOptimizationProblem problem) {
|
public static final GOParameters monteCarlo(
|
||||||
|
AbstractOptimizationProblem problem) {
|
||||||
MonteCarloSearch mc = new MonteCarloSearch();
|
MonteCarloSearch mc = new MonteCarloSearch();
|
||||||
Population pop = new Population();
|
Population pop = new Population();
|
||||||
pop.setPopulationSize(50);
|
pop.setPopulationSize(50);
|
||||||
@ -567,11 +584,10 @@ public class OptimizerFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static OptimizerRunnable optimize(OptimizerRunnable runnable) {
|
public static OptimizerRunnable optimize(OptimizerRunnable runnable) {
|
||||||
if (runnable != null) {
|
if (runnable == null) return null;
|
||||||
new Thread(runnable).run();
|
new Thread(runnable).run();
|
||||||
lastRunnable = runnable;
|
lastRunnable = runnable;
|
||||||
return runnable;
|
return runnable;
|
||||||
} else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -605,17 +621,13 @@ public class OptimizerFactory {
|
|||||||
public static BitSet optimizeToBinary(final int optType,
|
public static BitSet optimizeToBinary(final int optType,
|
||||||
AbstractOptimizationProblem problem, String outputFilePrefix) {
|
AbstractOptimizationProblem problem, String outputFilePrefix) {
|
||||||
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
|
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
|
||||||
if (runnable != null)
|
return (runnable != null) ? runnable.getBinarySolution() : null;
|
||||||
return runnable.getBinarySolution();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ///////////////////////////// Optimize a given runnable
|
// ///////////////////////////// Optimize a given runnable
|
||||||
public static BitSet optimizeToBinary(OptimizerRunnable runnable) {
|
public static BitSet optimizeToBinary(OptimizerRunnable runnable) {
|
||||||
optimize(runnable);
|
optimize(runnable);
|
||||||
if (runnable != null)
|
return (runnable != null) ? runnable.getBinarySolution() : null;
|
||||||
return runnable.getBinarySolution();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double[] optimizeToDouble(GOParameters params,
|
public static double[] optimizeToDouble(GOParameters params,
|
||||||
@ -628,16 +640,12 @@ public class OptimizerFactory {
|
|||||||
public static double[] optimizeToDouble(final int optType,
|
public static double[] optimizeToDouble(final int optType,
|
||||||
AbstractOptimizationProblem problem, String outputFilePrefix) {
|
AbstractOptimizationProblem problem, String outputFilePrefix) {
|
||||||
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
|
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
|
||||||
if (runnable != null)
|
return (runnable != null) ? runnable.getDoubleSolution() : null;
|
||||||
return runnable.getDoubleSolution();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double[] optimizeToDouble(OptimizerRunnable runnable) {
|
public static double[] optimizeToDouble(OptimizerRunnable runnable) {
|
||||||
optimize(runnable);
|
optimize(runnable);
|
||||||
if (runnable != null)
|
return (runnable != null) ? runnable.getDoubleSolution() : null;
|
||||||
return runnable.getDoubleSolution();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IndividualInterface optimizeToInd(GOParameters params,
|
public static IndividualInterface optimizeToInd(GOParameters params,
|
||||||
@ -650,16 +658,12 @@ public class OptimizerFactory {
|
|||||||
public static IndividualInterface optimizeToInd(final int optType,
|
public static IndividualInterface optimizeToInd(final int optType,
|
||||||
AbstractOptimizationProblem problem, String outputFilePrefix) {
|
AbstractOptimizationProblem problem, String outputFilePrefix) {
|
||||||
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
|
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
|
||||||
if (runnable != null)
|
return (runnable != null) ? runnable.getResult() : null;
|
||||||
return runnable.getResult();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IndividualInterface optimizeToInd(OptimizerRunnable runnable) {
|
public static IndividualInterface optimizeToInd(OptimizerRunnable runnable) {
|
||||||
optimize(runnable);
|
optimize(runnable);
|
||||||
if (runnable != null)
|
return (runnable != null) ? runnable.getResult() : null;
|
||||||
return runnable.getResult();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Population optimizeToPop(GOParameters params,
|
public static Population optimizeToPop(GOParameters params,
|
||||||
@ -672,16 +676,12 @@ public class OptimizerFactory {
|
|||||||
public static Population optimizeToPop(final int optType,
|
public static Population optimizeToPop(final int optType,
|
||||||
AbstractOptimizationProblem problem, String outputFilePrefix) {
|
AbstractOptimizationProblem problem, String outputFilePrefix) {
|
||||||
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
|
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
|
||||||
if (runnable != null)
|
return (runnable != null) ? runnable.getSolutionSet() : null;
|
||||||
return runnable.getSolutionSet();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Population optimizeToPop(OptimizerRunnable runnable) {
|
public static Population optimizeToPop(OptimizerRunnable runnable) {
|
||||||
optimize(runnable);
|
optimize(runnable);
|
||||||
if (runnable != null)
|
return (runnable != null) ? runnable.getSolutionSet() : null;
|
||||||
return runnable.getSolutionSet();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Population postProcess(int steps, double sigma, int nBest) {
|
public static Population postProcess(int steps, double sigma, int nBest) {
|
||||||
@ -746,9 +746,7 @@ public class OptimizerFactory {
|
|||||||
|
|
||||||
public static Vector<double[]> postProcessDblVec(
|
public static Vector<double[]> postProcessDblVec(
|
||||||
InterfacePostProcessParams ppp) {
|
InterfacePostProcessParams ppp) {
|
||||||
if (lastRunnable != null)
|
return (lastRunnable != null) ? postProcessDblVec(lastRunnable, ppp) : null;
|
||||||
return postProcessDblVec(lastRunnable, ppp);
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Vector<double[]> postProcessDblVec(OptimizerRunnable runnable,
|
public static Vector<double[]> postProcessDblVec(OptimizerRunnable runnable,
|
||||||
@ -825,7 +823,8 @@ public class OptimizerFactory {
|
|||||||
+ "\n8: Hill-Climbing \n9: Cluster-based niching ES \n10: Clustering Hill-Climbing";
|
+ "\n8: Hill-Climbing \n9: Cluster-based niching ES \n10: Clustering Hill-Climbing";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters standardDE(AbstractOptimizationProblem problem) {
|
public static final GOParameters standardDE(
|
||||||
|
AbstractOptimizationProblem problem) {
|
||||||
DifferentialEvolution de = new DifferentialEvolution();
|
DifferentialEvolution de = new DifferentialEvolution();
|
||||||
Population pop = new Population();
|
Population pop = new Population();
|
||||||
pop.setPopulationSize(50);
|
pop.setPopulationSize(50);
|
||||||
@ -839,7 +838,8 @@ public class OptimizerFactory {
|
|||||||
return makeParams(de, pop, problem, randSeed, defaultTerminator());
|
return makeParams(de, pop, problem, randSeed, defaultTerminator());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters standardES(AbstractOptimizationProblem problem) {
|
public static final GOParameters standardES(
|
||||||
|
AbstractOptimizationProblem problem) {
|
||||||
EvolutionStrategies es = new EvolutionStrategies();
|
EvolutionStrategies es = new EvolutionStrategies();
|
||||||
es.setMu(15);
|
es.setMu(15);
|
||||||
es.setLambda(50);
|
es.setLambda(50);
|
||||||
@ -863,7 +863,8 @@ public class OptimizerFactory {
|
|||||||
return makeParams(es, pop, problem, randSeed, defaultTerminator());
|
return makeParams(es, pop, problem, randSeed, defaultTerminator());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters standardGA(AbstractOptimizationProblem problem) {
|
public static final GOParameters standardGA(
|
||||||
|
AbstractOptimizationProblem problem) {
|
||||||
GeneticAlgorithm ga = new GeneticAlgorithm();
|
GeneticAlgorithm ga = new GeneticAlgorithm();
|
||||||
Population pop = new Population();
|
Population pop = new Population();
|
||||||
pop.setPopulationSize(100);
|
pop.setPopulationSize(100);
|
||||||
@ -873,7 +874,8 @@ public class OptimizerFactory {
|
|||||||
return makeParams(ga, pop, problem, randSeed, defaultTerminator());
|
return makeParams(ga, pop, problem, randSeed, defaultTerminator());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters standardPSO(AbstractOptimizationProblem problem) {
|
public static final GOParameters standardPSO(
|
||||||
|
AbstractOptimizationProblem problem) {
|
||||||
ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
|
ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
|
||||||
Population pop = new Population();
|
Population pop = new Population();
|
||||||
pop.setPopulationSize(30);
|
pop.setPopulationSize(30);
|
||||||
@ -884,12 +886,10 @@ public class OptimizerFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String terminatedBecause() {
|
public static String terminatedBecause() {
|
||||||
if (lastRunnable != null)
|
return (lastRunnable != null) ? lastRunnable.terminatedBecause() : null;
|
||||||
return lastRunnable.terminatedBecause();
|
|
||||||
else return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GOParameters tribes(AbstractOptimizationProblem problem) {
|
public static final GOParameters tribes(AbstractOptimizationProblem problem) {
|
||||||
Tribes tr = new Tribes();
|
Tribes tr = new Tribes();
|
||||||
Population pop = new Population();
|
Population pop = new Population();
|
||||||
pop.setPopulationSize(1); // only for init
|
pop.setPopulationSize(1); // only for init
|
||||||
|
Loading…
x
Reference in New Issue
Block a user