From d63d3145f0f2abcee1bb88457324f4aaf9b9320e Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sat, 8 Mar 2014 14:48:25 +0000 Subject: Slowly refactoring Parameters to fit the GUI a little better... --- README | 6 +- src/jcgp/CGP.java | 60 ++++---- src/jcgp/Main.java | 1 - src/jcgp/Parameters.java | 150 ------------------ src/jcgp/TruthTable.java | 2 +- src/jcgp/Utilities.java | 5 +- src/jcgp/ea/EvolutionaryAlgorithm.java | 12 -- src/jcgp/ea/Mutator.java | 9 -- src/jcgp/ea/StandardEA.java | 47 ------ src/jcgp/ea/StandardMutator.java | 31 ---- src/jcgp/fitness/FitnessFunction.java | 9 -- src/jcgp/fitness/TestCase.java | 42 ----- src/jcgp/fitness/TruthTableEvaluator.java | 30 ---- src/jcgp/function/Arithmetic.java | 123 --------------- src/jcgp/function/BitwiseLogic.java | 178 ---------------------- src/jcgp/function/BooleanLogic.java | 178 ---------------------- src/jcgp/function/Function.java | 12 -- src/jcgp/function/FunctionSet.java | 39 ----- src/jcgp/gui/GUI.java | 14 ++ src/jcgp/modules/Module.java | 15 ++ src/jcgp/modules/ea/EvolutionaryAlgorithm.java | 13 ++ src/jcgp/modules/ea/StandardEA.java | 50 ++++++ src/jcgp/modules/fitness/FitnessFunction.java | 9 ++ src/jcgp/modules/fitness/TestCase.java | 42 +++++ src/jcgp/modules/fitness/TruthTableEvaluator.java | 30 ++++ src/jcgp/modules/function/Arithmetic.java | 123 +++++++++++++++ src/jcgp/modules/function/BitwiseLogic.java | 178 ++++++++++++++++++++++ src/jcgp/modules/function/BooleanLogic.java | 178 ++++++++++++++++++++++ src/jcgp/modules/function/Function.java | 12 ++ src/jcgp/modules/function/FunctionSet.java | 39 +++++ src/jcgp/modules/mutator/Mutator.java | 9 ++ src/jcgp/modules/mutator/StandardMutator.java | 31 ++++ src/jcgp/parameters/BooleanParameter.java | 32 ++++ src/jcgp/parameters/DoubleParameter.java | 32 ++++ src/jcgp/parameters/IntegerParameter.java | 32 ++++ src/jcgp/parameters/Parameter.java | 22 +++ src/jcgp/parameters/Parameters.java | 31 ++++ src/jcgp/population/Chromosome.java | 2 +- src/jcgp/population/Node.java | 4 +- src/jcgp/population/Output.java | 2 +- src/jcgp/population/Population.java | 2 +- src/jcgp/tests/ChromosomeTests.java | 6 +- src/jcgp/tests/NodeTests.java | 6 +- src/jcgp/tests/OutputTests.java | 2 +- src/jcgp/tests/PopulationTests.java | 6 +- 45 files changed, 948 insertions(+), 908 deletions(-) delete mode 100644 src/jcgp/Parameters.java delete mode 100644 src/jcgp/ea/EvolutionaryAlgorithm.java delete mode 100644 src/jcgp/ea/Mutator.java delete mode 100644 src/jcgp/ea/StandardEA.java delete mode 100644 src/jcgp/ea/StandardMutator.java delete mode 100644 src/jcgp/fitness/FitnessFunction.java delete mode 100644 src/jcgp/fitness/TestCase.java delete mode 100644 src/jcgp/fitness/TruthTableEvaluator.java delete mode 100644 src/jcgp/function/Arithmetic.java delete mode 100644 src/jcgp/function/BitwiseLogic.java delete mode 100644 src/jcgp/function/BooleanLogic.java delete mode 100644 src/jcgp/function/Function.java delete mode 100644 src/jcgp/function/FunctionSet.java create mode 100644 src/jcgp/gui/GUI.java create mode 100644 src/jcgp/modules/Module.java create mode 100644 src/jcgp/modules/ea/EvolutionaryAlgorithm.java create mode 100644 src/jcgp/modules/ea/StandardEA.java create mode 100644 src/jcgp/modules/fitness/FitnessFunction.java create mode 100644 src/jcgp/modules/fitness/TestCase.java create mode 100644 src/jcgp/modules/fitness/TruthTableEvaluator.java create mode 100644 src/jcgp/modules/function/Arithmetic.java create mode 100644 src/jcgp/modules/function/BitwiseLogic.java create mode 100644 src/jcgp/modules/function/BooleanLogic.java create mode 100644 src/jcgp/modules/function/Function.java create mode 100644 src/jcgp/modules/function/FunctionSet.java create mode 100644 src/jcgp/modules/mutator/Mutator.java create mode 100644 src/jcgp/modules/mutator/StandardMutator.java create mode 100644 src/jcgp/parameters/BooleanParameter.java create mode 100644 src/jcgp/parameters/DoubleParameter.java create mode 100644 src/jcgp/parameters/IntegerParameter.java create mode 100644 src/jcgp/parameters/Parameter.java create mode 100644 src/jcgp/parameters/Parameters.java diff --git a/README b/README index b6ee389..529a270 100644 --- a/README +++ b/README @@ -162,4 +162,8 @@ This can be caught by the GUI to display an error message, if necessary. Added a new parameter, debug. When set to true, the system prints a lot of information to the console for debugging purposes. -Added some more functions. \ No newline at end of file +Added some more functions. + +8/3 + +Adding GUI package, refactoring CGP class to interact appropriately with GUI. \ No newline at end of file diff --git a/src/jcgp/CGP.java b/src/jcgp/CGP.java index 48273e9..6ac8ff8 100644 --- a/src/jcgp/CGP.java +++ b/src/jcgp/CGP.java @@ -2,35 +2,40 @@ package jcgp; import java.util.Random; -import jcgp.ea.EvolutionaryAlgorithm; -import jcgp.ea.Mutator; -import jcgp.ea.StandardEA; -import jcgp.ea.StandardMutator; -import jcgp.fitness.FitnessFunction; -import jcgp.fitness.TestCase; -import jcgp.fitness.TruthTableEvaluator; -import jcgp.function.Arithmetic; -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.FunctionSet; +import jcgp.modules.mutator.Mutator; +import jcgp.modules.mutator.StandardMutator; +import jcgp.parameters.BooleanParameter; +import jcgp.parameters.IntegerParameter; +import jcgp.parameters.Parameters; import jcgp.population.Population; -public final class CGP { +public class CGP { // CGP components - private EvolutionaryAlgorithm ea; + private EvolutionaryAlgorithm evolutionaryAlgorithm; private Mutator mutator; private Population population; private FitnessFunction fitnessFunction; + + public CGP() { initialise(); for (int i = 0; i < Parameters.getTotalGenerations(); i++) { Parameters.setCurrentGeneration(i); fitnessFunction.evaluate(population); - ea.evolve(population, mutator); - if (ea.getFittestChromosome().getFitness() >= 3) { + evolutionaryAlgorithm.evolve(population, mutator); + if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 3) { if (Parameters.getDebug()) { - ea.getFittestChromosome().printNodes(); + evolutionaryAlgorithm.getFittestChromosome().printNodes(); } break; } @@ -41,31 +46,30 @@ public final class CGP { * */ private void initialise() { + // make fundamental parameters + Parameters.add("Rows", new IntegerParameter(3)); + Parameters.add("Columns", new IntegerParameter(3)); + Parameters.add("Inputs", new IntegerParameter(3)); + Parameters.add("Outputs", new IntegerParameter(3)); + Parameters.add("population", new IntegerParameter(5)); + + Parameters.add("generations", new IntegerParameter(100)); + Parameters.add("runs", new IntegerParameter(5)); + + Parameters.add("debug", new BooleanParameter(false)); + // 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 parameters - Parameters.setColumns(3); - Parameters.setRows(6); - Parameters.setInputs(3); - Parameters.setOutputs(3); - Parameters.setLevelsBack(3); - Parameters.setPopulationSize(1, 4); - Parameters.setMutationRate(10); - Parameters.setTotalGenerations(5000); - Parameters.setTotalRuns(5); - Parameters.setMaxArity(functionSet.getMaxArity()); - Parameters.setDebug(false); - // 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 - ea = new StandardEA(); + evolutionaryAlgorithm = new StandardEA(); mutator = new StandardMutator(); // initialise population diff --git a/src/jcgp/Main.java b/src/jcgp/Main.java index 97b5c4a..d2b438d 100644 --- a/src/jcgp/Main.java +++ b/src/jcgp/Main.java @@ -4,7 +4,6 @@ public class Main { public static void main(String[] args) { CGP cgp = new CGP(); - cgp.getClass(); } } diff --git a/src/jcgp/Parameters.java b/src/jcgp/Parameters.java deleted file mode 100644 index cef1ea4..0000000 --- a/src/jcgp/Parameters.java +++ /dev/null @@ -1,150 +0,0 @@ -package jcgp; - -public class Parameters { - - - private static int rows = 0, columns = 0, inputs = 0, outputs = 0, levelsBack = 0, - mutationRate = 0, totalGenerations = 0, parents = 0, offspring = 0, - currentGeneration = 0, totalRuns = 0, currentRun = 0, - maxArity = 0, maxFitness = 0; - - private static boolean debug = false; - - public static int getRows() { - return rows; - } - - public static int getColumns() { - return columns; - } - - public static int getNodeCount() { - return rows * columns; - } - - public static int getInputs() { - return inputs; - } - - public static int getOutputs() { - return outputs; - } - - public static int getLevelsBack() { - return levelsBack; - } - - public static int getMutationRate() { - return mutationRate; - } - - public static int getPopulationSize() { - return offspring + parents; - } - - public static int getOffspringCount() { - return offspring; - } - - public static int getParentCount() { - return parents; - } - - public static int getTotalGenerations() { - return totalGenerations; - } - - public static int getCurrentGeneration() { - return currentGeneration; - } - - public static int getTotalRuns() { - return totalRuns; - } - - public static int getCurrentRun() { - return currentRun; - } - - public static int getMaxArity() { - return maxArity; - } - - public static int getMaxFitness() { - return maxFitness; - } - - public static boolean getDebug() { - return debug; - } - - public static void setRows(int rows) { - Parameters.rows = rows; - } - - public static void setColumns(int columns) { - Parameters.columns = columns; - } - - public static void setInputs(int inputs) { - Parameters.inputs = inputs; - } - - public static void setOutputs(int outputs) { - Parameters.outputs = outputs; - } - - public static void setLevelsBack(int levelsBack) { - Parameters.levelsBack = levelsBack; - } - - public static void setMutationRate(int mutationRate) { - Parameters.mutationRate = mutationRate; - } - - public static void setPopulationSize(int parents, int offspring) { - Parameters.parents = parents; - Parameters.offspring = offspring; - } - - public static void setOffspringCount(int offspring) { - Parameters.offspring = offspring; - } - - public static void setParentCount(int parents) { - Parameters.parents = parents; - } - - public static void setTotalGenerations(int totalGenerations) { - Parameters.totalGenerations = totalGenerations; - } - - public static void setCurrentGeneration(int currentGeneration) { - Parameters.currentGeneration = currentGeneration; - } - - public static void incrementCurrentGeneration() { - Parameters.currentGeneration++; - } - - public static void setTotalRuns(int totalRuns) { - Parameters.totalRuns = totalRuns; - } - - public static void setCurrentRun(int currentRun) { - Parameters.currentRun = currentRun; - } - - public static void setMaxArity(int maxArity) { - Parameters.maxArity = maxArity; - } - - public static void setMaxFitness(int maxFitness) { - Parameters.maxFitness = maxFitness; - } - - public static void setDebug(boolean debug) { - Parameters.debug = debug; - } - -} diff --git a/src/jcgp/TruthTable.java b/src/jcgp/TruthTable.java index 72712fc..e62f56b 100644 --- a/src/jcgp/TruthTable.java +++ b/src/jcgp/TruthTable.java @@ -1,6 +1,6 @@ package jcgp; -import jcgp.fitness.TestCase; +import jcgp.modules.fitness.TestCase; public class TruthTable { diff --git a/src/jcgp/Utilities.java b/src/jcgp/Utilities.java index 394a481..ff5387f 100644 --- a/src/jcgp/Utilities.java +++ b/src/jcgp/Utilities.java @@ -2,8 +2,9 @@ package jcgp; import java.util.Random; -import jcgp.function.Function; -import jcgp.function.FunctionSet; +import jcgp.modules.function.Function; +import jcgp.modules.function.FunctionSet; +import jcgp.parameters.Parameters; import jcgp.population.*; public class Utilities { diff --git a/src/jcgp/ea/EvolutionaryAlgorithm.java b/src/jcgp/ea/EvolutionaryAlgorithm.java deleted file mode 100644 index ba70671..0000000 --- a/src/jcgp/ea/EvolutionaryAlgorithm.java +++ /dev/null @@ -1,12 +0,0 @@ -package jcgp.ea; - -import jcgp.population.Chromosome; -import jcgp.population.Population; - -public interface EvolutionaryAlgorithm { - - public abstract void evolve(Population population, Mutator mutator); - - public abstract Chromosome getFittestChromosome(); - -} diff --git a/src/jcgp/ea/Mutator.java b/src/jcgp/ea/Mutator.java deleted file mode 100644 index df3fe1c..0000000 --- a/src/jcgp/ea/Mutator.java +++ /dev/null @@ -1,9 +0,0 @@ -package jcgp.ea; - -import jcgp.population.Chromosome; - -public interface Mutator { - - void mutate(Chromosome chromosome); - -} diff --git a/src/jcgp/ea/StandardEA.java b/src/jcgp/ea/StandardEA.java deleted file mode 100644 index 5f38513..0000000 --- a/src/jcgp/ea/StandardEA.java +++ /dev/null @@ -1,47 +0,0 @@ -package jcgp.ea; - -import jcgp.Parameters; -import jcgp.population.Chromosome; -import jcgp.population.Population; - -/** - * (1 + λ) EA. - * - * - * @author Eduardo Pedroni - * - */ -public class StandardEA implements EvolutionaryAlgorithm { - - private Chromosome fittestChromosome; - - @Override - public void evolve(Population population, Mutator mutator) { - // select fittest chromosome - int fittest = 0; - - for (int i = 1; i < Parameters.getPopulationSize(); i++) { - if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) { - fittest = i; - } - } - fittestChromosome = population.getChromosome(fittest); - population.setBestIndividual(fittest); - if (Parameters.getDebug()) { - System.out.println("Best fitness: " + fittestChromosome.getFitness()); - } - // create copies of fittest chromosome, mutate them - Chromosome fc = population.getChromosome(fittest); - for (int i = 0; i < Parameters.getPopulationSize(); i++) { - if (i != fittest) { - population.getChromosome(i).copyConnections(fc); - mutator.mutate(population.getChromosome(i)); - } - } - } - - @Override - public Chromosome getFittestChromosome() { - return fittestChromosome; - } -} diff --git a/src/jcgp/ea/StandardMutator.java b/src/jcgp/ea/StandardMutator.java deleted file mode 100644 index b9c0a4e..0000000 --- a/src/jcgp/ea/StandardMutator.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.ea; - -import jcgp.Parameters; -import jcgp.Utilities; -import jcgp.population.Chromosome; -import jcgp.population.MutableElement; -import jcgp.population.Node; -import jcgp.population.Output; - -public class StandardMutator implements Mutator { - - @Override - public void mutate(Chromosome chromosome) { - int mutations = (int) (Parameters.getMutationRate() * (((double) Parameters.getNodeCount() + Parameters.getOutputs()) / 100)); - - for (int i = 0; i < mutations; i++) { - MutableElement m = chromosome.getRandomMutableElement(); - - if (m instanceof Output) { - m.setConnection(chromosome.getRandomConnection()); - } else if (m instanceof Node) { - int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity()); - if (geneType < 1) { - ((Node) m).setFunction(Utilities.getRandomFunction()); - } else { - m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn())); - } - } - } - } -} diff --git a/src/jcgp/fitness/FitnessFunction.java b/src/jcgp/fitness/FitnessFunction.java deleted file mode 100644 index 6fa63ef..0000000 --- a/src/jcgp/fitness/FitnessFunction.java +++ /dev/null @@ -1,9 +0,0 @@ -package jcgp.fitness; - -import jcgp.population.Population; - -public interface FitnessFunction { - - public void evaluate(Population population); - -} diff --git a/src/jcgp/fitness/TestCase.java b/src/jcgp/fitness/TestCase.java deleted file mode 100644 index e506d38..0000000 --- a/src/jcgp/fitness/TestCase.java +++ /dev/null @@ -1,42 +0,0 @@ -package jcgp.fitness; - -import jcgp.Parameters; -import jcgp.exceptions.ParameterMismatchException; - -public class TestCase { - - private Object[] inputs; - private Object[] outputs; - - public TestCase(Object[] inputs, Object[] outputs) throws ParameterMismatchException { - if (inputs.length == Parameters.getInputs()) { - this.inputs = inputs; - } else { - throw new ParameterMismatchException(); - } - - if (outputs.length == Parameters.getOutputs()) { - this.outputs = outputs; - } else { - throw new ParameterMismatchException(); - } - - } - - public Object getInput(int index) { - return inputs[index]; - } - - public Object getOutput(int index) { - return outputs[index]; - } - - public Object[] getInputs() { - return inputs; - } - - public Object[] getOutputs() { - return outputs; - } - -} diff --git a/src/jcgp/fitness/TruthTableEvaluator.java b/src/jcgp/fitness/TruthTableEvaluator.java deleted file mode 100644 index 4adf435..0000000 --- a/src/jcgp/fitness/TruthTableEvaluator.java +++ /dev/null @@ -1,30 +0,0 @@ -package jcgp.fitness; - -import jcgp.Parameters; -import jcgp.TruthTable; -import jcgp.population.Population; - -public class TruthTableEvaluator implements FitnessFunction { - - @Override - public void evaluate(Population population) { - // for every chromosome in the population - for (int i = 0; i < Parameters.getPopulationSize(); i++) { - int fitness = 0; - // for every test case - for (int t = 0; t < TruthTable.getTestCaseCount(); t++) { - population.getChromosome(i).setInputs(TruthTable.getTestCase(t).getInputs()); - // check every output - for (int o = 0; o < Parameters.getOutputs(); o++) { - if (population.getChromosome(i).getOutput(o).calculate() == TruthTable.getTestCase(t).getOutput(o)) { - fitness++; - } - } - } - population.getChromosome(i).setFitness(fitness); - if (Parameters.getDebug()) { - System.out.println("active nodes: " + population.getChromosome(i).getActiveNodes().size()); - } - } - } -} diff --git a/src/jcgp/function/Arithmetic.java b/src/jcgp/function/Arithmetic.java deleted file mode 100644 index b0bd5ca..0000000 --- a/src/jcgp/function/Arithmetic.java +++ /dev/null @@ -1,123 +0,0 @@ -package jcgp.function; - -import jcgp.Parameters; -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class Arithmetic { - - public static class Addition extends Function { - - private int arity = 2; - - @Override - public Integer run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 + arg2; - - if (Parameters.getDebug()) { - System.out.println(arg1 + " + " + arg2 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Subtraction extends Function { - - private int arity = 2; - - @Override - public Integer run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 - arg2; - - if (Parameters.getDebug()) { - System.out.println(arg1 + " - " + arg2 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Multiplication extends Function { - - private int arity = 2; - - @Override - public Integer run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 * arg2; - - if (Parameters.getDebug()) { - System.out.println(arg1 + " * " + arg2 + " = " + result); - } - - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Division extends Function { - - private int arity = 2; - - @Override - public Integer run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result; - if (arg2 == 0) { - result = 0; - } else { - result = arg1 / arg2; - } - - - if (Parameters.getDebug()) { - System.out.println(arg1 + " / " + arg2 + " = " + result); - } - - - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - -} diff --git a/src/jcgp/function/BitwiseLogic.java b/src/jcgp/function/BitwiseLogic.java deleted file mode 100644 index 55f5df0..0000000 --- a/src/jcgp/function/BitwiseLogic.java +++ /dev/null @@ -1,178 +0,0 @@ -package jcgp.function; - -import jcgp.Parameters; -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class BitwiseLogic { - - public static class And extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 & arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " AND " + arg2 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Or extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 | arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " OR " + arg2 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Not extends Function { - private int arity = 1; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int result = ~arg1; - if (Parameters.getDebug()) { - System.out.println("NOT " + arg1 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Xor extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 ^ arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " XOR " + arg2 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Nand extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 & arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " NAND " + arg2 + " = " + ~result); - } - return ~result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Nor extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 | arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " NOR " + arg2 + " = " + ~result); - } - return ~result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Xnor extends Function { - private int arity = 2; - - @Override - public Object run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - int arg1 = ((int) connections[0].getValue()); - int arg2 = ((int) connections[1].getValue()); - int result = arg1 ^ arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " XNOR " + arg2 + " = " + ~result); - } - return ~result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - - -} diff --git a/src/jcgp/function/BooleanLogic.java b/src/jcgp/function/BooleanLogic.java deleted file mode 100644 index 713dcf8..0000000 --- a/src/jcgp/function/BooleanLogic.java +++ /dev/null @@ -1,178 +0,0 @@ -package jcgp.function; - -import jcgp.Parameters; -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class BooleanLogic { - - public static class And extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 && arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " AND " + arg2 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Or extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 || arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " OR " + arg2 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Not extends Function { - private int arity = 1; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean result = !arg1; - if (Parameters.getDebug()) { - System.out.println("NOT " + arg1 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Xor extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 ^ arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " XOR " + arg2 + " = " + result); - } - return result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Nand extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 && arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " NAND " + arg2 + " = " + !result); - } - return !result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Nor extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 || arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " NOR " + arg2 + " = " + !result); - } - return !result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - public static class Xnor extends Function { - private int arity = 2; - - @Override - public Boolean run(Connection... connections) { - if (connections.length < arity) { - throw new InvalidArgumentsException("Not enough connections were given."); - } else { - Boolean arg1 = ((Boolean) connections[0].getValue()); - Boolean arg2 = ((Boolean) connections[1].getValue()); - Boolean result = arg1 ^ arg2; - if (Parameters.getDebug()) { - System.out.println(arg1 + " XNOR " + arg2 + " = " + !result); - } - return !result; - } - } - - @Override - public int getArity() { - return arity; - } - } - - - -} diff --git a/src/jcgp/function/Function.java b/src/jcgp/function/Function.java deleted file mode 100644 index 584421a..0000000 --- a/src/jcgp/function/Function.java +++ /dev/null @@ -1,12 +0,0 @@ -package jcgp.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public abstract class Function { - - public abstract Object run(Connection ... connections) throws InvalidArgumentsException; - - public abstract int getArity(); - -} diff --git a/src/jcgp/function/FunctionSet.java b/src/jcgp/function/FunctionSet.java deleted file mode 100644 index cbe2f05..0000000 --- a/src/jcgp/function/FunctionSet.java +++ /dev/null @@ -1,39 +0,0 @@ -package jcgp.function; - -/** - * - * TODO: if function set flexibility is desired (i.e. add more functions as the program runs) - * an add function method should be created - * this would lead to concurrency problems, so tread lightly! - * - * - * @author Eduardo Pedroni - * - */ -public class FunctionSet { - private Function[] functionList; - private int maxArity = 0; - - public FunctionSet(Function ... functions) { - functionList = functions; - - for (Function function : functionList) { - if (function.getArity() > maxArity) { - maxArity = function.getArity(); - } - } - - } - - public int getFunctionCount() { - return functionList.length; - } - - public Function getFunction(int index) { - return functionList[index]; - } - - public int getMaxArity(){ - return maxArity; - } - } \ No newline at end of file diff --git a/src/jcgp/gui/GUI.java b/src/jcgp/gui/GUI.java new file mode 100644 index 0000000..0e4b91a --- /dev/null +++ b/src/jcgp/gui/GUI.java @@ -0,0 +1,14 @@ +package jcgp.gui; + +import javafx.application.Application; +import javafx.stage.Stage; + +public class GUI extends Application { + + @Override + public void start(Stage primaryStage) throws Exception { + + + } + +} diff --git a/src/jcgp/modules/Module.java b/src/jcgp/modules/Module.java new file mode 100644 index 0000000..a0da50a --- /dev/null +++ b/src/jcgp/modules/Module.java @@ -0,0 +1,15 @@ +package jcgp.modules; + +import jcgp.parameters.Parameter; +import jcgp.parameters.Parameters; + +public abstract class Module { + + /** + * Register a new parameter + */ + protected final void registerParameter(String key, Parameter value) { + Parameters.add(key, value); + }; + +} diff --git a/src/jcgp/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/modules/ea/EvolutionaryAlgorithm.java new file mode 100644 index 0000000..8de8c87 --- /dev/null +++ b/src/jcgp/modules/ea/EvolutionaryAlgorithm.java @@ -0,0 +1,13 @@ +package jcgp.modules.ea; + +import jcgp.modules.mutator.Mutator; +import jcgp.population.Chromosome; +import jcgp.population.Population; + +public interface EvolutionaryAlgorithm { + + public abstract void evolve(Population population, Mutator mutator); + + public abstract Chromosome getFittestChromosome(); + +} diff --git a/src/jcgp/modules/ea/StandardEA.java b/src/jcgp/modules/ea/StandardEA.java new file mode 100644 index 0000000..2db8776 --- /dev/null +++ b/src/jcgp/modules/ea/StandardEA.java @@ -0,0 +1,50 @@ +package jcgp.modules.ea; + +import jcgp.modules.mutator.Mutator; +import jcgp.parameters.Parameters; +import jcgp.parameters.IntegerParameter; +import jcgp.parameters.BooleanParameter; +import jcgp.population.Chromosome; +import jcgp.population.Population; + +/** + * (1 + λ) EA. + * + * + * @author Eduardo Pedroni + * + */ +public class StandardEA implements EvolutionaryAlgorithm { + + private Chromosome fittestChromosome; + + @Override + public void evolve(Population population, Mutator mutator) { + // select fittest chromosome + int fittest = 0; + + for (int i = 1; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) { + if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) { + fittest = i; + } + } + fittestChromosome = population.getChromosome(fittest); + population.setBestIndividual(fittest); + if (((BooleanParameter) Parameters.get("debug")).getValue()) { + System.out.println("Best fitness: " + fittestChromosome.getFitness()); + } + // create copies of fittest chromosome, mutate them + Chromosome fc = population.getChromosome(fittest); + for (int i = 0; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) { + if (i != fittest) { + population.getChromosome(i).copyConnections(fc); + mutator.mutate(population.getChromosome(i)); + } + } + } + + @Override + public Chromosome getFittestChromosome() { + return fittestChromosome; + } +} diff --git a/src/jcgp/modules/fitness/FitnessFunction.java b/src/jcgp/modules/fitness/FitnessFunction.java new file mode 100644 index 0000000..8ed1b56 --- /dev/null +++ b/src/jcgp/modules/fitness/FitnessFunction.java @@ -0,0 +1,9 @@ +package jcgp.modules.fitness; + +import jcgp.population.Population; + +public interface FitnessFunction { + + public void evaluate(Population population); + +} diff --git a/src/jcgp/modules/fitness/TestCase.java b/src/jcgp/modules/fitness/TestCase.java new file mode 100644 index 0000000..0cb09f1 --- /dev/null +++ b/src/jcgp/modules/fitness/TestCase.java @@ -0,0 +1,42 @@ +package jcgp.modules.fitness; + +import jcgp.exceptions.ParameterMismatchException; +import jcgp.parameters.Parameters; + +public class TestCase { + + private Object[] inputs; + private Object[] outputs; + + public TestCase(Object[] inputs, Object[] outputs) throws ParameterMismatchException { + if (inputs.length == Parameters.getInputs()) { + this.inputs = inputs; + } else { + throw new ParameterMismatchException(); + } + + if (outputs.length == Parameters.getOutputs()) { + this.outputs = outputs; + } else { + throw new ParameterMismatchException(); + } + + } + + public Object getInput(int index) { + return inputs[index]; + } + + public Object getOutput(int index) { + return outputs[index]; + } + + public Object[] getInputs() { + return inputs; + } + + public Object[] getOutputs() { + return outputs; + } + +} diff --git a/src/jcgp/modules/fitness/TruthTableEvaluator.java b/src/jcgp/modules/fitness/TruthTableEvaluator.java new file mode 100644 index 0000000..a69de96 --- /dev/null +++ b/src/jcgp/modules/fitness/TruthTableEvaluator.java @@ -0,0 +1,30 @@ +package jcgp.modules.fitness; + +import jcgp.TruthTable; +import jcgp.parameters.Parameters; +import jcgp.population.Population; + +public class TruthTableEvaluator implements FitnessFunction { + + @Override + public void evaluate(Population population) { + // for every chromosome in the population + for (int i = 0; i < Parameters.getPopulationSize(); i++) { + int fitness = 0; + // for every test case + for (int t = 0; t < TruthTable.getTestCaseCount(); t++) { + population.getChromosome(i).setInputs(TruthTable.getTestCase(t).getInputs()); + // check every output + for (int o = 0; o < Parameters.getOutputs(); o++) { + if (population.getChromosome(i).getOutput(o).calculate() == TruthTable.getTestCase(t).getOutput(o)) { + fitness++; + } + } + } + population.getChromosome(i).setFitness(fitness); + if (Parameters.getDebug()) { + System.out.println("active nodes: " + population.getChromosome(i).getActiveNodes().size()); + } + } + } +} diff --git a/src/jcgp/modules/function/Arithmetic.java b/src/jcgp/modules/function/Arithmetic.java new file mode 100644 index 0000000..73debd2 --- /dev/null +++ b/src/jcgp/modules/function/Arithmetic.java @@ -0,0 +1,123 @@ +package jcgp.modules.function; + +import jcgp.exceptions.InvalidArgumentsException; +import jcgp.parameters.Parameters; +import jcgp.population.Connection; + +public class Arithmetic { + + public static class Addition extends Function { + + private int arity = 2; + + @Override + public Integer run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Integer arg1 = ((Integer) connections[0].getValue()); + Integer arg2 = ((Integer) connections[1].getValue()); + Integer result = arg1 + arg2; + + if (Parameters.getDebug()) { + System.out.println(arg1 + " + " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Subtraction extends Function { + + private int arity = 2; + + @Override + public Integer run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Integer arg1 = ((Integer) connections[0].getValue()); + Integer arg2 = ((Integer) connections[1].getValue()); + Integer result = arg1 - arg2; + + if (Parameters.getDebug()) { + System.out.println(arg1 + " - " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Multiplication extends Function { + + private int arity = 2; + + @Override + public Integer run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Integer arg1 = ((Integer) connections[0].getValue()); + Integer arg2 = ((Integer) connections[1].getValue()); + Integer result = arg1 * arg2; + + if (Parameters.getDebug()) { + System.out.println(arg1 + " * " + arg2 + " = " + result); + } + + + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Division extends Function { + + private int arity = 2; + + @Override + public Integer run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Integer arg1 = ((Integer) connections[0].getValue()); + Integer arg2 = ((Integer) connections[1].getValue()); + Integer result; + if (arg2 == 0) { + result = 0; + } else { + result = arg1 / arg2; + } + + + if (Parameters.getDebug()) { + System.out.println(arg1 + " / " + arg2 + " = " + result); + } + + + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + +} diff --git a/src/jcgp/modules/function/BitwiseLogic.java b/src/jcgp/modules/function/BitwiseLogic.java new file mode 100644 index 0000000..a260715 --- /dev/null +++ b/src/jcgp/modules/function/BitwiseLogic.java @@ -0,0 +1,178 @@ +package jcgp.modules.function; + +import jcgp.exceptions.InvalidArgumentsException; +import jcgp.parameters.Parameters; +import jcgp.population.Connection; + +public class BitwiseLogic { + + public static class And extends Function { + private int arity = 2; + + @Override + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + int arg1 = ((int) connections[0].getValue()); + int arg2 = ((int) connections[1].getValue()); + int result = arg1 & arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " AND " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Or extends Function { + private int arity = 2; + + @Override + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + int arg1 = ((int) connections[0].getValue()); + int arg2 = ((int) connections[1].getValue()); + int result = arg1 | arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " OR " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Not extends Function { + private int arity = 1; + + @Override + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + int arg1 = ((int) connections[0].getValue()); + int result = ~arg1; + if (Parameters.getDebug()) { + System.out.println("NOT " + arg1 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Xor extends Function { + private int arity = 2; + + @Override + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + int arg1 = ((int) connections[0].getValue()); + int arg2 = ((int) connections[1].getValue()); + int result = arg1 ^ arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XOR " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Nand extends Function { + private int arity = 2; + + @Override + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + int arg1 = ((int) connections[0].getValue()); + int arg2 = ((int) connections[1].getValue()); + int result = arg1 & arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NAND " + arg2 + " = " + ~result); + } + return ~result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Nor extends Function { + private int arity = 2; + + @Override + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + int arg1 = ((int) connections[0].getValue()); + int arg2 = ((int) connections[1].getValue()); + int result = arg1 | arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NOR " + arg2 + " = " + ~result); + } + return ~result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Xnor extends Function { + private int arity = 2; + + @Override + public Object run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + int arg1 = ((int) connections[0].getValue()); + int arg2 = ((int) connections[1].getValue()); + int result = arg1 ^ arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XNOR " + arg2 + " = " + ~result); + } + return ~result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + + +} diff --git a/src/jcgp/modules/function/BooleanLogic.java b/src/jcgp/modules/function/BooleanLogic.java new file mode 100644 index 0000000..887e0e6 --- /dev/null +++ b/src/jcgp/modules/function/BooleanLogic.java @@ -0,0 +1,178 @@ +package jcgp.modules.function; + +import jcgp.exceptions.InvalidArgumentsException; +import jcgp.parameters.Parameters; +import jcgp.population.Connection; + +public class BooleanLogic { + + public static class And extends Function { + private int arity = 2; + + @Override + public Boolean run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Boolean arg1 = ((Boolean) connections[0].getValue()); + Boolean arg2 = ((Boolean) connections[1].getValue()); + Boolean result = arg1 && arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " AND " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Or extends Function { + private int arity = 2; + + @Override + public Boolean run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Boolean arg1 = ((Boolean) connections[0].getValue()); + Boolean arg2 = ((Boolean) connections[1].getValue()); + Boolean result = arg1 || arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " OR " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Not extends Function { + private int arity = 1; + + @Override + public Boolean run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Boolean arg1 = ((Boolean) connections[0].getValue()); + Boolean result = !arg1; + if (Parameters.getDebug()) { + System.out.println("NOT " + arg1 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Xor extends Function { + private int arity = 2; + + @Override + public Boolean run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Boolean arg1 = ((Boolean) connections[0].getValue()); + Boolean arg2 = ((Boolean) connections[1].getValue()); + Boolean result = arg1 ^ arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XOR " + arg2 + " = " + result); + } + return result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Nand extends Function { + private int arity = 2; + + @Override + public Boolean run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Boolean arg1 = ((Boolean) connections[0].getValue()); + Boolean arg2 = ((Boolean) connections[1].getValue()); + Boolean result = arg1 && arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NAND " + arg2 + " = " + !result); + } + return !result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Nor extends Function { + private int arity = 2; + + @Override + public Boolean run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Boolean arg1 = ((Boolean) connections[0].getValue()); + Boolean arg2 = ((Boolean) connections[1].getValue()); + Boolean result = arg1 || arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " NOR " + arg2 + " = " + !result); + } + return !result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + public static class Xnor extends Function { + private int arity = 2; + + @Override + public Boolean run(Connection... connections) { + if (connections.length < arity) { + throw new InvalidArgumentsException("Not enough connections were given."); + } else { + Boolean arg1 = ((Boolean) connections[0].getValue()); + Boolean arg2 = ((Boolean) connections[1].getValue()); + Boolean result = arg1 ^ arg2; + if (Parameters.getDebug()) { + System.out.println(arg1 + " XNOR " + arg2 + " = " + !result); + } + return !result; + } + } + + @Override + public int getArity() { + return arity; + } + } + + + +} diff --git a/src/jcgp/modules/function/Function.java b/src/jcgp/modules/function/Function.java new file mode 100644 index 0000000..3314c2f --- /dev/null +++ b/src/jcgp/modules/function/Function.java @@ -0,0 +1,12 @@ +package jcgp.modules.function; + +import jcgp.exceptions.InvalidArgumentsException; +import jcgp.population.Connection; + +public abstract class Function { + + public abstract Object run(Connection ... connections) throws InvalidArgumentsException; + + public abstract int getArity(); + +} diff --git a/src/jcgp/modules/function/FunctionSet.java b/src/jcgp/modules/function/FunctionSet.java new file mode 100644 index 0000000..8a2190a --- /dev/null +++ b/src/jcgp/modules/function/FunctionSet.java @@ -0,0 +1,39 @@ +package jcgp.modules.function; + +/** + * + * TODO: if function set flexibility is desired (i.e. add more functions as the program runs) + * an add function method should be created + * this would lead to concurrency problems, so tread lightly! + * + * + * @author Eduardo Pedroni + * + */ +public class FunctionSet { + private Function[] functionList; + private int maxArity = 0; + + public FunctionSet(Function ... functions) { + functionList = functions; + + for (Function function : functionList) { + if (function.getArity() > maxArity) { + maxArity = function.getArity(); + } + } + + } + + public int getFunctionCount() { + return functionList.length; + } + + public Function getFunction(int index) { + return functionList[index]; + } + + public int getMaxArity(){ + return maxArity; + } + } \ No newline at end of file diff --git a/src/jcgp/modules/mutator/Mutator.java b/src/jcgp/modules/mutator/Mutator.java new file mode 100644 index 0000000..10df8cd --- /dev/null +++ b/src/jcgp/modules/mutator/Mutator.java @@ -0,0 +1,9 @@ +package jcgp.modules.mutator; + +import jcgp.population.Chromosome; + +public interface Mutator { + + void mutate(Chromosome chromosome); + +} diff --git a/src/jcgp/modules/mutator/StandardMutator.java b/src/jcgp/modules/mutator/StandardMutator.java new file mode 100644 index 0000000..17bd0be --- /dev/null +++ b/src/jcgp/modules/mutator/StandardMutator.java @@ -0,0 +1,31 @@ +package jcgp.modules.mutator; + +import jcgp.Utilities; +import jcgp.parameters.Parameters; +import jcgp.population.Chromosome; +import jcgp.population.MutableElement; +import jcgp.population.Node; +import jcgp.population.Output; + +public class StandardMutator implements Mutator { + + @Override + public void mutate(Chromosome chromosome) { + int mutations = (int) (Parameters.getMutationRate() * (((double) Parameters.getNodeCount() + Parameters.getOutputs()) / 100)); + + for (int i = 0; i < mutations; i++) { + MutableElement m = chromosome.getRandomMutableElement(); + + if (m instanceof Output) { + m.setConnection(chromosome.getRandomConnection()); + } else if (m instanceof Node) { + int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity()); + if (geneType < 1) { + ((Node) m).setFunction(Utilities.getRandomFunction()); + } else { + m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn())); + } + } + } + } +} diff --git a/src/jcgp/parameters/BooleanParameter.java b/src/jcgp/parameters/BooleanParameter.java new file mode 100644 index 0000000..738b733 --- /dev/null +++ b/src/jcgp/parameters/BooleanParameter.java @@ -0,0 +1,32 @@ +package jcgp.parameters; + +import javafx.beans.property.SimpleBooleanProperty; + +public class BooleanParameter extends Parameter { + + private SimpleBooleanProperty value; + + public BooleanParameter(boolean value) { + + this.value.set(value); + + } + + public BooleanParameter(boolean value, boolean managed) { + this.value.set(value); + this.managed = managed; + } + + public void setValue(boolean value) { + this.value.set(value); + } + + public boolean getValue() { + return this.value.get(); + } + + public SimpleBooleanProperty valueProperty() { + return value; + } + +} diff --git a/src/jcgp/parameters/DoubleParameter.java b/src/jcgp/parameters/DoubleParameter.java new file mode 100644 index 0000000..9349502 --- /dev/null +++ b/src/jcgp/parameters/DoubleParameter.java @@ -0,0 +1,32 @@ +package jcgp.parameters; + +import javafx.beans.property.SimpleDoubleProperty; + +public class DoubleParameter extends Parameter { + + private SimpleDoubleProperty value; + + public DoubleParameter(double value) { + + this.value.set(value); + + } + + public DoubleParameter(double value, boolean managed) { + this.value.set(value); + this.managed = managed; + } + + public void setValue(double value) { + this.value.set(value); + } + + public double getValue() { + return this.value.get(); + } + + public SimpleDoubleProperty valueProperty() { + return value; + } + +} diff --git a/src/jcgp/parameters/IntegerParameter.java b/src/jcgp/parameters/IntegerParameter.java new file mode 100644 index 0000000..2aac3e1 --- /dev/null +++ b/src/jcgp/parameters/IntegerParameter.java @@ -0,0 +1,32 @@ +package jcgp.parameters; + +import javafx.beans.property.SimpleIntegerProperty; + +public class IntegerParameter extends Parameter { + + private SimpleIntegerProperty value; + + public IntegerParameter(int value) { + + this.value.set(value); + + } + + public IntegerParameter(int value, boolean managed) { + this.value.set(value); + this.managed = managed; + } + + public void setValue(int value) { + this.value.set(value); + } + + public int getValue() { + return this.value.get(); + } + + public SimpleIntegerProperty valueProperty() { + return value; + } + +} diff --git a/src/jcgp/parameters/Parameter.java b/src/jcgp/parameters/Parameter.java new file mode 100644 index 0000000..8b63868 --- /dev/null +++ b/src/jcgp/parameters/Parameter.java @@ -0,0 +1,22 @@ +package jcgp.parameters; + +public abstract class Parameter { + protected boolean managed = false; + protected boolean hidden = false; + + public void setManaged(boolean value) { + managed = value; + } + + public boolean isManaged() { + return managed; + } + + public void setHidden(boolean value) { + hidden = value; + } + + public boolean isHidden() { + return hidden; + } +} diff --git a/src/jcgp/parameters/Parameters.java b/src/jcgp/parameters/Parameters.java new file mode 100644 index 0000000..54b54e3 --- /dev/null +++ b/src/jcgp/parameters/Parameters.java @@ -0,0 +1,31 @@ +package jcgp.parameters; + +import java.util.HashMap; + +public class Parameters { + + private static HashMap parameters; + + public static void add(String key, Parameter value) { + parameters.put(key, value); + } + + public static Parameter get(String key) { + return parameters.get(key); + } + + public static boolean contains(String key) { + return parameters.containsKey(key); + } + +// private static int rows = 0, columns = 0, inputs = 0, outputs = 0, levelsBack = 0, +// mutationRate = 0, totalGenerations = 0, parents = 0, offspring = 0, +// currentGeneration = 0, totalRuns = 0, currentRun = 0, +// maxArity = 0, maxFitness = 0; + +// private static boolean debug = false; + + + + +} diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index 996e765..df2a9d4 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -2,9 +2,9 @@ package jcgp.population; import java.util.ArrayList; -import jcgp.Parameters; import jcgp.Utilities; import jcgp.exceptions.ParameterMismatchException; +import jcgp.parameters.Parameters; public class Chromosome { diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java index 1741235..4203f25 100644 --- a/src/jcgp/population/Node.java +++ b/src/jcgp/population/Node.java @@ -3,10 +3,10 @@ package jcgp.population; import java.util.ArrayList; import java.util.Arrays; -import jcgp.Parameters; import jcgp.Utilities; import jcgp.exceptions.InsufficientConnectionsException; -import jcgp.function.Function; +import jcgp.modules.function.Function; +import jcgp.parameters.Parameters; public class Node implements MutableElement, Connection { diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java index 0df51e9..89ca13c 100644 --- a/src/jcgp/population/Output.java +++ b/src/jcgp/population/Output.java @@ -2,7 +2,7 @@ package jcgp.population; import java.util.ArrayList; -import jcgp.Parameters; +import jcgp.parameters.Parameters; public class Output implements MutableElement { diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java index 9cd7430..3e7b590 100644 --- a/src/jcgp/population/Population.java +++ b/src/jcgp/population/Population.java @@ -1,6 +1,6 @@ package jcgp.population; -import jcgp.Parameters; +import jcgp.parameters.Parameters; public class Population { diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index 46463c5..c61785e 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -4,10 +4,10 @@ import static org.junit.Assert.*; import java.util.Random; -import jcgp.Parameters; import jcgp.Utilities; -import jcgp.function.Arithmetic; -import jcgp.function.FunctionSet; +import jcgp.modules.function.Arithmetic; +import jcgp.modules.function.FunctionSet; +import jcgp.parameters.Parameters; import jcgp.population.Chromosome; import jcgp.population.Connection; import jcgp.population.Input; diff --git a/src/jcgp/tests/NodeTests.java b/src/jcgp/tests/NodeTests.java index ee26d38..74b8140 100644 --- a/src/jcgp/tests/NodeTests.java +++ b/src/jcgp/tests/NodeTests.java @@ -4,10 +4,10 @@ import static org.junit.Assert.assertTrue; import java.util.Random; -import jcgp.Parameters; import jcgp.Utilities; -import jcgp.function.Arithmetic; -import jcgp.function.Function; +import jcgp.modules.function.Arithmetic; +import jcgp.modules.function.Function; +import jcgp.parameters.Parameters; import jcgp.population.Chromosome; import jcgp.population.Connection; import jcgp.population.Node; diff --git a/src/jcgp/tests/OutputTests.java b/src/jcgp/tests/OutputTests.java index 20e1c7f..b8f7d96 100644 --- a/src/jcgp/tests/OutputTests.java +++ b/src/jcgp/tests/OutputTests.java @@ -4,8 +4,8 @@ import static org.junit.Assert.assertTrue; import java.util.Random; -import jcgp.Parameters; import jcgp.Utilities; +import jcgp.parameters.Parameters; import jcgp.population.Chromosome; import jcgp.population.Connection; import jcgp.population.Output; diff --git a/src/jcgp/tests/PopulationTests.java b/src/jcgp/tests/PopulationTests.java index a6ab141..d646b90 100644 --- a/src/jcgp/tests/PopulationTests.java +++ b/src/jcgp/tests/PopulationTests.java @@ -4,10 +4,10 @@ import static org.junit.Assert.*; import java.util.Random; -import jcgp.Parameters; import jcgp.Utilities; -import jcgp.function.Arithmetic; -import jcgp.function.FunctionSet; +import jcgp.modules.function.Arithmetic; +import jcgp.modules.function.FunctionSet; +import jcgp.parameters.Parameters; import jcgp.population.Chromosome; import jcgp.population.Population; -- cgit v1.2.3