diff options
Diffstat (limited to 'src/jcgp/JCGP.java')
-rw-r--r-- | src/jcgp/JCGP.java | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index 213dd06..898f229 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -38,7 +38,6 @@ import org.reflections.Reflections; * and must implement jcgp.resources.Console. * * @author Eduardo Pedroni - * @see Resources, Module */ public class JCGP { @@ -121,7 +120,7 @@ public class JCGP { * Iterates through all classes in the classpath and builds a list * of instances of all valid evolutionary strategies found. In order * to be included in this list, a class must implement {@code EvolutionaryStrategy} - * and must contain a constructor with a Resources object as its only argument. + * and must contain a constructor with a {@code Resources} object as its only argument. */ private void createEvolutionaryStrategyList() { Set<Class<? extends EvolutionaryStrategy>> esList = reflections.getSubTypesOf(EvolutionaryStrategy.class); @@ -159,7 +158,7 @@ public class JCGP { * Iterates through all classes in the classpath and builds a list * of instances of all valid mutators found. In order to be included * in this list, a class must implement {@code Mutator} - * and must contain a constructor with a Resources object as its only argument. + * and must contain a constructor with a {@code Resources} object as its only argument. */ private void createMutatorList() { Set<Class<? extends Mutator>> mutatorList = reflections.getSubTypesOf(Mutator.class); @@ -198,7 +197,7 @@ public class JCGP { * Iterates through all classes in the classpath and builds a list * of instances of all valid problem types found. In order to be included * in this list, a class must implement {@code Problem} - * and must contain a constructor with a Resources object as its only argument. + * and must contain a constructor with a {@code Resources} object as its only argument. */ private void createProblemList() { Set<Class<? extends Problem>> problemTypes = reflections.getSubTypesOf(Problem.class); @@ -236,7 +235,7 @@ public class JCGP { } /** - * Returns a reference to the ModifiableResources used by the + * Returns a reference to the {@code ModifiableResources} used by the * experiment. <br> * Use this with care, since changing experiment parameters may * have unintended effects if not done properly. @@ -303,7 +302,7 @@ public class JCGP { /** - * @param mutator the index of the desired mutator. + * @param index the index of the desired mutator. */ public void setMutator(int index) { this.mutator = mutators.get(index); @@ -312,7 +311,7 @@ public class JCGP { /** - * @param evolutionaryStrategy the index of the desired evolutionary strategy. + * @param index the index of the desired evolutionary strategy. */ public void setEvolutionaryStrategy(int index) { this.evolutionaryStrategy = evolutionaryStrategies.get(index); @@ -321,11 +320,12 @@ public class JCGP { /** - * @param problem the index of the desired problem type. + * @param index the index of the desired problem type. */ public void setProblem(int index) { this.problem = problems.get(index); resources.setFunctionSet(problem.getFunctionSet()); + resources.setFitnessOrientation(problem.getFitnessOrientation()); } /** @@ -348,13 +348,14 @@ public class JCGP { if (resources.currentGeneration() < resources.generations()) { // we still have generations left to go - if (problem.isPerfectSolution(population.getFittest())) { + int perfect = problem.perfectSolutionFound(population); + if (perfect >= 0) { // log results - statistics.logRun(resources.currentGeneration(), population.getFittest().getFitness(), population.getFittest().getActiveNodes().size(), true); + statistics.logRun(resources.currentGeneration(), population.get(perfect).getFitness(), population.get(perfect).getActiveNodes().size(), true); resetStatisticsValues(); // solution has been found, start next run - resources.println("[CGP] Solution found, generation " + resources.currentGeneration() + ", chromosome " + population.getFittestIndex() + "\n"); + resources.println("[CGP] Solution found: generation " + resources.currentGeneration() + ", chromosome " + perfect + "\n"); if (resources.currentRun() < resources.runs()) { // there are still runs left @@ -369,17 +370,23 @@ public class JCGP { finished = true; } } else { - if (problem.isImprovement(population.getFittest())) { - printImprovement(); + // solution not found, look for improvement + int improvement = problem.hasImprovement(population); + + if (improvement >= 0) { + // there has been improvement, print it + printImprovement(improvement); lastImprovementGeneration = resources.currentGeneration(); - bestFitnessFound = population.getFittest().getFitness(); - activeNodes = population.getFittest().getActiveNodes().size(); + bestFitnessFound = population.get(improvement).getFitness(); + activeNodes = population.get(improvement).getActiveNodes().size(); } else { + // there has been no improvement, report generation reportGeneration(); } resources.incrementGeneration(); - // solution isn't perfect and we still have generations left, evolve more! - evolutionaryStrategy.evolve(population, mutator, (Resources) resources); + + // we still have generations left, evolve more! + evolutionaryStrategy.evolve(population, mutator); } } else { // the run has ended, tell the user and log it @@ -436,9 +443,9 @@ public class JCGP { * Used internally for reporting improvement, which happens independently of * the report interval parameter. */ - private void printImprovement() { + private void printImprovement(int chromosome) { resources.println("[CGP] Generation: " + resources.currentGeneration() + ", fittest chromosome (" - + population.getFittestIndex() + ") has fitness: " + population.getFittest().getFitness()); + + chromosome + ") has fitness: " + population.get(chromosome).getFitness()); } /** @@ -446,8 +453,8 @@ public class JCGP { * by the report interval parameter. */ private void reportGeneration() { - resources.reportln("[CGP] Generation: " + resources.currentGeneration() + ", fittest chromosome (" - + population.getFittestIndex() + ") has fitness: " + population.getFittest().getFitness()); + resources.reportln("[CGP] Generation: " + resources.currentGeneration() + ", best fitness: " + + problem.getBestFitness()); } /** @@ -545,7 +552,7 @@ public class JCGP { * @param chromosomeIndex the population index into which to parse. */ public void loadChromosome(File file, int chromosomeIndex) { - ChromosomeParser.parse(file, population.getChromosome(chromosomeIndex), resources); + ChromosomeParser.parse(file, population.get(chromosomeIndex), resources); } /** @@ -556,7 +563,7 @@ public class JCGP { * @param chromosomeIndex the index of the chromosome to save. */ public void saveChromosome(File file, int chromosomeIndex) { - ChromosomeParser.save(file, population.getChromosome(chromosomeIndex), resources); + ChromosomeParser.save(file, population.get(chromosomeIndex), resources); } /** |