diff options
| author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-14 18:13:21 +0000 | 
|---|---|---|
| committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-14 18:13:21 +0000 | 
| commit | afa484021ba94d12e98da682a9ff69c3837d5dbb (patch) | |
| tree | 2150c437cb74ccf141f8cfde228ebf5dd442314a | |
| parent | 6419b69faeb4736db1ccb50cfa5a030f9aa818aa (diff) | |
Generic data type functionality implemented. All tests were refactored to reflect this, and some chromosome tests were rewritten with more rigorous assertions.
| -rw-r--r-- | README | 10 | ||||
| -rw-r--r-- | src/jcgp/CGP.java | 12 | ||||
| -rw-r--r-- | src/jcgp/Parameters.java | 10 | ||||
| -rw-r--r-- | src/jcgp/ea/StandardEA.java | 5 | ||||
| -rw-r--r-- | src/jcgp/fitness/TestCase.java | 14 | ||||
| -rw-r--r-- | src/jcgp/fitness/TruthTableEvaluator.java | 1 | ||||
| -rw-r--r-- | src/jcgp/function/Addition.java | 14 | ||||
| -rw-r--r-- | src/jcgp/function/Function.java | 4 | ||||
| -rw-r--r-- | src/jcgp/function/InsufficientArgumentsException.java | 10 | ||||
| -rw-r--r-- | src/jcgp/function/InvalidArgumentsException.java | 20 | ||||
| -rw-r--r-- | src/jcgp/function/Subtraction.java | 14 | ||||
| -rw-r--r-- | src/jcgp/population/Chromosome.java | 2 | ||||
| -rw-r--r-- | src/jcgp/population/Connection.java | 2 | ||||
| -rw-r--r-- | src/jcgp/population/Input.java | 7 | ||||
| -rw-r--r-- | src/jcgp/population/Node.java | 2 | ||||
| -rw-r--r-- | src/jcgp/population/Output.java | 2 | ||||
| -rw-r--r-- | src/jcgp/tests/ChromosomeTests.java | 124 | ||||
| -rw-r--r-- | src/jcgp/tests/InputTests.java | 2 | ||||
| -rw-r--r-- | src/jcgp/tests/NodeTests.java | 99 | ||||
| -rw-r--r-- | src/jcgp/tests/OutputTests.java | 6 | ||||
| -rw-r--r-- | src/jcgp/tests/PopulationTests.java | 2 | 
21 files changed, 192 insertions, 170 deletions
| @@ -131,3 +131,13 @@ Minor update: Node now passes only the necessary number of arguments into its fu  connections as it knows which connections the function will use (based on arity). +14/2 + +Support for generic data types implemented.  + +Tests have been refactored to deal with the new data system. The system uses Object as its data type, allowing absolutely +any class to be used (and also primitive types). + + + + diff --git a/src/jcgp/CGP.java b/src/jcgp/CGP.java index 6cf5429..d2be161 100644 --- a/src/jcgp/CGP.java +++ b/src/jcgp/CGP.java @@ -24,9 +24,13 @@ public final class CGP {  	public CGP() {  		initialise(); -		fitnessFunction.evaluate(population); - -		ea.evolve(population, mutator); +		for (int i = 0; i < Parameters.getTotalGenerations(); i++) { +			Parameters.setCurrentGeneration(i); +			fitnessFunction.evaluate(population); +			ea.evolve(population, mutator); +		} +		 +		  	}  	/** @@ -51,7 +55,7 @@ public final class CGP {  		Parameters.setMaxArity(functionSet.getMaxArity());  		// initialise fitness function and truth table -		TruthTable.setTestCases(new TestCase(new int[] {2, 5, 4}, new int[] {1, 10, 15})); +		TruthTable.setTestCases(new TestCase(new Object[] {2, 5, 4}, new Object[] {1, 10, 15}));  		fitnessFunction = new TruthTableEvaluator();  		// initialise EA diff --git a/src/jcgp/Parameters.java b/src/jcgp/Parameters.java index 2a417db..26eb61c 100644 --- a/src/jcgp/Parameters.java +++ b/src/jcgp/Parameters.java @@ -5,7 +5,7 @@ public class Parameters {  	private static int rows, columns, inputs, outputs, levelsBack,   					   mutationRate, populationSize, totalGenerations,  					   currentGeneration = 0, totalRuns, currentRun = 0, -					   maxArity; +					   maxArity, maxFitness;  	public static int getRows() {  		return rows; @@ -58,6 +58,10 @@ public class Parameters {  	public static int getMaxArity() {  		return maxArity;  	} +	 +	public static int getMaxFitness() { +		return maxFitness; +	}  	public static void setRows(int rows) {  		Parameters.rows = rows; @@ -111,4 +115,8 @@ public class Parameters {  		Parameters.maxArity = maxArity;  	} +	public static void setMaxFitness(int maxFitness) { +		Parameters.maxFitness = maxFitness; +	} +	  } diff --git a/src/jcgp/ea/StandardEA.java b/src/jcgp/ea/StandardEA.java index f401d9c..b34c421 100644 --- a/src/jcgp/ea/StandardEA.java +++ b/src/jcgp/ea/StandardEA.java @@ -6,8 +6,11 @@ import jcgp.population.Population;  public class StandardEA implements EvolutionaryAlgorithm {  	@Override -	public void evolve(Population population, Mutator mutator) {		 +	public void evolve(Population population, Mutator mutator) {	 +		Chromosome select;  		for (Chromosome chromosome : population) { +			 +			  			mutator.mutate(chromosome);  		}  	} diff --git a/src/jcgp/fitness/TestCase.java b/src/jcgp/fitness/TestCase.java index 95129c6..4c10de7 100644 --- a/src/jcgp/fitness/TestCase.java +++ b/src/jcgp/fitness/TestCase.java @@ -4,10 +4,10 @@ import jcgp.Parameters;  public class TestCase { -	private int[] inputs; -	private int[] outputs; +	private Object[] inputs; +	private Object[] outputs; -	public TestCase(int[] inputs, int[] outputs) throws ParameterMismatchException { +	public TestCase(Object[] inputs, Object[] outputs) throws ParameterMismatchException {  		if (inputs.length == Parameters.getInputs()) {  			this.inputs = inputs;  		} else { @@ -22,19 +22,19 @@ public class TestCase {  	} -	public int getInput(int index) { +	public Object getInput(int index) {  		return inputs[index];  	} -	public int getOutput(int index) { +	public Object getOutput(int index) {  		return outputs[index];  	} -	public int[] getInputs() { +	public Object[] getInputs() {  		return inputs;  	} -	public int[] getOutputs() { +	public Object[] getOutputs() {  		return outputs;  	} diff --git a/src/jcgp/fitness/TruthTableEvaluator.java b/src/jcgp/fitness/TruthTableEvaluator.java index 987ea4c..4c26d60 100644 --- a/src/jcgp/fitness/TruthTableEvaluator.java +++ b/src/jcgp/fitness/TruthTableEvaluator.java @@ -19,6 +19,7 @@ public class TruthTableEvaluator implements FitnessFunction {  					}  				}  				chromosome.setFitness(fitness); +				System.out.println("Fitness: " + fitness);  			}  		}  	} diff --git a/src/jcgp/function/Addition.java b/src/jcgp/function/Addition.java index 45a8d35..f40bc24 100644 --- a/src/jcgp/function/Addition.java +++ b/src/jcgp/function/Addition.java @@ -3,19 +3,23 @@ package jcgp.function;  import jcgp.population.Connection;  public class Addition extends Function { +	 +	private int arity = 2;  	@Override -	public int run(Connection... connections) { -		if (connections.length > 0) { -			return connections[0].getValue() + connections[1].getValue(); +	public Object run(Connection... connections) { +		if (connections.length < arity) { +			throw new InvalidArgumentsException("Not enough connections were given."); +		} else if (connections[0].getValue() instanceof Integer) { +			return ((Integer) connections[0].getValue()) + ((Integer) connections[1].getValue());  		} else { -			throw new InsufficientArgumentsException(); +			throw new InvalidArgumentsException("Wrong data type, this function takes Integer.");  		}  	}  	@Override  	public int getArity() { -		return 2; +		return arity;  	}  } diff --git a/src/jcgp/function/Function.java b/src/jcgp/function/Function.java index 4900c68..118131a 100644 --- a/src/jcgp/function/Function.java +++ b/src/jcgp/function/Function.java @@ -4,8 +4,8 @@ import jcgp.population.Connection;  import jcgp.population.InsufficientConnectionsException;  public abstract class Function { -	 -	public abstract int run(Connection ... connections) throws InsufficientConnectionsException; +		 +	public abstract Object run(Connection ... connections) throws InsufficientConnectionsException;  	public abstract int getArity(); diff --git a/src/jcgp/function/InsufficientArgumentsException.java b/src/jcgp/function/InsufficientArgumentsException.java deleted file mode 100644 index a591b20..0000000 --- a/src/jcgp/function/InsufficientArgumentsException.java +++ /dev/null @@ -1,10 +0,0 @@ -package jcgp.function; - -public class InsufficientArgumentsException extends RuntimeException { - -	/** -	 *  -	 */ -	private static final long serialVersionUID = 2675108124600817777L; - -} diff --git a/src/jcgp/function/InvalidArgumentsException.java b/src/jcgp/function/InvalidArgumentsException.java new file mode 100644 index 0000000..cf55937 --- /dev/null +++ b/src/jcgp/function/InvalidArgumentsException.java @@ -0,0 +1,20 @@ +package jcgp.function; + +public class InvalidArgumentsException extends RuntimeException { + +	/** +	 *  +	 */ +	private static final long serialVersionUID = 2675108124600817777L; +	private String reason; +	 +	public InvalidArgumentsException(String reason) { +		super(); +		this.reason = reason; +	} +	 +	public String getReason() { +		return reason; +	} + +} diff --git a/src/jcgp/function/Subtraction.java b/src/jcgp/function/Subtraction.java index 8f107b1..d785614 100644 --- a/src/jcgp/function/Subtraction.java +++ b/src/jcgp/function/Subtraction.java @@ -4,18 +4,22 @@ import jcgp.population.Connection;  public class Subtraction extends Function { +	private int arity = 2; +  	@Override -	public int run(Connection... connections) { -		if (connections.length > 1) { -			return connections[0].getValue() - connections[1].getValue(); +	public Object run(Connection... connections) { +		if (connections.length < arity) { +			throw new InvalidArgumentsException("Not enough connections were given."); +		} else if (connections[0].getValue() instanceof Integer) { +			return ((Integer) connections[0].getValue()) - ((Integer) connections[1].getValue());  		} else { -			throw new InsufficientArgumentsException(); +			throw new InvalidArgumentsException("Wrong data type, this function takes Integer.");  		}  	}  	@Override  	public int getArity() { -		return 2; +		return arity;  	}  } diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index f61b780..12a8978 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -158,7 +158,7 @@ public class Chromosome {  	 * @param values  	 * @throws ParameterMismatchException  	 */ -	public void setInputs(int ... values) throws ParameterMismatchException { +	public void setInputs(Object ... values) throws ParameterMismatchException {  		// if the values provided don't match the specified number of inputs, the user should be warned  		if (values.length == inputs.length) {  			// set inputs for evaluation diff --git a/src/jcgp/population/Connection.java b/src/jcgp/population/Connection.java index 3312de3..ff11d7c 100644 --- a/src/jcgp/population/Connection.java +++ b/src/jcgp/population/Connection.java @@ -2,6 +2,6 @@ package jcgp.population;  public interface Connection { -	public int getValue(); +	public Object getValue();  } diff --git a/src/jcgp/population/Input.java b/src/jcgp/population/Input.java index ee008ce..2154dd9 100644 --- a/src/jcgp/population/Input.java +++ b/src/jcgp/population/Input.java @@ -2,18 +2,19 @@ package jcgp.population;  public class Input implements Connection { -	private int value = 0, index; +	private Object value = 0; +	private int index;  	public Input(int index) {  		this.index = index;  	} -	public void setValue(int newValue) { +	public void setValue(Object newValue) {  		value = newValue;  	}  	@Override -	public int getValue() { +	public Object getValue() {  		return value;  	} diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java index b2e29df..e58c1a4 100644 --- a/src/jcgp/population/Node.java +++ b/src/jcgp/population/Node.java @@ -23,7 +23,7 @@ public class Node implements MutableElement, Connection {  	}  	@Override -	public int getValue() { +	public Object getValue() {  		return function.run(Arrays.copyOfRange(connections, 0, function.getArity()));  	} diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java index f0bcbbf..dae7278 100644 --- a/src/jcgp/population/Output.java +++ b/src/jcgp/population/Output.java @@ -13,7 +13,7 @@ public class Output implements MutableElement {  		this.index = index;  	} -	public int calculate() { +	public Object calculate() {  		return source.getValue();  	} diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index d2407a6..8092d60 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -6,7 +6,6 @@ import java.util.Random;  import jcgp.Parameters;  import jcgp.Utilities; -import jcgp.fitness.ParameterMismatchException;  import jcgp.function.Addition;  import jcgp.function.FunctionSet;  import jcgp.function.Subtraction; @@ -31,8 +30,7 @@ import org.junit.Test;   *  - It should be able to return a random connection.   *  - It should contain a freely modifiable fitness value.   *  - For truth table evaluations, it should be able to have its inputs set. - *  - For truth table evaluations, the output should return a value according to what - *    it is set to. + *  - For truth table evaluations, the output should return a value according to the inputs.   *  - It should feature a clone constructor, which creates a deep copy of a    *    specified Chromosome object.   *  - It should be able to return a list of active nodes. @@ -40,7 +38,6 @@ import org.junit.Test;   *    to it.   *  - Same as above, but only looking at the active portion of a chromosome.   *   - *  TODO: bashing (strange value ranges, etc)   *     *  WARNING: changing parameters may cause the tests to incorrectly fail!   *  @@ -60,19 +57,19 @@ public class ChromosomeTests {  		Utilities.setResources(new Random(1234), functionSet);  		// initialise parameters -		Parameters.setColumns(30); -		Parameters.setRows(20); -		Parameters.setInputs(2); -		Parameters.setOutputs(4); -		Parameters.setLevelsBack(1);  		Parameters.setMutationRate(10); -		Parameters.setTotalGenerations(100); +		Parameters.setTotalGenerations(10);  		Parameters.setTotalRuns(5);  		Parameters.setMaxArity(functionSet.getMaxArity());  	}  	@Before  	public void setUp() throws Exception { +		Parameters.setColumns(10); +		Parameters.setRows(2); +		Parameters.setInputs(2); +		Parameters.setOutputs(4); +		Parameters.setLevelsBack(10);  		chromosome = new Chromosome();  	} @@ -81,7 +78,6 @@ public class ChromosomeTests {  	 */  	@Test  	public void cloneTest() { -		int[] testInputs;  		// create a clone, check to see if it really is a clone  		Chromosome clone = new Chromosome(chromosome); @@ -146,29 +142,26 @@ public class ChromosomeTests {  			}  		} -		// set input values -		testInputs = new int[Parameters.getInputs()]; -		for (int i = 0; i < Parameters.getInputs(); i++) { -			testInputs[i] = (i + 1) * 2 - 3; -		} -		chromosome.setInputs(testInputs); -		clone.setInputs(testInputs); +		// check cloning given a known topology +		chromosome = createKnownConfiguration(); +		clone = new Chromosome(chromosome); +		 +		Integer[] testInputs = new Integer[] {5, 8, 4}; +		chromosome.setInputs((Object[]) testInputs); +		clone.setInputs((Object[]) testInputs);  		// check that both chromosomes have the same outputs  		for (int i = 0; i < Parameters.getOutputs(); i++) { -			assertTrue("Incorrect output returned.", chromosome.getOutput(i).calculate() ==  -					clone.getOutput(i).calculate()); +			assertTrue("Incorrect output returned", ((Integer) chromosome.getOutput(i).calculate()) == ((Integer) clone.getOutput(i).calculate()));  		} -		// mutate an active node in clone, check that the same node in chromosome produces a different output -		// NOTE: given a small grid this mutation may actually pick the same connection (causing no effective change) and therefore the test would fail. -		Node node = clone.getActiveNodes().get(Utilities.getRandomInt(clone.getActiveNodes().size())); -		node.setConnection(clone.getRandomConnection(node.getColumn())); +		// mutate an output in clone, check that the same node in chromosome produces a different output +		clone.getOutput(1).setConnection(clone.getInput(2)); -		assertTrue("Mutation affected both nodes in both chromosomes.", node.getValue() != chromosome.getNode(node.getRow(), node.getColumn()).getValue()); +		assertTrue("Mutation affected nodes in both chromosomes.", +				clone.getOutput(1).calculate() != chromosome.getOutput(1).calculate());  	} -  	/**  	 *   	 */ @@ -217,7 +210,6 @@ public class ChromosomeTests {  		System.out.println(connectionOutOfRange + " nodes that disrespected levels back were picked.");  	} -  	/**  	 *   	 */ @@ -249,27 +241,28 @@ public class ChromosomeTests {  	 */  	@Test  	public void getOutputsTest() { -		// connect outputs to inputs, check that calculated outputs return input values -		for (int i = 0; i < Parameters.getOutputs(); i++) { -			chromosome.getOutput(i).setConnection(chromosome.getInput(i % Parameters.getInputs())); -			assertTrue("Incorrect output returned.", chromosome.getOutput(i).calculate() ==  -					chromosome.getInput(i % Parameters.getInputs()).getValue()); -		} +		chromosome = createKnownConfiguration(); + +		chromosome.setInputs(5, 8, 4); + +		// with this configuration, the outputs should be 13 and 25. +		assertTrue("Incorrect output returned.", (Integer) chromosome.getOutput(0).calculate() == 13); +		assertTrue("Incorrect output returned.", (Integer) chromosome.getOutput(1).calculate() == 25);  	}  	/** -	 * @throws ParameterMismatchException +	 *   	 */  	@Test -	public void setInputTest() throws ParameterMismatchException { +	public void setInputTest() {  		// set input values, check that acquired values are correct -		int[] testInputs = new int[Parameters.getInputs()]; +		Integer[] testInputs = new Integer[Parameters.getInputs()];  		for (int i = 0; i < Parameters.getInputs(); i++) {  			testInputs[i] = i * 2 - 3;  		} -		chromosome.setInputs(testInputs); +		chromosome.setInputs((Object[]) testInputs);  		for (int i = 0; i < Parameters.getInputs(); i++) { -			assertTrue("Incorrect input returned.", chromosome.getInput(i).getValue() == i * 2 - 3); +			assertTrue("Incorrect input returned.", ((Integer) chromosome.getInput(i).getValue()) == i * 2 - 3);  		}  	} @@ -294,20 +287,15 @@ public class ChromosomeTests {  	public void activeNodeTest() {  		// active node detection happens recursively, the user only calls a single method  		// set connections to a known configuration -		for (int i = 0; i < Parameters.getOutputs(); i++) { -			chromosome.getOutput(i).setConnection(chromosome.getNode(0, 0)); -		} - -		chromosome.getNode(0, 0).setConnection(chromosome.getInput(0)); - -		assertTrue("Active node not in list.", chromosome.getActiveNodes().contains(chromosome.getNode(0, 0))); - -		// change outputs, print list -		chromosome.getOutput(0).setConnection(chromosome.getNode(0, Parameters.getColumns() - 1)); - -		System.out.println("Active connections: " + chromosome.getActiveNodes().toString() + "\n"); +		chromosome = createKnownConfiguration(); +		 +		assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(0, 0))); +		assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(1, 1))); +		assertTrue("Active node missing from list.", chromosome.getActiveNodes().contains(chromosome.getNode(1, 2))); +		 +		assertTrue("List has the wrong number of nodes.", chromosome.getActiveNodes().size() == 3);  	} -	 +  	/**  	 *   	 */ @@ -317,12 +305,12 @@ public class ChromosomeTests {  		Chromosome c = new Chromosome(chromosome);  		assertTrue("Active nodes did not match.", chromosome.compareActiveTo(c));  		assertTrue("Symmetry not obeyed.", c.compareActiveTo(chromosome)); -		 +  		// create a new random chromosome, this time they should not match  		c = new Chromosome();  		assertTrue("Active nodes did match.", !chromosome.compareActiveTo(c));  	} -	 +  	/**  	 *   	 */ @@ -332,9 +320,37 @@ public class ChromosomeTests {  		Chromosome c = new Chromosome(chromosome);  		assertTrue("Chromosomes did not match.", chromosome.compareTo(c));  		assertTrue("Symmetry not obeyed.", c.compareTo(chromosome)); -		 +  		// create a new random chromosome, this time they should not match  		c = new Chromosome();  		assertTrue("Chromosomes did match.", !chromosome.compareTo(c));  	} +	/** +	 * Utility for creating a chromosome of known configuration. +	 * Topology is 3x3, with 3 inputs and 2 outputs. +	 * Given inputs 5, 8 and 4 outputs should be 13 and 25. +	 *  +	 * Active nodes (r, c): [0, 0], [1, 1], [1, 2] +	 *  +	 * @return the configured chromosome +	 */ +	private Chromosome createKnownConfiguration() { +		// with a small topology, build a chromosome of known connections and check outputs +		Parameters.setColumns(3); +		Parameters.setRows(3); +		Parameters.setInputs(3); +		Parameters.setOutputs(2); +		Parameters.setLevelsBack(3); + +		Chromosome c = new Chromosome(); + +		c.getNode(0, 0).initialise(Utilities.getFunction(0), c.getInput(0), c.getInput(1)); +		c.getNode(1, 1).initialise(Utilities.getFunction(0), c.getNode(0, 0), c.getInput(1)); +		c.getNode(1, 2).initialise(Utilities.getFunction(0), c.getNode(1, 1), c.getInput(2)); + +		c.getOutput(0).setConnection(c.getNode(0, 0)); +		c.getOutput(1).setConnection(c.getNode(1, 2)); + +		return c; +	}  } diff --git a/src/jcgp/tests/InputTests.java b/src/jcgp/tests/InputTests.java index 3803990..51d58d2 100644 --- a/src/jcgp/tests/InputTests.java +++ b/src/jcgp/tests/InputTests.java @@ -35,7 +35,7 @@ public class InputTests {  		// assign a value to input, check that the returned value is correct  		input.setValue(inputValue); -		assertTrue("Incorrect value returned.", input.getValue() == inputValue); +		assertTrue("Incorrect value returned.", ((Integer) input.getValue()) == inputValue);  	}  	@Test diff --git a/src/jcgp/tests/NodeTests.java b/src/jcgp/tests/NodeTests.java index 5b378d2..9ea6769 100644 --- a/src/jcgp/tests/NodeTests.java +++ b/src/jcgp/tests/NodeTests.java @@ -6,11 +6,11 @@ import java.util.Random;  import jcgp.Parameters;  import jcgp.Utilities; +import jcgp.function.Addition;  import jcgp.function.Function; -import jcgp.function.InsufficientArgumentsException; +import jcgp.function.Subtraction;  import jcgp.population.Chromosome;  import jcgp.population.Connection; -import jcgp.population.InsufficientConnectionsException;  import jcgp.population.Node;  import org.junit.Before; @@ -63,44 +63,25 @@ 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 Function() { -			private int arity = 2; +		node.initialise(new Addition(), +				new Connection[]{new Connection() { -			@Override -			public int run(Connection... connections) { - -				// add together the first n inputs if they exist, else throw exception -				if (connections.length >= arity) { -					return connections[0].getValue() + connections[1].getValue(); -				} else { -					throw new InsufficientArgumentsException(); -				} -			} +					@Override +					public Object getValue() { +						// hardcode a value +						return arg1; +					} -			@Override -			public int getArity() { -				// addition with arity 2 -				return arity; -			} +				}, +				new Connection() { -		}, new Connection[]{new Connection() { +					@Override +					public Object getValue() { +						// hardcode a value +						return arg2; +					} -			@Override -			public int getValue() { -				// hardcode a value -				return arg1; -			} - -		}, -		new Connection() { - -			@Override -			public int getValue() { -				// hardcode a value -				return arg2; -			} - -		}}); +				}});  	}  	@Test @@ -115,8 +96,7 @@ public class NodeTests {  		Function f = new Function() {  			@Override -			public int run(Connection... connections) -					throws InsufficientConnectionsException { +			public Object run(Connection... connections) {  				// blank  				return 0;  			} @@ -133,36 +113,19 @@ public class NodeTests {  		// check that the function returned by the node is f  		assertTrue("Incorrect function returned.", node.getFunction() == f);  		// check that it outputs 0 as it should -		assertTrue("Incorrect function output.", node.getValue() == 0); +		assertTrue("Incorrect function output.", ((Integer) node.getValue()) == 0);  	}  	@Test  	public void evaluationTest() {  		// check that addition is working -		assertTrue("Node did not return expected value (sum of arguments).", node.getValue() == arg1 + arg2); +		assertTrue("Node did not return expected value (sum of arguments). Output was: " + ((int) node.getValue()),  +				((int) node.getValue()) == arg1 + arg2);  		// put in a different function, check the output has changed appropriately -		node.setFunction(new Function() { +		node.setFunction(new Subtraction()); -			private int arity = 2; - -			@Override -			public int run(Connection... connections) throws InsufficientConnectionsException { -				// add together the first n inputs if they exist, else throw exception -				if (connections.length >= arity) { -					return connections[0].getValue() - connections[1].getValue(); -				} else { -					throw new InsufficientArgumentsException(); -				} -			} - -			@Override -			public int getArity() { -				return arity; -			} -		}); - -		assertTrue("Node did not return expected value (difference of arguments).", node.getValue() == arg1 - arg2); +		assertTrue("Node did not return expected value (difference of arguments).", ((Integer) node.getValue()) == arg1 - arg2);  	} @@ -173,7 +136,7 @@ public class NodeTests {  		conn0 = new Connection() {  			@Override -			public int getValue() { +			public Object getValue() {  				// blank  				return 0;  			} @@ -182,31 +145,31 @@ public class NodeTests {  		conn1 = new Connection() {  			@Override -			public int getValue() { +			public Object getValue() {  				// blank  				return 0;  			}  		};  		node.initialise(null, conn0, conn1); -		 +  		assertTrue("Connection 0 is incorrect.", node.getConnection(0) == conn0);  		assertTrue("Connection 1 is incorrect.", node.getConnection(1) == conn1); -		 +  		// make yet another connection, set it randomly, check that it is one of the node's connections  		conn2 = new Connection() {  			@Override -			public int getValue() { +			public Object getValue() {  				// blank  				return 0;  			}  		};  		node.setConnection(conn2); -		 +  		assertTrue("Connection was not found in node.", node.getConnection(0) == conn2 || node.getConnection(1) == conn2); -		 +  	} -	 +  } diff --git a/src/jcgp/tests/OutputTests.java b/src/jcgp/tests/OutputTests.java index 04eac7f..c877c03 100644 --- a/src/jcgp/tests/OutputTests.java +++ b/src/jcgp/tests/OutputTests.java @@ -64,13 +64,13 @@ public class OutputTests {  		output.setConnection(new Connection() {  			@Override -			public int getValue() { +			public Object getValue() {  				// test value  				return outputValue;  			}  		}); -		assertTrue("Incorrect evaluation.", output.calculate() == outputValue); +		assertTrue("Incorrect evaluation.", ((Integer) output.calculate()) == outputValue);  	}  	@Test @@ -79,7 +79,7 @@ public class OutputTests {  		Connection newConn = new Connection() {  			@Override -			public int getValue() { +			public Object getValue() {  				// blank  				return 0;  			} diff --git a/src/jcgp/tests/PopulationTests.java b/src/jcgp/tests/PopulationTests.java index a8f55a2..bb8fece 100644 --- a/src/jcgp/tests/PopulationTests.java +++ b/src/jcgp/tests/PopulationTests.java @@ -170,7 +170,5 @@ public class PopulationTests {  				}  			}  		} -  	} -  } | 
