From e7d7e8506a511d78f9e323ac09587f79ad503f42 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 13 Apr 2014 10:41:25 +0100 Subject: Performance suddenly decreased, looking into why --- src/jcgp/backend/function/IntegerArithmetic.java | 10 ++++------ .../backend/modules/es/EvolutionaryStrategy.java | 4 +--- src/jcgp/backend/modules/es/MuPlusLambda.java | 22 +++------------------- .../backend/modules/es/TournamentSelection.java | 8 -------- .../backend/modules/problem/TestCaseProblem.java | 13 ++++++------- src/jcgp/backend/population/Population.java | 22 ++++++++++++++++++++++ 6 files changed, 36 insertions(+), 43 deletions(-) (limited to 'src/jcgp/backend') diff --git a/src/jcgp/backend/function/IntegerArithmetic.java b/src/jcgp/backend/function/IntegerArithmetic.java index c08a72a..9bb02b5 100644 --- a/src/jcgp/backend/function/IntegerArithmetic.java +++ b/src/jcgp/backend/function/IntegerArithmetic.java @@ -1,6 +1,5 @@ package jcgp.backend.function; -import jcgp.backend.exceptions.InvalidArgumentsException; import jcgp.backend.population.Connection; public class IntegerArithmetic extends FunctionSet { @@ -20,7 +19,7 @@ public class IntegerArithmetic extends FunctionSet { @Override public Integer run(Connection... connections) { if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + throw new IllegalArgumentException("Not enough connections were given."); } else { Integer arg1 = ((Integer) connections[0].getValue()); Integer arg2 = ((Integer) connections[1].getValue()); @@ -45,7 +44,7 @@ public class IntegerArithmetic extends FunctionSet { @Override public Integer run(Connection... connections) { if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + throw new IllegalArgumentException("Not enough connections were given."); } else { Integer arg1 = ((Integer) connections[0].getValue()); Integer arg2 = ((Integer) connections[1].getValue()); @@ -70,12 +69,11 @@ public class IntegerArithmetic extends FunctionSet { @Override public Integer run(Connection... connections) { if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + throw new IllegalArgumentException("Not enough connections were given."); } else { Integer arg1 = ((Integer) connections[0].getValue()); Integer arg2 = ((Integer) connections[1].getValue()); Integer result = arg1 * arg2; - return result; } } @@ -95,7 +93,7 @@ public class IntegerArithmetic extends FunctionSet { @Override public Integer run(Connection... connections) { if (connections.length < 2) { - throw new InvalidArgumentsException("Not enough connections were given."); + throw new IllegalArgumentException("Not enough connections were given."); } else { Integer arg1 = ((Integer) connections[0].getValue()); Integer arg2 = ((Integer) connections[1].getValue()); diff --git a/src/jcgp/backend/modules/es/EvolutionaryStrategy.java b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java index 1117e99..8ab287d 100644 --- a/src/jcgp/backend/modules/es/EvolutionaryStrategy.java +++ b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java @@ -8,7 +8,5 @@ import jcgp.backend.resources.Resources; public interface EvolutionaryStrategy extends Module { public abstract void evolve(Population population, Mutator mutator, Resources resources); - - public abstract int getFittestChromosome(); - + } diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java index 67236f9..6a3883b 100644 --- a/src/jcgp/backend/modules/es/MuPlusLambda.java +++ b/src/jcgp/backend/modules/es/MuPlusLambda.java @@ -17,8 +17,6 @@ import jcgp.backend.resources.parameters.ParameterStatus; */ public class MuPlusLambda implements EvolutionaryStrategy { - private int fittestChromosome; - private IntegerParameter parents, offspring; private BooleanParameter report; @@ -62,22 +60,13 @@ public class MuPlusLambda implements EvolutionaryStrategy { @Override public void evolve(Population population, Mutator mutator, Resources resources) { // select fittest chromosomes - fittestChromosome = 0; - - if (report.get()) resources.reportln("[ES] Selecting fittest chromosome..."); - for (int i = 0; i < resources.populationSize(); i++) { - if (report.get()) resources.reportln("[ES] Chromosome " + i + ", fitness: " + population.getChromosome(i).getFitness()); - if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) { - fittestChromosome = i; - } - } - if (report.get()) resources.reportln("[ES] Selected chromosome: " + fittestChromosome); + if (report.get()) resources.reportln("[ES] Selected chromosome: " + population.getFittestIndex()); // create copies of fittest chromosome, mutate them for (int i = 0; i < resources.populationSize(); i++) { - if (i != fittestChromosome) { + if (i != population.getFittestIndex()) { if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i); - population.copyChromosome(fittestChromosome, i); + population.copyChromosome(population.getFittestIndex(), i); if (report.get()) resources.reportln("[ES] Mutating copied chromosome"); mutator.mutate(population.getChromosome(i), resources); } @@ -87,11 +76,6 @@ public class MuPlusLambda implements EvolutionaryStrategy { } - @Override - public int getFittestChromosome() { - return fittestChromosome; - } - @Override public Parameter[] getLocalParameters() { return new Parameter[] {parents, offspring, report}; diff --git a/src/jcgp/backend/modules/es/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java index 8286101..4070468 100644 --- a/src/jcgp/backend/modules/es/TournamentSelection.java +++ b/src/jcgp/backend/modules/es/TournamentSelection.java @@ -8,8 +8,6 @@ import jcgp.backend.resources.parameters.Parameter; public class TournamentSelection implements EvolutionaryStrategy { - private int fittestChromosome; - private IntegerParameter tournament; public TournamentSelection() { @@ -30,15 +28,9 @@ public class TournamentSelection implements EvolutionaryStrategy { public void evolve(Population population, Mutator mutator, Resources parameters) { tournament.set(tournament.get() + 1); - fittestChromosome = 0; // TODO implement this } - - @Override - public int getFittestChromosome() { - return fittestChromosome; - } @Override public String toString() { diff --git a/src/jcgp/backend/modules/problem/TestCaseProblem.java b/src/jcgp/backend/modules/problem/TestCaseProblem.java index 68318cf..ee72860 100644 --- a/src/jcgp/backend/modules/problem/TestCaseProblem.java +++ b/src/jcgp/backend/modules/problem/TestCaseProblem.java @@ -52,8 +52,6 @@ public abstract class TestCaseProblem extends Problem { private IntegerParameter maxFitness; private final int inputCount, outputCount; - private U type; - public TestCaseProblem(Resources resources) { super(); @@ -67,12 +65,14 @@ public abstract class TestCaseProblem extends Problem { } }; testCases = FXCollections.observableArrayList(); - //testCases = new ObservableList>(); } @Override public void evaluate(Population population, Resources resources) { + // set fittest to 0, change it whenever a fitter one is found + population.setFittest(0); + // for every chromosome in the population for (int i = 0; i < resources.populationSize(); i++) { // assume an initial fitness of 0 @@ -89,6 +89,9 @@ public abstract class TestCaseProblem extends Problem { } // assign the resulting fitness to the respective individual population.getChromosome(i).setFitness(fitness); + if (fitness >= population.getFittest().getFitness()) { + population.setFittest(i); + } } } @@ -147,10 +150,6 @@ public abstract class TestCaseProblem extends Problem { public int getOutputCount() { return outputCount; } - - public Class getType() { - return type.getClass(); - } } diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java index fdeec82..a850595 100644 --- a/src/jcgp/backend/population/Population.java +++ b/src/jcgp/backend/population/Population.java @@ -6,6 +6,7 @@ public class Population { private Chromosome[] chromosomes; private final Resources resources; + private int fittest = 0; /** * Initialise a random population according to the parameters specified @@ -76,5 +77,26 @@ public class Population { chromosomes[c].reinitialiseConnections(); } } + + public void setFittest(int fittest) { + this.fittest = fittest; + } + + public void setFittest(Chromosome fittest) { + for (int i = 0; i < chromosomes.length; i++) { + if (chromosomes[i] == fittest) { + this.fittest = i; + return; + } + } + } + + public Chromosome getFittest() { + return chromosomes[fittest]; + } + + public int getFittestIndex() { + return fittest; + } } -- cgit v1.2.3