aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend')
-rw-r--r--src/jcgp/backend/modules/Module.java2
-rw-r--r--src/jcgp/backend/modules/es/MuPlusLambda.java22
-rw-r--r--src/jcgp/backend/modules/es/TournamentSelection.java10
-rw-r--r--src/jcgp/backend/modules/fitness/TestCaseProblem.java8
-rw-r--r--src/jcgp/backend/modules/mutator/PointMutator.java16
-rw-r--r--src/jcgp/backend/population/Chromosome.java52
-rw-r--r--src/jcgp/backend/population/Node.java2
-rw-r--r--src/jcgp/backend/population/Population.java14
-rw-r--r--src/jcgp/backend/resources/ModifiableResources.java193
-rw-r--r--src/jcgp/backend/resources/Resources.java260
-rw-r--r--src/jcgp/backend/resources/parameters/BooleanParameter.java27
-rw-r--r--src/jcgp/backend/resources/parameters/DoubleParameter.java25
-rw-r--r--src/jcgp/backend/resources/parameters/IntegerParameter.java26
-rw-r--r--src/jcgp/backend/resources/parameters/Parameter.java22
-rw-r--r--src/jcgp/backend/tests/ChromosomeTests.java54
-rw-r--r--src/jcgp/backend/tests/NodeTests.java12
-rw-r--r--src/jcgp/backend/tests/OutputTests.java8
-rw-r--r--src/jcgp/backend/tests/PopulationTests.java29
18 files changed, 480 insertions, 302 deletions
diff --git a/src/jcgp/backend/modules/Module.java b/src/jcgp/backend/modules/Module.java
index 8a95737..a6b4d73 100644
--- a/src/jcgp/backend/modules/Module.java
+++ b/src/jcgp/backend/modules/Module.java
@@ -4,6 +4,6 @@ import jcgp.backend.resources.parameters.Parameter;
public interface Module {
- public abstract Parameter[] getLocalParameters();
+ public abstract Parameter<?>[] getLocalParameters();
}
diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java
index 53fb932..67236f9 100644
--- a/src/jcgp/backend/modules/es/MuPlusLambda.java
+++ b/src/jcgp/backend/modules/es/MuPlusLambda.java
@@ -25,11 +25,11 @@ public class MuPlusLambda implements EvolutionaryStrategy {
public MuPlusLambda(final Resources resources) {
parents = new IntegerParameter(1, "Parents") {
@Override
- public void validate(int newValue) {
- if (newValue + offspring.get() != resources.getInt("popSize")) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() + offspring.get() != resources.populationSize()) {
status = ParameterStatus.INVALID;
status.setDetails("Parents + offspring must equal population size.");
- } else if (newValue <= 0) {
+ } else if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("EA needs at least 1 parent.");
} else {
@@ -39,11 +39,11 @@ public class MuPlusLambda implements EvolutionaryStrategy {
};
offspring = new IntegerParameter(4, "Offspring") {
@Override
- public void validate(int newValue) {
- if (newValue + parents.get() != resources.getInt("popSize")) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() + parents.get() != resources.populationSize()) {
status = ParameterStatus.INVALID;
status.setDetails("Parents + offspring must equal population size.");
- } else if (newValue <= 0) {
+ } else if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("EA needs at least 1 offspring.");
} else {
@@ -53,7 +53,7 @@ public class MuPlusLambda implements EvolutionaryStrategy {
};
report = new BooleanParameter(false, "Report") {
@Override
- public void validate(boolean newValue) {
+ public void validate(Boolean newValue) {
// nothing
}
};
@@ -65,7 +65,7 @@ public class MuPlusLambda implements EvolutionaryStrategy {
fittestChromosome = 0;
if (report.get()) resources.reportln("[ES] Selecting fittest chromosome...");
- for (int i = 0; i < resources.getInt("popSize"); i++) {
+ for (int i = 0; i < resources.populationSize(); i++) {
if (report.get()) resources.reportln("[ES] Chromosome " + i + ", fitness: " + population.getChromosome(i).getFitness());
if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) {
fittestChromosome = i;
@@ -74,7 +74,7 @@ public class MuPlusLambda implements EvolutionaryStrategy {
if (report.get()) resources.reportln("[ES] Selected chromosome: " + fittestChromosome);
// create copies of fittest chromosome, mutate them
- for (int i = 0; i < resources.getInt("popSize"); i++) {
+ for (int i = 0; i < resources.populationSize(); i++) {
if (i != fittestChromosome) {
if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i);
population.copyChromosome(fittestChromosome, i);
@@ -83,6 +83,8 @@ public class MuPlusLambda implements EvolutionaryStrategy {
}
}
+ if (report.get()) resources.reportln("[ES] Generation is complete.");
+
}
@Override
@@ -91,7 +93,7 @@ public class MuPlusLambda implements EvolutionaryStrategy {
}
@Override
- public Parameter[] getLocalParameters() {
+ public Parameter<?>[] getLocalParameters() {
return new Parameter[] {parents, offspring, report};
}
diff --git a/src/jcgp/backend/modules/es/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java
index 3954de8..8286101 100644
--- a/src/jcgp/backend/modules/es/TournamentSelection.java
+++ b/src/jcgp/backend/modules/es/TournamentSelection.java
@@ -1,7 +1,5 @@
package jcgp.backend.modules.es;
-import java.util.HashMap;
-
import jcgp.backend.modules.mutator.Mutator;
import jcgp.backend.population.Population;
import jcgp.backend.resources.Resources;
@@ -13,22 +11,18 @@ public class TournamentSelection implements EvolutionaryStrategy {
private int fittestChromosome;
private IntegerParameter tournament;
- private HashMap<String, Parameter> localParameters;
public TournamentSelection() {
tournament = new IntegerParameter(1, "Tournament size") {
@Override
- public void validate(int newValue) {
+ public void validate(Number newValue) {
// TODO this
}
};
-
- localParameters = new HashMap<String, Parameter>();
- localParameters.put("tournament", tournament);
}
@Override
- public Parameter[] getLocalParameters() {
+ public Parameter<?>[] getLocalParameters() {
return new Parameter[] {tournament};
}
diff --git a/src/jcgp/backend/modules/fitness/TestCaseProblem.java b/src/jcgp/backend/modules/fitness/TestCaseProblem.java
index 4259285..7dd24af 100644
--- a/src/jcgp/backend/modules/fitness/TestCaseProblem.java
+++ b/src/jcgp/backend/modules/fitness/TestCaseProblem.java
@@ -56,7 +56,7 @@ public abstract class TestCaseProblem<U> extends Problem {
maxFitness = new IntegerParameter(0, "Max fitness", true, false) {
@Override
- public void validate(int newValue) {
+ public void validate(Number newValue) {
// blank
}
};
@@ -67,14 +67,14 @@ public abstract class TestCaseProblem<U> extends Problem {
@Override
public void evaluate(Population population, Resources resources) {
// for every chromosome in the population
- for (int i = 0; i < resources.getInt("popSize"); i++) {
+ for (int i = 0; i < resources.populationSize(); i++) {
// assume an initial fitness of 0
int fitness = 0;
// for each test case
for (int t = 0; t < testCases.size(); t++) {
population.getChromosome(i).setInputs(testCases.get(t).getInputs());
// check each output
- for (int o = 0; o < resources.getInt("outputs"); o++) {
+ for (int o = 0; o < resources.outputs(); o++) {
if (population.getChromosome(i).getOutput(o).calculate() == testCases.get(t).getOutput(o)) {
fitness++;
}
@@ -86,7 +86,7 @@ public abstract class TestCaseProblem<U> extends Problem {
}
@Override
- public Parameter[] getLocalParameters() {
+ public Parameter<?>[] getLocalParameters() {
return new Parameter[]{maxFitness};
}
diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java
index 1f38cfe..ab8efad 100644
--- a/src/jcgp/backend/modules/mutator/PointMutator.java
+++ b/src/jcgp/backend/modules/mutator/PointMutator.java
@@ -18,11 +18,11 @@ public class PointMutator implements Mutator {
public PointMutator(final Resources resources) {
mutationRate = new DoubleParameter(50, "Percent mutation", false, false) {
@Override
- public void validate(double newValue) {
- if (newValue <= 0 || newValue > 100) {
+ public void validate(Number newValue) {
+ if (newValue.doubleValue() <= 0 || newValue.doubleValue() > 100) {
status = ParameterStatus.INVALID;
status.setDetails("Mutation rate must be > 0 and <= 100");
- } else if ((int) ((newValue / 100) * resources.getDouble("nodes")) <= 0) {
+ } else if ((int) ((newValue.doubleValue() / 100) * (double) resources.nodes()) <= 0) {
status = ParameterStatus.WARNING;
status.setDetails("With mutation rate " + mutationRate.get() + ", 0 genes will be mutated.");
} else {
@@ -33,7 +33,7 @@ public class PointMutator implements Mutator {
report = new BooleanParameter(false, "Report") {
@Override
- public void validate(boolean newValue) {
+ public void validate(Boolean newValue) {
// blank
}
};
@@ -41,7 +41,7 @@ public class PointMutator implements Mutator {
@Override
public void mutate(Chromosome chromosome, Resources resources) {
- int mutations = (int) ((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("outputs")))) / 100));
+ int mutations = (int) ((mutationRate.get()) * (((((double) resources.nodes() + resources.outputs()))) / 100));
if (report.get()) resources.reportln("[Mutator] Number of mutations to be performed: " + mutations);
for (int i = 0; i < mutations; i++) {
MutableElement m = chromosome.getRandomMutableElement();
@@ -54,7 +54,7 @@ public class PointMutator implements Mutator {
if (report.get()) resources.reportln("to " + ((Output) m).getSource().toString());
} else if (m instanceof Node) {
- int geneType = resources.getRandomInt(1 + resources.getInt("arity"));
+ int geneType = resources.getRandomInt(1 + resources.arity());
if (geneType < 1) {
if (report.get()) resources.report("changed function from " + ((Node) m).getFunction().getName() + " ");
@@ -62,7 +62,7 @@ public class PointMutator implements Mutator {
if (report.get()) resources.reportln("to " + ((Node) m).getFunction().getName());
} else {
- int connection = resources.getRandomInt(resources.getInt("arity"));
+ int connection = resources.getRandomInt(resources.arity());
if (report.get()) resources.report("changed connection " + connection + " from " + ((Node) m).getConnection(connection) + " ");
m.setConnection(connection, chromosome.getRandomConnection(((Node) m).getColumn()));
@@ -74,7 +74,7 @@ public class PointMutator implements Mutator {
}
@Override
- public Parameter[] getLocalParameters() {
+ public Parameter<?>[] getLocalParameters() {
return new Parameter[] {mutationRate, report};
}
diff --git a/src/jcgp/backend/population/Chromosome.java b/src/jcgp/backend/population/Chromosome.java
index bbada14..71e19ec 100644
--- a/src/jcgp/backend/population/Chromosome.java
+++ b/src/jcgp/backend/population/Chromosome.java
@@ -31,7 +31,7 @@ public class Chromosome {
// allocate memory for all elements of the chromosome
instantiateElements();
// set random connections so that the chromosome can be evaluated
- initialiseConnections();
+ reinitialiseConnections();
}
/**
@@ -55,22 +55,22 @@ public class Chromosome {
*
*/
private void instantiateElements() {
- inputs = new Input[(resources.getInt("inputs"))];
- for (int i = 0; i < (resources.getInt("inputs")); i++) {
+ inputs = new Input[(resources.inputs())];
+ for (int i = 0; i < inputs.length; i++) {
inputs[i] = new Input(i);
}
- int arity = resources.getInt("arity");
+ int arity = resources.arity();
// rows first
- nodes = new Node[(resources.getInt("rows"))][(resources.getInt("columns"))];
- for (int r = 0; r < (resources.getInt("rows")); r++) {
- for (int c = 0; c < (resources.getInt("columns")); c++) {
+ 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);
}
}
- outputs = new Output[(resources.getInt("outputs"))];
- for (int o = 0; o < (resources.getInt("outputs")); o++) {
+ outputs = new Output[resources.outputs()];
+ for (int o = 0; o < outputs.length; o++) {
outputs[o] = new Output(this, o);
}
}
@@ -78,9 +78,9 @@ public class Chromosome {
/**
*
*/
- private void initialiseConnections() {
+ public void reinitialiseConnections() {
- int arity = resources.getInt("arity");
+ int arity = resources.arity();
// initialise nodes - [rows][columns]
for (int r = 0; r < nodes.length; r++) {
@@ -103,7 +103,7 @@ public class Chromosome {
* @param clone
*/
public void copyGenes(Chromosome clone) {
- int arity = resources.getInt("arity");
+ int arity = resources.arity();
// copy nodes - [rows][columns]
for (int r = 0; r < nodes.length; r++) {
@@ -164,6 +164,7 @@ public class Chromosome {
/**
*
+ *
* @param values
* @throws ParameterMismatchException
*/
@@ -186,7 +187,7 @@ public class Chromosome {
*/
public MutableElement getRandomMutableElement() {
// choose output or node
- int index = resources.getRandomInt(outputs.length + (resources.getInt("rows")) * (resources.getInt("columns")));
+ int index = resources.getRandomInt(outputs.length + (resources.rows() * resources.columns()));
if (index < outputs.length) {
// outputs
@@ -194,7 +195,7 @@ public class Chromosome {
} else {
// node
index -= outputs.length;
- return nodes[index / (resources.getInt("columns"))][index % (resources.getInt("columns"))];
+ return nodes[index / resources.columns()][index % resources.columns()];
}
}
@@ -208,7 +209,7 @@ public class Chromosome {
*/
public Connection getRandomConnection(int column) {
// work out the allowed range obeying levels back
- int allowedColumns = ((column >= (resources.getInt("levelsBack"))) ? (resources.getInt("levelsBack")) : column);
+ int allowedColumns = column >= resources.levelsBack() ? resources.levelsBack() : column;
int offset = ((column - allowedColumns) * nodes.length) - inputs.length;
// choose input or allowed node
@@ -235,14 +236,14 @@ public class Chromosome {
*/
public Connection getRandomConnection() {
// choose output or node
- int index = resources.getRandomInt(inputs.length + (resources.getInt("columns")) * (resources.getInt("rows")));
+ int index = resources.getRandomInt(inputs.length + (resources.columns() * resources.rows()));
if (index < inputs.length) {
// outputs
return inputs[index];
} else {
// node
index -= inputs.length;
- return nodes[index / (resources.getInt("columns"))][index % (resources.getInt("columns"))];
+ return nodes[index / resources.columns()][index % resources.columns()];
}
}
@@ -273,19 +274,18 @@ public class Chromosome {
output.getActiveNodes(activeNodes);
}
}
-
}
public boolean compareTo(Chromosome chromosome) {
- for (int r = 0; r < (resources.getInt("rows")); r++) {
- for (int c = 0; c < (resources.getInt("columns")); c++) {
+ for (int r = 0; r < resources.rows(); r++) {
+ for (int c = 0; c < resources.columns(); c++) {
if (!(nodes[r][c].copyOf(chromosome.getNode(r, c)))) {
return false;
}
}
}
- for (int o = 0; o < (resources.getInt("outputs")); o++) {
+ for (int o = 0; o < resources.outputs(); o++) {
if (!(outputs[o].copyOf(chromosome.getOutput(o)))) {
return false;
}
@@ -311,11 +311,11 @@ public class Chromosome {
public void printNodes() {
// TODO make this proper
- int arity = resources.getInt("arity");
+ int arity = resources.arity();
- for (int r = 0; r < (resources.getInt("rows")); r++) {
+ for (int r = 0; r < resources.rows(); r++) {
System.out.print("r: " + r + "\t");
- for (int c = 0; c < (resources.getInt("columns")); c++) {
+ for (int c = 0; c < resources.columns(); c++) {
System.out.print("N: (" + r + ", " + c + ") ");
for (int i = 0; i < arity; i++) {
System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).toString() + ") ");
@@ -325,9 +325,11 @@ public class Chromosome {
System.out.print("\n");
}
- for (int o = 0; o < (resources.getInt("outputs")); o++) {
+ for (int o = 0; o < resources.outputs(); o++) {
System.out.print("o: " + o + " (" + outputs[o].getSource().toString() + ")\t");
}
+
+ System.out.println();
}
public Resources getResources() {
diff --git a/src/jcgp/backend/population/Node.java b/src/jcgp/backend/population/Node.java
index 87a2f99..6a558a4 100644
--- a/src/jcgp/backend/population/Node.java
+++ b/src/jcgp/backend/population/Node.java
@@ -37,7 +37,7 @@ public class Node extends Gene implements MutableElement, Connection {
public void initialise(Function newFunction, Connection ... newConnections) throws InsufficientConnectionsException {
function = newFunction;
- if (newConnections.length == chromosome.getResources().getInt("arity")) {
+ if (newConnections.length == chromosome.getResources().arity()) {
connections = newConnections;
} else {
throw new InsufficientConnectionsException();
diff --git a/src/jcgp/backend/population/Population.java b/src/jcgp/backend/population/Population.java
index d2e6058..fdeec82 100644
--- a/src/jcgp/backend/population/Population.java
+++ b/src/jcgp/backend/population/Population.java
@@ -5,7 +5,7 @@ import jcgp.backend.resources.Resources;
public class Population {
private Chromosome[] chromosomes;
- private Resources resources;
+ private final Resources resources;
/**
* Initialise a random population according to the parameters specified
@@ -16,7 +16,7 @@ public class Population {
public Population(Resources resources) {
this.resources = resources;
- chromosomes = new Chromosome[(resources.getInt("popSize"))];
+ chromosomes = new Chromosome[resources.populationSize()];
for (int c = 0; c < chromosomes.length; c++) {
chromosomes[c] = new Chromosome(resources);
}
@@ -31,8 +31,7 @@ public class Population {
public Population(Chromosome parent, Resources resources) {
this.resources = resources;
- chromosomes = new Chromosome[(resources.getInt("popSize"))];
- // generate the rest of the individuals
+ chromosomes = new Chromosome[resources.populationSize()];
for (int c = 0; c < chromosomes.length; c++) {
chromosomes[c] = new Chromosome(parent);
}
@@ -72,7 +71,10 @@ public class Population {
}
}
-
-
+ public void reinitialise() {
+ for (int c = 0; c < chromosomes.length; c++) {
+ chromosomes[c].reinitialiseConnections();
+ }
+ }
}
diff --git a/src/jcgp/backend/resources/ModifiableResources.java b/src/jcgp/backend/resources/ModifiableResources.java
index a221f73..689f846 100644
--- a/src/jcgp/backend/resources/ModifiableResources.java
+++ b/src/jcgp/backend/resources/ModifiableResources.java
@@ -1,8 +1,6 @@
package jcgp.backend.resources;
import jcgp.backend.function.FunctionSet;
-import jcgp.backend.resources.parameters.BooleanParameter;
-import jcgp.backend.resources.parameters.DoubleParameter;
import jcgp.backend.resources.parameters.IntegerParameter;
/**
@@ -21,19 +19,191 @@ public class ModifiableResources extends Resources {
super();
}
- public void set(String key, Object value) {
- if (parameters.get(key) instanceof IntegerParameter) {
- ((IntegerParameter) parameters.get(key)).set(((Integer) value).intValue());
- } else if (parameters.get(key) instanceof DoubleParameter) {
- ((DoubleParameter) parameters.get(key)).set(((Double) value).doubleValue());
- } else if (parameters.get(key) instanceof BooleanParameter) {
- ((BooleanParameter) parameters.get(key)).set(((Boolean) value).booleanValue());
- }
+ /**
+ * @param rows the rows to set
+ */
+ public void setRows(int rows) {
+ this.rows.set(rows);
+ }
+
+ /**
+ * @param columns the columns to set
+ */
+ public void setColumns(int columns) {
+ this.columns.set(columns);
+ }
+
+ /**
+ * @param inputs the inputs to set
+ */
+ public void setInputs(int inputs) {
+ this.inputs.set(inputs);
+ }
+
+ /**
+ * @param outputs the outputs to set
+ */
+ public void setOutputs(int outputs) {
+ this.outputs.set(outputs);
+ }
+
+ /**
+ * @param populationSize the populationSize to set
+ */
+ public void setPopulationSize(int populationSize) {
+ this.populationSize.set(populationSize);
+ }
+
+ /**
+ * @param levelsBack the levelsBack to set
+ */
+ public void setLevelsBack(int levelsBack) {
+ this.levelsBack.set(levelsBack);
+ }
+
+ /**
+ * @param currentGeneration the currentGeneration to set
+ */
+ public void setCurrentGeneration(int currentGeneration) {
+ this.currentGeneration.set(currentGeneration);
+ }
+
+ /**
+ * @param generations the generations to set
+ */
+ public void setGenerations(int generations) {
+ this.generations.set(generations);
+ }
+
+ /**
+ * @param currentRun the currentRun to set
+ */
+ public void setCurrentRun(int currentRun) {
+ this.currentRun.set(currentRun);
+ }
+
+ /**
+ * @param runs the runs to set
+ */
+ public void setRuns(int runs) {
+ this.runs.set(runs);
+ }
+
+ /**
+ * @param arity the arity to set
+ */
+ public void setArity(int arity) {
+ this.arity.set(arity);
+ }
+
+ /**
+ * @param seed the seed to set
+ */
+ public void setSeed(int seed) {
+ this.seed.set(seed);
+ }
+
+ /**
+ * @param report the report to set
+ */
+ public void setReport(int report) {
+ this.report.set(report);
+ }
+
+ /**
+ * @return the rows
+ */
+ public IntegerParameter getRowsParameter() {
+ return rows;
+ }
+
+ /**
+ * @return the columns
+ */
+ public IntegerParameter getColumnsParameter() {
+ return columns;
+ }
+
+ /**
+ * @return the inputs
+ */
+ public IntegerParameter getInputsParameter() {
+ return inputs;
+ }
+
+ /**
+ * @return the outputs
+ */
+ public IntegerParameter getOutputsParameter() {
+ return outputs;
+ }
+
+ /**
+ * @return the populationSize
+ */
+ public IntegerParameter getPopulationSizeParameter() {
+ return populationSize;
+ }
+
+ /**
+ * @return the levelsBack
+ */
+ public IntegerParameter getLevelsBackParameter() {
+ return levelsBack;
+ }
+
+ /**
+ * @return the currentGeneration
+ */
+ public IntegerParameter getCurrentGenerationParameter() {
+ return currentGeneration;
+ }
+
+ /**
+ * @return the generations
+ */
+ public IntegerParameter getGenerationsParameter() {
+ return generations;
+ }
+
+ /**
+ * @return the currentRun
+ */
+ public IntegerParameter getCurrentRunParameter() {
+ return currentRun;
+ }
+
+ /**
+ * @return the runs
+ */
+ public IntegerParameter getRunsParameter() {
+ return runs;
+ }
+
+ /**
+ * @return the arity
+ */
+ public IntegerParameter getArityParameter() {
+ return arity;
+ }
+
+ /**
+ * @return the seed
+ */
+ public IntegerParameter getSeedParameter() {
+ return seed;
+ }
+
+ /**
+ * @return the report
+ */
+ public IntegerParameter getReportParameter() {
+ return report;
}
public void setFunctionSet(FunctionSet functionSet) {
this.functionSet = functionSet;
- set("arity", functionSet.getMaxArity());
+ setArity(functionSet.getMaxArity());
}
public void setConsole(Console console) {
@@ -56,5 +226,4 @@ public class ModifiableResources extends Resources {
console.print(s);
}
}
-
}
diff --git a/src/jcgp/backend/resources/Resources.java b/src/jcgp/backend/resources/Resources.java
index 13e0c51..c83fa35 100644
--- a/src/jcgp/backend/resources/Resources.java
+++ b/src/jcgp/backend/resources/Resources.java
@@ -1,16 +1,12 @@
package jcgp.backend.resources;
-import java.util.HashMap;
import java.util.Random;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import jcgp.backend.function.Function;
import jcgp.backend.function.FunctionSet;
-import jcgp.backend.resources.parameters.BooleanParameter;
-import jcgp.backend.resources.parameters.DoubleParameter;
import jcgp.backend.resources.parameters.IntegerParameter;
-import jcgp.backend.resources.parameters.Parameter;
import jcgp.backend.resources.parameters.ParameterStatus;
/**
@@ -23,12 +19,12 @@ import jcgp.backend.resources.parameters.ParameterStatus;
*
*/
public class Resources {
- protected HashMap<String, Parameter> parameters = new HashMap<String, Parameter>();
+ protected IntegerParameter rows, columns, inputs, outputs, populationSize,
+ levelsBack, currentGeneration, generations, currentRun, runs,
+ arity, seed, report;
protected Random numberGenerator = new Random();
-
protected FunctionSet functionSet;
-
// GUI console
protected Console console;
@@ -36,175 +32,234 @@ public class Resources {
createBaseParameters();
}
- public int getInt(String key) {
- if (parameters.get(key) instanceof IntegerParameter) {
- return ((IntegerParameter) parameters.get(key)).get();
- } else if (parameters.get(key) instanceof DoubleParameter) {
- return (int) ((DoubleParameter) parameters.get(key)).get();
- } else {
- throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to int.");
- }
+ /**
+ * @return the rows
+ */
+ public int rows() {
+ return rows.get();
}
-
- public double getDouble(String key) {
- if (parameters.get(key) instanceof IntegerParameter) {
- return ((IntegerParameter) parameters.get(key)).get();
- } else if (parameters.get(key) instanceof DoubleParameter) {
- return ((DoubleParameter) parameters.get(key)).get();
- } else {
- throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to double.");
- }
+
+ /**
+ * @return the columns
+ */
+ public int columns() {
+ return columns.get();
}
-
- public boolean getBoolean(String key) {
- if (parameters.get(key) instanceof BooleanParameter) {
- return ((BooleanParameter) parameters.get(key)).get();
- } else {
- throw new ClassCastException("Could not cast " + parameters.get(key).getClass() + " to int.");
- }
+
+ /**
+ * @return the inputs
+ */
+ public int inputs() {
+ return inputs.get();
}
-
- public Parameter getParameter(String key) {
- return parameters.get(key);
+
+ /**
+ * @return the outputs
+ */
+ public int outputs() {
+ return outputs.get();
}
-
- public boolean contains(String key) {
- return parameters.containsKey(key);
+
+ /**
+ * @return the populationSize
+ */
+ public int populationSize() {
+ return populationSize.get();
}
-
+
+ /**
+ * @return the levelsBack
+ */
+ public int levelsBack() {
+ return levelsBack.get();
+ }
+
+ /**
+ * @return the nodes
+ */
+ public int nodes() {
+ return columns.get() * rows.get();
+ }
+
+ /**
+ * @return the currentGeneration
+ */
+ public int currentGeneration() {
+ return currentGeneration.get();
+ }
+
+ /**
+ * @return the generations
+ */
+ public int generations() {
+ return generations.get();
+ }
+
+ /**
+ * @return the currentRun
+ */
+ public int currentRun() {
+ return currentRun.get();
+ }
+
+ /**
+ * @return the runs
+ */
+ public int runs() {
+ return runs.get();
+ }
+
+ /**
+ * @return the arity
+ */
+ public int arity() {
+ return arity.get();
+ }
+
+ /**
+ * @return the seed
+ */
+ public int seed() {
+ return seed.get();
+ }
+
+ /**
+ * @return the report
+ */
+ public int report() {
+ return report.get();
+ }
+
private void createBaseParameters() {
- parameters.put("rows", new IntegerParameter(8, "Rows", false, true) {
+ rows = new IntegerParameter(5, "Rows", false, true) {
@Override
- public void validate(int newValue) {
- if (newValue <= 0) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Chromosome must have at least 1 row.");
} else {
status = ParameterStatus.VALID;
}
}
- });
- parameters.put("columns", new IntegerParameter(9, "Columns", false, true) {
+ };
+
+ columns = new IntegerParameter(5, "Columns", false, true) {
@Override
- public void validate(int newValue) {
- if (newValue <= 0) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Chromosome must have at least 1 column.");
} else {
status = ParameterStatus.VALID;
}
}
- });
- parameters.put("inputs", new IntegerParameter(3, "Inputs", false, true) {
+ };
+
+ inputs = new IntegerParameter(3, "Inputs", false, true) {
@Override
- public void validate(int newValue) {
- if (newValue <= 0) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Chromosome must have at least 1 input.");
} else {
status = ParameterStatus.VALID;
}
}
- });
- parameters.put("outputs", new IntegerParameter(3, "Outputs", false, true) {
+ };
+
+ outputs = new IntegerParameter(3, "Outputs", false, true) {
@Override
- public void validate(int newValue) {
- if (newValue <= 0) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Chromosome must have at least 1 output.");
} else {
status = ParameterStatus.VALID;
}
}
- });
- parameters.put("popSize", new IntegerParameter(5, "Population", false, true) {
+ };
+
+ populationSize = new IntegerParameter(5, "Population", false, true) {
@Override
- public void validate(int newValue) {
- if (newValue <= 0) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Population size must be at least 1.");
} else {
status = ParameterStatus.VALID;
}
}
- });
- parameters.put("levelsBack", new IntegerParameter(2, "Levels back", false, true) {
+ };
+
+ levelsBack = new IntegerParameter(2, "Levels back", false, true) {
@Override
- public void validate(int newValue) {
- if (newValue <= 0) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Levels back must be at least 1.");
- } else if (newValue > getInt("columns")) {
+ } else if (newValue.intValue() > columns.get()) {
status = ParameterStatus.INVALID;
status.setDetails("Levels back must be less than or equal to the number of columns.");
} else {
status = ParameterStatus.VALID;
}
}
- });
-
- IntegerParameter nodes = new IntegerParameter(1, "Nodes", true, false) {
- @Override
- public void validate(int newValue) {
- // blank
- }
};
- nodes.valueProperty().bind(((IntegerParameter) parameters.get("rows")).valueProperty().multiply(((IntegerParameter) parameters.get("columns")).valueProperty()));
- parameters.put("nodes", nodes);
-
- parameters.put("generations", new IntegerParameter(1000000, "Generations") {
+
+ generations = new IntegerParameter(1000000, "Generations") {
@Override
- public void validate(int newValue) {
- if (newValue <= 0) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Number of generations must be greater than 0.");
- } else if (newValue < getInt("currentGen")) {
+ } else if (newValue.intValue() < currentGeneration.get()) {
status = ParameterStatus.WARNING_RESET;
status.setDetails("Setting generations to less than the current generation will cause the experiment to restart.");
} else {
status = ParameterStatus.VALID;
}
}
- });
- parameters.put("currentGen", new IntegerParameter(1, "Generation", true, false) {
+ };
+
+ currentGeneration = new IntegerParameter(1, "Generation", true, false) {
@Override
- public void validate(int newValue) {
+ public void validate(Number newValue) {
// blank
}
- });
+ };
- parameters.put("runs", new IntegerParameter(5, "Runs") {
+ runs = new IntegerParameter(5, "Runs") {
@Override
- public void validate(int newValue) {
- if (newValue <= 0) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
status.setDetails("Number of runs must be greater than 0.");
- } else if (newValue < getInt("currentRun")) {
+ } else if (newValue.intValue() < currentRun.get()) {
status = ParameterStatus.WARNING_RESET;
status.setDetails("Setting runs to less than the current run will cause the experiment to restart.");
} else {
status = ParameterStatus.VALID;
}
}
- });
- parameters.put("currentRun", new IntegerParameter(1, "Run", true, false) {
+ };
+
+ currentRun = new IntegerParameter(1, "Run", true, false) {
@Override
- public void validate(int newValue) {
+ public void validate(Number newValue) {
// blank
}
- });
+ };
- //parameters.put("arity", new IntegerParameter(functionSet.getMaxArity(), "Max arity", true, false) {
- parameters.put("arity", new IntegerParameter(0, "Max arity", true, false) {
+ arity = new IntegerParameter(0, "Max arity", true, false) {
@Override
- public void validate(int newValue) {
+ public void validate(Number newValue) {
// blank
}
- });
+ };
- IntegerParameter seed = new IntegerParameter(1234, "Seed", false, true) {
+ seed = new IntegerParameter(1234, "Seed", false, true) {
@Override
- public void validate(int newValue) {
+ public void validate(Number newValue) {
status = ParameterStatus.VALID;
}
};
@@ -217,19 +272,18 @@ public class Resources {
}
});
numberGenerator.setSeed(seed.get());
- parameters.put("seed", seed);
- parameters.put("report", new IntegerParameter(1, "Report", false, false) {
+ report = new IntegerParameter(1, "Report", false, false) {
@Override
- public void validate(int newValue) {
- if (newValue > getInt("generations")) {
+ public void validate(Number newValue) {
+ if (newValue.intValue() > generations.get()) {
status = ParameterStatus.WARNING;
status.setDetails("No reports will be printed.");
} else {
status = ParameterStatus.VALID;
}
}
- });
+ };
}
/*
@@ -271,8 +325,8 @@ public class Resources {
* These are affected by parameter report
*/
public void reportln(String s) {
- if (getInt("report") > 0) {
- if (getInt("currentGen") % getInt("report") == 0) {
+ if (report.get() > 0) {
+ if (currentGeneration.get() % report.get() == 0) {
System.out.println(s);
if (console != null) {
console.println(s);
@@ -282,8 +336,8 @@ public class Resources {
}
public void report(String s) {
- if (getInt("report") > 0) {
- if (getInt("currentGen") % getInt("report") == 0) {
+ if (report.get() > 0) {
+ if (currentGeneration.get() % report.get() == 0) {
System.out.print(s);
if (console != null) {
console.print(s);
diff --git a/src/jcgp/backend/resources/parameters/BooleanParameter.java b/src/jcgp/backend/resources/parameters/BooleanParameter.java
index cd17649..cc74a64 100644
--- a/src/jcgp/backend/resources/parameters/BooleanParameter.java
+++ b/src/jcgp/backend/resources/parameters/BooleanParameter.java
@@ -2,13 +2,11 @@ package jcgp.backend.resources.parameters;
import javafx.beans.property.SimpleBooleanProperty;
-public abstract class BooleanParameter extends Parameter {
-
- private SimpleBooleanProperty value;
-
+public abstract class BooleanParameter extends Parameter<Boolean> {
+
public BooleanParameter(boolean value, String name, boolean monitor, boolean critical) {
super(name, monitor, critical);
- this.value = new SimpleBooleanProperty(value);
+ this.valueProperty = new SimpleBooleanProperty(value);
}
/**
@@ -20,23 +18,6 @@ public abstract class BooleanParameter extends Parameter {
*/
public BooleanParameter(boolean value, String name) {
super(name, false, false);
- this.value = new SimpleBooleanProperty(value);
- }
-
- public boolean get() {
- return value.get();
- }
-
- public void set(boolean newValue) {
- if (!value.isBound()) {
- value.set(newValue);
- }
- }
-
- public abstract void validate(boolean newValue);
-
- @Override
- public SimpleBooleanProperty valueProperty() {
- return value;
+ this.valueProperty = new SimpleBooleanProperty(value);
}
}
diff --git a/src/jcgp/backend/resources/parameters/DoubleParameter.java b/src/jcgp/backend/resources/parameters/DoubleParameter.java
index 8464c83..b109446 100644
--- a/src/jcgp/backend/resources/parameters/DoubleParameter.java
+++ b/src/jcgp/backend/resources/parameters/DoubleParameter.java
@@ -2,35 +2,20 @@ package jcgp.backend.resources.parameters;
import javafx.beans.property.SimpleDoubleProperty;
-public abstract class DoubleParameter extends Parameter {
-
- protected SimpleDoubleProperty value;
+public abstract class DoubleParameter extends Parameter<Number> {
public DoubleParameter(double value, String name, boolean monitor, boolean critical) {
super(name, monitor, critical);
- this.value = new SimpleDoubleProperty(value);
+ this.valueProperty = new SimpleDoubleProperty(value);
}
public DoubleParameter(double value, String name) {
super(name, false, false);
- this.value = new SimpleDoubleProperty(value);
+ this.valueProperty = new SimpleDoubleProperty(value);
}
- public double get() {
- return value.get();
- }
-
- public void set(double newValue) {
- if (!value.isBound()) {
- value.set(newValue);
- }
- }
-
@Override
- public SimpleDoubleProperty valueProperty() {
- return value;
+ public Double get() {
+ return super.get().doubleValue();
}
-
- public abstract void validate(double newValue);
-
}
diff --git a/src/jcgp/backend/resources/parameters/IntegerParameter.java b/src/jcgp/backend/resources/parameters/IntegerParameter.java
index 2a7b2a7..7cf68bd 100644
--- a/src/jcgp/backend/resources/parameters/IntegerParameter.java
+++ b/src/jcgp/backend/resources/parameters/IntegerParameter.java
@@ -2,36 +2,20 @@ package jcgp.backend.resources.parameters;
import javafx.beans.property.SimpleIntegerProperty;
-public abstract class IntegerParameter extends Parameter {
-
- private SimpleIntegerProperty value;
+public abstract class IntegerParameter extends Parameter<Number> {
public IntegerParameter(int value, String name, boolean monitor, boolean critical) {
super(name, monitor, critical);
- this.value = new SimpleIntegerProperty(value);
+ this.valueProperty = new SimpleIntegerProperty(value);
}
public IntegerParameter(int value, String name) {
super(name, false, false);
- this.value = new SimpleIntegerProperty(value);
+ this.valueProperty = new SimpleIntegerProperty(value);
}
- public int get() {
- return value.get();
- }
-
- public void set(int newValue) {
- if (!value.isBound()) {
- validate(newValue);
- value.set(newValue);
- }
- }
-
@Override
- public SimpleIntegerProperty valueProperty() {
- return value;
+ public Integer get() {
+ return super.get().intValue();
}
-
- public abstract void validate(int newValue);
-
}
diff --git a/src/jcgp/backend/resources/parameters/Parameter.java b/src/jcgp/backend/resources/parameters/Parameter.java
index 7e12ff8..3990ae6 100644
--- a/src/jcgp/backend/resources/parameters/Parameter.java
+++ b/src/jcgp/backend/resources/parameters/Parameter.java
@@ -1,8 +1,9 @@
package jcgp.backend.resources.parameters;
import javafx.beans.property.Property;
+import javafx.beans.property.ReadOnlyProperty;
-public abstract class Parameter {
+public abstract class Parameter<T> {
protected boolean monitor, critical, reset = false;
@@ -10,6 +11,8 @@ public abstract class Parameter {
protected String name;
+ protected Property<T> valueProperty;
+
public Parameter(String name, boolean monitor, boolean critical) {
this.name = name;
this.monitor = monitor;
@@ -36,6 +39,19 @@ public abstract class Parameter {
return status;
}
- public abstract Property<?> valueProperty();
-
+ public ReadOnlyProperty<T> valueProperty() {
+ return valueProperty;
+ }
+
+ public T get() {
+ return valueProperty.getValue();
+ }
+
+ public void set(T newValue) {
+ if (!valueProperty.isBound()) {
+ valueProperty.setValue(newValue);
+ }
+ }
+
+ public abstract void validate(T newValue);
}
diff --git a/src/jcgp/backend/tests/ChromosomeTests.java b/src/jcgp/backend/tests/ChromosomeTests.java
index a16ba75..07326a7 100644
--- a/src/jcgp/backend/tests/ChromosomeTests.java
+++ b/src/jcgp/backend/tests/ChromosomeTests.java
@@ -2,6 +2,7 @@ package jcgp.backend.tests;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import jcgp.backend.function.IntegerArithmetic;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Input;
@@ -45,6 +46,7 @@ public class ChromosomeTests {
@BeforeClass
public static void setUpBeforeClass() {
resources = new ModifiableResources();
+ resources.setFunctionSet(new IntegerArithmetic());
}
@Before
@@ -62,7 +64,7 @@ public class ChromosomeTests {
// compare all elements, one by one
// check outputs
- for (int o = 0; o < resources.getInt("outputs"); o++) {
+ for (int o = 0; o < resources.outputs(); o++) {
// check that no cross-references exist between chromosomes
assertTrue("Cloned chromosome contains a reference to a member of the original chromosome.",
clone.getOutput(o) != chromosome.getOutput(o) &&
@@ -80,8 +82,8 @@ public class ChromosomeTests {
}
}
// check nodes, rows first
- for (int row = 0; row < resources.getInt("rows"); row++) {
- for (int column = 0; column < resources.getInt("columns"); column++) {
+ for (int row = 0; row < resources.rows(); row++) {
+ for (int column = 0; column < resources.columns(); column++) {
// check that nodes are not pointers to the same instance
assertTrue("Both chromosomes contain a reference to the same node.", clone.getNode(row, column) != chromosome.getNode(row, column));
// check that both nodes reference their own position in the grid correctly
@@ -91,7 +93,7 @@ public class ChromosomeTests {
assertTrue("Equivalent nodes have different functions.", clone.getNode(row, column).getFunction() == chromosome.getNode(row, column).getFunction());
// compare each connection
- for (int connection = 0; connection < resources.getInt("arity"); connection++) {
+ for (int connection = 0; connection < resources.arity(); connection++) {
// first look at whether they are actually the same instance
assertTrue("Nodes are connected to the same connection instance.",
clone.getNode(row, column).getConnection(connection) != chromosome.getNode(row, column).getConnection(connection));
@@ -130,12 +132,12 @@ public class ChromosomeTests {
clone.setInputs((Object[]) testInputs);
// check that both chromosomes have the same outputs
- for (int i = 0; i < resources.getInt("outputs"); i++) {
+ for (int i = 0; i < resources.outputs(); i++) {
assertTrue("Incorrect output returned", ((Integer) chromosome.getOutput(i).calculate()) == ((Integer) clone.getOutput(i).calculate()));
}
// mutate an output in clone, check that the same node in chromosome produces a different output
- clone.getOutput(1).setConnection(resources.getRandomInt(resources.getInt("arity")), clone.getInput(2));
+ clone.getOutput(1).setConnection(resources.getRandomInt(resources.arity()), clone.getInput(2));
assertTrue("Mutation affected nodes in both chromosomes.",
clone.getOutput(1).calculate() != chromosome.getOutput(1).calculate());
@@ -164,7 +166,7 @@ public class ChromosomeTests {
// get random connections with the last column as reference, check that they're all within range
int connectionNodes = 0, connectionOutOfRange = 0, connectionInputs = 0, connectionPicks = 100000;
- int chosenColumn = resources.getInt("columns") - 1;
+ int chosenColumn = resources.columns() - 1;
for (int i = 0; i < connectionPicks; i++) {
Connection c = chromosome.getRandomConnection(chosenColumn);
if (c instanceof Node) {
@@ -180,10 +182,10 @@ public class ChromosomeTests {
}
}
- System.out.println("Out of " + connectionPicks + " connections picked from " + ((chosenColumn >= resources.getInt("levelsBack")) ? resources.getInt("levelsBack") : chosenColumn) * resources.getInt("rows") +
- " allowed nodes and " + resources.getInt("inputs") + " inputs, " + connectionNodes + " were nodes and " + connectionInputs + " were inputs.");
+ System.out.println("Out of " + connectionPicks + " connections picked from " + ((chosenColumn >= resources.levelsBack()) ? resources.levelsBack() : chosenColumn) * resources.rows() +
+ " allowed nodes and " + resources.inputs() + " inputs, " + connectionNodes + " were nodes and " + connectionInputs + " were inputs.");
- System.out.println("Node/input ratio: " + (chosenColumn >= resources.getInt("levelsBack") ? resources.getInt("levelsBack") : chosenColumn) * resources.getDouble("rows") / resources.getDouble("inputs") +
+ System.out.println("Node/input ratio: " + (chosenColumn >= resources.levelsBack() ? resources.levelsBack() : chosenColumn) * (double) resources.rows() / (double) resources.inputs() +
", picked ratio: " + (double) connectionNodes / (double) connectionInputs);
System.out.println(connectionOutOfRange + " nodes that disrespected levels back were picked.");
@@ -207,10 +209,10 @@ public class ChromosomeTests {
fail("Return is neither Node nor Output.");
}
}
- System.out.println("Out of " + mutablePicks + " mutable elements picked from " + resources.getInt("nodes") +
- " nodes and " + resources.getInt("outputs") + " outputs, " + mutableNodes + " were nodes and " +
+ System.out.println("Out of " + mutablePicks + " mutable elements picked from " + resources.nodes() +
+ " nodes and " + resources.outputs() + " outputs, " + mutableNodes + " were nodes and " +
mutableOutputs + " were outputs.");
- System.out.println("Node/output ratio: " + resources.getDouble("nodes") / resources.getDouble("outputs") +
+ System.out.println("Node/output ratio: " + (double) resources.nodes() / (double) resources.outputs() +
", picked ratio: " + (double) mutableNodes / (double) mutableOutputs + "\n");
}
@@ -234,12 +236,12 @@ public class ChromosomeTests {
@Test
public void setInputTest() {
// set input values, check that acquired values are correct
- Integer[] testInputs = new Integer[resources.getInt("inputs")];
- for (int i = 0; i < resources.getInt("inputs"); i++) {
+ Integer[] testInputs = new Integer[resources.inputs()];
+ for (int i = 0; i < resources.inputs(); i++) {
testInputs[i] = i * 2 - 3;
}
chromosome.setInputs((Object[]) testInputs);
- for (int i = 0; i < resources.getInt("inputs"); i++) {
+ for (int i = 0; i < resources.inputs(); i++) {
assertTrue("Incorrect input returned.", ((Integer) chromosome.getInput(i).getValue()) == i * 2 - 3);
}
}
@@ -250,8 +252,8 @@ public class ChromosomeTests {
@Test
public void getNodeTest() {
// get all nodes one by one, check that they are all correct
- for (int r = 0; r < resources.getInt("rows"); r++) {
- for (int c = 0; c < resources.getInt("columns"); c++) {
+ for (int r = 0; r < resources.rows(); r++) {
+ for (int c = 0; c < resources.columns(); c++) {
assertTrue("Incorrect node returned.", chromosome.getNode(r, c).getColumn() == c &&
chromosome.getNode(r, c).getRow() == r);
}
@@ -271,7 +273,9 @@ public class ChromosomeTests {
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);
+ chromosome.printNodes();
+
+ assertTrue("List has the wrong number of nodes: " + chromosome.getActiveNodes(), chromosome.getActiveNodes().size() == 3);
}
/**
@@ -314,11 +318,11 @@ public class ChromosomeTests {
*/
private Chromosome createKnownConfiguration() {
// with a small topology, build a chromosome of known connections and check outputs
- resources.set("columns", 3);
- resources.set("rows", 3);
- resources.set("inputs", 3);
- resources.set("outputs", 2);
- resources.set("levelsBack", 3);
+ resources.setColumns(3);
+ resources.setRows(3);
+ resources.setInputs(3);
+ resources.setOutputs(2);
+ resources.setLevelsBack(3);
Chromosome c = new Chromosome(resources);
@@ -328,7 +332,7 @@ public class ChromosomeTests {
c.getOutput(0).setConnection(0, c.getNode(0, 0));
c.getOutput(1).setConnection(0, c.getNode(1, 2));
-
+
return c;
}
}
diff --git a/src/jcgp/backend/tests/NodeTests.java b/src/jcgp/backend/tests/NodeTests.java
index e8fe02f..0e08d92 100644
--- a/src/jcgp/backend/tests/NodeTests.java
+++ b/src/jcgp/backend/tests/NodeTests.java
@@ -7,7 +7,7 @@ import jcgp.backend.function.IntegerArithmetic;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Node;
-import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.ModifiableResources;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -32,7 +32,7 @@ public class NodeTests {
private Node node;
private static Chromosome chromosome;
- private static Resources resources;
+ private static ModifiableResources resources;
// these numbers will be used by the node under test
private final int arg1 = 2;
private final int arg2 = 5;
@@ -40,14 +40,14 @@ public class NodeTests {
@BeforeClass
public static void setUpBeforeClass() {
- resources = new Resources();
-
+ resources = new ModifiableResources();
+ resources.setFunctionSet(new IntegerArithmetic());
chromosome = new Chromosome(resources);
}
@Before
public void setUp() throws Exception {
- node = new Node(chromosome, 0, 0, resources.getInt("arity"));
+ node = new Node(chromosome, 0, 0, resources.arity());
// make node with anonymous addition function and hard-coded value connections
node.initialise(new IntegerArithmetic.Addition(),
new Connection[]{new Connection() {
@@ -174,7 +174,7 @@ public class NodeTests {
return 0;
}
};
- node.setConnection(resources.getRandomInt(resources.getInt("arity")), conn2);
+ node.setConnection(resources.getRandomInt(resources.arity()), conn2);
assertTrue("Connection was not found in node.", node.getConnection(0) == conn2 || node.getConnection(1) == conn2);
diff --git a/src/jcgp/backend/tests/OutputTests.java b/src/jcgp/backend/tests/OutputTests.java
index afe44a7..a331f2d 100644
--- a/src/jcgp/backend/tests/OutputTests.java
+++ b/src/jcgp/backend/tests/OutputTests.java
@@ -1,10 +1,11 @@
package jcgp.backend.tests;
import static org.junit.Assert.assertTrue;
+import jcgp.backend.function.IntegerArithmetic;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Output;
-import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.ModifiableResources;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -26,14 +27,15 @@ public class OutputTests {
private Output output;
private static Chromosome chromosome;
- private static Resources resources;
+ private static ModifiableResources resources;
// these are the test values
private final int outputValue = 10;
private final int outputIndex = 2;
@BeforeClass
public static void setUpBeforeClass() {
- resources = new Resources();
+ resources = new ModifiableResources();
+ resources.setFunctionSet(new IntegerArithmetic());
chromosome = new Chromosome(resources);
}
diff --git a/src/jcgp/backend/tests/PopulationTests.java b/src/jcgp/backend/tests/PopulationTests.java
index 31df8b9..fb8ced4 100644
--- a/src/jcgp/backend/tests/PopulationTests.java
+++ b/src/jcgp/backend/tests/PopulationTests.java
@@ -1,9 +1,10 @@
package jcgp.backend.tests;
import static org.junit.Assert.assertTrue;
+import jcgp.backend.function.IntegerArithmetic;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Population;
-import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.ModifiableResources;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -26,30 +27,12 @@ import org.junit.Test;
public class PopulationTests {
private Population population;
- private static Resources resources;
+ private static ModifiableResources resources;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
- resources = new Resources();
-
-
-// // initialise function set
-// FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction());
-//
-// // initialise utilities
-// Utilities.setResources(new Random(1234), functionSet);
-//
-// // initialise parameters
-// Resources.setColumns(20);
-// Resources.setRows(20);
-// Resources.setInputs(2);
-// Resources.setOutputs(4);
-// Resources.setLevelsBack(1);
-// Resources.setMutationRate(10);
-// Resources.setTotalGenerations(100);
-// Resources.setTotalRuns(5);
-// Resources.setPopulationSize(1, 4);
-// Resources.setMaxArity(functionSet.getMaxArity());
+ resources = new ModifiableResources();
+ resources.setFunctionSet(new IntegerArithmetic());
}
@Before
@@ -70,7 +53,7 @@ public class PopulationTests {
chromosomes++;
}
- assertTrue("Incorrect number of chromosomes generated.", chromosomes == resources.getInt("popSize"));
+ assertTrue("Incorrect number of chromosomes generated.", chromosomes == resources.populationSize());
}
@Test