Refactor @Parameter annotation to be applied to setter methods.

fixes #29
- Change Parameter annotation to exist on methods
- Add default value to name field to make it optional
- Adjust classes that already use the annotation
This commit is contained in:
Fabian Becker 2014-11-01 13:27:33 +01:00
parent 032a4ce087
commit 4909cdd6bc
12 changed files with 24 additions and 36 deletions

View File

@ -25,13 +25,11 @@ import java.util.logging.Logger;
public abstract class AbstractOptimizationParameters implements InterfaceOptimizationParameters, Serializable { public abstract class AbstractOptimizationParameters implements InterfaceOptimizationParameters, Serializable {
protected static final Logger LOGGER = Logger.getLogger(AbstractOptimizationParameters.class.getName()); protected static final Logger LOGGER = Logger.getLogger(AbstractOptimizationParameters.class.getName());
@Parameter(name = "Random Seed", description = "Random number seed, set to zero to use current system time.")
protected long randomSeed = (long) 0.0; protected long randomSeed = (long) 0.0;
/** /**
* The optimizer to be executed. * The optimizer to be executed.
*/ */
@Parameter(name = "Optimizer", description = "Choose an optimization strategy.")
protected InterfaceOptimizer optimizer; protected InterfaceOptimizer optimizer;
/** /**
@ -39,14 +37,12 @@ public abstract class AbstractOptimizationParameters implements InterfaceOptimiz
* When changed it is automatically applied to the * When changed it is automatically applied to the
* selected optimizer. * selected optimizer.
*/ */
@Parameter(name = "Problem", description = "Choose the problem that is to optimize and the EA individual parameters.")
protected InterfaceOptimizationProblem problem; protected InterfaceOptimizationProblem problem;
/** /**
* The termination criteria that terminated an * The termination criteria that terminated an
* optimization run. * optimization run.
*/ */
@Parameter(name = "Terminator", description = "Choose a termination criterion.")
protected InterfaceTerminator terminator; protected InterfaceTerminator terminator;
/** /**
@ -54,7 +50,6 @@ public abstract class AbstractOptimizationParameters implements InterfaceOptimiz
* This can be enabled in the UI and will perform additional * This can be enabled in the UI and will perform additional
* optimization e.g. with Hill Climbing. * optimization e.g. with Hill Climbing.
*/ */
@Parameter(name = "Post Processing", description = "Parameters for the post processing step.")
protected InterfacePostProcessParams postProcessParams = new PostProcessParams(false); protected InterfacePostProcessParams postProcessParams = new PostProcessParams(false);
transient protected InterfacePopulationChangedEventListener populationChangedEventListener; transient protected InterfacePopulationChangedEventListener populationChangedEventListener;
@ -183,6 +178,7 @@ public abstract class AbstractOptimizationParameters implements InterfaceOptimiz
} }
@Override @Override
@Parameter(description = "The optimization strategy to use.")
public void setOptimizer(InterfaceOptimizer optimizer) { public void setOptimizer(InterfaceOptimizer optimizer) {
this.optimizer = optimizer; this.optimizer = optimizer;
this.optimizer.setProblem(this.problem); this.optimizer.setProblem(this.problem);
@ -219,6 +215,7 @@ public abstract class AbstractOptimizationParameters implements InterfaceOptimiz
* @param problem * @param problem
*/ */
@Override @Override
@Parameter(description = "Choose the problem that is to optimize and the EA individual parameters.")
public void setProblem(InterfaceOptimizationProblem problem) { public void setProblem(InterfaceOptimizationProblem problem) {
this.problem = problem; this.problem = problem;
this.optimizer.setProblem(this.problem); this.optimizer.setProblem(this.problem);
@ -236,6 +233,7 @@ public abstract class AbstractOptimizationParameters implements InterfaceOptimiz
* @param x Long seed. * @param x Long seed.
*/ */
@Override @Override
@Parameter(name = "seed", description = "Random number seed, set to zero to use current system time.")
public void setRandomSeed(long x) { public void setRandomSeed(long x) {
randomSeed = x; randomSeed = x;
} }
@ -257,6 +255,7 @@ public abstract class AbstractOptimizationParameters implements InterfaceOptimiz
* @param term The new terminator * @param term The new terminator
*/ */
@Override @Override
@Parameter(description = "The termination criterion.")
public void setTerminator(InterfaceTerminator term) { public void setTerminator(InterfaceTerminator term) {
this.terminator = term; this.terminator = term;
} }
@ -272,6 +271,7 @@ public abstract class AbstractOptimizationParameters implements InterfaceOptimiz
} }
@Override @Override
@Parameter(description = "Parameters for the post processing step.")
public void setPostProcessParams(InterfacePostProcessParams ppp) { public void setPostProcessParams(InterfacePostProcessParams ppp) {
postProcessParams = ppp; postProcessParams = ppp;
} }

View File

@ -21,7 +21,6 @@ public class EvaluationTerminator implements InterfaceTerminator,
/** /**
* Number of fitness calls on the problem which is optimized. * Number of fitness calls on the problem which is optimized.
*/ */
@Parameter(name = "Max. Fitness Calls", description = "Number of calls to fitness function.")
protected int maxFitnessCalls = 1000; protected int maxFitnessCalls = 1000;
public EvaluationTerminator() { public EvaluationTerminator() {
@ -67,6 +66,7 @@ public class EvaluationTerminator implements InterfaceTerminator,
return ret; return ret;
} }
@Parameter(name = "calls", description = "Number of calls to fitness function.")
public void setFitnessCalls(int x) { public void setFitnessCalls(int x) {
maxFitnessCalls = x; maxFitnessCalls = x;
} }

View File

@ -15,7 +15,6 @@ import eva2.util.annotation.Parameter;
*/ */
@Description("Terminate if a maximum time (seconds) was reached.") @Description("Terminate if a maximum time (seconds) was reached.")
public class MaximumTimeTerminator implements InterfaceTerminator { public class MaximumTimeTerminator implements InterfaceTerminator {
@Parameter(name = "maxTime", description = "Maximum time in seconds")
private int maximumTime = 5; private int maximumTime = 5;
private long startTime; private long startTime;
@ -49,6 +48,7 @@ public class MaximumTimeTerminator implements InterfaceTerminator {
return maximumTime; return maximumTime;
} }
@Parameter(name = "time", description = "Maximum time in seconds")
public void setMaximumTime(int maximumTime) { public void setMaximumTime(int maximumTime) {
this.maximumTime = maximumTime; this.maximumTime = maximumTime;
} }

View File

@ -14,7 +14,6 @@ import java.util.ArrayList;
*/ */
public abstract class AbstractOptimizer implements InterfaceOptimizer { public abstract class AbstractOptimizer implements InterfaceOptimizer {
@Parameter(name = "Population", description = "Edit the properties of the population used.")
protected Population population = new Population(); protected Population population = new Population();
protected InterfaceOptimizationProblem optimizationProblem = new F1Problem(); protected InterfaceOptimizationProblem optimizationProblem = new F1Problem();
@ -56,6 +55,7 @@ public abstract class AbstractOptimizer implements InterfaceOptimizer {
} }
@Override @Override
@Parameter(description = "Edit the properties of the population used.")
public void setPopulation(Population pop) { public void setPopulation(Population pop) {
this.population = pop; this.population = pop;
} }

View File

@ -1,13 +1,12 @@
package eva2.optimization.strategies; package eva2.optimization.strategies;
import eva2.optimization.population.InterfacePopulationChangedEventListener;
import eva2.optimization.individuals.AbstractEAIndividual; import eva2.optimization.individuals.AbstractEAIndividual;
import eva2.optimization.population.InterfacePopulationChangedEventListener;
import eva2.optimization.population.InterfaceSolutionSet; import eva2.optimization.population.InterfaceSolutionSet;
import eva2.optimization.population.Population; import eva2.optimization.population.Population;
import eva2.problems.AbstractOptimizationProblem; import eva2.problems.AbstractOptimizationProblem;
import eva2.problems.F1Problem; import eva2.problems.F1Problem;
import eva2.util.annotation.Description; import eva2.util.annotation.Description;
import eva2.util.annotation.Parameter;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,13 +18,8 @@ import java.util.ArrayList;
public class AdaptiveDifferentialEvolution extends AbstractOptimizer { public class AdaptiveDifferentialEvolution extends AbstractOptimizer {
protected Population population; protected Population population;
@Parameter(name = "groups", description = "Number of sub-groups to use during optimization.")
protected int nonOverlappingGroups = 5; protected int nonOverlappingGroups = 5;
@Parameter(name = "F", description = "Differential Weight")
protected double differentialWeight = 0.8; protected double differentialWeight = 0.8;
@Parameter(name = "CR", description = "Crossover Rate")
protected double crossoverRate = 0.6; protected double crossoverRate = 0.6;

View File

@ -23,7 +23,6 @@ public class ArtificialBeeColony extends AbstractOptimizer implements Serializab
/** /**
* A food source which could not be improved through "maxTrials" trials is abandoned by its employed bee. * A food source which could not be improved through "maxTrials" trials is abandoned by its employed bee.
*/ */
@Parameter(name = "trials", description = "Maximum number of trials until bee abandons the food source")
protected int maxTrials = 100; protected int maxTrials = 100;
protected AbstractEAIndividual bestIndividual; protected AbstractEAIndividual bestIndividual;
@ -243,6 +242,7 @@ public class ArtificialBeeColony extends AbstractOptimizer implements Serializab
this.population = pop; this.population = pop;
} }
@Parameter(name = "trials", description = "Maximum number of trials until bee abandons the food source")
public void setMaxTrials(int maxTrials) { public void setMaxTrials(int maxTrials) {
this.maxTrials = maxTrials; this.maxTrials = maxTrials;
} }

View File

@ -31,16 +31,12 @@ import java.util.Vector;
public class DifferentialEvolution extends AbstractOptimizer implements java.io.Serializable { public class DifferentialEvolution extends AbstractOptimizer implements java.io.Serializable {
protected transient Population children = null; protected transient Population children = null;
@Parameter(name = "DEType", description = "Mutation type for DE")
private eva2.optimization.enums.DEType DEType; private eva2.optimization.enums.DEType DEType;
@Parameter(name = "F", description = "Differential Weight")
private double differentialWeight = 0.8; private double differentialWeight = 0.8;
@Parameter(name = "CR", description = "Crossover Rate")
private double crossoverRate = 0.6; private double crossoverRate = 0.6;
@Parameter(name = "Lambda", description = "Enhance greediness through amplification of the differential vector to the best individual for DE2.")
private double lambda = 0.6; private double lambda = 0.6;
private double mt = 0.05; private double mt = 0.05;
@ -697,6 +693,7 @@ public class DifferentialEvolution extends AbstractOptimizer implements java.io.
* *
* @param f * @param f
*/ */
@Parameter(name = "F", description = "Differential Weight")
public void setDifferentialWeight(double f) { public void setDifferentialWeight(double f) {
this.differentialWeight = f; this.differentialWeight = f;
} }
@ -715,6 +712,7 @@ public class DifferentialEvolution extends AbstractOptimizer implements java.io.
* *
* @param k * @param k
*/ */
@Parameter(name = "CR", description = "Crossover Rate")
public void setCrossoverRate(double k) { public void setCrossoverRate(double k) {
if (k < 0) { if (k < 0) {
k = 0; k = 0;
@ -739,6 +737,7 @@ public class DifferentialEvolution extends AbstractOptimizer implements java.io.
* *
* @param l * @param l
*/ */
@Parameter(description = "Enhance greediness through amplification of the differential vector to the best individual for DE2.")
public void setLambda(double l) { public void setLambda(double l) {
this.lambda = l; this.lambda = l;
} }
@ -775,6 +774,7 @@ public class DifferentialEvolution extends AbstractOptimizer implements java.io.
* *
* @param s The type. * @param s The type.
*/ */
@Parameter(name = "type", description = "Mutation type for DE")
public void setDEType(eva2.optimization.enums.DEType s) { public void setDEType(eva2.optimization.enums.DEType s) {
this.DEType = s; this.DEType = s;
// show mt for trig. DE only // show mt for trig. DE only

View File

@ -24,14 +24,8 @@ import eva2.util.annotation.Parameter;
*/ */
@Description(value = "This is an Evolution Strategy. Note that the population size depends on mu (number of parents) and lambda (number of offspring)") @Description(value = "This is an Evolution Strategy. Note that the population size depends on mu (number of parents) and lambda (number of offspring)")
public class EvolutionStrategies extends AbstractOptimizer implements java.io.Serializable { public class EvolutionStrategies extends AbstractOptimizer implements java.io.Serializable {
@Parameter(description = "Mu", name = "mu")
protected int mu = 5; protected int mu = 5;
@Parameter(description = "Lambda", name = "lambda")
protected int lambda = 20; protected int lambda = 20;
@Parameter(description = "Determines whether the +-Strategy should be used.", name = "usePlus")
protected boolean usePlusStrategy = false; protected boolean usePlusStrategy = false;
private InterfaceSelection parentSelection = new SelectRandom(); private InterfaceSelection parentSelection = new SelectRandom();
private InterfaceSelection partnerSelection = new SelectRandom(); private InterfaceSelection partnerSelection = new SelectRandom();
@ -369,6 +363,7 @@ public class EvolutionStrategies extends AbstractOptimizer implements java.io.Se
* *
* @param elitism * @param elitism
*/ */
@Parameter(description = "Determines whether the +-Strategy should be used.", name = "usePlus")
public void setPlusStrategy(boolean elitism) { public void setPlusStrategy(boolean elitism) {
this.usePlusStrategy = elitism; this.usePlusStrategy = elitism;
} }
@ -405,8 +400,9 @@ public class EvolutionStrategies extends AbstractOptimizer implements java.io.Se
/** /**
* This method allows you to set parent population size myu * This method allows you to set parent population size myu
* *
* @param myu The parent population size. * @param mu The parent population size.
*/ */
@Parameter(description = "The parent population size.")
public void setMu(int mu) { public void setMu(int mu) {
this.mu = mu; this.mu = mu;
} }
@ -424,6 +420,7 @@ public class EvolutionStrategies extends AbstractOptimizer implements java.io.Se
* *
* @param lambda The children population size. * @param lambda The children population size.
*/ */
@Parameter(description = "The children population size.")
public void setLambda(int lambda) { public void setLambda(int lambda) {
this.lambda = lambda; this.lambda = lambda;
} }

View File

@ -26,7 +26,6 @@ public class FloodAlgorithm extends AbstractOptimizer implements java.io.Seriali
GAIndividualBinaryData bestIndividual, testIndividual; GAIndividualBinaryData bestIndividual, testIndividual;
public double initialFloodPeak = 2000.0, currentFloodPeak; public double initialFloodPeak = 2000.0, currentFloodPeak;
@Parameter(name = "drainRate", description = "Set the drain rate that reduces the current flood level each generation.")
public double drainRate = 1.0; public double drainRate = 1.0;
public FloodAlgorithm() { public FloodAlgorithm() {
@ -227,6 +226,7 @@ public class FloodAlgorithm extends AbstractOptimizer implements java.io.Seriali
return this.drainRate; return this.drainRate;
} }
@Parameter(description = "Set the drain rate that reduces the current flood level each generation.")
public void setDrainRate(double a) { public void setDrainRate(double a) {
this.drainRate = a; this.drainRate = a;
if (this.drainRate < 0) { if (this.drainRate < 0) {

View File

@ -29,7 +29,6 @@ import eva2.util.annotation.Parameter;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.Serializable; import java.io.Serializable;
import java.util.Vector;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
@ -72,10 +71,8 @@ public abstract class AbstractOptimizationProblem implements InterfaceOptimizati
protected AbstractEAIndividual template = null; protected AbstractEAIndividual template = null;
@Parameter(name = "defaultAccuracy", description = "A default threshold to identify optima - e.g. the assumed minimal distance between any two optima.")
private double defaultAccuracy = 0.001; // default accuracy for identifying optima. private double defaultAccuracy = 0.001; // default accuracy for identifying optima.
@Parameter(name = "problemDimension", description = "Length of the x vector to be optimized.")
protected int problemDimension = 10; protected int problemDimension = 10;
/** /**
@ -561,6 +558,7 @@ public abstract class AbstractOptimizationProblem implements InterfaceOptimizati
return defaultAccuracy; return defaultAccuracy;
} }
@Parameter(name = "accuracy", description = "A default threshold to identify optima - e.g. the assumed minimal distance between any two optima.")
public void setDefaultAccuracy(double defAcc) { public void setDefaultAccuracy(double defAcc) {
defaultAccuracy = defAcc; defaultAccuracy = defAcc;
} }

View File

@ -6,9 +6,7 @@ import eva2.util.annotation.Parameter;
* This class is under construction. * This class is under construction.
*/ */
public abstract class AbstractParallelOptimizationProblem extends AbstractOptimizationProblem { public abstract class AbstractParallelOptimizationProblem extends AbstractOptimizationProblem {
@Parameter(name = "localCPUs", description = "Set the number of local CPUS (only active in non-parallelized mode).")
private int localCPUs = 4; private int localCPUs = 4;
@Parameter(name = "paralellize", description = "Toggle between parallel and serial implementation.")
private boolean parallelize = false; private boolean parallelize = false;
@Override @Override
@ -34,6 +32,7 @@ public abstract class AbstractParallelOptimizationProblem extends AbstractOptimi
return this.parallelize; return this.parallelize;
} }
@Parameter(description = "Toggle between parallel and serial implementation.")
public void setParallelize(boolean b) { public void setParallelize(boolean b) {
this.parallelize = b; this.parallelize = b;
} }
@ -43,6 +42,7 @@ public abstract class AbstractParallelOptimizationProblem extends AbstractOptimi
* *
* @param n Number of processors. * @param n Number of processors.
*/ */
@Parameter(name = "cpu", description = "Set the number of local CPUS (only active in non-parallelized mode).")
public void setNumberLocalCPUs(int n) { public void setNumberLocalCPUs(int n) {
this.localCPUs = n; this.localCPUs = n;
} }

View File

@ -3,9 +3,8 @@ package eva2.util.annotation;
import java.lang.annotation.*; import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) @Target(ElementType.METHOD)
public @interface Parameter { public @interface Parameter {
String name(); String name() default "";
String description(); String description();
} }