From ef7a850a8f2f81ccaa07f25d9b7ad602e84d88c9 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 16 Feb 2014 22:41:46 +0000 Subject: Light refactoring, a good amount of testing done, added some new functions. --- README | 17 +++ src/jcgp/CGP.java | 12 +- src/jcgp/Parameters.java | 10 ++ src/jcgp/ea/EvolutionaryAlgorithm.java | 3 +- src/jcgp/ea/StandardEA.java | 14 ++- src/jcgp/fitness/TruthTableEvaluator.java | 4 +- src/jcgp/function/Addition.java | 31 ------ src/jcgp/function/Arithmetic.java | 123 +++++++++++++++++++++ src/jcgp/function/BitwiseLogic.java | 178 ++++++++++++++++++++++++++++++ src/jcgp/function/BooleanLogic.java | 178 ++++++++++++++++++++++++++++++ src/jcgp/function/Multiplication.java | 31 ------ src/jcgp/function/Subtraction.java | 31 ------ src/jcgp/population/Chromosome.java | 18 +++ src/jcgp/population/Connection.java | 1 + src/jcgp/population/Input.java | 5 + src/jcgp/population/Node.java | 6 + src/jcgp/population/Output.java | 6 +- src/jcgp/tests/ChromosomeTests.java | 5 +- src/jcgp/tests/NodeTests.java | 37 ++++++- src/jcgp/tests/OutputTests.java | 12 ++ src/jcgp/tests/PopulationTests.java | 5 +- 21 files changed, 610 insertions(+), 117 deletions(-) delete mode 100644 src/jcgp/function/Addition.java create mode 100644 src/jcgp/function/Arithmetic.java create mode 100644 src/jcgp/function/BitwiseLogic.java create mode 100644 src/jcgp/function/BooleanLogic.java delete mode 100644 src/jcgp/function/Multiplication.java delete mode 100644 src/jcgp/function/Subtraction.java diff --git a/README b/README index 5152bc8..b6ee389 100644 --- a/README +++ b/README @@ -146,3 +146,20 @@ for (1+λ) and (1,λ) EAs, for example. It no longer makes sense for Population the specification change. Population can still return an addressed chromosome in general, which it does by returning parents followed by offspring. The chromosome copyConnections method has been made public to facilitate the EA implementation. + + +16/2 + +Methods were added to Chromosome and Connection to allow the chromosome to be printed to the console. This way its behaviour can be verified to make +sure the fitness evaluations are working correctly and any perfect solution found really is producing perfect outputs. + +A test case has been calculated by hand from the printed chromosome, and indeed the system appears to be working. + +However, if the implemented run() in Function subclasses is set to print the operation it has carried out, the first function execution of each output +prints twice -> because it needs to check what it is to cast it. Very inefficient... +Instead, we'll attempt to cast it without checking, and a ClassCastException will be raised if a problem happens, telling the user they did something wrong. +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 diff --git a/src/jcgp/CGP.java b/src/jcgp/CGP.java index 0ccd74e..48273e9 100644 --- a/src/jcgp/CGP.java +++ b/src/jcgp/CGP.java @@ -9,10 +9,8 @@ import jcgp.ea.StandardMutator; import jcgp.fitness.FitnessFunction; import jcgp.fitness.TestCase; import jcgp.fitness.TruthTableEvaluator; -import jcgp.function.Addition; +import jcgp.function.Arithmetic; import jcgp.function.FunctionSet; -import jcgp.function.Multiplication; -import jcgp.function.Subtraction; import jcgp.population.Population; public final class CGP { @@ -30,7 +28,10 @@ public final class CGP { Parameters.setCurrentGeneration(i); fitnessFunction.evaluate(population); ea.evolve(population, mutator); - if (ea.getBestFitness() >= 3) { + if (ea.getFittestChromosome().getFitness() >= 3) { + if (Parameters.getDebug()) { + ea.getFittestChromosome().printNodes(); + } break; } } @@ -41,7 +42,7 @@ public final class CGP { */ private void initialise() { // initialise function set - FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction(), new Multiplication()); + FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction(), new Arithmetic.Multiplication()); // initialise utilities Utilities.setResources(new Random(1234), functionSet); @@ -57,6 +58,7 @@ public final class CGP { 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})); diff --git a/src/jcgp/Parameters.java b/src/jcgp/Parameters.java index 0a6abd9..cef1ea4 100644 --- a/src/jcgp/Parameters.java +++ b/src/jcgp/Parameters.java @@ -8,6 +8,8 @@ public class Parameters { currentGeneration = 0, totalRuns = 0, currentRun = 0, maxArity = 0, maxFitness = 0; + private static boolean debug = false; + public static int getRows() { return rows; } @@ -71,6 +73,10 @@ public class Parameters { public static int getMaxFitness() { return maxFitness; } + + public static boolean getDebug() { + return debug; + } public static void setRows(int rows) { Parameters.rows = rows; @@ -137,4 +143,8 @@ public class Parameters { Parameters.maxFitness = maxFitness; } + public static void setDebug(boolean debug) { + Parameters.debug = debug; + } + } diff --git a/src/jcgp/ea/EvolutionaryAlgorithm.java b/src/jcgp/ea/EvolutionaryAlgorithm.java index e084da0..ba70671 100644 --- a/src/jcgp/ea/EvolutionaryAlgorithm.java +++ b/src/jcgp/ea/EvolutionaryAlgorithm.java @@ -1,11 +1,12 @@ package jcgp.ea; +import jcgp.population.Chromosome; import jcgp.population.Population; public interface EvolutionaryAlgorithm { public abstract void evolve(Population population, Mutator mutator); - public abstract int getBestFitness(); + public abstract Chromosome getFittestChromosome(); } diff --git a/src/jcgp/ea/StandardEA.java b/src/jcgp/ea/StandardEA.java index 901333b..5f38513 100644 --- a/src/jcgp/ea/StandardEA.java +++ b/src/jcgp/ea/StandardEA.java @@ -13,7 +13,7 @@ import jcgp.population.Population; */ public class StandardEA implements EvolutionaryAlgorithm { - private int bestFitness = 0; + private Chromosome fittestChromosome; @Override public void evolve(Population population, Mutator mutator) { @@ -25,9 +25,11 @@ public class StandardEA implements EvolutionaryAlgorithm { fittest = i; } } - bestFitness = population.getChromosome(fittest).getFitness(); + fittestChromosome = population.getChromosome(fittest); population.setBestIndividual(fittest); - System.out.println("Best fitness: " + bestFitness); + 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++) { @@ -37,9 +39,9 @@ public class StandardEA implements EvolutionaryAlgorithm { } } } - + @Override - public int getBestFitness() { - return bestFitness; + public Chromosome getFittestChromosome() { + return fittestChromosome; } } diff --git a/src/jcgp/fitness/TruthTableEvaluator.java b/src/jcgp/fitness/TruthTableEvaluator.java index 2281b3f..4adf435 100644 --- a/src/jcgp/fitness/TruthTableEvaluator.java +++ b/src/jcgp/fitness/TruthTableEvaluator.java @@ -22,7 +22,9 @@ public class TruthTableEvaluator implements FitnessFunction { } } population.getChromosome(i).setFitness(fitness); - System.out.println("active nodes: " + population.getChromosome(i).getActiveNodes().size()); + if (Parameters.getDebug()) { + System.out.println("active nodes: " + population.getChromosome(i).getActiveNodes().size()); + } } } } diff --git a/src/jcgp/function/Addition.java b/src/jcgp/function/Addition.java deleted file mode 100644 index 3a8f123..0000000 --- a/src/jcgp/function/Addition.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class Addition 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 if (connections[0].getValue() instanceof Integer) { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 + arg2; - - System.out.println(arg1 + " + " + arg2 + " = " + result); - return result; - } else { - throw new InvalidArgumentsException("Wrong data type, this function takes Integer."); - } - } - - @Override - public int getArity() { - return arity; - } - -} diff --git a/src/jcgp/function/Arithmetic.java b/src/jcgp/function/Arithmetic.java new file mode 100644 index 0000000..b0bd5ca --- /dev/null +++ b/src/jcgp/function/Arithmetic.java @@ -0,0 +1,123 @@ +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 new file mode 100644 index 0000000..55f5df0 --- /dev/null +++ b/src/jcgp/function/BitwiseLogic.java @@ -0,0 +1,178 @@ +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 new file mode 100644 index 0000000..713dcf8 --- /dev/null +++ b/src/jcgp/function/BooleanLogic.java @@ -0,0 +1,178 @@ +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/Multiplication.java b/src/jcgp/function/Multiplication.java deleted file mode 100644 index 986faa8..0000000 --- a/src/jcgp/function/Multiplication.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class Multiplication 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 if (connections[0].getValue() instanceof Integer) { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 * arg2; - - System.out.println(arg1 + " * " + arg2 + " = " + result); - return result; - } else { - throw new InvalidArgumentsException("Wrong data type, this function takes Integer."); - } - } - - @Override - public int getArity() { - return arity; - } - -} diff --git a/src/jcgp/function/Subtraction.java b/src/jcgp/function/Subtraction.java deleted file mode 100644 index cfbb907..0000000 --- a/src/jcgp/function/Subtraction.java +++ /dev/null @@ -1,31 +0,0 @@ -package jcgp.function; - -import jcgp.exceptions.InvalidArgumentsException; -import jcgp.population.Connection; - -public class Subtraction 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 if (connections[0].getValue() instanceof Integer) { - Integer arg1 = ((Integer) connections[0].getValue()); - Integer arg2 = ((Integer) connections[1].getValue()); - Integer result = arg1 - arg2; - - System.out.println(arg1 + " - " + arg2 + " = " + result); - return result; - } else { - throw new InvalidArgumentsException("Wrong data type, this function takes Integer."); - } - } - - @Override - public int getArity() { - return arity; - } - -} diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index 3e61a10..996e765 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -300,4 +300,22 @@ public class Chromosome { } return false; } + + public void printNodes() { + for (int r = 0; r < Parameters.getRows(); r++) { + System.out.print("r: " + r + "\t"); + for (int c = 0; c < Parameters.getColumns(); c++) { + System.out.print("N: (" + r + ", " + c + ") "); + for (int i = 0; i < Parameters.getMaxArity(); i++) { + System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).getDescription() + ") "); + } + System.out.print("F: " + nodes[r][c].getFunction().toString() + "\t"); + } + System.out.print("\n"); + } + + for (int o = 0; o < Parameters.getOutputs(); o++) { + System.out.print("o: " + o + " (" + outputs[o].getSource().getDescription() + ")\t"); + } + } } diff --git a/src/jcgp/population/Connection.java b/src/jcgp/population/Connection.java index 751fe10..63a171e 100644 --- a/src/jcgp/population/Connection.java +++ b/src/jcgp/population/Connection.java @@ -4,5 +4,6 @@ public interface Connection { public Object getValue(); + public String getDescription(); } diff --git a/src/jcgp/population/Input.java b/src/jcgp/population/Input.java index e793bf6..5c545d6 100644 --- a/src/jcgp/population/Input.java +++ b/src/jcgp/population/Input.java @@ -21,4 +21,9 @@ public class Input implements Connection { public int getIndex() { return index; } + + @Override + public String getDescription() { + return "i: " + index; + } } diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java index 35d33cb..1741235 100644 --- a/src/jcgp/population/Node.java +++ b/src/jcgp/population/Node.java @@ -25,6 +25,7 @@ public class Node implements MutableElement, Connection { @Override public Object getValue() { + //System.out.print("Calculating node: (" + row + ", " + column + ") > "); return function.run(Arrays.copyOfRange(connections, 0, function.getArity())); } @@ -110,4 +111,9 @@ public class Node implements MutableElement, Connection { } return false; } + + @Override + public String getDescription() { + return "n: " + row + ", " + column; + } } diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java index b3ce74f..0df51e9 100644 --- a/src/jcgp/population/Output.java +++ b/src/jcgp/population/Output.java @@ -2,6 +2,8 @@ package jcgp.population; import java.util.ArrayList; +import jcgp.Parameters; + public class Output implements MutableElement { private Connection source; @@ -15,7 +17,9 @@ public class Output implements MutableElement { public Object calculate() { Object result = source.getValue(); - System.out.println("Output " + index + ": " + result); + if (Parameters.getDebug()) { + System.out.println("Output " + index + ": " + result); + } return result; } diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index c943da8..46463c5 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -6,9 +6,8 @@ import java.util.Random; import jcgp.Parameters; import jcgp.Utilities; -import jcgp.function.Addition; +import jcgp.function.Arithmetic; import jcgp.function.FunctionSet; -import jcgp.function.Subtraction; import jcgp.population.Chromosome; import jcgp.population.Connection; import jcgp.population.Input; @@ -50,7 +49,7 @@ public class ChromosomeTests { @BeforeClass public static void setUpBeforeClass() { // initialise function set - FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction()); + FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction()); // initialise utilities Utilities.setResources(new Random(1234), functionSet); diff --git a/src/jcgp/tests/NodeTests.java b/src/jcgp/tests/NodeTests.java index 9ea6769..ee26d38 100644 --- a/src/jcgp/tests/NodeTests.java +++ b/src/jcgp/tests/NodeTests.java @@ -6,9 +6,8 @@ import java.util.Random; import jcgp.Parameters; import jcgp.Utilities; -import jcgp.function.Addition; +import jcgp.function.Arithmetic; import jcgp.function.Function; -import jcgp.function.Subtraction; import jcgp.population.Chromosome; import jcgp.population.Connection; import jcgp.population.Node; @@ -63,7 +62,7 @@ public class NodeTests { public void setUp() throws Exception { node = new Node(chromosome, 0, 0); // make node with anonymous addition function and hard-coded value connections - node.initialise(new Addition(), + node.initialise(new Arithmetic.Addition(), new Connection[]{new Connection() { @Override @@ -72,6 +71,12 @@ public class NodeTests { return arg1; } + @Override + public String getDescription() { + // blank + return null; + } + }, new Connection() { @@ -81,6 +86,12 @@ public class NodeTests { return arg2; } + @Override + public String getDescription() { + // blank + return null; + } + }}); } @@ -123,7 +134,7 @@ public class NodeTests { ((int) node.getValue()) == arg1 + arg2); // put in a different function, check the output has changed appropriately - node.setFunction(new Subtraction()); + node.setFunction(new Arithmetic.Subtraction()); assertTrue("Node did not return expected value (difference of arguments).", ((Integer) node.getValue()) == arg1 - arg2); @@ -141,6 +152,12 @@ public class NodeTests { return 0; } + @Override + public String getDescription() { + // blank + return null; + } + }; conn1 = new Connection() { @@ -150,6 +167,12 @@ public class NodeTests { return 0; } + @Override + public String getDescription() { + // blank + return null; + } + }; node.initialise(null, conn0, conn1); @@ -164,6 +187,12 @@ public class NodeTests { // blank return 0; } + + @Override + public String getDescription() { + // blank + return null; + } }; node.setConnection(conn2); diff --git a/src/jcgp/tests/OutputTests.java b/src/jcgp/tests/OutputTests.java index c877c03..20e1c7f 100644 --- a/src/jcgp/tests/OutputTests.java +++ b/src/jcgp/tests/OutputTests.java @@ -68,6 +68,12 @@ public class OutputTests { // test value return outputValue; } + + @Override + public String getDescription() { + // blank + return null; + } }); assertTrue("Incorrect evaluation.", ((Integer) output.calculate()) == outputValue); @@ -83,6 +89,12 @@ public class OutputTests { // blank return 0; } + + @Override + public String getDescription() { + // blank + return null; + } }; output.setConnection(newConn); diff --git a/src/jcgp/tests/PopulationTests.java b/src/jcgp/tests/PopulationTests.java index b8639bf..a6ab141 100644 --- a/src/jcgp/tests/PopulationTests.java +++ b/src/jcgp/tests/PopulationTests.java @@ -6,9 +6,8 @@ import java.util.Random; import jcgp.Parameters; import jcgp.Utilities; -import jcgp.function.Addition; +import jcgp.function.Arithmetic; import jcgp.function.FunctionSet; -import jcgp.function.Subtraction; import jcgp.population.Chromosome; import jcgp.population.Population; @@ -39,7 +38,7 @@ public class PopulationTests { @BeforeClass public static void setUpBeforeClass() throws Exception { // initialise function set - FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction()); + FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction()); // initialise utilities Utilities.setResources(new Random(1234), functionSet); -- cgit v1.2.3