From 3fc4bf4a2ea7c6cff9810b76964dd3c1388c0b3f Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 11 Feb 2014 08:30:36 +0000 Subject: Updated README, made a few changes to Chromosome's mutation methods, will be working on that throughout the day --- README | 6 +++++ src/jcgp/Utilities.java | 4 ++++ src/jcgp/population/Chromosome.java | 46 +++++++++++++++++++++++++++++++++++++ src/jcgp/tests/ChromosomeTests.java | 6 ++++- 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README b/README index 6040391..3095efa 100644 --- a/README +++ b/README @@ -85,3 +85,9 @@ takes advantage of common Nodes to save memory. Additionally, this will require the chromosome and simply be triggered from the Mutator. This reduces the number of Utilities functions, which is not necessarily a problem. +7/2 + +The resource classes have been refactored so that tests can be implemented more easily. Parameters, Utilities and TruthTable +are now top-level classes and work independently from CGP, allowing them to be initialised for testing purposes. Some chromosome +tests have been written and more tests will be written in the next few days. + diff --git a/src/jcgp/Utilities.java b/src/jcgp/Utilities.java index 76f996e..f052595 100644 --- a/src/jcgp/Utilities.java +++ b/src/jcgp/Utilities.java @@ -29,6 +29,8 @@ public class Utilities { * This method may always pick inputs, as they can be picked * regardless of the column. * + * TODO remove? + * * @param chromosome the chromosome to pick from * @param column the column to use as reference * @return a random connection @@ -55,6 +57,8 @@ public class Utilities { * This method may always pick inputs, as they can be picked * regardless of the column. * + * TODO remove? + * * @param chromosome the chromosome to pick from * @param column the column to use as reference * @param levelsBack whether or not to respect levels back diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java index 195b95c..0dc3ff9 100644 --- a/src/jcgp/population/Chromosome.java +++ b/src/jcgp/population/Chromosome.java @@ -123,5 +123,51 @@ public class Chromosome { return nodes[index / Parameters.getColumns()][index % Parameters.getColumns()]; } } + + /** + * Returns a random allowed connection respecting levels back. + * This method may always pick inputs, as they can be picked + * regardless of the column. + * + * @param column the column to use as reference + * @return a random connection + */ + public Connection getRandomConnection(int column) { + + + + return null; + + } + + /** + * Returns a random allowed connection. + * + * This method may always pick inputs, as they can be picked + * regardless of the column. + * + * TODO optimise for less random generations + * + * @param column the column to use as reference + * @param levelsBack whether or not to respect levels back + * @return a random connection + */ + public Connection getRandomConnection(int column, boolean levelsBack) { + + if (levelsBack) { + return getRandomConnection(chromosome, column); + } else { + // choose input or node + int connectionType = Utilities.getRandomInt(inputs.length + (nodes.length * column)); + if (connectionType < inputs.length) { + // input + return chromosome.getInput(getRandomInt(Parameters.getInputs())); + } else { + // node + return chromosome.getNode(getRandomInt(Parameters.getRows()), getRandomInt(column)); + } + } + + } } diff --git a/src/jcgp/tests/ChromosomeTests.java b/src/jcgp/tests/ChromosomeTests.java index 75e3596..50d17b9 100644 --- a/src/jcgp/tests/ChromosomeTests.java +++ b/src/jcgp/tests/ChromosomeTests.java @@ -11,6 +11,7 @@ import jcgp.function.FunctionSet; import jcgp.function.Subtraction; import jcgp.population.Chromosome; import jcgp.population.Input; +import jcgp.population.MutableElement; import jcgp.population.Node; import jcgp.population.Output; @@ -90,7 +91,10 @@ public class ChromosomeTests { assertTrue("Incorrect output returned.", outputReturn); // get a mutable element, check that it is a Mutable - boolean mutableReturn = chromosome.getRandomMutableElement() instanceof Output; + boolean mutableReturn = chromosome.getRandomMutableElement() != chromosome.getRandomMutableElement() && chromosome.getRandomMutableElement() instanceof MutableElement; + assertTrue("Returned the same element.", mutableReturn); + + // set a fitness value, check if returned value is the same chromosome.setFitness(10); -- cgit v1.2.3