From 6e7747e5b85f4ca93683ed5166f6e480cc58e6fa Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 10 Feb 2014 09:33:54 +0000 Subject: Refactored the resources mechanics, implemented a few of the chromosome tests --- src/jcgp/tests/ChromosomeTests.java | 103 ++++++++++++++++++++++++++++++++++++ src/jcgp/tests/Tests.java | 22 -------- 2 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 src/jcgp/tests/ChromosomeTests.java delete mode 100644 src/jcgp/tests/Tests.java (limited to 'src/jcgp/tests') diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java new file mode 100644 index 0000000..123cbd1 --- /dev/null +++ b/src/jcgp/tests/ChromosomeTests.java @@ -0,0 +1,103 @@ +package jcgp.tests; + +import static org.junit.Assert.assertTrue; + +import java.util.Random; + +import jcgp.Parameters; +import jcgp.Utilities; +import jcgp.function.Addition; +import jcgp.function.FunctionSet; +import jcgp.function.Subtraction; +import jcgp.population.Chromosome; +import jcgp.population.Input; +import jcgp.population.Node; +import jcgp.population.Output; + +import org.junit.Before; +import org.junit.Test; + +/** + * + * Tests which cover the behaviour specified for a chromosome. + * + * - The chromosome should be able to return a specified node, input or output. + * - It should be able to return a MutableElement indexed by row and column, + * where column NodeCount returns outputs. + * - It should be able to return a Connection indexed by row and column, where + * column 0 returns inputs. + * - It should contain a freely modifiable fitness value. + * - It should be a good citizen - fully initialised upon instantiation. + * - It should feature a clone constructor, which creates a deep copy of a + * specified Chromosome object. + * - It should be able to return the number of active nodes. + * - For truth table evaluations, it should be able to have its inputs set. + * + * @author Eduardo Pedroni + * + */ +public class ChromosomeTests { + + Chromosome chromosome; + int inputCount = 3, + rows = 3, + columns = 3, + outputCount = 3; + + @Before + public void setUp() throws Exception { + // initialise function set + FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction()); + + // initialise utilities + Utilities.setResources(new Random(1234), functionSet); + + // initialise parameters + Parameters.setColumns(columns); + Parameters.setRows(rows); + Parameters.setInputs(inputCount); + Parameters.setOutputs(outputCount); + Parameters.setLevelsBack(1); + Parameters.setMutationRate(10); + Parameters.setTotalGenerations(100); + Parameters.setTotalRuns(5); + Parameters.setMaxArity(functionSet.getMaxArity()); + + chromosome = new Chromosome(inputCount, rows, columns, outputCount); + + } + + @Test + public void test() { + // pick arbitrary node, assume that if one node is right, all nodes are right + boolean nodeReturn = chromosome.getNode(1, 2).getColumn() == 2 + && chromosome.getNode(1, 2).getRow() == 1 + && chromosome.getNode(1, 2) instanceof Node; + assertTrue("Incorrect node returned.", nodeReturn); + + // set input values, check that acquired values are correct + chromosome.setInputs(4, 5, 6); + boolean inputReturn = chromosome.getInput(0).getValue() == 4 && chromosome.getInput(0) instanceof Input + && chromosome.getInput(1).getValue() == 5 && chromosome.getInput(0) instanceof Input + && chromosome.getInput(2).getValue() == 6 && chromosome.getInput(0) instanceof Input; + assertTrue("Incorrect inputs returned.", inputReturn); + + // connect outputs to inputs, check that calculated outputs return input values + for (int i = 0; i < outputCount; i++) { + chromosome.getOutput(i).setConnection(chromosome.getInput(0)); + } + boolean outputReturn = chromosome.getOutput(0).calculate() == 4 && chromosome.getOutput(0) instanceof Output + && chromosome.getOutput(1).calculate() == 4 && chromosome.getOutput(0) instanceof Output + && chromosome.getOutput(2).calculate() == 4 && chromosome.getOutput(0) instanceof Output; + assertTrue("Incorrect output returned.", outputReturn); + + // get a mutable element, that that it is a Mutable + boolean mutableReturn = chromosome.getMutable() instanceof Output; + + // set a fitness value, check if returned value is the same + chromosome.setFitness(10); + assertTrue("Incorrect fitness returned.", chromosome.getFitness() == 10); + + } + +} diff --git a/src/jcgp/tests/Tests.java b/src/jcgp/tests/Tests.java deleted file mode 100644 index 8fbf0be..0000000 --- a/src/jcgp/tests/Tests.java +++ /dev/null @@ -1,22 +0,0 @@ -package jcgp.tests; - -import static org.junit.Assert.fail; - -import org.junit.Before; -import org.junit.Test; - -public class Tests { - - @Before - public void setUp() throws Exception { - - - } - - @Test - public void test() { - fail("Not yet implemented"); - - } - -} -- cgit v1.2.3