From 0f553039e4a9d24a071214fc1e6212bd8578127d Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Sat, 22 Dec 2012 12:27:10 +0000 Subject: [PATCH] Little fixes in LTGA (Logging, deprecated method) Refactored InterfaceOptimizer (Set* to set*) --- src/eva2/OptimizerFactory.java | 2 +- src/eva2/server/go/strategies/BOA.java | 2 +- .../go/strategies/BinaryScatterSearch.java | 2 +- .../CHCAdaptiveSearchAlgorithm.java | 2 +- .../go/strategies/ClusterBasedNichingEA.java | 2 +- .../go/strategies/ClusteringHillClimbing.java | 2 +- .../go/strategies/DifferentialEvolution.java | 2 +- .../server/go/strategies/EsDpiNiching.java | 2 +- .../go/strategies/EvolutionStrategies.java | 2 +- .../strategies/EvolutionaryProgramming.java | 2 +- .../server/go/strategies/FloodAlgorithm.java | 2 +- .../go/strategies/GeneticAlgorithm.java | 2 +- .../strategies/GradientDescentAlgorithm.java | 2 +- .../server/go/strategies/HillClimbing.java | 2 +- .../go/strategies/InterfaceOptimizer.java | 3 +- .../server/go/strategies/IslandModelEA.java | 10 +- src/eva2/server/go/strategies/LTGA.java | 697 +++++++++--------- .../go/strategies/MemeticAlgorithm.java | 2 +- .../go/strategies/MonteCarloSearch.java | 2 +- .../go/strategies/MultiObjectiveCMAES.java | 4 +- .../go/strategies/MultiObjectiveEA.java | 2 +- .../go/strategies/NelderMeadSimplex.java | 2 +- src/eva2/server/go/strategies/NichePSO.java | 2 +- .../ParticleFilterOptimization.java | 2 +- .../strategies/ParticleSwarmOptimization.java | 2 +- .../PopulationBasedIncrementalLearning.java | 2 +- .../server/go/strategies/ScatterSearch.java | 2 +- .../go/strategies/SimulatedAnnealing.java | 2 +- .../server/go/strategies/SteadyStateGA.java | 2 +- .../go/strategies/ThresholdAlgorithm.java | 2 +- src/eva2/server/go/strategies/Tribes.java | 2 +- .../go/strategies/WingedMultiObjectiveEA.java | 2 +- 32 files changed, 385 insertions(+), 385 deletions(-) diff --git a/src/eva2/OptimizerFactory.java b/src/eva2/OptimizerFactory.java index 57b00fab..4d5efcea 100644 --- a/src/eva2/OptimizerFactory.java +++ b/src/eva2/OptimizerFactory.java @@ -373,7 +373,7 @@ public class OptimizerFactory { setTemplateOperators(problem, mutator, 1., new NoCrossover(), 0); HillClimbing hc = new HillClimbing(); - hc.SetIdentifier("-"+popSize+"-"+mutator.getStringRepresentation()); + hc.setIdentifier("-"+popSize+"-"+mutator.getStringRepresentation()); hc.getPopulation().setTargetSize(popSize); hc.addPopulationChangedEventListener(listener); hc.SetProblem(problem); diff --git a/src/eva2/server/go/strategies/BOA.java b/src/eva2/server/go/strategies/BOA.java index 9b463e9e..f5168f7d 100644 --- a/src/eva2/server/go/strategies/BOA.java +++ b/src/eva2/server/go/strategies/BOA.java @@ -592,7 +592,7 @@ public class BOA implements InterfaceOptimizer, java.io.Serializable { return new SolutionSet(this.population); } - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/BinaryScatterSearch.java b/src/eva2/server/go/strategies/BinaryScatterSearch.java index 6f4fb067..85555a89 100644 --- a/src/eva2/server/go/strategies/BinaryScatterSearch.java +++ b/src/eva2/server/go/strategies/BinaryScatterSearch.java @@ -723,7 +723,7 @@ public class BinaryScatterSearch implements InterfaceOptimizer, java.io.Serializ return new SolutionSet(this.refSet); } - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/CHCAdaptiveSearchAlgorithm.java b/src/eva2/server/go/strategies/CHCAdaptiveSearchAlgorithm.java index 86925b54..69824742 100644 --- a/src/eva2/server/go/strategies/CHCAdaptiveSearchAlgorithm.java +++ b/src/eva2/server/go/strategies/CHCAdaptiveSearchAlgorithm.java @@ -261,7 +261,7 @@ public class CHCAdaptiveSearchAlgorithm implements InterfaceOptimizer, java.io.S /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/ClusterBasedNichingEA.java b/src/eva2/server/go/strategies/ClusterBasedNichingEA.java index 664936f4..7c4babe2 100644 --- a/src/eva2/server/go/strategies/ClusterBasedNichingEA.java +++ b/src/eva2/server/go/strategies/ClusterBasedNichingEA.java @@ -996,7 +996,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/ClusteringHillClimbing.java b/src/eva2/server/go/strategies/ClusteringHillClimbing.java index f79dcf8d..d465d0e5 100644 --- a/src/eva2/server/go/strategies/ClusteringHillClimbing.java +++ b/src/eva2/server/go/strategies/ClusteringHillClimbing.java @@ -97,7 +97,7 @@ InterfaceOptimizer, Serializable, InterfaceAdditionalPopulationInformer { setLocalSearchMethod(getLocalSearchMethod()); } - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/DifferentialEvolution.java b/src/eva2/server/go/strategies/DifferentialEvolution.java index d9dd2b57..ee140e5d 100644 --- a/src/eva2/server/go/strategies/DifferentialEvolution.java +++ b/src/eva2/server/go/strategies/DifferentialEvolution.java @@ -687,7 +687,7 @@ public class DifferentialEvolution implements InterfaceOptimizer, java.io.Serial /** This method allows you to set an identifier for the algorithm * @param name The identifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/EsDpiNiching.java b/src/eva2/server/go/strategies/EsDpiNiching.java index 127c193c..4bbd94ac 100644 --- a/src/eva2/server/go/strategies/EsDpiNiching.java +++ b/src/eva2/server/go/strategies/EsDpiNiching.java @@ -1062,7 +1062,7 @@ public class EsDpiNiching implements InterfaceOptimizer, Serializable, Interface return identifier; } - public void SetIdentifier(String name) { + public void setIdentifier(String name) { identifier = name; } diff --git a/src/eva2/server/go/strategies/EvolutionStrategies.java b/src/eva2/server/go/strategies/EvolutionStrategies.java index 63056a0c..a946ef5a 100644 --- a/src/eva2/server/go/strategies/EvolutionStrategies.java +++ b/src/eva2/server/go/strategies/EvolutionStrategies.java @@ -323,7 +323,7 @@ public class EvolutionStrategies implements InterfaceOptimizer, java.io.Serializ * * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.identifier = name; } diff --git a/src/eva2/server/go/strategies/EvolutionaryProgramming.java b/src/eva2/server/go/strategies/EvolutionaryProgramming.java index 9ae9ac2b..a484a689 100644 --- a/src/eva2/server/go/strategies/EvolutionaryProgramming.java +++ b/src/eva2/server/go/strategies/EvolutionaryProgramming.java @@ -153,7 +153,7 @@ public class EvolutionaryProgramming implements InterfaceOptimizer, java.io.Seri /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/FloodAlgorithm.java b/src/eva2/server/go/strategies/FloodAlgorithm.java index 68bf664b..ec0d558f 100644 --- a/src/eva2/server/go/strategies/FloodAlgorithm.java +++ b/src/eva2/server/go/strategies/FloodAlgorithm.java @@ -200,7 +200,7 @@ public class FloodAlgorithm implements InterfaceOptimizer, java.io.Serializable /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/GeneticAlgorithm.java b/src/eva2/server/go/strategies/GeneticAlgorithm.java index 7a3e11d0..86ecf86f 100644 --- a/src/eva2/server/go/strategies/GeneticAlgorithm.java +++ b/src/eva2/server/go/strategies/GeneticAlgorithm.java @@ -225,7 +225,7 @@ public class GeneticAlgorithm implements InterfaceOptimizer, java.io.Serializabl /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/GradientDescentAlgorithm.java b/src/eva2/server/go/strategies/GradientDescentAlgorithm.java index 5dff23b3..f1689fcb 100644 --- a/src/eva2/server/go/strategies/GradientDescentAlgorithm.java +++ b/src/eva2/server/go/strategies/GradientDescentAlgorithm.java @@ -316,7 +316,7 @@ public class GradientDescentAlgorithm implements InterfaceOptimizer, java.io.Ser /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/HillClimbing.java b/src/eva2/server/go/strategies/HillClimbing.java index 6fe86a76..65866502 100644 --- a/src/eva2/server/go/strategies/HillClimbing.java +++ b/src/eva2/server/go/strategies/HillClimbing.java @@ -208,7 +208,7 @@ public class HillClimbing implements InterfaceOptimizer, java.io.Serializable { /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/InterfaceOptimizer.java b/src/eva2/server/go/strategies/InterfaceOptimizer.java index 7470238b..74b76f73 100644 --- a/src/eva2/server/go/strategies/InterfaceOptimizer.java +++ b/src/eva2/server/go/strategies/InterfaceOptimizer.java @@ -83,8 +83,9 @@ public interface InterfaceOptimizer { /** * This method allows you to set an identifier for the algorithm * @param name The identifier + * @deprecated */ - public void SetIdentifier(String name); + public void setIdentifier(String name); public String getIdentifier(); /** diff --git a/src/eva2/server/go/strategies/IslandModelEA.java b/src/eva2/server/go/strategies/IslandModelEA.java index 23ae10de..58ea0c2a 100644 --- a/src/eva2/server/go/strategies/IslandModelEA.java +++ b/src/eva2/server/go/strategies/IslandModelEA.java @@ -109,7 +109,7 @@ public class IslandModelEA implements InterfacePopulationChangedEventListener, I this.m_Islands = new InterfaceOptimizer[this.m_numLocalCPUs]; for (int i = 0; i < this.m_numLocalCPUs; i++) { this.m_Islands[i] = (InterfaceOptimizer) this.m_Optimizer.clone(); - this.m_Islands[i].SetIdentifier(""+i); + this.m_Islands[i].setIdentifier(""+i); this.m_Islands[i].init(); if (this.m_LogLocalChanges) this.m_Islands[i].addPopulationChangedEventListener(this); @@ -129,7 +129,7 @@ public class IslandModelEA implements InterfacePopulationChangedEventListener, I this.m_Islands = new InterfaceOptimizer[nodesList.length]; for (int i = 0; i < nodesList.length; i++) { this.m_Islands[i] = (InterfaceOptimizer) RMIProxyRemoteThread.newInstance(this.m_Optimizer, nodesList[i]); - this.m_Islands[i].SetIdentifier(""+i); + this.m_Islands[i].setIdentifier(""+i); this.m_Islands[i].init(); if (this.m_LogLocalChanges) this.m_Islands[i].addPopulationChangedEventListener(myLocal); @@ -178,7 +178,7 @@ public class IslandModelEA implements InterfacePopulationChangedEventListener, I this.m_Islands = new InterfaceOptimizer[this.m_numLocalCPUs]; for (int i = 0; i < this.m_numLocalCPUs; i++) { this.m_Islands[i] = (InterfaceOptimizer) this.m_Optimizer.clone(); - this.m_Islands[i].SetIdentifier(""+i); + this.m_Islands[i].setIdentifier(""+i); this.m_Islands[i].init(); if (this.m_LogLocalChanges) this.m_Islands[i].addPopulationChangedEventListener(this); @@ -196,7 +196,7 @@ public class IslandModelEA implements InterfacePopulationChangedEventListener, I this.m_Islands = new InterfaceOptimizer[nodesList.length]; for (int i = 0; i < nodesList.length; i++) { this.m_Islands[i] = (InterfaceOptimizer) RMIProxyRemoteThread.newInstance(this.m_Optimizer, nodesList[i]); - this.m_Islands[i].SetIdentifier(""+i); + this.m_Islands[i].setIdentifier(""+i); this.m_Islands[i].init(); if (this.m_LogLocalChanges) this.m_Islands[i].addPopulationChangedEventListener(myLocal); @@ -390,7 +390,7 @@ public class IslandModelEA implements InterfacePopulationChangedEventListener, I /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/LTGA.java b/src/eva2/server/go/strategies/LTGA.java index b10fdc9c..42118e2d 100644 --- a/src/eva2/server/go/strategies/LTGA.java +++ b/src/eva2/server/go/strategies/LTGA.java @@ -23,375 +23,374 @@ import eva2.server.go.problems.InterfaceOptimizationProblem; import eva2.tools.Pair; import eva2.tools.math.SpecialFunction; -public class LTGA implements InterfaceOptimizer, java.io.Serializable, InterfacePopulationChangedEventListener{ - - private static final Logger LOGGER = Logger.getLogger(BOA.class.getName()); - transient private InterfacePopulationChangedEventListener m_Listener = null; - private String m_Identifier = "LTGA"; - - private int probDim = 8; - private int fitCrit = -1; - private int popSize = 50; - private Population population = new Population(); - private AbstractOptimizationProblem problem = new BKnapsackProblem(); - private AbstractEAIndividual template = null; - private int generationCycle = 500; - - public LTGA(){ - } - - public LTGA(LTGA l){ - this.m_Listener = l.m_Listener; - this.m_Identifier = l.m_Identifier; - this.probDim = l.probDim; - this.popSize = l.popSize; - this.population = (Population)l.population.clone(); - this.problem = (AbstractOptimizationProblem)l.problem.clone(); +public class LTGA implements InterfaceOptimizer, java.io.Serializable, InterfacePopulationChangedEventListener { + + private static final Logger LOGGER = Logger.getLogger(LTGA.class.getName()); + transient private InterfacePopulationChangedEventListener m_Listener = null; + private String m_Identifier = "LTGA"; + private int probDim = 8; + private int fitCrit = -1; + private int popSize = 50; + private Population population = new Population(); + private AbstractOptimizationProblem problem = new BKnapsackProblem(); + private AbstractEAIndividual template = null; + private int generationCycle = 500; + + public LTGA() { + } + + public LTGA(LTGA l) { + this.m_Listener = l.m_Listener; + this.m_Identifier = l.m_Identifier; + this.probDim = l.probDim; + this.popSize = l.popSize; + this.population = (Population) l.population.clone(); + this.problem = (AbstractOptimizationProblem) l.problem.clone(); this.template = (AbstractEAIndividual) template.clone(); - } - - public Object clone() { - return new LTGA(this); - } + } - @Override - public String getName() { - return "Linkage Tree Genetic Algorithm"; - } - - public static String globalInfo() { - return "Basic implementation of the Linkage Tree Genetic Algorithm based on the works by Dirk Thierens."; - } + @Override + public Object clone() { + return new LTGA(this); + } - @Override - public void addPopulationChangedEventListener( - InterfacePopulationChangedEventListener ea) { - this.m_Listener = ea; - - } + @Override + public String getName() { + return "Linkage Tree Genetic Algorithm"; + } - @Override - public boolean removePopulationChangedEventListener( - InterfacePopulationChangedEventListener ea) { - if (m_Listener == ea) { - m_Listener = null; - return true; - } else - return false; - } - - private void defaultInit(){ - if (population == null) { - this.population = new Population(this.popSize); - } else { - this.population.setTargetPopSize(this.popSize); - } - this.template = this.problem.getIndividualTemplate(); - if (!(template instanceof InterfaceDataTypeBinary)) { - LOGGER.log(Level.WARNING, "Requiring binary data!"); - } else { - Object dim = BeanInspector.callIfAvailable(problem, - "getProblemDimension", null); - if (dim == null) - LOGGER.log(Level.WARNING, "Coudn't get problem dimension!"); - probDim = (Integer) dim; - ((InterfaceDataTypeBinary) this.template).SetBinaryGenotype(new BitSet(probDim)); - } - this.population.addPopulationChangedEventListener(this); - this.population.setNotifyEvalInterval(this.generationCycle); - } - - private static BitSet getBinaryData(AbstractEAIndividual indy) { - if (indy instanceof InterfaceGAIndividual) - return ((InterfaceGAIndividual) indy).getBGenotype(); - else if (indy instanceof InterfaceDataTypeBinary) - return ((InterfaceDataTypeBinary) indy).getBinaryData(); - else { - throw new RuntimeException( - "Unable to get binary representation for " - + indy.getClass()); - } - } + public static String globalInfo() { + return "Basic implementation of the Linkage Tree Genetic Algorithm based on the works by Dirk Thierens."; + } - @Override - public void init() { - defaultInit(); - this.problem.initPopulation(this.population); - this.evaluatePopulation(this.population); - this.firePropertyChangedEvent(Population.nextGenerationPerformed); - } - - private void evaluatePopulation(Population pop) { - for (int i = 0; i < pop.size(); i++) { - evaluate(pop.getEAIndividual(i)); - } - } - - /** - * evaluate the given Individual and increments the counter. if the - * individual is null, only the counter is incremented - * - * @param indy - * the individual you want to evaluate - */ - private void evaluate(AbstractEAIndividual indy) { - // evaluate the given individual if it is not null - if (indy == null) { - LOGGER.log(Level.WARNING, "tried to evaluate null"); - return; - } - this.problem.evaluate(indy); - // increment the number of evaluations - this.population.incrFunctionCalls(); - } + @Override + public void addPopulationChangedEventListener( + InterfacePopulationChangedEventListener ea) { + this.m_Listener = ea; - @Override - public void initByPopulation(Population pop, boolean reset) { - if (reset) { - init(); - } else { - defaultInit(); - this.population = pop; - } - } - - private Stack> buildLinkageTree(){ - // the final tree - Stack> linkageTree = new Stack>(); - // the stack to cluster here clusters can be removed - Stack> workingStack = new Stack>(); - // add the problem variables to the stacks - for(int i=0; i s1 = new HashSet(); - Set s2 = new HashSet(); - s1.add(i); - s2.add(i); - linkageTree.add(s1); - workingStack.add(s2); - } + } + + @Override + public boolean removePopulationChangedEventListener( + InterfacePopulationChangedEventListener ea) { + if (m_Listener == ea) { + m_Listener = null; + return true; + } else { + return false; + } + } + + private void defaultInit() { + if (population == null) { + this.population = new Population(this.popSize); + } else { + this.population.setTargetPopSize(this.popSize); + } + this.template = this.problem.getIndividualTemplate(); + if (!(template instanceof InterfaceDataTypeBinary)) { + LOGGER.log(Level.WARNING, "Requiring binary data!"); + } else { + Object dim = BeanInspector.callIfAvailable(problem, + "getProblemDimension", null); + if (dim == null) { + LOGGER.log(Level.WARNING, "Couldn't get problem dimension!"); + } + probDim = (Integer) dim; + ((InterfaceDataTypeBinary) this.template).SetBinaryGenotype(new BitSet(probDim)); + } + this.population.addPopulationChangedEventListener(this); + this.population.setNotifyEvalInterval(this.generationCycle); + } + + private static BitSet getBinaryData(AbstractEAIndividual indy) { + if (indy instanceof InterfaceGAIndividual) { + return ((InterfaceGAIndividual) indy).getBGenotype(); + } else if (indy instanceof InterfaceDataTypeBinary) { + return ((InterfaceDataTypeBinary) indy).getBinaryData(); + } else { + throw new RuntimeException( + "Unable to get binary representation for " + + indy.getClass()); + } + } + + @Override + public void init() { + defaultInit(); + this.problem.initPopulation(this.population); + this.evaluatePopulation(this.population); + this.firePropertyChangedEvent(Population.nextGenerationPerformed); + } + + private void evaluatePopulation(Population pop) { + for (int i = 0; i < pop.size(); i++) { + evaluate(pop.getEAIndividual(i)); + } + } + + /** + * evaluate the given Individual and increments the counter. if the + * individual is null, only the counter is incremented + * + * @param indy the individual you want to evaluate + */ + private void evaluate(AbstractEAIndividual indy) { + // evaluate the given individual if it is not null + if (indy == null) { + LOGGER.log(Level.WARNING, "tried to evaluate null"); + return; + } + this.problem.evaluate(indy); + // increment the number of evaluations + this.population.incrFunctionCalls(); + } + + @Override + public void initByPopulation(Population pop, boolean reset) { + if (reset) { + init(); + } else { + defaultInit(); + this.population = pop; + } + } + + private Stack> buildLinkageTree() { + // the final tree + Stack> linkageTree = new Stack>(); + // the stack to cluster here clusters can be removed + Stack> workingStack = new Stack>(); + // add the problem variables to the stacks + for (int i = 0; i < this.probDim; i++) { + Set s1 = new HashSet(); + Set s2 = new HashSet(); + s1.add(i); + s2.add(i); + linkageTree.add(s1); + workingStack.add(s2); + } // double[] probMass = calculateProbabilityMassFunction(); - // until there is only one cluster left - while(workingStack.size()>1){ - Pair, Set> toCluster = findNearestClusters(workingStack); - // remove the second cluster from the working set - workingStack.remove(toCluster.tail); - // add all elements from the second cluster to the first one - toCluster.head.addAll(toCluster.tail); - // add the combined cluster to the linkage tree - linkageTree.add(toCluster.head); - } - return linkageTree; - } - - private Pair, Set> findNearestClusters(Stack> stack) { - Set bestI = new HashSet(); - Set bestJ = new HashSet(); - double bestScore = Double.MAX_VALUE; - for(int i=0; i s1 = stack.get(i); - for(int j=i+1; j s2 = stack.get(j); - double currDist = calculateDistance(s1, s2); - // better cluster found - if(currDist < bestScore){ - bestI = s1; - bestJ = s2; - bestScore = currDist; - } - } - } - // return the best pair - return new Pair, Set>(bestI, bestJ); - } + // until there is only one cluster left + while (workingStack.size() > 1) { + Pair, Set> toCluster = findNearestClusters(workingStack); + // remove the second cluster from the working set + workingStack.remove(toCluster.tail); + // add all elements from the second cluster to the first one + toCluster.head.addAll(toCluster.tail); + // add the combined cluster to the linkage tree + linkageTree.add(toCluster.head); + } + return linkageTree; + } - private double calculateDistance(Set s1, Set s2) { - double entropy1 = calculateEntropy(s1); - double entropy2 = calculateEntropy(s2); - Set combined = new HashSet(); - combined.addAll(s1); - combined.addAll(s2); - double entropy3 = calculateEntropy(combined); - return 2 - ((entropy1 + entropy2) / (entropy3)); - } + private Pair, Set> findNearestClusters(Stack> stack) { + Set bestI = new HashSet(); + Set bestJ = new HashSet(); + double bestScore = Double.MAX_VALUE; + for (int i = 0; i < stack.size(); i++) { + Set s1 = stack.get(i); + for (int j = i + 1; j < stack.size(); j++) { + Set s2 = stack.get(j); + double currDist = calculateDistance(s1, s2); + // better cluster found + if (currDist < bestScore) { + bestI = s1; + bestJ = s2; + bestScore = currDist; + } + } + } + // return the best pair + return new Pair, Set>(bestI, bestJ); + } - private double calculateEntropy(Set s){ - double entropy = 0.0; - // for possible states {0,1} do - for(int i=0; i<=1; i++){ - int count = 0; - // for every individual - for(int k=0; k s1, Set s2) { + double entropy1 = calculateEntropy(s1); + double entropy2 = calculateEntropy(s2); + Set combined = new HashSet(); + combined.addAll(s1); + combined.addAll(s2); + double entropy3 = calculateEntropy(combined); + return 2 - ((entropy1 + entropy2) / (entropy3)); + } - @Override - public void optimize() { - this.problem.evaluatePopulationStart(this.population); - Stack> linkageTree = buildLinkageTree(); - Population newPop = new Population(this.popSize); - for(int i=0; i<(this.popSize/2); i++){ - Population indies = this.population.getRandNIndividuals(2); - Population newIndies = buildNewIndies(indies, linkageTree); - newPop.addAll(newIndies); - } - this.population.clear(); - this.population.addAll(newPop); - this.problem.evaluatePopulationEnd(this.population); - } - - private Population buildNewIndies(Population indies, - Stack> linkageTree) { - if(indies.size() != 2){ - return indies; - } - AbstractEAIndividual indy1 = indies.getEAIndividual(0); - AbstractEAIndividual indy2 = indies.getEAIndividual(1); - BitSet gen1 = getBinaryData(indy1); - BitSet gen2 = getBinaryData(indy2); - for(Set mask: linkageTree){ - BitSet newGene1 = (BitSet) gen1.clone(); - BitSet newGene2 = (BitSet) gen2.clone(); - boolean same = true; - for(Integer exchange: mask){ - if(newGene1.get(exchange) != newGene2.get(exchange)){ - same = false; - } - newGene1.set(exchange, gen2.get(exchange)); - newGene2.set(exchange, gen1.get(exchange)); - } - if(!same){ - AbstractEAIndividual newIndy1 = (AbstractEAIndividual) this.template.clone(); - AbstractEAIndividual newIndy2 = (AbstractEAIndividual) this.template.clone(); - ((InterfaceDataTypeBinary) newIndy1).SetBinaryGenotype(newGene1); - ((InterfaceDataTypeBinary) newIndy2).SetBinaryGenotype(newGene2); - evaluate(newIndy1); - evaluate(newIndy2); - if(Math.min(newIndy1.getFitness(0), newIndy2.getFitness(0)) < Math.min(indy1.getFitness(0), indy2.getFitness(0))){ - indy1 = newIndy1; - indy2 = newIndy2; - } - } - } - Population result = new Population(2); - result.add(indy1); - result.add(indy2); - return result; - } + private double calculateEntropy(Set s) { + double entropy = 0.0; + // for possible states {0,1} do + for (int i = 0; i <= 1; i++) { + int count = 0; + // for every individual + for (int k = 0; k < this.popSize; k++) { + BitSet b = getBinaryData(this.population.getEAIndividual(k)); + boolean addCount = true; + // for every specified Bit + for (Integer value : s) { + // is the bit not set correctly + if (b.get(value) != (i == 1)) { + addCount = false; + break; + } + } + if (addCount) { + count++; + } + addCount = true; + } + entropy += ((double) count) * SpecialFunction.logb((double) count, 2.0); + count = 0; + } + return entropy; + } - /** - * Something has changed - */ - protected void firePropertyChangedEvent(String name) { - if (this.m_Listener != null) - this.m_Listener.registerPopulationStateChanged(this, name); - } + @Override + public void optimize() { + this.problem.evaluatePopulationStart(this.population); + Stack> linkageTree = buildLinkageTree(); + Population newPop = new Population(this.popSize); + for (int i = 0; i < (this.popSize / 2); i++) { + Population indies = this.population.getRandNIndividuals(2); + Population newIndies = buildNewIndies(indies, linkageTree); + newPop.addAll(newIndies); + } + this.population.clear(); + this.population.addAll(newPop); + this.problem.evaluatePopulationEnd(this.population); + } - @Override - public Population getPopulation() { - return this.population; - } + private Population buildNewIndies(Population indies, + Stack> linkageTree) { + if (indies.size() != 2) { + return indies; + } + AbstractEAIndividual indy1 = indies.getEAIndividual(0); + AbstractEAIndividual indy2 = indies.getEAIndividual(1); + BitSet gen1 = getBinaryData(indy1); + BitSet gen2 = getBinaryData(indy2); + for (Set mask : linkageTree) { + BitSet newGene1 = (BitSet) gen1.clone(); + BitSet newGene2 = (BitSet) gen2.clone(); + boolean same = true; + for (Integer exchange : mask) { + if (newGene1.get(exchange) != newGene2.get(exchange)) { + same = false; + } + newGene1.set(exchange, gen2.get(exchange)); + newGene2.set(exchange, gen1.get(exchange)); + } + if (!same) { + AbstractEAIndividual newIndy1 = (AbstractEAIndividual) this.template.clone(); + AbstractEAIndividual newIndy2 = (AbstractEAIndividual) this.template.clone(); + ((InterfaceDataTypeBinary) newIndy1).SetBinaryGenotype(newGene1); + ((InterfaceDataTypeBinary) newIndy2).SetBinaryGenotype(newGene2); + evaluate(newIndy1); + evaluate(newIndy2); + if (Math.min(newIndy1.getFitness(0), newIndy2.getFitness(0)) < Math.min(indy1.getFitness(0), indy2.getFitness(0))) { + indy1 = newIndy1; + indy2 = newIndy2; + } + } + } + Population result = new Population(2); + result.add(indy1); + result.add(indy2); + return result; + } - @Override - public void setPopulation(Population pop) { - this.population = pop; - } + /** + * Something has changed + */ + protected void firePropertyChangedEvent(String name) { + if (this.m_Listener != null) { + this.m_Listener.registerPopulationStateChanged(this, name); + } + } - @Override - public InterfaceSolutionSet getAllSolutions() { - return new SolutionSet(this.population); - } + @Override + public Population getPopulation() { + return this.population; + } - @Override - public void SetIdentifier(String name) { - this.m_Identifier = name; - } + @Override + public void setPopulation(Population pop) { + this.population = pop; + } - @Override - public String getIdentifier() { - return this.m_Identifier; - } + @Override + public InterfaceSolutionSet getAllSolutions() { + return new SolutionSet(this.population); + } - @Override - public void SetProblem(InterfaceOptimizationProblem problem) { - this.problem = (AbstractOptimizationProblem) problem; - } + @Override + public void setIdentifier(String name) { + this.m_Identifier = name; + } - @Override - public InterfaceOptimizationProblem getProblem() { - return this.problem; - } + @Override + public String getIdentifier() { + return this.m_Identifier; + } - @Override - public String getStringRepresentation() { - return "Linkage Tree GA"; - } + @Override + public void SetProblem(InterfaceOptimizationProblem problem) { + this.problem = (AbstractOptimizationProblem) problem; + } - @Override - public void freeWilly() { - - } + @Override + public InterfaceOptimizationProblem getProblem() { + return this.problem; + } - @SuppressWarnings("deprecation") - @Override - public void registerPopulationStateChanged(Object source, String name) { - // The events of the interim hill climbing population will be caught here - if (name.compareTo(Population.funCallIntervalReached) == 0) { - // set funcalls to real value - this.population.SetFunctionCalls(((Population)source).getFunctionCalls()); - this.firePropertyChangedEvent(Population.nextGenerationPerformed); - } - } - - public static void main(String[] args) { - LTGA ltga = new LTGA(); - ltga.init(); - ltga.optimize(); - System.out.println(ltga.popSize); - Population p = ltga.getPopulation(); - System.out.println(p.getFunctionCalls()+"\t"+p.size()); - System.out.println(p.getBestEAIndividual().getStringRepresentation()); - ltga.optimize(); - p = ltga.getPopulation(); - System.out.println(p.getFunctionCalls()+"\t"+p.size()); - System.out.println(p.getBestEAIndividual().getStringRepresentation()); - ltga.optimize(); - p = ltga.getPopulation(); - System.out.println(p.getFunctionCalls()+"\t"+p.size()); - System.out.println(p.getBestEAIndividual().getStringRepresentation()); - ltga.optimize(); - p = ltga.getPopulation(); - System.out.println(p.getFunctionCalls()+"\t"+p.size()); - System.out.println(p.getBestEAIndividual().getStringRepresentation()); - ltga.optimize(); - p = ltga.getPopulation(); - System.out.println(p.getFunctionCalls()+"\t"+p.size()); - System.out.println(p.getBestEAIndividual().getStringRepresentation()); - ltga.optimize(); - p = ltga.getPopulation(); - System.out.println(p.getFunctionCalls()+"\t"+p.size()); - System.out.println(p.getBestEAIndividual().getStringRepresentation()); - ltga.optimize(); - p = ltga.getPopulation(); - System.out.println(p.getFunctionCalls()+"\t"+p.size()); - System.out.println(p.getBestEAIndividual().getStringRepresentation()); - } + @Override + public String getStringRepresentation() { + return "Linkage Tree GA"; + } + @Override + public void freeWilly() { + } + + @Override + public void registerPopulationStateChanged(Object source, String name) { + // The events of the interim hill climbing population will be caught here + if (name.compareTo(Population.funCallIntervalReached) == 0) { + // set funcalls to real value + this.population.setFunctionCalls(((Population) source).getFunctionCalls()); + this.firePropertyChangedEvent(Population.nextGenerationPerformed); + } + } + + public static void main(String[] args) { + LTGA ltga = new LTGA(); + ltga.init(); + ltga.optimize(); + System.out.println(ltga.popSize); + Population p = ltga.getPopulation(); + System.out.println(p.getFunctionCalls() + "\t" + p.size()); + System.out.println(p.getBestEAIndividual().getStringRepresentation()); + ltga.optimize(); + p = ltga.getPopulation(); + System.out.println(p.getFunctionCalls() + "\t" + p.size()); + System.out.println(p.getBestEAIndividual().getStringRepresentation()); + ltga.optimize(); + p = ltga.getPopulation(); + System.out.println(p.getFunctionCalls() + "\t" + p.size()); + System.out.println(p.getBestEAIndividual().getStringRepresentation()); + ltga.optimize(); + p = ltga.getPopulation(); + System.out.println(p.getFunctionCalls() + "\t" + p.size()); + System.out.println(p.getBestEAIndividual().getStringRepresentation()); + ltga.optimize(); + p = ltga.getPopulation(); + System.out.println(p.getFunctionCalls() + "\t" + p.size()); + System.out.println(p.getBestEAIndividual().getStringRepresentation()); + ltga.optimize(); + p = ltga.getPopulation(); + System.out.println(p.getFunctionCalls() + "\t" + p.size()); + System.out.println(p.getBestEAIndividual().getStringRepresentation()); + ltga.optimize(); + p = ltga.getPopulation(); + System.out.println(p.getFunctionCalls() + "\t" + p.size()); + System.out.println(p.getBestEAIndividual().getStringRepresentation()); + } } diff --git a/src/eva2/server/go/strategies/MemeticAlgorithm.java b/src/eva2/server/go/strategies/MemeticAlgorithm.java index 5fb44442..45f11977 100644 --- a/src/eva2/server/go/strategies/MemeticAlgorithm.java +++ b/src/eva2/server/go/strategies/MemeticAlgorithm.java @@ -255,7 +255,7 @@ public class MemeticAlgorithm implements InterfaceOptimizer, * @param name * The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/MonteCarloSearch.java b/src/eva2/server/go/strategies/MonteCarloSearch.java index a11eb8d9..2e2031e1 100644 --- a/src/eva2/server/go/strategies/MonteCarloSearch.java +++ b/src/eva2/server/go/strategies/MonteCarloSearch.java @@ -182,7 +182,7 @@ public class MonteCarloSearch implements InterfaceOptimizer, java.io.Serializabl /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/MultiObjectiveCMAES.java b/src/eva2/server/go/strategies/MultiObjectiveCMAES.java index 2c8bf443..0a0ce07b 100644 --- a/src/eva2/server/go/strategies/MultiObjectiveCMAES.java +++ b/src/eva2/server/go/strategies/MultiObjectiveCMAES.java @@ -74,10 +74,10 @@ public class MultiObjectiveCMAES implements InterfaceOptimizer, Serializable { * (non-Javadoc) * * @see - * eva2.server.go.strategies.InterfaceOptimizer#SetIdentifier(java.lang. + * eva2.server.go.strategies.InterfaceOptimizer#setIdentifier(java.lang. * String) */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/MultiObjectiveEA.java b/src/eva2/server/go/strategies/MultiObjectiveEA.java index 6263f9d2..6e20d264 100644 --- a/src/eva2/server/go/strategies/MultiObjectiveEA.java +++ b/src/eva2/server/go/strategies/MultiObjectiveEA.java @@ -201,7 +201,7 @@ public class MultiObjectiveEA implements InterfaceOptimizer, java.io.Serializabl /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/NelderMeadSimplex.java b/src/eva2/server/go/strategies/NelderMeadSimplex.java index cfb98da8..bb9587af 100644 --- a/src/eva2/server/go/strategies/NelderMeadSimplex.java +++ b/src/eva2/server/go/strategies/NelderMeadSimplex.java @@ -58,7 +58,7 @@ public class NelderMeadSimplex implements InterfaceOptimizer, Serializable, Inte return new NelderMeadSimplex(this); } - public void SetIdentifier(String name) { + public void setIdentifier(String name) { m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/NichePSO.java b/src/eva2/server/go/strategies/NichePSO.java index 8edcc5c0..9cff01ba 100644 --- a/src/eva2/server/go/strategies/NichePSO.java +++ b/src/eva2/server/go/strategies/NichePSO.java @@ -1234,7 +1234,7 @@ public class NichePSO implements InterfaceAdditionalPopulationInformer, Interfac * This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/ParticleFilterOptimization.java b/src/eva2/server/go/strategies/ParticleFilterOptimization.java index 6d0bd2da..94038f4f 100644 --- a/src/eva2/server/go/strategies/ParticleFilterOptimization.java +++ b/src/eva2/server/go/strategies/ParticleFilterOptimization.java @@ -282,7 +282,7 @@ public class ParticleFilterOptimization implements InterfaceOptimizer, java.io.S /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/ParticleSwarmOptimization.java b/src/eva2/server/go/strategies/ParticleSwarmOptimization.java index f6e34986..e3addb9e 100644 --- a/src/eva2/server/go/strategies/ParticleSwarmOptimization.java +++ b/src/eva2/server/go/strategies/ParticleSwarmOptimization.java @@ -1712,7 +1712,7 @@ public class ParticleSwarmOptimization implements InterfaceOptimizer, java.io.Se * * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/PopulationBasedIncrementalLearning.java b/src/eva2/server/go/strategies/PopulationBasedIncrementalLearning.java index 16fba301..62b366fc 100644 --- a/src/eva2/server/go/strategies/PopulationBasedIncrementalLearning.java +++ b/src/eva2/server/go/strategies/PopulationBasedIncrementalLearning.java @@ -180,7 +180,7 @@ public class PopulationBasedIncrementalLearning implements InterfaceOptimizer, j /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/ScatterSearch.java b/src/eva2/server/go/strategies/ScatterSearch.java index e9baad16..e312ba30 100644 --- a/src/eva2/server/go/strategies/ScatterSearch.java +++ b/src/eva2/server/go/strategies/ScatterSearch.java @@ -664,7 +664,7 @@ public class ScatterSearch implements InterfaceOptimizer, java.io.Serializable, ///////////// Trivials... - public void SetIdentifier(String name) { + public void setIdentifier(String name) { m_Identifier = name; } diff --git a/src/eva2/server/go/strategies/SimulatedAnnealing.java b/src/eva2/server/go/strategies/SimulatedAnnealing.java index 2b6ed73a..5425d55f 100644 --- a/src/eva2/server/go/strategies/SimulatedAnnealing.java +++ b/src/eva2/server/go/strategies/SimulatedAnnealing.java @@ -201,7 +201,7 @@ public class SimulatedAnnealing implements InterfaceOptimizer, java.io.Serializa /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/SteadyStateGA.java b/src/eva2/server/go/strategies/SteadyStateGA.java index 43a150a8..76e8b3e7 100644 --- a/src/eva2/server/go/strategies/SteadyStateGA.java +++ b/src/eva2/server/go/strategies/SteadyStateGA.java @@ -152,7 +152,7 @@ public class SteadyStateGA implements InterfaceOptimizer, java.io.Serializable { /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/ThresholdAlgorithm.java b/src/eva2/server/go/strategies/ThresholdAlgorithm.java index 95cf1f74..a67a460c 100644 --- a/src/eva2/server/go/strategies/ThresholdAlgorithm.java +++ b/src/eva2/server/go/strategies/ThresholdAlgorithm.java @@ -192,7 +192,7 @@ public class ThresholdAlgorithm implements InterfaceOptimizer, java.io.Serializa /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/Tribes.java b/src/eva2/server/go/strategies/Tribes.java index 277c405b..e95a384b 100644 --- a/src/eva2/server/go/strategies/Tribes.java +++ b/src/eva2/server/go/strategies/Tribes.java @@ -715,7 +715,7 @@ public class Tribes implements InterfaceOptimizer, java.io.Serializable { public void freeWilly() {} - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() { diff --git a/src/eva2/server/go/strategies/WingedMultiObjectiveEA.java b/src/eva2/server/go/strategies/WingedMultiObjectiveEA.java index 6ca1e8a4..bd968a7d 100644 --- a/src/eva2/server/go/strategies/WingedMultiObjectiveEA.java +++ b/src/eva2/server/go/strategies/WingedMultiObjectiveEA.java @@ -246,7 +246,7 @@ public class WingedMultiObjectiveEA implements InterfaceOptimizer, java.io.Seria /** This method allows you to set an identifier for the algorithm * @param name The indenifier */ - public void SetIdentifier(String name) { + public void setIdentifier(String name) { this.m_Identifier = name; } public String getIdentifier() {