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/JCGP.java | 12 ++++++------ 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 ++++++++++++++++++++++ src/jcgp/gui/GUI.java | 7 +++++-- src/jcgp/gui/population/ChromosomePane.java | 9 ++++----- src/jcgp/gui/population/GUIGene.java | 2 +- src/jcgp/gui/population/PopulationPane.java | 20 ++++++++++++++++++-- src/jcgp/gui/settings/SettingsPane.java | 20 ++++++++++++-------- src/jcgp/gui/settings/testcase/TestCaseTable.java | 22 ++++++++++++++-------- 13 files changed, 96 insertions(+), 75 deletions(-) diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index 6d881d4..e124dbf 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -158,9 +158,9 @@ public class JCGP { if (resources.currentGeneration() < resources.generations()) { // we still have generations left to go - if (problem.isPerfectSolution(population.getChromosome(evolutionaryStrategy.getFittestChromosome()))) { + if (problem.isPerfectSolution(population.getFittest())) { // solution has been found, start next run - resources.println("[CGP] Solution found, generation " + resources.currentGeneration() + ", chromosome " + evolutionaryStrategy.getFittestChromosome()); + resources.println("[CGP] Solution found, generation " + resources.currentGeneration() + ", chromosome " + population.getFittestIndex()); if (resources.currentRun() < resources.runs()) { // there are still runs left @@ -179,8 +179,8 @@ public class JCGP { } else { // the run has ended, check if any more runs must be done resources.println("[CGP] Solution not found, highest fitness achieved was " - + population.getChromosome(evolutionaryStrategy.getFittestChromosome()).getFitness() - + " by chromosome " + evolutionaryStrategy.getFittestChromosome()); + + population.getFittest().getFitness() + + " by chromosome " + population.getFittestIndex()); if (resources.currentRun() < resources.runs()) { // the run has ended but there are still runs left @@ -188,7 +188,7 @@ public class JCGP { resources.setCurrentGeneration(0); // start a new population - population.reinitialise();; + population.reinitialise(); } else { // no more generations and no more runs, we're done finished = true; @@ -203,7 +203,7 @@ public class JCGP { private void report() { if (resources.report() > 0) { if (resources.currentGeneration() % resources.report() == 0) { - resources.println("[CGP] Generation: " + resources.currentGeneration() + ", fitness: " + population.getChromosome(evolutionaryStrategy.getFittestChromosome()).getFitness()); + resources.println("[CGP] Generation: " + resources.currentGeneration() + ", fitness: " + population.getFittest().getFitness()); } } } 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; + } } diff --git a/src/jcgp/gui/GUI.java b/src/jcgp/gui/GUI.java index 6d87de1..e7a4a23 100644 --- a/src/jcgp/gui/GUI.java +++ b/src/jcgp/gui/GUI.java @@ -169,13 +169,13 @@ public class GUI extends Application { reset(); } populationPane.unlockOutputs(); - jcgp.nextGeneration(); console.flush(); populationPane.updateGenes(); populationPane.relockOutputs(); settingsPane.revalidateParameters(); + settingsPane.updateControls(false, jcgp.isFinished()); } } @@ -185,7 +185,6 @@ public class GUI extends Application { jcgp.reset(); populationPane.remakeTabs(); settingsPane.revalidateParameters(); - System.out.println("[reset] call: " + jcgp.isFinished()); settingsPane.updateControls(false, jcgp.isFinished()); console.flush(); } @@ -231,4 +230,8 @@ public class GUI extends Application { public void hideGeneValues() { populationPane.hideValues(); } + + public void setEvaluating(boolean value) { + populationPane.setEvaluating(value); + } } diff --git a/src/jcgp/gui/population/ChromosomePane.java b/src/jcgp/gui/population/ChromosomePane.java index 0279d09..4a47f34 100644 --- a/src/jcgp/gui/population/ChromosomePane.java +++ b/src/jcgp/gui/population/ChromosomePane.java @@ -26,12 +26,13 @@ public class ChromosomePane extends ScrollPane { private int rows, columns; private boolean target = false; - private boolean evaluating = false; + private PopulationPane parent; - public ChromosomePane(Chromosome chromosome, GUI gui) { + public ChromosomePane(Chromosome chromosome, GUI gui, PopulationPane parent) { super(); final Resources resources = gui.getExperiment().getResources(); + this.parent = parent; rows = resources.rows(); columns = resources.columns(); @@ -143,7 +144,6 @@ public class ChromosomePane extends ScrollPane { } public void setInputs(Object[] values) { - evaluating = true; for (int i = 0; i < guiInputs.length; i++) { guiInputs[i].setValue(values[i]); guiInputs[i].updateText(); @@ -167,7 +167,6 @@ public class ChromosomePane extends ScrollPane { } public void hideValues() { - evaluating = false; for (int i = 0; i < guiInputs.length; i++) { guiInputs[i].updateText(); } @@ -185,6 +184,6 @@ public class ChromosomePane extends ScrollPane { * @return the evaluating */ public boolean isEvaluating() { - return evaluating; + return parent.isEvaluating(); } } diff --git a/src/jcgp/gui/population/GUIGene.java b/src/jcgp/gui/population/GUIGene.java index 5ce839e..c41261d 100644 --- a/src/jcgp/gui/population/GUIGene.java +++ b/src/jcgp/gui/population/GUIGene.java @@ -12,7 +12,7 @@ import jcgp.backend.population.Gene; public abstract class GUIGene extends Group { - public static final double NODE_RADIUS = 30; + public static final double NODE_RADIUS = 35; public static final double SPACING = 15; public static final double THETA = Math.PI / 1.4; diff --git a/src/jcgp/gui/population/PopulationPane.java b/src/jcgp/gui/population/PopulationPane.java index 64b61c4..28b0ad9 100644 --- a/src/jcgp/gui/population/PopulationPane.java +++ b/src/jcgp/gui/population/PopulationPane.java @@ -10,6 +10,8 @@ import jcgp.gui.GUI; public class PopulationPane extends TabPane { private GUI gui; + private TestCase currentTestCase; + private boolean evaluating = false; public PopulationPane(GUI gui) { super(); @@ -25,7 +27,7 @@ public class PopulationPane extends TabPane { Tab tab; ChromosomePane cp; for (int i = 0; i < jcgp.getResources().populationSize(); i++) { - cp = new ChromosomePane(jcgp.getPopulation().getChromosome(i), gui); + cp = new ChromosomePane(jcgp.getPopulation().getChromosome(i), gui, this); tab = new Tab("Chr " + i); tab.setContent(cp); getTabs().add(tab); @@ -33,6 +35,9 @@ public class PopulationPane extends TabPane { } public void updateGenes() { + if (evaluating) { + evaluateTestCase(currentTestCase); + } for (int i = 0; i < getChildrenUnmodifiable().size(); i++) { ((ChromosomePane) getTabs().get(i).getContent()).updateGenes(); } @@ -51,8 +56,10 @@ public class PopulationPane extends TabPane { } public void evaluateTestCase(TestCase testCase) { - if (gui.getExperiment().getProblem() instanceof TestCaseProblem) { + if (gui.getExperiment().getProblem() instanceof TestCaseProblem && testCase != null) { + currentTestCase = testCase; if (testCase.getInputs().length == gui.getExperiment().getResources().inputs()) { + evaluating = true; for (int i = 0; i < getTabs().size(); i++) { ((ChromosomePane) getTabs().get(i).getContent()).setInputs(testCase.getInputs()); } @@ -64,8 +71,17 @@ public class PopulationPane extends TabPane { } public void hideValues() { + evaluating = false; for (int i = 0; i < getTabs().size(); i++) { ((ChromosomePane) getTabs().get(i).getContent()).hideValues(); } } + + public boolean isEvaluating() { + return evaluating; + } + + public void setEvaluating(boolean value) { + evaluating = value; + } } diff --git a/src/jcgp/gui/settings/SettingsPane.java b/src/jcgp/gui/settings/SettingsPane.java index 9fcee8f..2ab9650 100644 --- a/src/jcgp/gui/settings/SettingsPane.java +++ b/src/jcgp/gui/settings/SettingsPane.java @@ -211,14 +211,15 @@ public class SettingsPane extends AnchorPane { public void handle(ActionEvent event) { jcgp.setProblem(problemCBox.getSelectionModel().getSelectedIndex()); if (jcgp.getProblem().getLocalParameters() != null) { - showTestCaseContainer.getChildren().clear(); refreshParameters(jcgp.getProblem().getLocalParameters(), problemParameters); - refreshFunctions(jcgp.getProblem().getFunctionSet(), nodeFunctions, gui); - testCaseTable.close(); - if (jcgp.getProblem() instanceof TestCaseProblem) { - showTestCaseContainer.getChildren().add(showTestCaseButton); - testCaseTable = new TestCaseTable((TestCaseProblem) jcgp.getProblem(), gui); - } + } + testCaseTable.close(); + gui.setEvaluating(false); + refreshFunctions(jcgp.getProblem().getFunctionSet(), nodeFunctions, gui); + showTestCaseContainer.getChildren().clear(); + if (jcgp.getProblem() instanceof TestCaseProblem) { + showTestCaseContainer.getChildren().add(showTestCaseButton); + testCaseTable = new TestCaseTable((TestCaseProblem) jcgp.getProblem(), gui); } gui.reset(); } @@ -396,10 +397,12 @@ public class SettingsPane extends AnchorPane { */ public void revalidateParameters() { runPause.setDisable(false); + step.setDisable(false); for (GUIParameter parameter : parameters) { parameter.validate(); if (parameter.requiresReset()) { runPause.setDisable(true); + step.setDisable(true); } } } @@ -429,10 +432,11 @@ public class SettingsPane extends AnchorPane { problemPane.setDisable(running); runPause.setText(running ? "Pause" : "Run"); - System.out.println("[updateControls] run pause disable: " + finished); runPause.setDisable(finished); step.setDisable(running || finished); reset.setDisable(running); + + testCaseTable.getTable().setDisable(running); } public TestCaseTable getTestCaseTable() { diff --git a/src/jcgp/gui/settings/testcase/TestCaseTable.java b/src/jcgp/gui/settings/testcase/TestCaseTable.java index 7e72cbd..b84f9ab 100644 --- a/src/jcgp/gui/settings/testcase/TestCaseTable.java +++ b/src/jcgp/gui/settings/testcase/TestCaseTable.java @@ -27,10 +27,12 @@ import jcgp.gui.GUI; */ public class TestCaseTable extends Stage { + private TableView> table; + public TestCaseTable(final TestCaseProblem problem, final GUI gui) { super(); - TableView> tv = new TableView>(); + table = new TableView>(); ObservableList> testCaseList = problem.getTestCases(); ArrayList, String>> inputs = new ArrayList, String>>(problem.getInputCount()); @@ -48,7 +50,7 @@ public class TestCaseTable extends Stage { } }); tc.setSortable(false); - tc.prefWidthProperty().bind(tv.widthProperty().divide(problem.getInputCount() + problem.getOutputCount())); + tc.prefWidthProperty().bind(table.widthProperty().divide(problem.getInputCount() + problem.getOutputCount())); } for (int o = 0; o < problem.getOutputCount(); o++) { @@ -62,15 +64,15 @@ public class TestCaseTable extends Stage { } }); tc.setSortable(false); - tc.prefWidthProperty().bind(tv.widthProperty().divide(problem.getInputCount() + problem.getOutputCount())); + tc.prefWidthProperty().bind(table.widthProperty().divide(problem.getInputCount() + problem.getOutputCount())); } - tv.getColumns().addAll(inputs); - tv.getColumns().addAll(outputs); + table.getColumns().addAll(inputs); + table.getColumns().addAll(outputs); - tv.setItems(testCaseList); + table.setItems(testCaseList); - tv.getSelectionModel().selectedItemProperty().addListener(new ChangeListener>() { + table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener>() { @Override public void changed( ObservableValue> observable, TestCase oldValue, TestCase newValue) { @@ -85,6 +87,10 @@ public class TestCaseTable extends Stage { } }); - setScene(new Scene(tv)); + setScene(new Scene(table)); + } + + public TableView> getTable() { + return table; } } -- cgit v1.2.3