Refactoring, renaming and better comments.
This commit is contained in:
Fabian Becker 2013-01-29 11:18:39 +00:00
parent 27f94e088e
commit d3ea94fe5c
5 changed files with 413 additions and 331 deletions

View File

@ -36,13 +36,16 @@ public class PBILPopulation extends Population implements Cloneable, java.io.Ser
} }
} }
@Override
public Object clone() { public Object clone() {
return (Object) new PBILPopulation(this); return (Object) new PBILPopulation(this);
} }
/** This method inits the state of the population AFTER the individuals /**
* have been inited by a problem * This method inits the state of the population AFTER the individuals
* have been inited by a problem.
*/ */
@Override
public void init() { public void init() {
this.generationCount = 0; this.generationCount = 0;
this.functionCallCount = 0; this.functionCallCount = 0;
@ -56,7 +59,8 @@ public class PBILPopulation extends Population implements Cloneable, java.io.Ser
} }
} }
/** This method allows you to learn from several examples /**
* This method allows you to learn from several examples
* @param examples A population of examples. * @param examples A population of examples.
* @param learnRate The learning rate. * @param learnRate The learning rate.
*/ */
@ -74,7 +78,8 @@ public class PBILPopulation extends Population implements Cloneable, java.io.Ser
} }
} }
/** This method creates a new population based on the bit probability vector /**
* This method creates a new population based on the bit probability vector.
*/ */
public void initPBIL() { public void initPBIL() {
InterfaceGAIndividual tmpIndy, template = (InterfaceGAIndividual)((AbstractEAIndividual)this.get(0)).clone(); InterfaceGAIndividual tmpIndy, template = (InterfaceGAIndividual)((AbstractEAIndividual)this.get(0)).clone();
@ -93,7 +98,8 @@ public class PBILPopulation extends Population implements Cloneable, java.io.Ser
} }
} }
/** This method allows you to mutate the bit probability vector /**
* This method allows you to mutate the bit probability vector
* @param mutationRate The mutation rate. * @param mutationRate The mutation rate.
*/ */
public void mutateProbabilityVector(double mutationRate, double sigma) { public void mutateProbabilityVector(double mutationRate, double sigma) {
@ -104,7 +110,8 @@ public class PBILPopulation extends Population implements Cloneable, java.io.Ser
} }
} }
/** This method will build a probability vector from the current population /**
* This method will build a probability vector from the current population.
*/ */
public void buildProbabilityVector() { public void buildProbabilityVector() {
int dim = ((InterfaceGAIndividual)this.get(0)).getGenotypeLength(); int dim = ((InterfaceGAIndividual)this.get(0)).getGenotypeLength();
@ -125,20 +132,24 @@ public class PBILPopulation extends Population implements Cloneable, java.io.Ser
} }
} }
/** This method allows you to set the current probability vector. /**
* This method allows you to set the current probability vector.
* @param pv The new probability vector. * @param pv The new probability vector.
*/ */
public void SetProbabilityVector(double[] pv) { public void setProbabilityVector(double[] pv) {
this.m_ProbabilityVector = pv; this.m_ProbabilityVector = pv;
} }
public double[] getProbabilityVector() { public double[] getProbabilityVector() {
return this.m_ProbabilityVector; return this.m_ProbabilityVector;
} }
/** This method will return a string description of the GAIndividal /**
* noteably the Genotype. * This method will return a string description of the GAIndividal
* notably the Genotype.
* @return A descriptive string * @return A descriptive string
*/ */
@Override
public String getStringRepresentation() { public String getStringRepresentation() {
String result = ""; String result = "";
result += "PBIL-Population:\n"; result += "PBIL-Population:\n";

View File

@ -41,29 +41,66 @@ import java.util.logging.Logger;
public class Population extends ArrayList implements PopulationInterface, Cloneable, java.io.Serializable { public class Population extends ArrayList implements PopulationInterface, Cloneable, java.io.Serializable {
private static final Logger LOGGER = Logger.getLogger(Population.class.getName()); private static final Logger LOGGER = Logger.getLogger(Population.class.getName());
/**
* Number of generations.
*/
protected int generationCount = 0; protected int generationCount = 0;
/**
* Number of function calls.
*/
protected int functionCallCount = 0; protected int functionCallCount = 0;
/**
* Size of the target population.
*/
protected int targetPopSize = 50; protected int targetPopSize = 50;
protected Population populationArchive = null; protected Population populationArchive = null;
/**
* Method by which the Population gets initialized.
*/
PopulationInitMethod initMethod = PopulationInitMethod.individualDefault; PopulationInitMethod initMethod = PopulationInitMethod.individualDefault;
private double[] seedPos = new double[10]; private double[] seedPos = new double[10];
private Pair<Integer, Integer> seedCardinality = new Pair<Integer, Integer>(5, 1); private Pair<Integer, Integer> seedCardinality = new Pair<Integer, Integer>(5, 1);
private double aroundDist = 0.1; private double aroundDist = 0.1;
transient private ArrayList<InterfacePopulationChangedEventListener> listeners = null; transient private ArrayList<InterfacePopulationChangedEventListener> listeners = null;
// the evaluation interval at which listeners are notified
/**
* The evaluation interval at which listeners are notified.
*/
protected int notifyEvalInterval = 0; protected int notifyEvalInterval = 0;
// additional data connected to the population
/**
* Additional data connected to the population.
*/
protected HashMap<String, Object> additionalPopData = null; protected HashMap<String, Object> additionalPopData = null;
// historical best indidivuals may be traced for a given number of generations. Set to -1 to trace all, set to 0 to not trace at all
/**
* historical best individuals may be traced for a given number of generations.
* Set to -1 to trace all, set to 0 to not trace at all
*/
int historyMaxLen = 0; int historyMaxLen = 0;
/**
* Best n Individuals in the history.
*/
private transient LinkedList<AbstractEAIndividual> m_History = new LinkedList<AbstractEAIndividual>(); private transient LinkedList<AbstractEAIndividual> m_History = new LinkedList<AbstractEAIndividual>();
// remember when the last sorted queue was prepared
/**
* Remember when the last sorted queue was prepared.
*/
private int lastQModCount = -1; private int lastQModCount = -1;
// a sorted queue (for efficiency)
/**
* A sorted queue (for efficiency).
*/
transient private ArrayList<AbstractEAIndividual> sortedArr = null; transient private ArrayList<AbstractEAIndividual> sortedArr = null;
private Comparator<Object> lastSortingComparator = null; private Comparator<Object> lastSortingComparator = null;
private InterfaceDistanceMetric popDistMetric = null; // an associated metric private InterfaceDistanceMetric popDistMetric = null; // an associated metric
// private AbstractEAIndividualComparator historyComparator = null;
public static final String funCallIntervalReached = "FunCallIntervalReached"; public static final String funCallIntervalReached = "FunCallIntervalReached";
public static final String populationInitialized = "PopulationReinitOccured"; public static final String populationInitialized = "PopulationReinitOccured";
public static final String nextGenerationPerformed = "NextGenerationPerformed"; public static final String nextGenerationPerformed = "NextGenerationPerformed";
@ -195,7 +232,6 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
if (population.seedPos != null) { if (population.seedPos != null) {
this.seedPos = population.seedPos.clone(); this.seedPos = population.seedPos.clone();
} }
// this.m_Listener = population.m_Listener;
if (population.listeners != null) { if (population.listeners != null) {
this.listeners = (ArrayList<InterfacePopulationChangedEventListener>) population.listeners.clone(); this.listeners = (ArrayList<InterfacePopulationChangedEventListener>) population.listeners.clone();
} else { } else {
@ -1654,7 +1690,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* *
* @param size * @param size
*/ */
public void setTargetSize(int size) { public final void setTargetSize(int size) {
this.targetPopSize = size; this.targetPopSize = size;
} }
@ -1665,7 +1701,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* @return * @return
*/ */
public Population setTargetPopSize(int size) { public Population setTargetPopSize(int size) {
setTargetSize(size); this.setTargetSize(size);
return this; return this;
} }
@ -2328,7 +2364,7 @@ public class Population extends ArrayList implements PopulationInterface, Clonea
* Calculate the fitness sum over all individuals for one criterion. * Calculate the fitness sum over all individuals for one criterion.
* *
* @param criterion * @param criterion
* @return the fitness sum over all individuals for one criterio * @return the fitness sum over all individuals for one criteria
*/ */
public double getFitSum(int criterion) { public double getFitSum(int criterion) {
double fSum = 0.; double fSum = 0.;

View File

@ -12,329 +12,360 @@ import eva2.server.go.populations.SolutionSet;
import eva2.server.go.problems.F1Problem; import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.InterfaceOptimizationProblem; import eva2.server.go.problems.InterfaceOptimizationProblem;
/** The traditional genetic algorithms as devised by Holland. To only special here /**
* it the plague factor which reduces the population size to tune from a global to * The traditional genetic algorithms as devised by Holland. To only special
* a more local search. But you have to be careful with that else the GA might not * here it the plague factor which reduces the population size to tune from a
* converge. * global to a more local search. But you have to be careful with that else the
* This is a implementation of Genetic Algorithms. * GA might not converge. This is a implementation of Genetic Algorithms.
* Copyright: Copyright (c) 2003 * Copyright: Copyright (c) 2003 Company: University of Tuebingen, Computer
* Company: University of Tuebingen, Computer Architecture * Architecture
* @author Felix Streichert *
* @version: $Revision: 307 $ * @author Felix Streichert
* $Date: 2007-12-04 14:31:47 +0100 (Tue, 04 Dec 2007) $ * @version: $Revision: 307 $ $Date: 2007-12-04 14:31:47 +0100 (Tue, 04 Dec
* $Author: mkron $ * 2007) $ $Author: mkron $
*/ */
public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializable { public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializable {
private Population m_Population = new Population(); private Population population = new Population();
private InterfaceOptimizationProblem m_Problem = new F1Problem(); private InterfaceOptimizationProblem optimizationProblem = new F1Problem();
private InterfaceSelection m_ParentSelection = new SelectXProbRouletteWheel(); private InterfaceSelection parentSelection = new SelectXProbRouletteWheel();
private InterfaceSelection m_PartnerSelection = new SelectRandom(); private InterfaceSelection partnerSelection = new SelectRandom();
private boolean m_UseElitism = true; private boolean useElitism = true;
private int m_Plague = 0; private int plague = 0;
private int m_NumberOfPartners = 1; private int numberOfPartners = 1;
private String identifier = "";
transient private InterfacePopulationChangedEventListener popChangedListener;
private String m_Identifier = ""; public GeneticAlgorithm() {
transient private InterfacePopulationChangedEventListener m_Listener; }
public GeneticAlgorithm() { public GeneticAlgorithm(GeneticAlgorithm ga) {
} this.population = (Population) ga.population.clone();
this.optimizationProblem = (InterfaceOptimizationProblem) ga.optimizationProblem.clone();
this.identifier = ga.identifier;
this.plague = ga.plague;
this.numberOfPartners = ga.numberOfPartners;
this.useElitism = ga.useElitism;
this.parentSelection = (InterfaceSelection) ga.parentSelection.clone();
this.partnerSelection = (InterfaceSelection) ga.partnerSelection.clone();
}
public GeneticAlgorithm(GeneticAlgorithm a) { @Override
this.m_Population = (Population)a.m_Population.clone(); public Object clone() {
this.m_Problem = (InterfaceOptimizationProblem)a.m_Problem.clone(); return (Object) new GeneticAlgorithm(this);
this.m_Identifier = a.m_Identifier; }
this.m_Plague = a.m_Plague;
this.m_NumberOfPartners = a.m_NumberOfPartners;
this.m_UseElitism = a.m_UseElitism;
this.m_ParentSelection = (InterfaceSelection)a.m_ParentSelection.clone();
this.m_PartnerSelection = (InterfaceSelection)a.m_PartnerSelection.clone();
}
public Object clone() { @Override
return (Object) new GeneticAlgorithm(this); public void init() {
} this.optimizationProblem.initPopulation(this.population);
this.evaluatePopulation(this.population);
this.firePropertyChangedEvent(Population.nextGenerationPerformed);
}
public void init() { /**
this.m_Problem.initPopulation(this.m_Population); * This method will init the optimizer with a given population
this.evaluatePopulation(this.m_Population); *
* @param reset If true the population is reset.
*/
@Override
public void initByPopulation(Population pop, boolean reset) {
this.population = (Population) pop.clone();
if (reset) {
this.optimizationProblem.initPopulation(population);
this.population.init();
this.evaluatePopulation(this.population);
this.firePropertyChangedEvent(Population.nextGenerationPerformed); this.firePropertyChangedEvent(Population.nextGenerationPerformed);
} }
}
/** This method will init the optimizer with a given population /**
* @param reset If true the population is reset. * This method will evaluate the current population using the given problem.
*/ *
public void initByPopulation(Population pop, boolean reset) { * @param population The population that is to be evaluated
this.m_Population = (Population)pop.clone(); */
if (reset) { private void evaluatePopulation(Population population) {
this.m_Problem.initPopulation(m_Population); this.optimizationProblem.evaluate(population);
this.m_Population.init(); population.incrGeneration();
this.evaluatePopulation(this.m_Population); }
this.firePropertyChangedEvent(Population.nextGenerationPerformed);
} /**
* This method will generate the offspring population from the given
* population of evaluated individuals.
*/
private Population generateChildren() {
Population result = population.cloneWithoutInds();
Population parents;
AbstractEAIndividual[] offSprings;
AbstractEAIndividual tmpIndy;
this.parentSelection.prepareSelection(this.population);
this.partnerSelection.prepareSelection(this.population);
parents = this.parentSelection.selectFrom(this.population, this.population.getTargetSize());
if (parents.getEAIndividual(0).getMutationOperator() instanceof InterfaceAdaptOperatorGenerational) {
((InterfaceAdaptOperatorGenerational) parents.getEAIndividual(0).getMutationOperator()).adaptAfterSelection(population, parents);
}
if (parents.getEAIndividual(0).getCrossoverOperator() instanceof InterfaceAdaptOperatorGenerational) {
((InterfaceAdaptOperatorGenerational) parents.getEAIndividual(0).getCrossoverOperator()).adaptAfterSelection(population, parents);
} }
/** This method will evaluate the current population using the for (int i = 0; i < parents.size(); i++) {
* given problem. tmpIndy = ((AbstractEAIndividual) parents.get(i));
* @param population The population that is to be evaluated if (tmpIndy == null) {
*/ System.out.println("Individual null " + i + " Population size: " + parents.size());
private void evaluatePopulation(Population population) { }
this.m_Problem.evaluate(population); if (this.population == null) {
population.incrGeneration(); System.out.println("population null " + i);
}
// ToDo: tmpIndy can be null. We shouldn't call a method on null..
offSprings = tmpIndy.mateWith(this.partnerSelection.findPartnerFor(tmpIndy, this.population, this.numberOfPartners));
offSprings[0].mutate();
result.add(i, offSprings[0]);
} }
this.evaluatePopulation(result);
// /** This method will assign fitness values to all individual in the if (parents.getEAIndividual(0).getMutationOperator() instanceof InterfaceAdaptOperatorGenerational) {
// * current population. ((InterfaceAdaptOperatorGenerational) parents.getEAIndividual(0).getMutationOperator()).adaptGenerational(population, parents, result, true);
// * @param population The population that is to be evaluated
// */
// private void defaultEvaluatePopulation(Population population) {
// GAIndividualBinaryData tmpIndy;
// for (int i = 0; i < population.size(); i++) {
// tmpIndy = (GAIndividualBinaryData) population.get(i);
// tmpIndy.SetFitness(0, tmpIndy.defaultEvaulateAsMiniBits());
// population.incrFunctionCalls();
// }
// population.incrGeneration();
// }
/** This method will generate the offspring population from the
* given population of evaluated individuals.
*/
private Population generateChildren() {
Population result = m_Population.cloneWithoutInds();
Population parents;
AbstractEAIndividual[] offSprings;
AbstractEAIndividual tmpIndy;
//this.m_NormationOperator.computeSelectionProbability(this.m_Population, "Fitness");
//System.out.println("Population:"+this.m_Population.getSolutionRepresentationFor());
this.m_ParentSelection.prepareSelection(this.m_Population);
this.m_PartnerSelection.prepareSelection(this.m_Population);
parents = this.m_ParentSelection.selectFrom(this.m_Population, this.m_Population.getTargetSize());
// System.out.println("Parents:"+parents.getStringRepresentation());
// double[] meas = parents.getPopulationMeasures();
if (parents.getEAIndividual(0).getMutationOperator() instanceof InterfaceAdaptOperatorGenerational) {
((InterfaceAdaptOperatorGenerational)parents.getEAIndividual(0).getMutationOperator()).adaptAfterSelection(m_Population, parents);
}
if (parents.getEAIndividual(0).getCrossoverOperator() instanceof InterfaceAdaptOperatorGenerational) {
((InterfaceAdaptOperatorGenerational)parents.getEAIndividual(0).getCrossoverOperator()).adaptAfterSelection(m_Population, parents);
}
for (int i = 0; i < parents.size(); i++) {
tmpIndy = ((AbstractEAIndividual)parents.get(i));
if (tmpIndy == null) System.out.println("Individual null " + i + " Population size: "+ parents.size());
if (this.m_Population == null) System.out.println("population null "+i);
offSprings = tmpIndy.mateWith(this.m_PartnerSelection.findPartnerFor(tmpIndy, this.m_Population, this.m_NumberOfPartners));
// for (int j = 0; j < offSprings.length; j++) {
// offSprings[j].mutate(); // quite useless if n-1 are thrown away...
// }
offSprings[0].mutate();
result.add(i, offSprings[0]);
}
this.evaluatePopulation(result);
if (parents.getEAIndividual(0).getMutationOperator() instanceof InterfaceAdaptOperatorGenerational) {
((InterfaceAdaptOperatorGenerational)parents.getEAIndividual(0).getMutationOperator()).adaptGenerational(m_Population, parents, result, true);
}
if (parents.getEAIndividual(0).getCrossoverOperator() instanceof InterfaceAdaptOperatorGenerational) {
((InterfaceAdaptOperatorGenerational)parents.getEAIndividual(0).getCrossoverOperator()).adaptGenerational(m_Population, parents, result, true);
}
return result;
} }
if (parents.getEAIndividual(0).getCrossoverOperator() instanceof InterfaceAdaptOperatorGenerational) {
((InterfaceAdaptOperatorGenerational) parents.getEAIndividual(0).getCrossoverOperator()).adaptGenerational(population, parents, result, true);
}
return result;
}
public void optimize() { @Override
Population nextGeneration; public void optimize() {
nextGeneration = this.generateChildren(); Population nextGeneration;
nextGeneration = this.generateChildren();
if (this.m_UseElitism) { if (this.useElitism) {
AbstractEAIndividual elite = this.m_Population.getBestEAIndividual(); AbstractEAIndividual elite = this.population.getBestEAIndividual();
if (elite != null) { if (elite != null) {
this.m_Population = nextGeneration; this.population = nextGeneration;
this.m_Population.remove(0);// This implements a random replacement strategy for the elite this.population.remove(0);// This implements a random replacement strategy for the elite
this.m_Population.add(elite); this.population.add(elite);
}
} else {
this.population = nextGeneration;
}
if (this.plague > 0) {
for (int i = 0; i < this.plague; i++) {
if (this.population.size() > 2) {
this.population.remove(this.population.getWorstEAIndividual());
} }
} else {
this.m_Population = nextGeneration;
} }
if (this.m_Plague > 0) { this.population.setTargetSize(this.population.size());
for (int i = 0; i < this.m_Plague; i++) if (this.m_Population.size() > 2) this.m_Population.remove(this.m_Population.getWorstEAIndividual());
this.m_Population.setTargetSize(this.m_Population.size());
}
// System.out.println("Population size: " + this.m_Population.size());
// System.out.println("Population: " + m_Population.getStringRepresentation());
// if (this.m_Population.getArchive() != null) {
// if (this.m_Population.getArchive().getArchive() != null) {
// System.out.println("Zwei Archive!");
// this.m_Population.getArchive().SetArchive(null);
// }
// }
// this.m_Population.incrGeneration();
this.firePropertyChangedEvent(Population.nextGenerationPerformed);
} }
this.firePropertyChangedEvent(Population.nextGenerationPerformed);
}
/** This method allows you to add the LectureGUI as listener to the Optimizer /**
* @param ea * This method allows you to add the LectureGUI as listener to the Optimizer
*/ *
public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) { * @param ea
this.m_Listener = ea; */
} @Override
public boolean removePopulationChangedEventListener( public void addPopulationChangedEventListener(InterfacePopulationChangedEventListener ea) {
InterfacePopulationChangedEventListener ea) { this.popChangedListener = ea;
if (m_Listener==ea) { }
m_Listener=null;
return true;
} else return false;
}
/** Something has changed
*/
protected void firePropertyChangedEvent (String name) {
if (this.m_Listener != null) this.m_Listener.registerPopulationStateChanged(this, name);
}
/** This method will set the problem that is to be optimized @Override
* @param problem public boolean removePopulationChangedEventListener(
*/ InterfacePopulationChangedEventListener ea) {
public void setProblem (InterfaceOptimizationProblem problem) { if (popChangedListener == ea) {
this.m_Problem = problem; popChangedListener = null;
} return true;
public InterfaceOptimizationProblem getProblem () { } else {
return this.m_Problem; return false;
} }
}
/** This method will return a string describing all properties of the optimizer /**
* and the applied methods. * Something has changed.
* @return A descriptive string */
*/ protected void firePropertyChangedEvent(String name) {
public String getStringRepresentation() { if (this.popChangedListener != null) {
String result = ""; this.popChangedListener.registerPopulationStateChanged(this, name);
result += "Genetic Algorithm:\n";
result += "Using:\n";
result += " Population Size = " + this.m_Population.getTargetSize() + "/" + this.m_Population.size() + "\n";
result += " Parent Selection = " + this.m_ParentSelection.getClass().toString() + "\n";
result += " Partner Selection = " + this.m_PartnerSelection.getClass().toString() + "\n";
result += " Number of Partners = " + this.m_NumberOfPartners + "\n";
result += " Elitism = " + this.m_UseElitism + "\n";
result += "=> The Optimization Problem: ";
result += this.m_Problem.getStringRepresentationForProblem(this) +"\n";
//result += this.m_Population.getStringRepresentation();
return result;
} }
/** This method allows you to set an identifier for the algorithm }
* @param name The indenifier
*/
public void setIdentifier(String name) {
this.m_Identifier = name;
}
public String getIdentifier() {
return this.m_Identifier;
}
/** This method is required to free the memory on a RMIServer, /**
* but there is nothing to implement. * This method will set the problem that is to be optimized
*/ *
public void freeWilly() { * @param problem
*/
@Override
public void setProblem(InterfaceOptimizationProblem problem) {
this.optimizationProblem = problem;
}
} @Override
/********************************************************************************************************************** public InterfaceOptimizationProblem getProblem() {
return this.optimizationProblem;
}
/**
* This method will return a string describing all properties of the
* optimizer and the applied methods.
*
* @return A descriptive string
*/
@Override
public String getStringRepresentation() {
String result = "";
result += "Genetic Algorithm:\n";
result += "Using:\n";
result += " Population Size = " + this.population.getTargetSize() + "/" + this.population.size() + "\n";
result += " Parent Selection = " + this.parentSelection.getClass().toString() + "\n";
result += " Partner Selection = " + this.partnerSelection.getClass().toString() + "\n";
result += " Number of Partners = " + this.numberOfPartners + "\n";
result += " Elitism = " + this.useElitism + "\n";
result += "=> The Optimization Problem: ";
result += this.optimizationProblem.getStringRepresentationForProblem(this) + "\n";
return result;
}
/**
* This method allows you to set an identifier for the algorithm.
*
* @param name The identifier
*/
@Override
public void setIdentifier(String name) {
this.identifier = name;
}
@Override
public String getIdentifier() {
return this.identifier;
}
/**
* This method is required to free the memory on a RMIServer, but there is
* nothing to implement.
*/
public void freeWilly() {
}
/**
* ********************************************************************************************************************
* These are for GUI * These are for GUI
*/ */
/** This method returns a global info string /**
* @return description * This method returns a global info string
*/ *
public static String globalInfo() { * @return description
return "This is a basic generational Genetic Algorithm."; */
} public static String globalInfo() {
/** This method will return a naming String return "This is a basic generational Genetic Algorithm.";
* @return The name of the algorithm }
*/
public String getName() {
return "GA";
}
/** Assuming that all optimizer will store thier data in a population /**
* we will allow acess to this population to query to current state * This method will return a naming String
* of the optimizer. *
* @return The population of current solutions to a given problem. * @return The name of the algorithm
*/ */
public Population getPopulation() { @Override
return this.m_Population; public String getName() {
} return "GA";
public void setPopulation(Population pop){ }
this.m_Population = pop;
}
public String populationTipText() {
return "Edit the properties of the population used.";
}
public InterfaceSolutionSet getAllSolutions() { /**
return new SolutionSet(getPopulation()); * Assuming that all optimizer will store thier data in a population we will
} * allow acess to this population to query to current state of the
// /** This method will set the normation method that is to be used. * optimizer.
// * @param normation *
// */ * @return The population of current solutions to a given problem.
// public void setNormationMethod (InterfaceNormation normation) { */
// this.m_NormationOperator = normation; @Override
// } public Population getPopulation() {
// public InterfaceNormation getNormationMethod () { return this.population;
// return this.m_NormationOperator; }
// }
// public String normationMethodTipText() {
// return "Select the normation method.";
// }
/** Choose a parent selection method. @Override
* @param selection public void setPopulation(Population pop) {
*/ this.population = pop;
public void setParentSelection(InterfaceSelection selection) { }
this.m_ParentSelection = selection;
}
public InterfaceSelection getParentSelection() {
return this.m_ParentSelection;
}
public String parentSelectionTipText() {
return "Choose a parent selection method.";
}
/** Enable/disable elitism. public String populationTipText() {
* @param elitism return "Edit the properties of the population used.";
*/ }
public void setElitism (boolean elitism) {
this.m_UseElitism = elitism;
}
public boolean getElitism() {
return this.m_UseElitism;
}
public String elitismTipText() {
return "Enable/disable elitism.";
}
/** The number of mating partners needed to create offsprings. @Override
* @param partners public InterfaceSolutionSet getAllSolutions() {
*/ return new SolutionSet(getPopulation());
public void setNumberOfPartners(int partners) { }
if (partners < 0) partners = 0;
this.m_NumberOfPartners = partners;
}
public int getNumberOfPartners() {
return this.m_NumberOfPartners;
}
public String numberOfPartnersTipText() {
return "The number of mating partners needed to create offsprings.";
}
/** Choose a selection method for selecting recombination partners for given parents. /**
* @param selection * Choose a parent selection method.
*/ *
public void setPartnerSelection(InterfaceSelection selection) { * @param selection
this.m_PartnerSelection = selection; */
} public void setParentSelection(InterfaceSelection selection) {
public InterfaceSelection getPartnerSelection() { this.parentSelection = selection;
return this.m_PartnerSelection; }
}
public String partnerSelectionTipText() { public InterfaceSelection getParentSelection() {
return "Choose a selection method for selecting recombination partners for given parents."; return this.parentSelection;
}
public String parentSelectionTipText() {
return "Choose a parent selection method.";
}
/**
* Enable/disable elitism.
*
* @param elitism
*/
public void setElitism(boolean elitism) {
this.useElitism = elitism;
}
public boolean getElitism() {
return this.useElitism;
}
public String elitismTipText() {
return "Enable/disable elitism.";
}
/**
* The number of mating partners needed to create offsprings.
*
* @param partners
*/
public void setNumberOfPartners(int partners) {
if (partners < 0) {
partners = 0;
} }
this.numberOfPartners = partners;
}
public int getNumberOfPartners() {
return this.numberOfPartners;
}
public String numberOfPartnersTipText() {
return "The number of mating partners needed to create offsprings.";
}
/**
* Choose a selection method for selecting recombination partners for given
* parents.
*
* @param selection
*/
public void setPartnerSelection(InterfaceSelection selection) {
this.partnerSelection = selection;
}
public InterfaceSelection getPartnerSelection() {
return this.partnerSelection;
}
public String partnerSelectionTipText() {
return "Choose a selection method for selecting recombination partners for given parents.";
}
} }

View File

@ -66,7 +66,7 @@ public class PopulationBasedIncrementalLearning implements InterfaceOptimizer, j
public void init() { public void init() {
this.m_Problem.initPopulation(this.m_Population); this.m_Problem.initPopulation(this.m_Population);
if ((m_initialProbabilities!=null) && (m_initialProbabilities.length==((PBILPopulation)m_Population).getProbabilityVector().length)) { if ((m_initialProbabilities!=null) && (m_initialProbabilities.length==((PBILPopulation)m_Population).getProbabilityVector().length)) {
((PBILPopulation)m_Population).SetProbabilityVector(m_initialProbabilities); ((PBILPopulation)m_Population).setProbabilityVector(m_initialProbabilities);
} else { } else {
if (m_initialProbabilities!=null) System.err.println("Warning: initial probability vector doesnt match in length!"); if (m_initialProbabilities!=null) System.err.println("Warning: initial probability vector doesnt match in length!");
} }

View File

@ -44,24 +44,25 @@ import javax.swing.JOptionPane;
public class Processor extends Thread implements InterfaceProcessor, InterfacePopulationChangedEventListener { public class Processor extends Thread implements InterfaceProcessor, InterfacePopulationChangedEventListener {
private static final Logger LOGGER = Logger.getLogger(Processor.class.getName()); private static final Logger LOGGER = Logger.getLogger(Processor.class.getName());
private volatile boolean m_optRunning; private volatile boolean isOptimizationRunning;
private InterfaceStatistics m_Statistics; private InterfaceStatistics m_Statistics;
private InterfaceGOParameters goParams; private InterfaceGOParameters goParams;
private boolean m_createInitialPopulations = true; private boolean m_createInitialPopulations = true;
private boolean saveParams = true; private boolean saveParams = true;
private RemoteStateListener m_ListenerModule; private RemoteStateListener remoteStateListener;
private boolean wasRestarted = false; private boolean wasRestarted = false;
private int runCounter = 0; private int runCounter = 0;
private Population resPop = null; private Population resPop = null;
private boolean userAborted = false; private boolean userAborted = false;
@Override
public void addListener(RemoteStateListener module) { public void addListener(RemoteStateListener module) {
LOGGER.log( LOGGER.log(
Level.FINEST, Level.FINEST,
"Processor: setting module as listener: " + ((module == null) "Processor: setting module as listener: " + ((module == null)
? "null" : module.toString())); ? "null" : module.toString()));
m_ListenerModule = module; remoteStateListener = module;
} }
/** /**
@ -70,10 +71,10 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
* *
* @see InterfaceNotifyOnInformers * @see InterfaceNotifyOnInformers
*/ */
public Processor(InterfaceStatistics Stat, ModuleAdapter Adapter, InterfaceGOParameters params) { public Processor(InterfaceStatistics Stat, ModuleAdapter moduleAdapter, InterfaceGOParameters params) {
goParams = params; goParams = params;
m_Statistics = Stat; m_Statistics = Stat;
m_ListenerModule = Adapter; remoteStateListener = moduleAdapter;
// the statistics want to be informed if the strategy or the optimizer (which provide statistical data as InterfaceAdditionalInformer) change. // the statistics want to be informed if the strategy or the optimizer (which provide statistical data as InterfaceAdditionalInformer) change.
if (Stat != null && (params != null)) { if (Stat != null && (params != null)) {
@ -85,11 +86,11 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
} }
public boolean isOptRunning() { public boolean isOptRunning() {
return m_optRunning; return isOptimizationRunning;
} }
protected void setOptRunning(boolean bRun) { protected void setOptRunning(boolean bRun) {
m_optRunning = bRun; isOptimizationRunning = bRun;
} }
/** /**
@ -129,6 +130,7 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
/** /**
* *
*/ */
@Override
public void restartOpt() { public void restartOpt() {
m_createInitialPopulations = false; m_createInitialPopulations = false;
if (isOptRunning()) { if (isOptRunning()) {
@ -143,6 +145,7 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
/** /**
* *
*/ */
@Override
public void stopOpt() { // this means user break public void stopOpt() { // this means user break
setOptRunning(false); setOptRunning(false);
} }
@ -150,8 +153,9 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
/** /**
* *
*/ */
@Override
public void run() { public void run() {
setPriority(1); this.setPriority(1);
while (true) { while (true) {
try { try {
Thread.sleep(200); Thread.sleep(200);
@ -191,9 +195,9 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
} }
//m_Statistics.stopOptPerformed(false); //m_Statistics.stopOptPerformed(false);
setOptRunning(false); // normal finish setOptRunning(false); // normal finish
if (m_ListenerModule != null) { if (remoteStateListener != null) {
m_ListenerModule.performedStop(); // is only needed in client server mode remoteStateListener.performedStop(); // is only needed in client server mode
m_ListenerModule.updateProgress(0, errMsg); remoteStateListener.updateProgress(0, errMsg);
} }
} }
return resPop; return resPop;
@ -213,11 +217,11 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
RNG.setRandomSeed(goParams.getSeed()); RNG.setRandomSeed(goParams.getSeed());
if (m_ListenerModule != null) { if (remoteStateListener != null) {
if (wasRestarted) { if (wasRestarted) {
m_ListenerModule.performedRestart(getInfoString()); remoteStateListener.performedRestart(getInfoString());
} else { } else {
m_ListenerModule.performedStart(getInfoString()); remoteStateListener.performedStart(getInfoString());
} }
} }
@ -238,8 +242,8 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
} }
//m_Statistics.createNextGenerationPerformed((PopulationInterface)this.m_ModulParameter.getOptimizer().getPopulation()); //m_Statistics.createNextGenerationPerformed((PopulationInterface)this.m_ModulParameter.getOptimizer().getPopulation());
if (m_ListenerModule != null) { if (remoteStateListener != null) {
m_ListenerModule.updateProgress(getStatusPercent(goParams.getOptimizer().getPopulation(), runCounter, m_Statistics.getStatisticsParameter().getMultiRuns()), null); remoteStateListener.updateProgress(getStatusPercent(goParams.getOptimizer().getPopulation(), runCounter, m_Statistics.getStatisticsParameter().getMultiRuns()), null);
} }
if (popLog != null) { if (popLog != null) {
EVAHELP.clearLog(popLog); EVAHELP.clearLog(popLog);
@ -274,11 +278,11 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
} }
setOptRunning(false); // normal finish setOptRunning(false); // normal finish
if (m_ListenerModule != null) { if (remoteStateListener != null) {
m_ListenerModule.performedStop(); // is only needed in client server mode remoteStateListener.performedStop(); // is only needed in client server mode
} }
if (m_ListenerModule != null) { if (remoteStateListener != null) {
m_ListenerModule.updateProgress(0, null); remoteStateListener.updateProgress(0, null);
} }
goParams.getOptimizer().removePopulationChangedEventListener(this); goParams.getOptimizer().removePopulationChangedEventListener(this);
return resultPop; return resultPop;
@ -380,8 +384,8 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
(PopulationInterface) this.goParams.getOptimizer().getPopulation(), (PopulationInterface) this.goParams.getOptimizer().getPopulation(),
this.goParams.getOptimizer(), this.goParams.getOptimizer(),
getInformerList()); getInformerList());
if (m_ListenerModule != null) { if (remoteStateListener != null) {
m_ListenerModule.updateProgress( remoteStateListener.updateProgress(
getStatusPercent( getStatusPercent(
goParams.getOptimizer().getPopulation(), goParams.getOptimizer().getPopulation(),
runCounter, runCounter,