diff options
Diffstat (limited to 'src/jcgp/JCGP.java')
-rw-r--r-- | src/jcgp/JCGP.java | 141 |
1 files changed, 99 insertions, 42 deletions
diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index 0c75f2b..962ddec 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -117,7 +117,7 @@ public class JCGP { private void createBaseParameters() { parameters.put("rows", new IntegerParameter(8, "Rows", false, true) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { if (newValue <= 0) { status = ParameterStatus.INVALID; status.setDetails("Chromosome must have at least 1 row."); @@ -128,7 +128,7 @@ public class JCGP { }); parameters.put("columns", new IntegerParameter(9, "Columns", false, true) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { if (newValue <= 0) { status = ParameterStatus.INVALID; status.setDetails("Chromosome must have at least 1 column."); @@ -139,7 +139,7 @@ public class JCGP { }); parameters.put("inputs", new IntegerParameter(3, "Inputs", false, true) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { if (newValue <= 0) { status = ParameterStatus.INVALID; status.setDetails("Chromosome must have at least 1 input."); @@ -150,7 +150,7 @@ public class JCGP { }); parameters.put("outputs", new IntegerParameter(3, "Outputs", false, true) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { if (newValue <= 0) { status = ParameterStatus.INVALID; status.setDetails("Chromosome must have at least 1 output."); @@ -161,7 +161,7 @@ public class JCGP { }); parameters.put("popSize", new IntegerParameter(5, "Population", false, true) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { if (newValue <= 0) { status = ParameterStatus.INVALID; status.setDetails("Population size must be at least 1."); @@ -172,7 +172,7 @@ public class JCGP { }); parameters.put("levelsBack", new IntegerParameter(2, "Levels back", false, true) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { if (newValue <= 0) { status = ParameterStatus.INVALID; status.setDetails("Levels back must be at least 1."); @@ -187,7 +187,7 @@ public class JCGP { IntegerParameter nodes = new IntegerParameter(1, "Nodes", true, false) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { // blank } }; @@ -196,12 +196,12 @@ public class JCGP { parameters.put("generations", new IntegerParameter(1000000, "Generations") { @Override - protected void validate(int newValue) { + public void validate(int newValue) { if (newValue <= 0) { status = ParameterStatus.INVALID; status.setDetails("Number of generations must be greater than 0."); - } else if (newValue <= getInt("currentGen")) { - status = ParameterStatus.WARNING; + } else if (newValue < getInt("currentGen")) { + status = ParameterStatus.WARNING_RESET; status.setDetails("Setting generations to less than the current generation will cause the experiment to restart."); } else { status = ParameterStatus.VALID; @@ -210,16 +210,19 @@ public class JCGP { }); parameters.put("currentGen", new IntegerParameter(1, "Generation", true, false) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { // blank } }); parameters.put("runs", new IntegerParameter(5, "Runs") { @Override - protected void validate(int newValue) { - if (newValue <= getInt("currentRun")) { - status = ParameterStatus.WARNING; + public void validate(int newValue) { + if (newValue <= 0) { + status = ParameterStatus.INVALID; + status.setDetails("Number of runs must be greater than 0."); + } else if (newValue < getInt("currentRun")) { + status = ParameterStatus.WARNING_RESET; status.setDetails("Setting runs to less than the current run will cause the experiment to restart."); } else { status = ParameterStatus.VALID; @@ -228,27 +231,27 @@ public class JCGP { }); parameters.put("currentRun", new IntegerParameter(1, "Run", true, false) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { // blank } }); parameters.put("arity", new IntegerParameter(0, "Max arity", true, false) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { // blank } }); - parameters.put("maxFitness", new IntegerParameter(6, "Max fitness", true, true) { + parameters.put("maxFitness", new IntegerParameter(3, "Max fitness", true, true) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { // blank } }); IntegerParameter seed = new IntegerParameter(123, "Seed", false, true) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { status = ParameterStatus.VALID; } }; @@ -264,7 +267,7 @@ public class JCGP { parameters.put("report", new IntegerParameter(1, "Report", false, false) { @Override - protected void validate(int newValue) { + public void validate(int newValue) { if (newValue > getInt("generations")) { status = ParameterStatus.WARNING; status.setDetails("No reports will be printed."); @@ -366,7 +369,7 @@ public class JCGP { } } - private Resources resources = new Resources(); + private final Resources resources = new Resources(); /* * The following arrays contain all available modules. These collections are read by the GUI @@ -382,7 +385,7 @@ public class JCGP { // evolutionary algorithms private EvolutionaryAlgorithm[] evolutionaryAlgorithms = new EvolutionaryAlgorithm[] { - new MuPlusLambda(), + new MuPlusLambda(resources), new TournamentSelection()}; private EvolutionaryAlgorithm evolutionaryAlgorithm; @@ -396,7 +399,7 @@ public class JCGP { */ private Population population; - private boolean solutionFound = false; + private boolean finished = false; public JCGP() { @@ -409,8 +412,7 @@ public class JCGP { fitnessFunction = fitnessFunctions[0]; - resources.setTestCases(new TestCase(new Integer[]{1, 2, 3}, new Integer[]{4, 5, 6}), - new TestCase(new Integer[]{4, 7, 4}, new Integer[]{6, 21, 2})); + resources.setTestCases(new TestCase(new Integer[]{1, 2, 3}, new Integer[]{-4, 5, 6})); } @@ -495,33 +497,88 @@ public class JCGP { } public void nextGeneration() { - fitnessFunction.evaluate(population, resources); - - if (population.getChromosome(evolutionaryAlgorithm.getFittestChromosome()).getFitness() >= resources.getInt("maxFitness")) { - solutionFound = true; - resources.println("Solution found!"); - population.getChromosome(evolutionaryAlgorithm.getFittestChromosome()).printNodes(); - } else { - resources.println("Generation: " + resources.getInt("currentGen") + ", fitness: " + population.getChromosome(evolutionaryAlgorithm.getFittestChromosome()).getFitness()); - resources.set("currentGen", resources.getInt("currentGen") + 1); + if (!finished) { + fitnessFunction.evaluate(population, resources); + + report(); + + if (resources.getInt("currentGen") < resources.getInt("generations")) { + // we still have generations left to go + if (population.getChromosome(evolutionaryAlgorithm.getFittestChromosome()).getFitness() >= resources.getInt("maxFitness")) { + // solution has been found, start next run + resources.println("Solution found in generation " + resources.getInt("currentGen") + ", chromosome: " + evolutionaryAlgorithm.getFittestChromosome()); + + if (resources.getInt("currentRun") < resources.getInt("runs")) { + // there are still runs left + resources.set("currentRun", resources.getInt("currentRun") + 1); + resources.set("currentGen", 0); + + // start a new population + population = new Population(resources); + } else { + // no more generations and no more runs, we're done + finished = true; + } + } else { + resources.set("currentGen", resources.getInt("currentGen") + 1); + } + + } else { + // the run has ended, check if any more runs must be done + resources.println("Solution not found, highest fitness achieved was " + + population.getChromosome(evolutionaryAlgorithm.getFittestChromosome()).getFitness() + + " by chromosome " + evolutionaryAlgorithm.getFittestChromosome()); + + if (resources.getInt("currentRun") < resources.getInt("runs")) { + // the run has ended but there are still runs left + resources.set("currentRun", resources.getInt("currentRun") + 1); + resources.set("currentGen", 0); + + // start a new population + population = new Population(resources); + } else { + // no more generations and no more runs, we're done + finished = true; + } + } } - + evolutionaryAlgorithm.evolve(population, mutator, resources); + } - public void start() { - while (resources.getInt("currentGen") <= resources.getInt("generations")) { - nextGeneration(); - - if (solutionFound) { - break; + private void report() { + if (resources.getInt("report") > 0) { + if (resources.getInt("currentGen") % resources.getInt("report") == 0) { + resources.println("Generation: " + resources.getInt("currentGen") + ", fitness: " + population.getChromosome(evolutionaryAlgorithm.getFittestChromosome()).getFitness()); } } + + } + + public void start() { + if (!finished) { + while (resources.getInt("currentGen") <= resources.getInt("generations")) { + nextGeneration(); + if (finished) { + break; + } + } + } + } public void reset() { - solutionFound = false; + finished = false; population = new Population(resources); + resources.set("currentGen", 1); + resources.set("currentRun", 1); + resources.println("-----------------------------"); + resources.println("New experiment"); } + public boolean isFinished() { + return finished; + } + } |