diff options
Diffstat (limited to 'src/jcgp/JCGP.java')
-rw-r--r-- | src/jcgp/JCGP.java | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index 2a6552c..f980c00 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -11,10 +11,10 @@ import jcgp.backend.modules.problem.DigitalCircuitProblem; import jcgp.backend.modules.problem.Problem; import jcgp.backend.modules.problem.SymbolicRegressionProblem; import jcgp.backend.modules.problem.TestCaseProblem; -import jcgp.backend.parser.ChromosomeParser; -import jcgp.backend.parser.FunctionParser; -import jcgp.backend.parser.ParameterParser; -import jcgp.backend.parser.TestCaseParser; +import jcgp.backend.parsers.ChromosomeParser; +import jcgp.backend.parsers.FunctionParser; +import jcgp.backend.parsers.ParameterParser; +import jcgp.backend.parsers.TestCaseParser; import jcgp.backend.population.Population; import jcgp.backend.resources.Console; import jcgp.backend.resources.ModifiableResources; @@ -50,19 +50,19 @@ public class JCGP { // mutators private Mutator[] mutators = new Mutator[] { new PointMutator(resources) }; - private Mutator mutator; + private Mutator mutator = mutators[0]; // evolutionary algorithms private EvolutionaryStrategy[] evolutionaryStrategies = new EvolutionaryStrategy[] { new MuPlusLambda(resources), - new TournamentSelection() }; - private EvolutionaryStrategy evolutionaryStrategy; + new TournamentSelection(resources) }; + private EvolutionaryStrategy evolutionaryStrategy = evolutionaryStrategies[0]; // problem types private Problem[] problems = new Problem[] { new SymbolicRegressionProblem(resources), new DigitalCircuitProblem(resources) }; - private Problem problem; + private Problem problem = problems[0]; /* * the population of chromosomes @@ -84,17 +84,14 @@ public class JCGP { jcgp.loadParameters(new File(args[0])); if (jcgp.getProblem() instanceof TestCaseProblem) { - ((TestCaseProblem<?>) jcgp.getProblem()).parse(new File(args[2])); + TestCaseParser.parse(new File(args[2]), (TestCaseProblem<?>) jcgp.getProblem(), jcgp.getResources()); } jcgp.start(); } public JCGP() { - setEvolutionaryStrategy(0); - setMutator(0); - setProblem(0); - + resources.setFunctionSet(problem.getFunctionSet()); population = new Population(resources); } @@ -159,14 +156,16 @@ public class JCGP { */ public void setMutator(int index) { this.mutator = mutators[index]; + resources.println("[CGP] Mutator selected: " + mutator.toString()); } /** * @param evolutionaryStrategy the evolutionaryAlgorithm to set */ - public void setEvolutionaryStrategy(int index) { + public void setEvolutionaryStrategy(int index) { this.evolutionaryStrategy = evolutionaryStrategies[index]; + resources.println("[CGP] Evolutionary strategy selected: " + evolutionaryStrategy.toString()); } @@ -179,6 +178,7 @@ public class JCGP { } public void nextGeneration() { + System.out.println("service: doing next gen"); if (!finished) { problem.evaluate(population, (Resources) resources); reportGeneration(); @@ -188,7 +188,6 @@ public class JCGP { if (problem.isPerfectSolution(population.getFittest())) { // solution has been found, start next run resources.println("[CGP] Solution found, generation " + resources.currentGeneration() + ", chromosome " + population.getFittestIndex()); - if (resources.currentRun() < resources.runs()) { // there are still runs left resources.setCurrentRun(resources.currentRun() + 1); @@ -202,6 +201,8 @@ public class JCGP { } } else { resources.setCurrentGeneration(resources.currentGeneration() + 1); + // solution isn't perfect and we still have generations left, evolve more! + evolutionaryStrategy.evolve(population, mutator, (Resources) resources); } } else { // the run has ended, check if any more runs must be done @@ -222,18 +223,11 @@ public class JCGP { } } } - - evolutionaryStrategy.evolve(population, mutator, (Resources) resources); - } private void reportGeneration() { - if (resources.reportInterval() > 0) { - if (resources.currentGeneration() % resources.reportInterval() == 0) { - resources.println("[CGP] Generation: " + resources.currentGeneration() + ", fittest chromosome (" - + population.getFittestIndex() + ") has fitness: " + population.getFittest().getFitness()); - } - } + resources.reportln("[CGP] Generation: " + resources.currentGeneration() + ", fittest chromosome (" + + population.getFittestIndex() + ") has fitness: " + population.getFittest().getFitness()); } public void start() { @@ -249,13 +243,24 @@ public class JCGP { population = new Population(resources); resources.setCurrentGeneration(1); resources.setCurrentRun(1); - resources.println("-----------------------------"); - resources.println("New experiment: " + problem.toString()); + resources.println("*********************************************************"); + resources.println("[CGP] New experiment: " + problem.toString()); + resources.println("[CGP] Rows: " + resources.rows()); + resources.println("[CGP] Columns: " + resources.columns()); + resources.println("[CGP] Levels back: " + resources.levelsBack()); + resources.println("[CGP] Population size: " + resources.populationSize()); + resources.println("[CGP] Total generations: " + resources.generations()); + resources.println("[CGP] Total runs: " + resources.runs()); + resources.println("[CGP] Report interval: " + resources.reportInterval()); + resources.println("[CGP] Seed: " + resources.seed()); + resources.println(""); + resources.println("[CGP] Evolutionary strategy: " + evolutionaryStrategy.toString()); + resources.println("[CGP] Mutator: " + mutator.toString()); } public void loadParameters(File file) { ParameterParser.parseParameters(file, resources); - FunctionParser.parseFunctions(file, problem); + FunctionParser.parseFunctions(file, problem, resources); reset(); } @@ -263,7 +268,7 @@ public class JCGP { if (problem instanceof TestCaseProblem) { TestCaseParser.parseParameters(file, resources); reset(); - ((TestCaseProblem<?>) problem).parse(file); + TestCaseParser.parse(file, (TestCaseProblem<?>) problem, resources); } } |