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:
Andreas Dräger 2008-03-27 15:55:39 +00:00
parent fe490f4b22
commit 2db50903f8

View File

@ -1,3 +1,6 @@
/*
* Copyright (c) ZBiT, University of Tübingen, Germany
*/
package javaeva;
import java.util.BitSet;
@ -64,25 +67,35 @@ import javaeva.server.modules.GOParameters;
* @since 2.0
* @author mkron
* @author Andreas Dr&auml;ger <andreas.draeger@uni-tuebingen.de>
* @date 17.04.2007 Copyright (c) ZBiT, University of T&uuml;bingen, Germany
* Compiler: JDK 1.5.0_10
* @date 17.04.2007
*/
public class OptimizerFactory {
private static InterfaceTerminator term = null;
public final static int STD_ES = 1;
public final static int CMA_ES = 2;
public final static int STD_GA = 3;
public final static int PSO = 4;
public final static int DE = 5;
public final static int TRIBES = 6;
public final static int RANDOM = 7;
public final static int HILLCL = 8;
public final static int CBN_ES = 9;
public final static int CL_HILLCL = 10;
public final static int defaultFitCalls = 10000;
public final static int randSeed = 0;
private static OptimizerRunnable lastRunnable = null;
/**
@ -102,7 +115,7 @@ public class OptimizerFactory {
bAnd));
}
public static GOParameters cbnES(AbstractOptimizationProblem problem) {
public static final GOParameters cbnES(AbstractOptimizationProblem problem) {
ClusterBasedNichingEA cbn = new ClusterBasedNichingEA();
EvolutionStrategies es = new EvolutionStrategies();
es.setMu(15);
@ -112,7 +125,7 @@ public class OptimizerFactory {
ClusteringDensityBased clustering = new ClusteringDensityBased(0.1);
cbn.setConvergenceCA((ClusteringDensityBased) clustering.clone());
cbn.setDifferentationCA(clustering);
cbn.setShowCycle(0); // dont do graphical output
cbn.setShowCycle(0); // don't do graphical output
Population pop = new Population();
pop.setPopulationSize(100);
@ -121,7 +134,7 @@ public class OptimizerFactory {
return makeParams(cbn, pop, problem, randSeed, defaultTerminator());
}
public static GOParameters clusteringHillClimbing(
public static final GOParameters clusteringHillClimbing(
AbstractOptimizationProblem problem) {
ClusteringHillClimbing chc = new ClusteringHillClimbing();
chc.SetProblem(problem);
@ -138,23 +151,25 @@ public class OptimizerFactory {
return makeParams(chc, pop, problem, randSeed, defaultTerminator());
}
public static GOParameters cmaES(AbstractOptimizationProblem problem) {
public static final GOParameters cmaES(AbstractOptimizationProblem problem) {
EvolutionStrategies es = new EvolutionStrategies();
es.setMu(15);
es.setLambda(50);
es.setPlusStrategy(false);
// TODO improve this by adding getEAIndividual to AbstractEAIndividual?
AbstractEAIndividual indyTemplate = problem.getIndividualTemplate();
if ((indyTemplate != null) && (indyTemplate instanceof InterfaceESIndividual)) {
AbstractEAIndividual indyTemplate = problem.getIndividualTemplate();
if ((indyTemplate != null)
&& (indyTemplate instanceof InterfaceESIndividual)) {
// Set CMA operator for mutation
AbstractEAIndividual indy = (AbstractEAIndividual)indyTemplate;
AbstractEAIndividual indy = (AbstractEAIndividual) indyTemplate;
MutateESCovarianceMartixAdaption cmaMut = new MutateESCovarianceMartixAdaption();
cmaMut.setCheckConstraints(true);
indy.setMutationOperator(cmaMut);
indy.setCrossoverOperator(new CrossoverESDefault());
} 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;
}
@ -335,7 +350,6 @@ public class OptimizerFactory {
tmpIndi.setCrossoverProbability(0);
HillClimbing hc = new HillClimbing();
// System.out.println(hc.getStringRepresentation());
hc.getPopulation().setPopulationSize(pop);
hc.addPopulationChangedEventListener(listener);
hc.SetProblem(problem);
@ -470,13 +484,14 @@ public class OptimizerFactory {
*
* @return the default number of fitness call done before termination
*/
public static int getDefaultFitCalls() {
public static final int getDefaultFitCalls() {
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) {
case STD_ES:
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;
GOParameters params = getParams(optType, problem);
if (params != null) {
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;
}
@ -524,7 +541,8 @@ public class OptimizerFactory {
return OptimizerFactory.term;
}
public static GOParameters hillClimbing(AbstractOptimizationProblem problem) {
public static final GOParameters hillClimbing(
AbstractOptimizationProblem problem) {
HillClimbing hc = new HillClimbing();
hc.SetProblem(problem);
Population pop = new Population();
@ -534,9 +552,7 @@ public class OptimizerFactory {
}
public static int lastEvalsPerformed() {
if (lastRunnable != null)
return lastRunnable.getProgress();
else return -1;
return (lastRunnable != null) ? lastRunnable.getProgress() : -1;
}
// /////////////////////// Creating default strategies
@ -552,7 +568,8 @@ public class OptimizerFactory {
return params;
}
public static GOParameters monteCarlo(AbstractOptimizationProblem problem) {
public static final GOParameters monteCarlo(
AbstractOptimizationProblem problem) {
MonteCarloSearch mc = new MonteCarloSearch();
Population pop = new Population();
pop.setPopulationSize(50);
@ -567,11 +584,10 @@ public class OptimizerFactory {
}
public static OptimizerRunnable optimize(OptimizerRunnable runnable) {
if (runnable != null) {
new Thread(runnable).run();
lastRunnable = runnable;
return runnable;
} else return null;
if (runnable == null) return null;
new Thread(runnable).run();
lastRunnable = runnable;
return runnable;
}
/**
@ -605,17 +621,13 @@ public class OptimizerFactory {
public static BitSet optimizeToBinary(final int optType,
AbstractOptimizationProblem problem, String outputFilePrefix) {
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
if (runnable != null)
return runnable.getBinarySolution();
else return null;
return (runnable != null) ? runnable.getBinarySolution() : null;
}
// ///////////////////////////// Optimize a given runnable
public static BitSet optimizeToBinary(OptimizerRunnable runnable) {
optimize(runnable);
if (runnable != null)
return runnable.getBinarySolution();
else return null;
return (runnable != null) ? runnable.getBinarySolution() : null;
}
public static double[] optimizeToDouble(GOParameters params,
@ -628,16 +640,12 @@ public class OptimizerFactory {
public static double[] optimizeToDouble(final int optType,
AbstractOptimizationProblem problem, String outputFilePrefix) {
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
if (runnable != null)
return runnable.getDoubleSolution();
else return null;
return (runnable != null) ? runnable.getDoubleSolution() : null;
}
public static double[] optimizeToDouble(OptimizerRunnable runnable) {
optimize(runnable);
if (runnable != null)
return runnable.getDoubleSolution();
else return null;
return (runnable != null) ? runnable.getDoubleSolution() : null;
}
public static IndividualInterface optimizeToInd(GOParameters params,
@ -650,16 +658,12 @@ public class OptimizerFactory {
public static IndividualInterface optimizeToInd(final int optType,
AbstractOptimizationProblem problem, String outputFilePrefix) {
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
if (runnable != null)
return runnable.getResult();
else return null;
return (runnable != null) ? runnable.getResult() : null;
}
public static IndividualInterface optimizeToInd(OptimizerRunnable runnable) {
optimize(runnable);
if (runnable != null)
return runnable.getResult();
else return null;
return (runnable != null) ? runnable.getResult() : null;
}
public static Population optimizeToPop(GOParameters params,
@ -672,16 +676,12 @@ public class OptimizerFactory {
public static Population optimizeToPop(final int optType,
AbstractOptimizationProblem problem, String outputFilePrefix) {
OptimizerRunnable runnable = optimize(optType, problem, outputFilePrefix);
if (runnable != null)
return runnable.getSolutionSet();
else return null;
return (runnable != null) ? runnable.getSolutionSet() : null;
}
public static Population optimizeToPop(OptimizerRunnable runnable) {
optimize(runnable);
if (runnable != null)
return runnable.getSolutionSet();
else return null;
return (runnable != null) ? runnable.getSolutionSet() : null;
}
public static Population postProcess(int steps, double sigma, int nBest) {
@ -746,9 +746,7 @@ public class OptimizerFactory {
public static Vector<double[]> postProcessDblVec(
InterfacePostProcessParams ppp) {
if (lastRunnable != null)
return postProcessDblVec(lastRunnable, ppp);
else return null;
return (lastRunnable != null) ? postProcessDblVec(lastRunnable, ppp) : null;
}
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";
}
public static GOParameters standardDE(AbstractOptimizationProblem problem) {
public static final GOParameters standardDE(
AbstractOptimizationProblem problem) {
DifferentialEvolution de = new DifferentialEvolution();
Population pop = new Population();
pop.setPopulationSize(50);
@ -839,7 +838,8 @@ public class OptimizerFactory {
return makeParams(de, pop, problem, randSeed, defaultTerminator());
}
public static GOParameters standardES(AbstractOptimizationProblem problem) {
public static final GOParameters standardES(
AbstractOptimizationProblem problem) {
EvolutionStrategies es = new EvolutionStrategies();
es.setMu(15);
es.setLambda(50);
@ -863,7 +863,8 @@ public class OptimizerFactory {
return makeParams(es, pop, problem, randSeed, defaultTerminator());
}
public static GOParameters standardGA(AbstractOptimizationProblem problem) {
public static final GOParameters standardGA(
AbstractOptimizationProblem problem) {
GeneticAlgorithm ga = new GeneticAlgorithm();
Population pop = new Population();
pop.setPopulationSize(100);
@ -873,7 +874,8 @@ public class OptimizerFactory {
return makeParams(ga, pop, problem, randSeed, defaultTerminator());
}
public static GOParameters standardPSO(AbstractOptimizationProblem problem) {
public static final GOParameters standardPSO(
AbstractOptimizationProblem problem) {
ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
Population pop = new Population();
pop.setPopulationSize(30);
@ -884,12 +886,10 @@ public class OptimizerFactory {
}
public static String terminatedBecause() {
if (lastRunnable != null)
return lastRunnable.terminatedBecause();
else return null;
return (lastRunnable != null) ? lastRunnable.terminatedBecause() : null;
}
public static GOParameters tribes(AbstractOptimizationProblem problem) {
public static final GOParameters tribes(AbstractOptimizationProblem problem) {
Tribes tr = new Tribes();
Population pop = new Population();
pop.setPopulationSize(1); // only for init