Implemented DE Parameter selection from CLI
This commit is contained in:
parent
7a7f8f0d67
commit
b005bb7a96
@ -2,12 +2,14 @@ package eva2.cli;
|
||||
|
||||
import eva2.OptimizerFactory;
|
||||
import eva2.optimization.OptimizationStateListener;
|
||||
import eva2.optimization.enums.DETypeEnum;
|
||||
import eva2.optimization.go.InterfacePopulationChangedEventListener;
|
||||
import eva2.optimization.modules.OptimizationParameters;
|
||||
import eva2.optimization.operator.terminators.CombinedTerminator;
|
||||
import eva2.optimization.operator.terminators.EvaluationTerminator;
|
||||
import eva2.optimization.operator.terminators.FitnessValueTerminator;
|
||||
import eva2.optimization.problems.AbstractOptimizationProblem;
|
||||
import eva2.optimization.strategies.DifferentialEvolution;
|
||||
import eva2.optimization.strategies.InterfaceOptimizer;
|
||||
import org.apache.commons.cli.*;
|
||||
import org.reflections.Reflections;
|
||||
@ -165,7 +167,7 @@ public class Main implements OptimizationStateListener, InterfacePopulationChang
|
||||
|
||||
if (index >= 0) {
|
||||
programParams = Arrays.copyOfRange(args, 0, index);
|
||||
optimizerParams = Arrays.copyOfRange(args, index + 1, args.length - 1);
|
||||
optimizerParams = Arrays.copyOfRange(args, index + 1, args.length);
|
||||
} else {
|
||||
optimizerParams = new String[]{};
|
||||
}
|
||||
@ -246,13 +248,48 @@ public class Main implements OptimizationStateListener, InterfacePopulationChang
|
||||
*/
|
||||
private void createOptimizerFromName(String optimizerName, String[] optimizerParams) throws Exception {
|
||||
Options opt = new Options();
|
||||
CommandLineParser cliParser = new BasicParser();
|
||||
CommandLine commandLine = null;
|
||||
|
||||
switch(optimizerName) {
|
||||
case "DifferentialEvolution":
|
||||
System.out.println("DE");
|
||||
opt.addOption("-F", true, "Differential Weight");
|
||||
opt.addOption("-CR", true, "Crossover Rate");
|
||||
opt.addOption("-DEType", true, "DE Type (");
|
||||
opt.addOption("F", true, "Differential Weight");
|
||||
opt.addOption("CR", true, "Crossover Rate");
|
||||
opt.addOption("DEType", true, "DE Type (");
|
||||
/**
|
||||
* Parse default options.
|
||||
*/
|
||||
try {
|
||||
commandLine = cliParser.parse(opt, optimizerParams);
|
||||
} catch (ParseException e) {
|
||||
showHelp(opt);
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
|
||||
double f = 0.8, lambda = 0.6, cr = 0.6;
|
||||
if (commandLine.hasOption("F")) {
|
||||
f = Double.parseDouble(commandLine.getOptionValue("F"));
|
||||
}
|
||||
|
||||
if (commandLine.hasOption("CR")) {
|
||||
cr = Double.parseDouble(commandLine.getOptionValue("CR"));
|
||||
}
|
||||
|
||||
this.optimizer = OptimizerFactory.createDifferentialEvolution(this.problem, this.populationSize, f, lambda, cr, this);
|
||||
|
||||
if (commandLine.hasOption("DEType")) {
|
||||
((DifferentialEvolution)this.optimizer).setDEType(
|
||||
DETypeEnum.getFromId(
|
||||
Integer.parseInt(commandLine.getOptionValue("DEType"))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
case "GeneticAlgorithm":
|
||||
System.out.println("Genetic Algorithm");
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Unsupported Optimizer");
|
||||
@ -280,10 +317,6 @@ public class Main implements OptimizationStateListener, InterfacePopulationChang
|
||||
|
||||
LOGGER.log(Level.INFO, "Running {0}", "Differential Evolution");
|
||||
|
||||
double f = 0.8, lambda = 0.6, cr = 0.6;
|
||||
|
||||
InterfaceOptimizer optimizer = OptimizerFactory.createDifferentialEvolution(this.problem, this.populationSize, f, lambda, cr, this);
|
||||
|
||||
OptimizationParameters params = OptimizerFactory.makeParams(optimizer, this.populationSize, this.problem);
|
||||
double[] result = OptimizerFactory.optimizeToDouble(params);
|
||||
|
||||
|
@ -3,4 +3,30 @@ package eva2.optimization.enums;
|
||||
public enum DETypeEnum {
|
||||
DE1_Rand_1, DE2_CurrentToBest, DE_Best_1, DE_Best_2, TrigonometricDE, DE_CurrentToRand;
|
||||
//", "DE2 - DE/current-to-best/1", "DE/best/2", "Trigonometric DE"};
|
||||
|
||||
/**
|
||||
* A method to translate the "old" integer tags into the enum type.
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public static DETypeEnum getFromId(int id) {
|
||||
switch (id) {
|
||||
case 0:
|
||||
return DE1_Rand_1;
|
||||
case 1:
|
||||
return DE2_CurrentToBest;
|
||||
case 2:
|
||||
return DE_Best_1;
|
||||
case 3:
|
||||
return DE_Best_2;
|
||||
case 4:
|
||||
return TrigonometricDE;
|
||||
case 5:
|
||||
return DE_CurrentToRand;
|
||||
default:
|
||||
System.err.println("Error: invalid old DEType ID in DETypeEnum getFromId! Using DE_Best_1.");
|
||||
return DE_Best_1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package eva2.optimization.strategies;
|
||||
import eva2.optimization.go.InterfacePopulationChangedEventListener;
|
||||
import eva2.optimization.population.InterfaceSolutionSet;
|
||||
import eva2.optimization.population.Population;
|
||||
import eva2.optimization.problems.AbstractOptimizationProblem;
|
||||
import eva2.optimization.problems.F1Problem;
|
||||
import eva2.optimization.problems.InterfaceOptimizationProblem;
|
||||
import eva2.util.annotation.Description;
|
||||
|
||||
@ -12,7 +14,8 @@ import eva2.util.annotation.Description;
|
||||
@Description(text = "Artificial Bee Colony Optimizer")
|
||||
public class ArtificialBeeColony implements InterfaceOptimizer {
|
||||
|
||||
|
||||
protected AbstractOptimizationProblem optimizationProblem = new F1Problem();
|
||||
protected Population population;
|
||||
|
||||
public ArtificialBeeColony() {
|
||||
|
||||
@ -59,12 +62,12 @@ public class ArtificialBeeColony implements InterfaceOptimizer {
|
||||
|
||||
@Override
|
||||
public Population getPopulation() {
|
||||
return null;
|
||||
return this.population;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPopulation(Population pop) {
|
||||
|
||||
this.population = pop;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -82,14 +85,19 @@ public class ArtificialBeeColony implements InterfaceOptimizer {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will set the problem that is to be optimized
|
||||
*
|
||||
* @param problem
|
||||
*/
|
||||
@Override
|
||||
public void setProblem(InterfaceOptimizationProblem problem) {
|
||||
|
||||
this.optimizationProblem = (AbstractOptimizationProblem) problem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InterfaceOptimizationProblem getProblem() {
|
||||
return null;
|
||||
return this.optimizationProblem;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user