diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-05-19 01:36:03 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-05-19 01:36:03 +0100 |
commit | f13ebf70037a6aa28dc53804c1f0cef6e2f23102 (patch) | |
tree | 2295186b9655207d036df6c871902a760743e993 | |
parent | 2a655a0b1654a2721089f0f8d99b846d7e198f38 (diff) |
Minor refactorings
-rw-r--r-- | src/jcgp/JCGP.java | 3 | ||||
-rw-r--r-- | src/jcgp/backend/function/Function.java | 2 | ||||
-rw-r--r-- | src/jcgp/backend/function/UnsignedInteger.java | 9 | ||||
-rw-r--r-- | src/jcgp/backend/modules/problem/DigitalCircuitProblem.java | 12 | ||||
-rw-r--r-- | src/jcgp/backend/modules/problem/Problem.java | 2 | ||||
-rw-r--r-- | src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java | 4 | ||||
-rw-r--r-- | src/jcgp/backend/modules/problem/TestCaseProblem.java | 16 | ||||
-rw-r--r-- | src/jcgp/backend/population/Chromosome.java | 4 | ||||
-rw-r--r-- | src/jcgp/backend/population/Node.java | 57 | ||||
-rw-r--r-- | src/jcgp/backend/tests/NodeTests.java | 2 | ||||
-rw-r--r-- | src/jcgp/gui/settings/testcase/TestCaseTable.java | 4 |
11 files changed, 42 insertions, 73 deletions
diff --git a/src/jcgp/JCGP.java b/src/jcgp/JCGP.java index 5a86303..99ed556 100644 --- a/src/jcgp/JCGP.java +++ b/src/jcgp/JCGP.java @@ -20,7 +20,6 @@ import jcgp.backend.parsers.TestCaseParser; import jcgp.backend.population.Population; import jcgp.backend.resources.Console; import jcgp.backend.resources.ModifiableResources; -import jcgp.backend.resources.Resources; import jcgp.backend.statistics.StatisticsLogger; /** @@ -234,7 +233,7 @@ public class JCGP { */ public void nextGeneration() { if (!finished) { - problem.evaluate(population, (Resources) resources); + problem.evaluate(population); if (resources.currentGeneration() < resources.generations()) { diff --git a/src/jcgp/backend/function/Function.java b/src/jcgp/backend/function/Function.java index 2e1f3c6..f277228 100644 --- a/src/jcgp/backend/function/Function.java +++ b/src/jcgp/backend/function/Function.java @@ -17,7 +17,7 @@ public abstract class Function { * @param args the function arguments. * @return the function result. */ - public abstract Object run(Object... args); + public abstract Object run(Object... arguments); /** * @return the arity of the function. diff --git a/src/jcgp/backend/function/UnsignedInteger.java b/src/jcgp/backend/function/UnsignedInteger.java index d23862c..553f934 100644 --- a/src/jcgp/backend/function/UnsignedInteger.java +++ b/src/jcgp/backend/function/UnsignedInteger.java @@ -39,15 +39,6 @@ public class UnsignedInteger { } /** - * Makes a new instance of UnsignedInteger with a specified value. - * - * @param i the value with which to initialise. - */ - public UnsignedInteger(Integer i) { - value = i; - } - - /** * Makes a new instance of UnsignedInteger from the string representation * of an unsigned integer. * diff --git a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java index e2f17c3..ff77a0d 100644 --- a/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java +++ b/src/jcgp/backend/modules/problem/DigitalCircuitProblem.java @@ -31,9 +31,9 @@ public class DigitalCircuitProblem extends TestCaseProblem<UnsignedInteger> { } @Override - public void evaluate(Population population, Resources resources) { + public void evaluate(Population population) { // for every chromosome in the population - for (int i = 0; i < resources.populationSize(); i++) { + for (int i = 0; i < getResources().populationSize(); i++) { // assume an initial fitness of 0 int fitness = 0; @@ -41,13 +41,13 @@ public class DigitalCircuitProblem extends TestCaseProblem<UnsignedInteger> { for (int t = 0; t < testCases.size(); t++) { population.get(i).setInputs((Object[]) testCases.get(t).getInputs()); // check each output - for (int o = 0; o < resources.outputs(); o++) { + for (int o = 0; o < getResources().outputs(); o++) { Integer output = ((UnsignedInteger) population.get(i).getOutput(o).calculate()).get(); - Integer matches = ~(output ^ testCases.get(t).getOutput(o).get()); + Integer matches = ~(output ^ testCases.get(t).getOutputs()[o].get()); // check only the relevant bits int bits; - if (resources.inputs() < 5) { - bits = (int) Math.pow(2.0, (double) resources.inputs()); + if (getResources().inputs() < 5) { + bits = (int) Math.pow(2.0, (double) getResources().inputs()); } else { bits = 32; } diff --git a/src/jcgp/backend/modules/problem/Problem.java b/src/jcgp/backend/modules/problem/Problem.java index 2af2373..6785733 100644 --- a/src/jcgp/backend/modules/problem/Problem.java +++ b/src/jcgp/backend/modules/problem/Problem.java @@ -74,7 +74,7 @@ public abstract class Problem extends Module { * @param population the population to be evaluated. * @param resources parameters and utilities for optional reference. */ - public abstract void evaluate(Population population, Resources resources); + public abstract void evaluate(Population population); /** * Used to assert whether a given population contains a perfect solution diff --git a/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java b/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java index 3b5f539..6bc4790 100644 --- a/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java +++ b/src/jcgp/backend/modules/problem/SymbolicRegressionProblem.java @@ -84,7 +84,7 @@ public class SymbolicRegressionProblem extends TestCaseProblem<Double> { } @Override - public void evaluate(Population population, Resources resources) { + public void evaluate(Population population) { // for every chromosome in the population for (int i = 0; i < getResources().populationSize(); i++) { // assume an initial fitness of 0 @@ -95,7 +95,7 @@ public class SymbolicRegressionProblem extends TestCaseProblem<Double> { // check each output for (int o = 0; o < getResources().outputs(); o++) { Double cgpValue = (Double) population.get(i).getOutput(o).calculate(); - Double dataValue = testCases.get(t).getOutput(o); + Double dataValue = testCases.get(t).getOutputs()[o]; if (hitsBasedFitness.get()) { if (Math.abs(cgpValue - dataValue) <= errorThreshold.get()) { fitness++; diff --git a/src/jcgp/backend/modules/problem/TestCaseProblem.java b/src/jcgp/backend/modules/problem/TestCaseProblem.java index 188e236..964860c 100644 --- a/src/jcgp/backend/modules/problem/TestCaseProblem.java +++ b/src/jcgp/backend/modules/problem/TestCaseProblem.java @@ -50,22 +50,6 @@ public abstract class TestCaseProblem<T> extends Problem { } /** - * @param index the index to return. - * @return the indexed input. - */ - public U getInput(int index) { - return inputs[index]; - } - - /** - * @param index the index to return. - * @return the indexed output. - */ - public U getOutput(int index) { - return outputs[index]; - } - - /** * @return the complete array of inputs. */ public U[] getInputs() { diff --git a/src/jcgp/backend/population/Chromosome.java b/src/jcgp/backend/population/Chromosome.java index e28032c..d84c5f0 100644 --- a/src/jcgp/backend/population/Chromosome.java +++ b/src/jcgp/backend/population/Chromosome.java @@ -112,13 +112,11 @@ public class Chromosome implements Comparable<Chromosome> { inputs[i] = new Input(i); } - int arity = resources.arity(); - // rows first nodes = new Node[(resources.rows())][(resources.columns())]; for (int r = 0; r < nodes.length; r++) { for (int c = 0; c < nodes[r].length; c++) { - nodes[r][c] = new Node(this, r, c, arity); + nodes[r][c] = new Node(this, r, c); } } outputs = new Output[resources.outputs()]; diff --git a/src/jcgp/backend/population/Node.java b/src/jcgp/backend/population/Node.java index 7712c50..6696694 100644 --- a/src/jcgp/backend/population/Node.java +++ b/src/jcgp/backend/population/Node.java @@ -40,24 +40,14 @@ public class Node implements Mutable, Connection { * @param chromosome the chromosome this node belongs to. * @param row the node's row. * @param column the node's column. - * @param arity the maximum arity of the experiment. */ - public Node(Chromosome chromosome, int row, int column, int arity) { + public Node(Chromosome chromosome, int row, int column) { this.chromosome = chromosome; this.column = column; this.row = row; } /** - * Sets the node function. - * - * @param newFunction the new function to set. - */ - public void setFunction(Function newFunction) { - function = newFunction; - } - - /** * Initialises the node with the specified values. * The number of connections passed as argument must * be exactly the same as the experiment arity, or @@ -66,7 +56,7 @@ public class Node implements Mutable, Connection { * @param newFunction the node function to set. * @param newConnections the node connections to set. */ - public void initialise(Function newFunction, Connection ... newConnections) { + public void initialise(Function newFunction, Connection... newConnections) { function = newFunction; if (newConnections.length == chromosome.getResources().arity()) { connections = newConnections; @@ -97,12 +87,37 @@ public class Node implements Mutable, Connection { } /** + * Sets the node function. + * + * @param newFunction the new function to set. + */ + public void setFunction(Function newFunction) { + function = newFunction; + } + + /** * @param index the connection to return. * @return the indexed connection. */ public Connection getConnection(int index) { return connections[index]; } + + /** + * This method sets the indexed connection to the specified new connection. + * If the given connection is null or disrespects levels back, it is discarded + * and no connections are changed. + * + * @param index the connection index to set. + * @param newConnection the {@code Connection} to connect to. + */ + public void setConnection(int index, Connection newConnection) { + // connection must not be null + if (newConnection != null) { + connections[index] = newConnection; + chromosome.recomputeActiveNodes(); + } + } /** * For package use, this is a recursive method @@ -127,24 +142,6 @@ public class Node implements Mutable, Connection { } } } - - /** - * This method sets the indexed connection to the specified new connection. - * If the given connection is null or disrespects levels back, it is discarded - * and no connections are changed. - * - * @param index the connection index to set. - * @param newConnection the {@code Connection} to connect to. - */ - public void setConnection(int index, Connection newConnection) { - // connection must not be null - if (newConnection != null) { - //if () { - connections[index] = newConnection; - chromosome.recomputeActiveNodes(); - //} - } - } @Override public boolean copyOf(Mutable element) { diff --git a/src/jcgp/backend/tests/NodeTests.java b/src/jcgp/backend/tests/NodeTests.java index ee940a0..4daa6d3 100644 --- a/src/jcgp/backend/tests/NodeTests.java +++ b/src/jcgp/backend/tests/NodeTests.java @@ -45,7 +45,7 @@ public class NodeTests { @Before public void setUp() throws Exception { - node = new Node(chromosome, 0, 0, resources.arity()); + node = new Node(chromosome, 0, 0); // make node with addition function and hard-coded value connections node.initialise(resources.getFunction(0), new Connection[]{new Connection() { diff --git a/src/jcgp/gui/settings/testcase/TestCaseTable.java b/src/jcgp/gui/settings/testcase/TestCaseTable.java index d4f789c..605b75e 100644 --- a/src/jcgp/gui/settings/testcase/TestCaseTable.java +++ b/src/jcgp/gui/settings/testcase/TestCaseTable.java @@ -62,7 +62,7 @@ public class TestCaseTable extends Stage { @Override public ObservableValue<String> call(CellDataFeatures<TestCase<Object>, String> param) { // create a new string property and give it the test case value, no need for dynamic binding - this wont change often - return new SimpleStringProperty(param.getValue().getInput(index).toString()); + return new SimpleStringProperty(param.getValue().getInputs()[index].toString()); } }); tc.setSortable(false); @@ -79,7 +79,7 @@ public class TestCaseTable extends Stage { @Override public ObservableValue<String> call(CellDataFeatures<TestCase<Object>, String> param) { // create a new string property and give it the test case value, no need for dynamic binding - this wont change often - return new SimpleStringProperty(param.getValue().getOutput(index).toString()); + return new SimpleStringProperty(param.getValue().getOutputs()[index].toString()); } }); tc.setSortable(false); |