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

View File

@ -21,7 +21,6 @@ public class EvaluationTerminator implements InterfaceTerminator,
/**
* 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;
public EvaluationTerminator() {
@ -67,6 +66,7 @@ public class EvaluationTerminator implements InterfaceTerminator,
return ret;
}
@Parameter(name = "calls", description = "Number of calls to fitness function.")
public void setFitnessCalls(int x) {
maxFitnessCalls = x;
}

View File

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

View File

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

View File

@ -1,13 +1,12 @@
package eva2.optimization.strategies;
import eva2.optimization.population.InterfacePopulationChangedEventListener;
import eva2.optimization.individuals.AbstractEAIndividual;
import eva2.optimization.population.InterfacePopulationChangedEventListener;
import eva2.optimization.population.InterfaceSolutionSet;
import eva2.optimization.population.Population;
import eva2.problems.AbstractOptimizationProblem;
import eva2.problems.F1Problem;
import eva2.util.annotation.Description;
import eva2.util.annotation.Parameter;
import java.util.ArrayList;
@ -19,13 +18,8 @@ import java.util.ArrayList;
public class AdaptiveDifferentialEvolution extends AbstractOptimizer {
protected Population population;
@Parameter(name = "groups", description = "Number of sub-groups to use during optimization.")
protected int nonOverlappingGroups = 5;
@Parameter(name = "F", description = "Differential Weight")
protected double differentialWeight = 0.8;
@Parameter(name = "CR", description = "Crossover Rate")
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.
*/
@Parameter(name = "trials", description = "Maximum number of trials until bee abandons the food source")
protected int maxTrials = 100;
protected AbstractEAIndividual bestIndividual;
@ -243,6 +242,7 @@ public class ArtificialBeeColony extends AbstractOptimizer implements Serializab
this.population = pop;
}
@Parameter(name = "trials", description = "Maximum number of trials until bee abandons the food source")
public void setMaxTrials(int maxTrials) {
this.maxTrials = maxTrials;
}

View File

@ -31,16 +31,12 @@ import java.util.Vector;
public class DifferentialEvolution extends AbstractOptimizer implements java.io.Serializable {
protected transient Population children = null;
@Parameter(name = "DEType", description = "Mutation type for DE")
private eva2.optimization.enums.DEType DEType;
@Parameter(name = "F", description = "Differential Weight")
private double differentialWeight = 0.8;
@Parameter(name = "CR", description = "Crossover Rate")
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 mt = 0.05;
@ -697,6 +693,7 @@ public class DifferentialEvolution extends AbstractOptimizer implements java.io.
*
* @param f
*/
@Parameter(name = "F", description = "Differential Weight")
public void setDifferentialWeight(double f) {
this.differentialWeight = f;
}
@ -715,6 +712,7 @@ public class DifferentialEvolution extends AbstractOptimizer implements java.io.
*
* @param k
*/
@Parameter(name = "CR", description = "Crossover Rate")
public void setCrossoverRate(double k) {
if (k < 0) {
k = 0;
@ -739,6 +737,7 @@ public class DifferentialEvolution extends AbstractOptimizer implements java.io.
*
* @param l
*/
@Parameter(description = "Enhance greediness through amplification of the differential vector to the best individual for DE2.")
public void setLambda(double l) {
this.lambda = l;
}
@ -775,6 +774,7 @@ public class DifferentialEvolution extends AbstractOptimizer implements java.io.
*
* @param s The type.
*/
@Parameter(name = "type", description = "Mutation type for DE")
public void setDEType(eva2.optimization.enums.DEType s) {
this.DEType = s;
// 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)")
public class EvolutionStrategies extends AbstractOptimizer implements java.io.Serializable {
@Parameter(description = "Mu", name = "mu")
protected int mu = 5;
@Parameter(description = "Lambda", name = "lambda")
protected int lambda = 20;
@Parameter(description = "Determines whether the +-Strategy should be used.", name = "usePlus")
protected boolean usePlusStrategy = false;
private InterfaceSelection parentSelection = new SelectRandom();
private InterfaceSelection partnerSelection = new SelectRandom();
@ -369,6 +363,7 @@ public class EvolutionStrategies extends AbstractOptimizer implements java.io.Se
*
* @param elitism
*/
@Parameter(description = "Determines whether the +-Strategy should be used.", name = "usePlus")
public void setPlusStrategy(boolean 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
*
* @param myu The parent population size.
* @param mu The parent population size.
*/
@Parameter(description = "The parent population size.")
public void setMu(int mu) {
this.mu = mu;
}
@ -424,6 +420,7 @@ public class EvolutionStrategies extends AbstractOptimizer implements java.io.Se
*
* @param lambda The children population size.
*/
@Parameter(description = "The children population size.")
public void setLambda(int lambda) {
this.lambda = lambda;
}

View File

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

View File

@ -29,7 +29,6 @@ import eva2.util.annotation.Parameter;
import javax.swing.*;
import java.awt.*;
import java.io.Serializable;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
@ -72,10 +71,8 @@ public abstract class AbstractOptimizationProblem implements InterfaceOptimizati
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.
@Parameter(name = "problemDimension", description = "Length of the x vector to be optimized.")
protected int problemDimension = 10;
/**
@ -561,6 +558,7 @@ public abstract class AbstractOptimizationProblem implements InterfaceOptimizati
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) {
defaultAccuracy = defAcc;
}

View File

@ -6,9 +6,7 @@ import eva2.util.annotation.Parameter;
* This class is under construction.
*/
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;
@Parameter(name = "paralellize", description = "Toggle between parallel and serial implementation.")
private boolean parallelize = false;
@Override
@ -34,6 +32,7 @@ public abstract class AbstractParallelOptimizationProblem extends AbstractOptimi
return this.parallelize;
}
@Parameter(description = "Toggle between parallel and serial implementation.")
public void setParallelize(boolean b) {
this.parallelize = b;
}
@ -43,6 +42,7 @@ public abstract class AbstractParallelOptimizationProblem extends AbstractOptimi
*
* @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) {
this.localCPUs = n;
}

View File

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