Commit e3e574f8 authored by Fabian Becker's avatar Fabian Becker
Browse files

Introduce new AbstractOptimizer

refs #21
- Remove methods implemented in AbstractOptimizer from several optimizers
parent 1f2f84a3
......@@ -396,7 +396,7 @@ public class PostProcess {
// HC depends heavily on the selected mutation operator!
hc.setProblem(problem);
mute.init(problem.getIndividualTemplate(), problem);
hc.SetMutationOperator(mute);
hc.setMutationOperator(mute);
if (pop.size() != pop.getTargetSize()) {
System.err.println(pop.size() + " vs. " + pop.getTargetSize());
System.err.println("warning: population size and vector size dont match! (PostProcess::processWithHC)");
......
......@@ -493,7 +493,7 @@ public class ANPSO extends NichePSO implements InterfaceOptimizer, InterfaceAddi
}
getMainSwarm().getPopulation().incrFunctionCallsBy(calls);
this.SetSubSwarms(newSubSwarms);
this.setSubSwarms(newSubSwarms);
}
/**
......@@ -928,7 +928,7 @@ public class ANPSO extends NichePSO implements InterfaceOptimizer, InterfaceAddi
anpso.getMainSwarm().setPhi1(1.2);
anpso.getMainSwarm().setPhi2(0.6); // ANPSO uses communication in the main swarm
//Possible topologies are: "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" in that order starting by 0.
anpso.SetMainSwarmTopologyTag(3); //"Multi-Swarm" favors the formation of groups in the main swarm
anpso.setMainSwarmTopologyTag(3); //"Multi-Swarm" favors the formation of groups in the main swarm
anpso.setMainSwarmTopologyRange(2); // range for topologies like random, grid etc. (does not affect "Multi-Swarm")
anpso.setMaxInitialSubSwarmSize(0); // deactivate early reinits
......@@ -948,7 +948,7 @@ public class ANPSO extends NichePSO implements InterfaceOptimizer, InterfaceAddi
anpso.getMainSwarm().setPhi1(1.2);
anpso.getMainSwarm().setPhi2(1.2); // ANPSO uses communication in the main swarm
//Possible topologies are: "Linear", "Grid", "Star", "Multi-Swarm", "Tree", "HPSO", "Random" in that order starting by 0.
anpso.SetMainSwarmTopologyTag(3); //"Multi-Swarm" favors the formation of groups in the main swarm
anpso.setMainSwarmTopologyTag(3); //"Multi-Swarm" favors the formation of groups in the main swarm
anpso.setMainSwarmTopologyRange(4); // range for topologies like random, grid etc. (does not affect "Multi-Swarm")
// es gibt kein species size limit wie im orig-paper, aber sie berichten dort, dass sie für
......@@ -985,7 +985,7 @@ public class ANPSO extends NichePSO implements InterfaceOptimizer, InterfaceAddi
*/
public static OptimizationParameters starTopoANPSO(AbstractOptimizationProblem problem, long randSeed, int evalCnt, int topology, int topologyRange) {
OptimizationParameters params = starANPSO(problem, randSeed, evalCnt);
((ANPSO) params.getOptimizer()).SetMainSwarmTopologyTag(topology);
((ANPSO) params.getOptimizer()).setMainSwarmTopologyTag(topology);
((ANPSO) params.getOptimizer()).setMainSwarmTopologyRange(topologyRange);
((ANPSO) params.getOptimizer()).getMainSwarm().setInertnessOrChi(0.73);
......
package eva2.optimization.strategies;
import eva2.optimization.go.InterfacePopulationChangedEventListener;
import eva2.optimization.population.Population;
import eva2.problems.InterfaceOptimizationProblem;
import eva2.util.annotation.Parameter;
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;
protected ArrayList<InterfacePopulationChangedEventListener> populationChangedEventListeners;
abstract public Object clone();
@Override
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
if (populationChangedEventListeners == null) {
populationChangedEventListeners = new ArrayList<>();
}
populationChangedEventListeners.add(ea);
}
@Override
public boolean removePopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
return populationChangedEventListeners != null && populationChangedEventListeners.remove(ea);
}
/**
* Something has changed
*
* @param name Event name
*/
protected void firePropertyChangedEvent(String name) {
if (this.populationChangedEventListeners != null) {
for (InterfacePopulationChangedEventListener listener : this.populationChangedEventListeners) {
listener.registerPopulationStateChanged(this, name);
}
}
}
@Override
public Population getPopulation() {
return this.population;
}
@Override
public void setPopulation(Population pop) {
this.population = pop;
}
/**
* This method will set the problem that is to be optimized
*
* @param problem
*/
@Override
public void setProblem(InterfaceOptimizationProblem problem) {
this.optimizationProblem = problem;
}
@Override
public InterfaceOptimizationProblem getProblem() {
return this.optimizationProblem;
}
}
......@@ -14,12 +14,11 @@ import java.util.ArrayList;
*
*/
@Description(value = "Artificial Bee Colony Optimizer")
public class ArtificialBeeColony implements InterfaceOptimizer {
public class ArtificialBeeColony extends AbstractOptimizer {
protected AbstractOptimizationProblem optimizationProblem = new F1Problem();
protected Population population;
private ArrayList<InterfacePopulationChangedEventListener> populationChangedEventListeners;
public ArtificialBeeColony() {
......@@ -39,19 +38,6 @@ public class ArtificialBeeColony implements InterfaceOptimizer {
return "Artificial Bee Colony";
}
@Override
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
if (populationChangedEventListeners == null) {
populationChangedEventListeners = new ArrayList<>();
}
populationChangedEventListeners.add(ea);
}
@Override
public boolean removePopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
return populationChangedEventListeners != null && populationChangedEventListeners.remove(ea);
}
@Override
public void initialize() {
......@@ -73,19 +59,6 @@ public class ArtificialBeeColony implements InterfaceOptimizer {
}
}
/**
* Something has changed
*
* @param name Event name
*/
protected void firePropertyChangedEvent(String name) {
if (this.populationChangedEventListeners != null) {
for (InterfacePopulationChangedEventListener listener : this.populationChangedEventListeners) {
listener.registerPopulationStateChanged(this, name);
}
}
}
/**
* This method will evaluate the current population using the given problem.
*
......@@ -104,36 +77,11 @@ public class ArtificialBeeColony implements InterfaceOptimizer {
}
@Override
public Population getPopulation() {
return this.population;
}
@Override
public void setPopulation(Population pop) {
this.population = pop;
}
@Override
public InterfaceSolutionSet getAllSolutions() {
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 this.optimizationProblem;
}
@Override
public String getStringRepresentation() {
return this.toString();
......
......@@ -2,7 +2,6 @@ package eva2.optimization.strategies;
import eva2.gui.BeanInspector;
import eva2.optimization.enums.BOAScoringMethods;
import eva2.optimization.go.InterfacePopulationChangedEventListener;
import eva2.optimization.individuals.AbstractEAIndividual;
import eva2.optimization.individuals.GAIndividualBinaryData;
import eva2.optimization.individuals.InterfaceDataTypeBinary;
......@@ -12,7 +11,6 @@ import eva2.optimization.population.Population;
import eva2.optimization.population.SolutionSet;
import eva2.problems.AbstractOptimizationProblem;
import eva2.problems.BKnapsackProblem;
import eva2.problems.InterfaceOptimizationProblem;
import eva2.tools.Pair;
import eva2.tools.math.BayNet;
import eva2.tools.math.RNG;
......@@ -34,18 +32,15 @@ import java.util.logging.Logger;
* Optimization Algorithm' the works by Martin Pelikan and David E. Goldberg.
* Genetic and Evolutionary Computation Conference (GECCO-99), pp. 525-532
*/
@Description(value = "Basic implementation of the Bayesian Optimization Algorithm based on the works by Martin Pelikan and David E. Goldberg.")
public class BOA implements InterfaceOptimizer, java.io.Serializable {
@Description("Basic implementation of the Bayesian Optimization Algorithm based on the works by Martin Pelikan and David E. Goldberg.")
public class BOA extends AbstractOptimizer implements java.io.Serializable {
private static final Logger LOGGER = Logger.getLogger(BOA.class.getName());
transient private InterfacePopulationChangedEventListener populationChangedEventListener = null;
private String identifier = "BOA";
private int probDim = 8;
private int fitCrit = -1;
private int PopSize = 50;
private int numberOfParents = 3;
private transient BayNet network = null;
private Population population = new Population();
private AbstractOptimizationProblem problem = new BKnapsackProblem();
private AbstractOptimizationProblem optimizationProblem = new BKnapsackProblem();
private AbstractEAIndividual template = null;
private double learningSetRatio = 0.5;
private double resampleRatio = 0.5;
......@@ -82,15 +77,13 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
}
public BOA(BOA b) {
this.populationChangedEventListener = b.populationChangedEventListener;
this.identifier = b.identifier;
this.probDim = b.probDim;
this.fitCrit = b.fitCrit;
this.PopSize = b.PopSize;
this.numberOfParents = b.numberOfParents;
this.network = (BayNet) b.network.clone();
this.population = (Population) b.population.clone();
this.problem = (AbstractOptimizationProblem) b.problem.clone();
this.optimizationProblem = (AbstractOptimizationProblem) b.optimizationProblem.clone();
this.template = (AbstractEAIndividual) b.template.clone();
this.learningSetRatio = b.learningSetRatio;
this.resampleRatio = b.resampleRatio;
......@@ -104,7 +97,6 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
System.arraycopy(b.edgeRate[i], 0, this.edgeRate[i], 0, this.edgeRate[i].length);
}
this.scoringMethod = b.scoringMethod;
// this.printExtraOutput = b.printExtraOutput;
this.printNetworks = b.printNetworks;
this.printMetrics = b.printMetrics;
this.printEdgeRate = b.printEdgeRate;
......@@ -121,12 +113,6 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
return "Bayesian Optimization Algorithm";
}
@Override
public void addPopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
this.populationChangedEventListener = ea;
}
private void createDirectoryIfNeeded(String directoryName) {
File theDir = new File(directoryName);
// if the directory does not exist, create it
......@@ -136,17 +122,6 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
}
}
@Override
public boolean removePopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
if (populationChangedEventListener == ea) {
populationChangedEventListener = null;
return true;
} else {
return false;
}
}
private static BitSet getBinaryData(AbstractEAIndividual indy) {
if (indy instanceof InterfaceGAIndividual) {
return ((InterfaceGAIndividual) indy).getBGenotype();
......@@ -171,7 +146,7 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
LOGGER.log(Level.WARNING, "tried to evaluate null");
return;
}
this.problem.evaluate(indy);
this.optimizationProblem.evaluate(indy);
// increment the number of evaluations
this.population.incrFunctionCalls();
}
......@@ -191,11 +166,11 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
} else {
this.population.setTargetPopSize(this.PopSize);
}
this.template = this.problem.getIndividualTemplate();
this.template = this.optimizationProblem.getIndividualTemplate();
if (!(template instanceof InterfaceDataTypeBinary)) {
LOGGER.log(Level.WARNING, "Requiring binary data!");
} else {
Object dim = BeanInspector.callIfAvailable(problem,
Object dim = BeanInspector.callIfAvailable(optimizationProblem,
"getProblemDimension", null);
if (dim == null) {
LOGGER.log(Level.WARNING, "Coudn't get problem dimension!");
......@@ -212,7 +187,7 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
@Override
public void initialize() {
defaultInit();
this.problem.initializePopulation(this.population);
this.optimizationProblem.initializePopulation(this.population);
this.evaluatePopulation(this.population);
this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED);
}
......@@ -495,7 +470,7 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
@Override
public void optimize() {
this.problem.evaluatePopulationStart(this.population);
this.optimizationProblem.evaluatePopulationStart(this.population);
// get the best individuals from the population
Population best = this.population.getBestNIndividuals(
calcLearningSetSize(), this.fitCrit);
......@@ -516,7 +491,7 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
this.count++;
// we are done with one generation
this.firePropertyChangedEvent(Population.NEXT_GENERATION_PERFORMED);
this.problem.evaluatePopulationEnd(this.population);
this.optimizationProblem.evaluatePopulationEnd(this.population);
// print output if desired
// if (this.printExtraOutput) {
if (printNetworks) {
......@@ -534,40 +509,11 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable {
// }
}
/**
* Something has changed
*/
protected void firePropertyChangedEvent(String name) {
if (this.populationChangedEventListener != null) {
this.populationChangedEventListener.registerPopulationStateChanged(this, name);
}
}
@Override
public Population getPopulation() {
return this.population;
}
@Override
public void setPopulation(Population pop) {
this.population = pop;
}
@Override
public InterfaceSolutionSet getAllSolutions() {
return new SolutionSet(this.population);
}
@Override
public void setProblem(InterfaceOptimizationProblem problem) {
this.problem = (AbstractOptimizationProblem) problem;
}
@Override
public InterfaceOptimizationProblem getProblem() {
return this.problem;
}
@Override
public String getStringRepresentation() {
return "Bayesian Network";
......
......@@ -29,9 +29,7 @@ import java.util.BitSet;
* research, vol. 37, no. 11, pp. 1977-1986 (2010)
*/
@Description("A basic implementation of a Binary ScatterSearch")
public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializable, InterfacePopulationChangedEventListener {
transient private InterfacePopulationChangedEventListener populationChangedEventListener = null;
private String identifier = "BinaryScatterSearch";
public class BinaryScatterSearch extends AbstractOptimizer implements java.io.Serializable, InterfacePopulationChangedEventListener {
private int MaxImpIter = 5;
private int poolSize = 100;
private int refSetSize = 10;
......@@ -44,7 +42,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
private double g2 = 1.0 / 3.0;
private boolean firstTime = true;
private AbstractEAIndividual template = null;
private AbstractOptimizationProblem problem = new B1Problem();
private AbstractOptimizationProblem optimizationProblem = new B1Problem();
private Population pool = new Population();
private Population refSet = new Population(10);
private AdaptiveCrossoverEAMixer cross = new AdaptiveCrossoverEAMixer(new CM1(), new CM2(), new CM3(), new CM4(), new CM5(), new CM6(), new CM7());
......@@ -62,8 +60,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
* @param b
*/
public BinaryScatterSearch(BinaryScatterSearch b) {
this.populationChangedEventListener = b.populationChangedEventListener;
this.identifier = b.identifier;
this.populationChangedEventListeners = b.populationChangedEventListeners;
this.MaxImpIter = b.MaxImpIter;
this.poolSize = b.poolSize;
this.refSetSize = b.refSetSize;
......@@ -76,7 +73,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
this.g2 = b.g2;
this.firstTime = b.firstTime;
this.template = (AbstractEAIndividual) b.template.clone();
this.problem = (AbstractOptimizationProblem) b.problem.clone();
this.optimizationProblem = (AbstractOptimizationProblem) b.optimizationProblem.clone();
this.pool = (Population) b.pool.clone();
this.refSet = (Population) b.refSet.clone();
this.cross = b.cross;
......@@ -104,7 +101,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
this.th2 = upperThreshold;
this.g1 = perCentFirstIndGenerator;
this.g2 = perCentSecondIndGenerator;
this.problem = prob;
this.optimizationProblem = prob;
}
/**
......@@ -131,7 +128,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
this.th2 = upperThreshold;
this.g1 = perCentFirstIndGenerator;
this.g2 = perCentSecondIndGenerator;
this.problem = prob;
this.optimizationProblem = prob;
this.cross = cross;
}
......@@ -148,23 +145,6 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
return "BSS";
}
@Override
public void addPopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
this.populationChangedEventListener = ea;
}
@Override
public boolean removePopulationChangedEventListener(
InterfacePopulationChangedEventListener ea) {
if (populationChangedEventListener == ea) {
populationChangedEventListener = null;
return true;
} else {
return false;
}
}
/**
* evaluate the given Individual and increments the counter. if the
* individual is null, only the counter is incremented
......@@ -177,7 +157,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
System.err.println("tried to evaluate null");
return;
}
this.problem.evaluate(indy);
this.optimizationProblem.evaluate(indy);
// increment the number of evaluations
this.refSet.incrFunctionCalls();
}
......@@ -187,11 +167,11 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
*/
private void defaultInit() {
this.refSet = new Population();
this.template = this.problem.getIndividualTemplate();
this.template = this.optimizationProblem.getIndividualTemplate();
if (!(template instanceof InterfaceDataTypeBinary)) {
System.err.println("Requiring binary data!");
} else {
Object dim = BeanInspector.callIfAvailable(problem, "getProblemDimension", null);
Object dim = BeanInspector.callIfAvailable(optimizationProblem, "getProblemDimension", null);
if (dim == null) {
System.err.println("Couldnt get problem dimension!");
}
......@@ -199,7 +179,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
((InterfaceDataTypeBinary) this.template).setBinaryGenotype(new BitSet(probDim));
}
this.firstTime = true;
this.cross.init(this.template, problem, refSet, Double.MAX_VALUE);
this.cross.init(this.template, optimizationProblem, refSet, Double.MAX_VALUE);
refSet.addPopulationChangedEventListener(this);
this.refSet.setNotifyEvalInterval(this.generationCycle);
}
......@@ -246,7 +226,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
* 000...000, then 010101...01, 101010...10, 001001001...001,
* 110110110...110 and so on The returned population is evaluated.
*
* @param pop the initial Population
* @param numToInit
* @return the new Population
*/
private Population generateG1(int numToInit) {
......@@ -471,7 +451,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
* @param pop the generated Pool
*/
private void initRefSet(Population pop) {
this.problem.evaluatePopulationStart(this.refSet);
this.optimizationProblem.evaluatePopulationStart(this.refSet);
this.pool = pop;
refSetUpdate(true);
Population best = this.refSet.getBestNIndividuals(this.refSetSize / 2, fitCrit);
......@@ -484,7 +464,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
this.refSet.add(x);
}
}
this.problem.evaluatePopulationEnd(this.refSet);
this.optimizationProblem.evaluatePopulationEnd(this.refSet);
}
/**
......@@ -665,7 +645,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
for (int i = 0; i < this.refSet.size(); i++) {
pop.add(this.refSet.getEAIndividual(i));
}
this.cross.update(indy1, problem, refSet, indy1.getFitness(0));
this.cross.update(indy1, optimizationProblem, refSet, indy1.getFitness(0));
result = this.cross.mate(indy1, pop)[0];
//result = indy1.mateWith(s)[0];
} else if (pop.size() > 0) {
......@@ -701,7 +681,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
@Override
public void optimize() {
problem.evaluatePopulationStart(refSet);
optimizationProblem.evaluatePopulationStart(refSet);
int funCallsStart = this.refSet.getFunctionCalls();
do {
// generate a new Pool
......@@ -738,12 +718,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
refSetUpdate(true);
}
} while (refSet.getFunctionCalls() - funCallsStart < generationCycle);
problem.evaluatePopulationEnd(refSet);
}
@Override
public Population getPopulation() {
return this.refSet;
optimizationProblem.evaluatePopulationEnd(refSet);
}
@Override
......@@ -761,27 +736,11 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
return new SolutionSet(this.refSet);
}
@Override
public void setProblem(InterfaceOptimizationProblem problem) {
this.problem = (AbstractOptimizationProblem) problem;
}
@Override
public InterfaceOptimizationProblem getProblem() {
return this.problem;
}
@Override
public String getStringRepresentation() {
return "BinaryScatterSearch";
}
protected void firePropertyChangedEvent(String name) {
if (this.populationChangedEventListener != null) {
this.populationChangedEventListener.registerPopulationStateChanged(this, name);
}
}
@Override
public void registerPopulationStateChanged(Object source, String name) {
// The events of the interim hill climbing population will be caught here
......@@ -795,7 +754,6 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ
}
}
//----------GUI----------
public int getPoolSize() {
return this.poolSize;
}
......
......@@ -44,10 +44,9 @@ import java.util.*;
* species clustering actually makes sense).
*/
@Description("This is a versatile species based niching EA method.")
public class ClusterBasedNichingEA implements InterfacePopulationChangedEventListener, InterfaceAdditionalPopulationInformer, InterfaceOptimizer, java.io.Serializable {