aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/jcgp/CGP.java12
-rw-r--r--src/jcgp/Parameters.java10
-rw-r--r--src/jcgp/ea/EvolutionaryAlgorithm.java3
-rw-r--r--src/jcgp/ea/StandardEA.java14
-rw-r--r--src/jcgp/fitness/TruthTableEvaluator.java4
-rw-r--r--src/jcgp/function/Addition.java31
-rw-r--r--src/jcgp/function/Arithmetic.java123
-rw-r--r--src/jcgp/function/BitwiseLogic.java178
-rw-r--r--src/jcgp/function/BooleanLogic.java178
-rw-r--r--src/jcgp/function/Multiplication.java31
-rw-r--r--src/jcgp/function/Subtraction.java31
-rw-r--r--src/jcgp/population/Chromosome.java18
-rw-r--r--src/jcgp/population/Connection.java1
-rw-r--r--src/jcgp/population/Input.java5
-rw-r--r--src/jcgp/population/Node.java6
-rw-r--r--src/jcgp/population/Output.java6
-rw-r--r--src/jcgp/tests/ChromosomeTests.java5
-rw-r--r--src/jcgp/tests/NodeTests.java37
-rw-r--r--src/jcgp/tests/OutputTests.java12
-rw-r--r--src/jcgp/tests/PopulationTests.java5
20 files changed, 593 insertions, 117 deletions
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);