diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-18 09:08:41 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-18 09:08:41 +0100 |
commit | 88314e71f908efcfc38da3b800319c171a6ccceb (patch) | |
tree | df42e3af2652dff815d1269bb81906a7af51e089 /src/jcgp/JCGP.java | |
parent | e7d7e8506a511d78f9e323ac09587f79ad503f42 (diff) |
Added parsers, did a bit of testing, switched to java8
Diffstat (limited to 'src/jcgp/JCGP.java')
-rw-r--r-- | src/jcgp/JCGP.java | 70 |
1 files changed, 56 insertions, 14 deletions
diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index e124dbf..1e847fb 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -1,5 +1,7 @@ package jcgp; +import java.io.File; + import jcgp.backend.modules.es.EvolutionaryStrategy; import jcgp.backend.modules.es.MuPlusLambda; import jcgp.backend.modules.es.TournamentSelection; @@ -8,6 +10,11 @@ import jcgp.backend.modules.mutator.PointMutator; import jcgp.backend.modules.problem.DigitalCircuit; import jcgp.backend.modules.problem.Problem; import jcgp.backend.modules.problem.SymbolicRegression; +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.population.Population; import jcgp.backend.resources.Console; import jcgp.backend.resources.ModifiableResources; @@ -16,13 +23,13 @@ import jcgp.backend.resources.Resources; /** * * Top-level CGP class. This class is the entry point for a CGP experiment. - * <p> + * <br> * An instance of JCGP encapsulates the entire experiment. It contains a Resources * object which can be retrieved via a getter. Modules can be selected using their * respective setters and function sets can be selected through the resources. * * The flow of the experiment is controlled using start() and nextGeneration(). The - * experiment can be reset with reset(), + * experiment can be reset with reset(), TODO comment * * * @author Eduardo Pedroni @@ -48,7 +55,7 @@ public class JCGP { // evolutionary algorithms private EvolutionaryStrategy[] evolutionaryStrategies = new EvolutionaryStrategy[] { new MuPlusLambda(resources), - new TournamentSelection()}; + new TournamentSelection() }; private EvolutionaryStrategy evolutionaryStrategy; // problem types @@ -63,6 +70,26 @@ public class JCGP { private Population population; private boolean finished = false; + /** + * TODO comment this! + * + * @param args + */ + public static void main(String... args) { + if (args.length < 1) { + System.err.println("JCGP requires at least a .par file."); + System.exit(1); + } + JCGP jcgp = new JCGP(); + jcgp.loadParameters(new File(args[0])); + + if (jcgp.getProblem() instanceof TestCaseProblem) { + TestCaseParser.parse(new File(args[2]), (TestCaseProblem<?>) jcgp.getProblem()); + } + + jcgp.start(); + } + public JCGP() { setEvolutionaryStrategy(0); setMutator(0); @@ -154,7 +181,7 @@ public class JCGP { public void nextGeneration() { if (!finished) { problem.evaluate(population, (Resources) resources); - report(); + reportGeneration(); if (resources.currentGeneration() < resources.generations()) { // we still have generations left to go @@ -200,24 +227,20 @@ public class JCGP { } - private void report() { - if (resources.report() > 0) { - if (resources.currentGeneration() % resources.report() == 0) { - resources.println("[CGP] Generation: " + resources.currentGeneration() + ", fitness: " + population.getFittest().getFitness()); + private void reportGeneration() { + if (resources.reportInterval() > 0) { + if (resources.currentGeneration() % resources.reportInterval() == 0) { + resources.println("[CGP] Generation: " + resources.currentGeneration()); } } } public void start() { if (!finished) { - while (resources.currentGeneration() <= resources.generations()) { + while (!finished) { nextGeneration(); - if (finished) { - break; - } } } - } public void reset() { @@ -228,6 +251,26 @@ public class JCGP { resources.println("-----------------------------"); resources.println("New experiment: " + problem.toString()); } + + public void loadParameters(File file) { + ParameterParser.parseParameters(file, resources); + FunctionParser.parseFunctions(file, problem); + reset(); + } + + public void loadTestCases(File file) { + if (problem instanceof TestCaseProblem) { + TestCaseParser.parse(file, (TestCaseProblem<?>) problem); + } + } + + public void loadChromosome(File file) { + ChromosomeParser.parse(file, population.getChromosome(0), resources); + } + + public void saveChromosome(File file, int chromosomeIndex) { + ChromosomeParser.save(file, population.getChromosome(chromosomeIndex)); + } public boolean isFinished() { return finished; @@ -236,5 +279,4 @@ public class JCGP { public void setConsole(Console console) { resources.setConsole(console); } - } |