From c79d30e3e301df05c67961461564a53ba4c4a1ec Mon Sep 17 00:00:00 2001 From: Marcel Kronfeld Date: Fri, 19 Nov 2010 09:37:22 +0000 Subject: [PATCH] Adding the ERP starter which allows running the external runtime problem directly from the console. And some minor cosmetics. --- .../problems/AbstractProblemDoubleOffset.java | 2 +- src/eva2/server/go/problems/ERPStarter.java | 171 ++++++++++++++++++ .../strategies/ParticleSwarmOptimization.java | 13 +- 3 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 src/eva2/server/go/problems/ERPStarter.java diff --git a/src/eva2/server/go/problems/AbstractProblemDoubleOffset.java b/src/eva2/server/go/problems/AbstractProblemDoubleOffset.java index 5880c50f..64deda0c 100644 --- a/src/eva2/server/go/problems/AbstractProblemDoubleOffset.java +++ b/src/eva2/server/go/problems/AbstractProblemDoubleOffset.java @@ -11,7 +11,7 @@ public abstract class AbstractProblemDoubleOffset extends AbstractProblemDouble // protected AbstractEAIndividual m_OverallBest = null; protected int m_ProblemDimension = 10; - protected double m_XOffSet = 0.0; + protected double m_XOffSet = 0.0; // TODO make them private, implement eval() and create abstract evalWithoutOffsets protected double m_YOffSet = 0.0; // protected boolean m_UseTestConstraint = false; diff --git a/src/eva2/server/go/problems/ERPStarter.java b/src/eva2/server/go/problems/ERPStarter.java new file mode 100644 index 00000000..1cae393a --- /dev/null +++ b/src/eva2/server/go/problems/ERPStarter.java @@ -0,0 +1,171 @@ +package eva2.server.go.problems; + +import eva2.OptimizerFactory; +import eva2.OptimizerRunnable; +import eva2.client.EvAClient; +import eva2.gui.BeanInspector; +import eva2.server.go.individuals.AbstractEAIndividual; +import eva2.server.go.operators.crossover.CrossoverESDefault; +import eva2.server.go.operators.moso.MOSONoConvert; +import eva2.server.go.operators.moso.MOSOWeightedFitness; +import eva2.server.go.operators.mutation.MutateESRankMuCMA; +import eva2.server.go.operators.selection.SelectBestIndividuals; +import eva2.server.go.operators.terminators.EvaluationTerminator; +import eva2.server.go.populations.InterfaceSolutionSet; +import eva2.server.go.populations.Population; +import eva2.server.go.strategies.GeneticAlgorithm; +import eva2.server.go.strategies.InterfaceOptimizer; +import eva2.server.modules.GOParameters; +import eva2.server.stat.StatisticsStandalone; +import eva2.tools.BasicResourceLoader; +import eva2.tools.StringTools; +import eva2.tools.ToolBox; +import eva2.tools.math.Mathematics; + +public class ERPStarter { + + /** + * Start an external runtime problem with some basic configs. The + * default variant uses equally weighs all objectives through an equally weighted sum. + * Arguments: --csv --cmd --maxEvals [--multiObjective] [--gui] + * @param args + */ + public static void main(String[] args) { + ExternalRuntimeProblem erp; + boolean useMultiObjective=false, startGUI=false; + // define the number of evaluations + int maxEvals = 5000; + // prefix for data output file - set null to deactivate + String outputFilePrefix = "erpTest"; + + /** Argument handling ****************/ + String[] keys= new String[]{"--gui", "--multiObjective", "--csv", "--cmd", "--maxEvals"}; + int[] arities = new int[]{0, 0, 1, 1, 1}; + Object[] values = new Object[6]; + + Integer[] unknownArgs = StringTools.parseArguments(args, keys, arities, values, true); + + if (unknownArgs.length>0 || (values[2]==null) || (values[3]==null) || (values[4]==null)) { + System.err.println("Missing or unrecognized command line options: "); + for (int i=0; i --cmd --maxEvals [--multiObjective] [--gui]"); + return; + } + System.out.println("Parsing file: " + values[2]); + erp = parseCSV((String)values[2]); + String cmd = (String)values[3]; + + maxEvals = Integer.parseInt((String)values[4]); + System.out.println("Setting maxEvals: " + maxEvals); + useMultiObjective=(values[1]!=null); + startGUI=(values[0]!=null); + // set target function and working directory + System.out.println("Setting target function command: " + cmd); + erp.setCommand(cmd); + int indexOfLastSlash = cmd.lastIndexOf(System.getProperty("file.separator")); + erp.setWorkingDirectory(cmd.substring(0, indexOfLastSlash)); + + /******************/ + // create optimization instance + InterfaceOptimizer opt = null; + if (useMultiObjective) { + // this uses a multi-objective strategy + erp.setMosoConverter(new MOSONoConvert()); + opt = OptimizerFactory.createMultiObjectiveEA(new GeneticAlgorithm(), 50, erp, null); + } else { + // this uses a single objective (5,20)-CMA-ES with equally weighted criteria +// erp.setMosoConverter(new MOSOWeightedFitness()); + opt = OptimizerFactory.createEvolutionStrategy(5, 20, false, new MutateESRankMuCMA(), 1., new CrossoverESDefault(), 0., new SelectBestIndividuals(), erp, null); + } + + System.out.println("Optimizer instance: " + BeanInspector.toString(opt)); + System.out.println("Problem instance: " + BeanInspector.toString(erp)); + // Instantiate optimization + GOParameters params = new GOParameters(opt, erp, new EvaluationTerminator(maxEvals)); + if (startGUI) { + EvAClient.initClientGUI(params, null, null, null); + } else { + OptimizerRunnable rnbl = new OptimizerRunnable(params, new StatisticsStandalone(outputFilePrefix, 1, 3, true), false); + // actually start the optimization + OptimizerFactory.optimize(rnbl); + + // some data output + InterfaceSolutionSet solSet = opt.getAllSolutions(); + Population sols = solSet.getSolutions(); + System.out.println("*** Solutions found: " + sols.size()); + System.out.println("Best allover solution: " + AbstractEAIndividual.getDefaultStringRepresentation((AbstractEAIndividual)rnbl.getResult())); + System.out.println("Best last solution: " + AbstractEAIndividual.getDefaultStringRepresentation(solSet.getCurrentPopulation().getBestEAIndividual())); + System.out.println("Last solution set:"); + for (int i=0; i1)) { + double[][] weights = ToolBox.getCols(filteredObjectives, 6); + if (Mathematics.areFinite(weights)<0) erp.setMosoConverter(new MOSOWeightedFitness(weights)); + } + erp.setProblemDimension(filteredVars.length); + erp.setRange(range); +// erp.setInitialRange(range); + // produce an initial range around the seed position if available, otherwise use domain range + double[][] initialRange = new double[filteredVars.length][2]; + for (int i=0; i= 0) && (tmpIndex < pop.size())) { this.compareAndSetAttractor(localBestFitness, localBestPosition, (AbstractEAIndividual)pop.get(tmpIndex), useHistoric); } @@ -1820,7 +1821,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se // tree, hpso // GenericObjectEditor.setShowProperty(cls, "treeBranchDegree", (topology==PSOTopologyEnum.tree) || (topology==PSOTopologyEnum.hpso)); // linear - GenericObjectEditor.setShowProperty(cls, "wrapTopology", (topology==PSOTopologyEnum.linear)); + GenericObjectEditor.setShowProperty(cls, "wrapTopology", (topology==PSOTopologyEnum.linear) || (topology==PSOTopologyEnum.grid)); // dms GenericObjectEditor.setShowProperty(cls, "dmsRegroupGens", (topology==PSOTopologyEnum.dms)); } @@ -1888,7 +1889,10 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se m_Show = show; if (!show) m_Plot = null; } - + public String showTipText() { + return "Activate for debugging in 2D"; + } + /** * @return the checkSpeedLimit **/ @@ -1929,7 +1933,10 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se public void setSleepTime(int sleepTime) { this.sleepTime = sleepTime; } - + public String sleepTimeTipText() { + return "Sleep for a time between iterations - to be used with debugging and the show option."; + } + public double getSubSwarmRadius() { return m_swarmRadius; }