diff options
Diffstat (limited to 'src/jcgp/CGP.java')
-rw-r--r-- | src/jcgp/CGP.java | 309 |
1 files changed, 195 insertions, 114 deletions
diff --git a/src/jcgp/CGP.java b/src/jcgp/CGP.java index 16d2be7..6b68fd8 100644 --- a/src/jcgp/CGP.java +++ b/src/jcgp/CGP.java @@ -1,153 +1,234 @@ package jcgp; import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; import java.util.Random; import javafx.beans.property.Property; -import javafx.beans.property.SimpleIntegerProperty; +import jcgp.function.Arithmetic; +import jcgp.function.BitwiseLogic; +import jcgp.function.BooleanLogic; +import jcgp.function.Function; +import jcgp.function.FunctionSet; import jcgp.modules.ea.EvolutionaryAlgorithm; import jcgp.modules.ea.StandardEA; import jcgp.modules.fitness.FitnessFunction; import jcgp.modules.fitness.TestCase; -import jcgp.modules.fitness.TruthTableEvaluator; -import jcgp.modules.function.Arithmetic; -import jcgp.modules.function.BitwiseLogic; -import jcgp.modules.function.BooleanLogic; -import jcgp.modules.function.FunctionSet; +import jcgp.modules.fitness.TestCaseEvaluator; import jcgp.modules.mutator.Mutator; import jcgp.modules.mutator.PointMutator; import jcgp.parameters.BooleanParameter; -import jcgp.parameters.DoubleParameter; import jcgp.parameters.IntegerParameter; import jcgp.parameters.Parameter; import jcgp.population.Population; public class CGP { - // CGP components + /** + * + * + * @author Eduardo Pedroni + * + */ + public static class Resources { + private HashMap<String, Parameter> moduleParameters = new HashMap<String, Parameter>(); + private HashMap<String, Parameter> coreParameters = new HashMap<String, Parameter>(); + private HashMap<String, Parameter> allParameters = new HashMap<String, Parameter>(); + + private Random numberGenerator; + + // function sets + private FunctionSet[] functionSets = new FunctionSet[] { + new Arithmetic(), + new BitwiseLogic(), + new BooleanLogic() }; + private FunctionSet functionSet = functionSets[0]; + + private TestCase[] testCases; + + public Resources() { + createCoreParameters(); + + numberGenerator = new Random((int) get("seed")); + functionSet = functionSets[0]; + + set("arity", functionSet.getMaxArity()); + } + + public Object get(String key) { + return allParameters.get(key).getValue(); + } + + public void set(String key, Object value) { + allParameters.get(key).setValue(value); + } + + public Property<?> getProperty(String key) { + return allParameters.get(key).valueProperty(); + } + + public void setManagedParameter(String key, boolean value) { + allParameters.get(key).setManaged(value); + } + + public void setHiddenParameter(String key, boolean value) { + allParameters.get(key).setHidden(value); + } + + public boolean contains(String key) { + return allParameters.containsKey(key); + } + + private void createCoreParameters() { + coreParameters.put("rows", new IntegerParameter(9, "Rows")); + coreParameters.put("columns", new IntegerParameter(10, "Columns")); + coreParameters.put("inputs", new IntegerParameter(3, "Inputs")); + coreParameters.put("outputs", new IntegerParameter(3, "Outputs")); + coreParameters.put("popSize", new IntegerParameter(5, "Population")); + coreParameters.put("levelsBack", new IntegerParameter(2, "Levels back")); + + coreParameters.put("nodes", new IntegerParameter(90, "Nodes", true, true)); + + coreParameters.put("generations", new IntegerParameter(1000000, "Generations")); + coreParameters.put("currentGen", new IntegerParameter(0, "Generation")); + coreParameters.put("runs", new IntegerParameter(5, "Runs")); + + coreParameters.put("arity", new IntegerParameter(0, "Max arity", true, true)); + + coreParameters.put("seed", new IntegerParameter(123, "Random seed", true, true)); + + coreParameters.put("debug", new BooleanParameter(false, "Debug")); + + allParameters.putAll(coreParameters); + } + + private void resetParameters(EvolutionaryAlgorithm ea, Mutator mu, FitnessFunction ff) { + Iterator<Entry<String, Parameter>> it = coreParameters.entrySet().iterator(); + while (it.hasNext()) { + ((Parameter) ((Map.Entry<String, Parameter>) it.next())).reset(); + } + + allParameters.clear(); + allParameters.putAll(coreParameters); + addModuleParameters(ea, mu, ff); + } + + private void addModuleParameters(EvolutionaryAlgorithm ea, Mutator mu, FitnessFunction ff) { + moduleParameters.clear(); + moduleParameters.putAll(ea.activate(this)); + moduleParameters.putAll(mu.activate(this)); + moduleParameters.putAll(ff.activate(this)); + + allParameters.putAll(moduleParameters); + } + + /* + * Utility functions + */ + public int getRandomInt(int limit) { + return numberGenerator.nextInt(limit); + } + + public double getRandomDouble(int limit) { + return numberGenerator.nextDouble() * limit; + } + + public double getRandomDouble() { + return numberGenerator.nextDouble(); + } + + /* + * Function set functions + */ + public Function getRandomFunction() { + return functionSet.getFunction(numberGenerator.nextInt(functionSet.getFunctionCount())); + } + + public Function getFunction(int index) { + return functionSet.getFunction(index); + } + + public void setFunctionSet(int index) { + functionSet = functionSets[index]; + } + + /* + * Test cases + */ + public void setTestCases(TestCase ... testCases) { + this.testCases = testCases; + } + + public TestCase getTestCase(int index) { + return testCases[index]; + } + + public int getTestCaseCount() { + return testCases.length; + } + } + + private Resources resources = new Resources(); + + /* + * The following arrays contain all available modules. These collections are read by the GUI + * when generating menus, so modules not added here will *NOT* be selectable in the GUI. + * + * Each array is accompanied by a field which contains a reference to the currently selected + * module, 0 by default. + */ + // mutators private Mutator[] mutators = new Mutator[] { - new PointMutator()}; + new PointMutator() }; private Mutator mutator = mutators[0]; + // evolutionary algorithms private EvolutionaryAlgorithm[] evolutionaryAlgorithms = new EvolutionaryAlgorithm[] { - new StandardEA()}; + new StandardEA() }; private EvolutionaryAlgorithm evolutionaryAlgorithm = evolutionaryAlgorithms[0]; + // fitness evaluators private FitnessFunction[] fitnessFunctions = new FitnessFunction[] { - new TruthTableEvaluator()}; + new TestCaseEvaluator() }; private FitnessFunction fitnessFunction = fitnessFunctions[0]; - private FunctionSet[] functionSets = new FunctionSet[] { - new FunctionSet("Arithmetic", - new Arithmetic.Addition(), - new Arithmetic.Subtraction(), - new Arithmetic.Multiplication(), - new Arithmetic.Division()), - - new FunctionSet("32-bit logic", - new BitwiseLogic.And(), - new BitwiseLogic.Or(), - new BitwiseLogic.Nand(), - new BitwiseLogic.Nor(), - new BitwiseLogic.Xor(), - new BitwiseLogic.Xnor(), - new BitwiseLogic.Not()), - - new FunctionSet("1-bit logic", - new BooleanLogic.And(), - new BooleanLogic.Or(), - new BooleanLogic.Nand(), - new BooleanLogic.Nor(), - new BooleanLogic.Xor(), - new BooleanLogic.Xnor(), - new BooleanLogic.Not())}; + // the population of chromosomes + private Population population = new Population(resources); - - - private Population population; - - private HashMap<String, Parameter> parameters = new HashMap<String, Parameter>(); public CGP() { - createBaseParameters(); - - for (int i = 0; i < (int) get("generations"); i++) { - - set("currentGen", ((int) get("currentGen")) + 1); - fitnessFunction.evaluate(population); - evolutionaryAlgorithm.evolve(population, mutator); - - if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 3) { - break; - } - } - } - - private void createBaseParameters() { - // make fundamental parameters - parameters.put("rows", new IntegerParameter(3, "Rows")); - parameters.put("columns", new IntegerParameter(3, "Columns")); - parameters.put("inputs", new IntegerParameter(3, "Inputs")); - parameters.put("outputs", new IntegerParameter(3, "Outputs")); - parameters.put("popSize", new IntegerParameter(5, "Population")); + resources.addModuleParameters(evolutionaryAlgorithm, mutator, fitnessFunction); - parameters.put("nodes", new IntegerParameter(9, "", true, true)); + resources.setTestCases(new TestCase(new Integer[]{1, 2, 3}, new Integer[]{4, 5, 6}), + new TestCase(new Integer[]{1, 12, 4}, new Integer[]{6, 21, 2})); - parameters.put("gens", new IntegerParameter(100, "Generations")); - parameters.put("currentGen", new IntegerParameter(0, "Generation")); - parameters.put("runs", new IntegerParameter(5, "Runs")); - - parameters.put("debug", new BooleanParameter(false, "Debug")); +// for (int i = 0; i < (int) resources.get("generations"); i++) { +// +// resources.set("currentGen", ((int) resources.get("currentGen")) + 1); +// +// fitnessFunction.evaluate(population, resources); +// evolutionaryAlgorithm.evolve(population, mutator, resources); +// +// System.out.println("fitness: " + evolutionaryAlgorithm.getFittestChromosome().getFitness()); +// +// if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 6) { +// System.out.println("solution found"); +// evolutionaryAlgorithm.getFittestChromosome().printNodes(); +// break; +// } +// } } -// /** -// * -// */ -// private void loadModules() { -// // initialise function set -// FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction(), new Arithmetic.Multiplication()); -// -// // initialise utilities -// Utilities.setResources(new Random(1234), functionSet); -// -// // initialise fitness function and truth table -// TruthTable.setTestCases(new TestCase(new Object[] {2, 5, 4}, new Object[] {1, 10, 15})); -// fitnessFunction = new TruthTableEvaluator(); -// -// // initialise EA -// evolutionaryAlgorithm = new StandardEA(); -// mutator = new StandardMutator(); -// -// // initialise population -// population = new Population(); -// } - - // Parameter methods - public void register(String key, Parameter value) { - parameters.put(key, value); - } - - /** - * @param key - * @return - */ - public Object get(String key) { - return parameters.get(key).getValue(); - } - - public void set(String key, Object value) { - parameters.get(key).setValue(value); - } - - @SuppressWarnings("rawtypes") - public Property getProperty(String key) { - return parameters.get(key).valueProperty(); + + public Resources getResources() { + return resources; } - - public boolean contains(String key) { - return parameters.containsKey(key); + + + public Population getPopulation() { + return population; } - // Utility methods - } |