aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/jcgp/CGP.java137
-rw-r--r--src/jcgp/modules/Module.java1
-rw-r--r--src/jcgp/modules/function/Arithmetic.java18
-rw-r--r--src/jcgp/modules/function/BitwiseLogic.java29
-rw-r--r--src/jcgp/modules/function/BooleanLogic.java29
-rw-r--r--src/jcgp/modules/function/FunctionSet.java10
-rw-r--r--src/jcgp/modules/mutator/PointMutator.java (renamed from src/jcgp/modules/mutator/StandardMutator.java)11
-rw-r--r--src/jcgp/parameters/BooleanParameter.java25
-rw-r--r--src/jcgp/parameters/DoubleParameter.java24
-rw-r--r--src/jcgp/parameters/IntegerParameter.java25
-rw-r--r--src/jcgp/parameters/Parameter.java15
-rw-r--r--src/jcgp/parameters/Parameters.java14
-rw-r--r--src/jcgp/population/Chromosome.java42
-rw-r--r--src/jcgp/population/Node.java4
-rw-r--r--src/jcgp/population/Output.java5
-rw-r--r--src/jcgp/population/Population.java50
16 files changed, 218 insertions, 221 deletions
diff --git a/src/jcgp/CGP.java b/src/jcgp/CGP.java
index 6ac8ff8..1004001 100644
--- a/src/jcgp/CGP.java
+++ b/src/jcgp/CGP.java
@@ -1,79 +1,140 @@
package jcgp;
+import java.util.HashMap;
import java.util.Random;
+import javafx.beans.property.SimpleIntegerProperty;
import jcgp.modules.ea.EvolutionaryAlgorithm;
import jcgp.modules.ea.StandardEA;
import jcgp.modules.fitness.FitnessFunction;
import jcgp.modules.fitness.TestCase;
import jcgp.modules.fitness.TruthTableEvaluator;
import jcgp.modules.function.Arithmetic;
+import jcgp.modules.function.BitwiseLogic;
+import jcgp.modules.function.BooleanLogic;
import jcgp.modules.function.FunctionSet;
import jcgp.modules.mutator.Mutator;
-import jcgp.modules.mutator.StandardMutator;
+import jcgp.modules.mutator.PointMutator;
import jcgp.parameters.BooleanParameter;
+import jcgp.parameters.DoubleParameter;
import jcgp.parameters.IntegerParameter;
+import jcgp.parameters.Parameter;
import jcgp.parameters.Parameters;
import jcgp.population.Population;
public class CGP {
// CGP components
- private EvolutionaryAlgorithm evolutionaryAlgorithm;
- private Mutator mutator;
- private Population population;
- private FitnessFunction fitnessFunction;
+ private Mutator[] mutators = new Mutator[] {
+ new PointMutator()};
+ private Mutator mutator = mutators[0];
+
+ private EvolutionaryAlgorithm[] evolutionaryAlgorithms = new EvolutionaryAlgorithm[] {
+ new StandardEA()};
+ private EvolutionaryAlgorithm evolutionaryAlgorithm = evolutionaryAlgorithms[0];
+
+ private FitnessFunction[] fitnessFunctions = new FitnessFunction[] {
+ new TruthTableEvaluator()};
+ private FitnessFunction fitnessFunction = fitnessFunctions[0];
+
+ private FunctionSet[] functionSets = new FunctionSet[] {
+ new FunctionSet("Arithmetic",
+ new Arithmetic.Addition(),
+ new Arithmetic.Subtraction(),
+ new Arithmetic.Multiplication(),
+ new Arithmetic.Division()),
+
+ new FunctionSet("32-bit logic",
+ new BitwiseLogic.And(),
+ new BitwiseLogic.Or(),
+ new BitwiseLogic.Nand(),
+ new BitwiseLogic.Nor(),
+ new BitwiseLogic.Xor(),
+ new BitwiseLogic.Xnor(),
+ new BitwiseLogic.Not()),
+
+ new FunctionSet("1-bit logic",
+ new BooleanLogic.And(),
+ new BooleanLogic.Or(),
+ new BooleanLogic.Nand(),
+ new BooleanLogic.Nor(),
+ new BooleanLogic.Xor(),
+ new BooleanLogic.Xnor(),
+ new BooleanLogic.Not())};
+ private Population population;
+
+ private static HashMap<String, Parameter> parameters = new HashMap<String, Parameter>();
+
public CGP() {
- initialise();
+ createBaseParameters();
for (int i = 0; i < Parameters.getTotalGenerations(); i++) {
- Parameters.setCurrentGeneration(i);
+
+ ((SimpleIntegerProperty) get("currentGen").getValue()).set(i);
fitnessFunction.evaluate(population);
evolutionaryAlgorithm.evolve(population, mutator);
+
if (evolutionaryAlgorithm.getFittestChromosome().getFitness() >= 3) {
- if (Parameters.getDebug()) {
- evolutionaryAlgorithm.getFittestChromosome().printNodes();
- }
break;
}
}
}
- /**
- *
- */
- private void initialise() {
+ private void createBaseParameters() {
// make fundamental parameters
- Parameters.add("Rows", new IntegerParameter(3));
- Parameters.add("Columns", new IntegerParameter(3));
- Parameters.add("Inputs", new IntegerParameter(3));
- Parameters.add("Outputs", new IntegerParameter(3));
- Parameters.add("population", new IntegerParameter(5));
-
- Parameters.add("generations", new IntegerParameter(100));
- Parameters.add("runs", new IntegerParameter(5));
-
- Parameters.add("debug", new BooleanParameter(false));
+ parameters.put("rows", new IntegerParameter(3, "Rows"));
+ parameters.put("columns", new IntegerParameter(3, "Columns"));
+ parameters.put("inputs", new IntegerParameter(3, "Inputs"));
+ parameters.put("outputs", new IntegerParameter(3, "Outputs"));
+ parameters.put("popSize", new IntegerParameter(5, "Population"));
- // initialise function set
- FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction(), new Arithmetic.Multiplication());
+ parameters.put("nodes", new IntegerParameter(9, "", true, true));
- // initialise utilities
- Utilities.setResources(new Random(1234), functionSet);
+ parameters.put("gens", new IntegerParameter(100, "Generations"));
+ parameters.put("currentGen", new IntegerParameter(0, "Generation"));
+ parameters.put("runs", new IntegerParameter(5, "Runs"));
- // initialise fitness function and truth table
- TruthTable.setTestCases(new TestCase(new Object[] {2, 5, 4}, new Object[] {1, 10, 15}));
- fitnessFunction = new TruthTableEvaluator();
-
- // initialise EA
- evolutionaryAlgorithm = new StandardEA();
- mutator = new StandardMutator();
-
- // initialise population
- population = new Population();
+ parameters.put("debug", new BooleanParameter(false, "Debug"));
+ }
+// /**
+// *
+// */
+// private void loadModules() {
+// // initialise function set
+// FunctionSet functionSet = new FunctionSet(new Arithmetic.Addition(), new Arithmetic.Subtraction(), new Arithmetic.Multiplication());
+//
+// // initialise utilities
+// Utilities.setResources(new Random(1234), functionSet);
+//
+// // initialise fitness function and truth table
+// TruthTable.setTestCases(new TestCase(new Object[] {2, 5, 4}, new Object[] {1, 10, 15}));
+// fitnessFunction = new TruthTableEvaluator();
+//
+// // initialise EA
+// evolutionaryAlgorithm = new StandardEA();
+// mutator = new StandardMutator();
+//
+// // initialise population
+// population = new Population();
+// }
+
+ // Parameter methods
+ public static void registerParameter(String key, Parameter value) {
+ parameters.put(key, value);
+ }
+
+ public static Parameter get(String key) {
+ return parameters.get(key);
+ }
+
+ public static boolean contains(String key) {
+ return parameters.containsKey(key);
}
+
+ // Utility methods
+
}
diff --git a/src/jcgp/modules/Module.java b/src/jcgp/modules/Module.java
index a0da50a..b8d44d2 100644
--- a/src/jcgp/modules/Module.java
+++ b/src/jcgp/modules/Module.java
@@ -11,5 +11,4 @@ public abstract class Module {
protected final void registerParameter(String key, Parameter value) {
Parameters.add(key, value);
};
-
}
diff --git a/src/jcgp/modules/function/Arithmetic.java b/src/jcgp/modules/function/Arithmetic.java
index 73debd2..aa5e9bf 100644
--- a/src/jcgp/modules/function/Arithmetic.java
+++ b/src/jcgp/modules/function/Arithmetic.java
@@ -1,7 +1,6 @@
package jcgp.modules.function;
import jcgp.exceptions.InvalidArgumentsException;
-import jcgp.parameters.Parameters;
import jcgp.population.Connection;
public class Arithmetic {
@@ -19,9 +18,6 @@ public class Arithmetic {
Integer arg2 = ((Integer) connections[1].getValue());
Integer result = arg1 + arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " + " + arg2 + " = " + result);
- }
return result;
}
}
@@ -45,9 +41,6 @@ public class Arithmetic {
Integer arg2 = ((Integer) connections[1].getValue());
Integer result = arg1 - arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " - " + arg2 + " = " + result);
- }
return result;
}
}
@@ -71,11 +64,6 @@ public class Arithmetic {
Integer arg2 = ((Integer) connections[1].getValue());
Integer result = arg1 * arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " * " + arg2 + " = " + result);
- }
-
-
return result;
}
}
@@ -103,12 +91,6 @@ public class Arithmetic {
} else {
result = arg1 / arg2;
}
-
-
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " / " + arg2 + " = " + result);
- }
-
return result;
}
diff --git a/src/jcgp/modules/function/BitwiseLogic.java b/src/jcgp/modules/function/BitwiseLogic.java
index a260715..c8452e6 100644
--- a/src/jcgp/modules/function/BitwiseLogic.java
+++ b/src/jcgp/modules/function/BitwiseLogic.java
@@ -1,7 +1,6 @@
package jcgp.modules.function;
import jcgp.exceptions.InvalidArgumentsException;
-import jcgp.parameters.Parameters;
import jcgp.population.Connection;
public class BitwiseLogic {
@@ -17,9 +16,7 @@ public class BitwiseLogic {
int arg1 = ((int) connections[0].getValue());
int arg2 = ((int) connections[1].getValue());
int result = arg1 & arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " AND " + arg2 + " = " + result);
- }
+
return result;
}
}
@@ -41,9 +38,7 @@ public class BitwiseLogic {
int arg1 = ((int) connections[0].getValue());
int arg2 = ((int) connections[1].getValue());
int result = arg1 | arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " OR " + arg2 + " = " + result);
- }
+
return result;
}
}
@@ -64,9 +59,7 @@ public class BitwiseLogic {
} else {
int arg1 = ((int) connections[0].getValue());
int result = ~arg1;
- if (Parameters.getDebug()) {
- System.out.println("NOT " + arg1 + " = " + result);
- }
+
return result;
}
}
@@ -88,9 +81,7 @@ public class BitwiseLogic {
int arg1 = ((int) connections[0].getValue());
int arg2 = ((int) connections[1].getValue());
int result = arg1 ^ arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " XOR " + arg2 + " = " + result);
- }
+
return result;
}
}
@@ -112,9 +103,7 @@ public class BitwiseLogic {
int arg1 = ((int) connections[0].getValue());
int arg2 = ((int) connections[1].getValue());
int result = arg1 & arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " NAND " + arg2 + " = " + ~result);
- }
+
return ~result;
}
}
@@ -136,9 +125,7 @@ public class BitwiseLogic {
int arg1 = ((int) connections[0].getValue());
int arg2 = ((int) connections[1].getValue());
int result = arg1 | arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " NOR " + arg2 + " = " + ~result);
- }
+
return ~result;
}
}
@@ -160,9 +147,7 @@ public class BitwiseLogic {
int arg1 = ((int) connections[0].getValue());
int arg2 = ((int) connections[1].getValue());
int result = arg1 ^ arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " XNOR " + arg2 + " = " + ~result);
- }
+
return ~result;
}
}
diff --git a/src/jcgp/modules/function/BooleanLogic.java b/src/jcgp/modules/function/BooleanLogic.java
index 887e0e6..f98d1db 100644
--- a/src/jcgp/modules/function/BooleanLogic.java
+++ b/src/jcgp/modules/function/BooleanLogic.java
@@ -1,7 +1,6 @@
package jcgp.modules.function;
import jcgp.exceptions.InvalidArgumentsException;
-import jcgp.parameters.Parameters;
import jcgp.population.Connection;
public class BooleanLogic {
@@ -17,9 +16,7 @@ public class BooleanLogic {
Boolean arg1 = ((Boolean) connections[0].getValue());
Boolean arg2 = ((Boolean) connections[1].getValue());
Boolean result = arg1 && arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " AND " + arg2 + " = " + result);
- }
+
return result;
}
}
@@ -41,9 +38,7 @@ public class BooleanLogic {
Boolean arg1 = ((Boolean) connections[0].getValue());
Boolean arg2 = ((Boolean) connections[1].getValue());
Boolean result = arg1 || arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " OR " + arg2 + " = " + result);
- }
+
return result;
}
}
@@ -64,9 +59,7 @@ public class BooleanLogic {
} else {
Boolean arg1 = ((Boolean) connections[0].getValue());
Boolean result = !arg1;
- if (Parameters.getDebug()) {
- System.out.println("NOT " + arg1 + " = " + result);
- }
+
return result;
}
}
@@ -88,9 +81,7 @@ public class BooleanLogic {
Boolean arg1 = ((Boolean) connections[0].getValue());
Boolean arg2 = ((Boolean) connections[1].getValue());
Boolean result = arg1 ^ arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " XOR " + arg2 + " = " + result);
- }
+
return result;
}
}
@@ -112,9 +103,7 @@ public class BooleanLogic {
Boolean arg1 = ((Boolean) connections[0].getValue());
Boolean arg2 = ((Boolean) connections[1].getValue());
Boolean result = arg1 && arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " NAND " + arg2 + " = " + !result);
- }
+
return !result;
}
}
@@ -136,9 +125,7 @@ public class BooleanLogic {
Boolean arg1 = ((Boolean) connections[0].getValue());
Boolean arg2 = ((Boolean) connections[1].getValue());
Boolean result = arg1 || arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " NOR " + arg2 + " = " + !result);
- }
+
return !result;
}
}
@@ -160,9 +147,7 @@ public class BooleanLogic {
Boolean arg1 = ((Boolean) connections[0].getValue());
Boolean arg2 = ((Boolean) connections[1].getValue());
Boolean result = arg1 ^ arg2;
- if (Parameters.getDebug()) {
- System.out.println(arg1 + " XNOR " + arg2 + " = " + !result);
- }
+
return !result;
}
}
diff --git a/src/jcgp/modules/function/FunctionSet.java b/src/jcgp/modules/function/FunctionSet.java
index 8a2190a..fb3724f 100644
--- a/src/jcgp/modules/function/FunctionSet.java
+++ b/src/jcgp/modules/function/FunctionSet.java
@@ -13,16 +13,18 @@ package jcgp.modules.function;
public class FunctionSet {
private Function[] functionList;
private int maxArity = 0;
+ private String name;
- public FunctionSet(Function ... functions) {
+ public FunctionSet(String name, Function ... functions) {
functionList = functions;
-
for (Function function : functionList) {
if (function.getArity() > maxArity) {
maxArity = function.getArity();
}
}
+ this.name = name;
+
}
public int getFunctionCount() {
@@ -36,4 +38,8 @@ public class FunctionSet {
public int getMaxArity(){
return maxArity;
}
+
+ public String getName() {
+ return name;
+ }
} \ No newline at end of file
diff --git a/src/jcgp/modules/mutator/StandardMutator.java b/src/jcgp/modules/mutator/PointMutator.java
index 17bd0be..b06d678 100644
--- a/src/jcgp/modules/mutator/StandardMutator.java
+++ b/src/jcgp/modules/mutator/PointMutator.java
@@ -1,17 +1,22 @@
package jcgp.modules.mutator;
import jcgp.Utilities;
+import jcgp.parameters.IntegerParameter;
import jcgp.parameters.Parameters;
import jcgp.population.Chromosome;
import jcgp.population.MutableElement;
import jcgp.population.Node;
import jcgp.population.Output;
-public class StandardMutator implements Mutator {
+public class PointMutator implements Mutator {
+ public PointMutator() {
+ Parameters.add("mutRate", new IntegerParameter(10, "Mutation rate"));
+ }
+
@Override
public void mutate(Chromosome chromosome) {
- int mutations = (int) (Parameters.getMutationRate() * (((double) Parameters.getNodeCount() + Parameters.getOutputs()) / 100));
+ int mutations = (int) (((int) Parameters.get("mutRate").getValue()) * ((((double) Parameters.get("nodes").getValue()) + ((double) Parameters.get("outputs").getValue())) / 100));
for (int i = 0; i < mutations; i++) {
MutableElement m = chromosome.getRandomMutableElement();
@@ -19,7 +24,7 @@ public class StandardMutator implements Mutator {
if (m instanceof Output) {
m.setConnection(chromosome.getRandomConnection());
} else if (m instanceof Node) {
- int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity());
+ int geneType = Utilities.getRandomInt(1 + ((int) Parameters.get("Max arity").getValue()));
if (geneType < 1) {
((Node) m).setFunction(Utilities.getRandomFunction());
} else {
diff --git a/src/jcgp/parameters/BooleanParameter.java b/src/jcgp/parameters/BooleanParameter.java
index 738b733..9402620 100644
--- a/src/jcgp/parameters/BooleanParameter.java
+++ b/src/jcgp/parameters/BooleanParameter.java
@@ -3,30 +3,31 @@ package jcgp.parameters;
import javafx.beans.property.SimpleBooleanProperty;
public class BooleanParameter extends Parameter {
-
- private SimpleBooleanProperty value;
-
- public BooleanParameter(boolean value) {
-
- this.value.set(value);
+ public BooleanParameter(boolean value, String name) {
+ this.value = new SimpleBooleanProperty(value);
+ this.name = name;
}
- public BooleanParameter(boolean value, boolean managed) {
- this.value.set(value);
+ public BooleanParameter(boolean value, String name, boolean managed, boolean hidden) {
+ this.value = new SimpleBooleanProperty(value);
+ this.name = name;
this.managed = managed;
+ this.hidden = hidden;
}
public void setValue(boolean value) {
- this.value.set(value);
+ ((SimpleBooleanProperty) this.value).set(value);
}
- public boolean getValue() {
- return this.value.get();
+ @Override
+ public Object getValue() {
+ return ((SimpleBooleanProperty) this.value).get();
}
+ @Override
public SimpleBooleanProperty valueProperty() {
- return value;
+ return (SimpleBooleanProperty) value;
}
}
diff --git a/src/jcgp/parameters/DoubleParameter.java b/src/jcgp/parameters/DoubleParameter.java
index 9349502..2c2ec34 100644
--- a/src/jcgp/parameters/DoubleParameter.java
+++ b/src/jcgp/parameters/DoubleParameter.java
@@ -3,30 +3,30 @@ package jcgp.parameters;
import javafx.beans.property.SimpleDoubleProperty;
public class DoubleParameter extends Parameter {
-
- private SimpleDoubleProperty value;
-
- public DoubleParameter(double value) {
-
- this.value.set(value);
+ public DoubleParameter(double value, String name) {
+ this.value = new SimpleDoubleProperty(value);
+ this.name = name;
}
- public DoubleParameter(double value, boolean managed) {
- this.value.set(value);
+ public DoubleParameter(double value, String name, boolean managed, boolean hidden) {
+ this.value = new SimpleDoubleProperty(value);
+ this.name = name;
this.managed = managed;
+ this.hidden = hidden;
}
public void setValue(double value) {
- this.value.set(value);
+ ((SimpleDoubleProperty) this.value).set(value);
}
- public double getValue() {
- return this.value.get();
+ @Override
+ public Object getValue() {
+ return ((SimpleDoubleProperty) this.value).get();
}
public SimpleDoubleProperty valueProperty() {
- return value;
+ return (SimpleDoubleProperty) value;
}
}
diff --git a/src/jcgp/parameters/IntegerParameter.java b/src/jcgp/parameters/IntegerParameter.java
index 2aac3e1..6b4c20f 100644
--- a/src/jcgp/parameters/IntegerParameter.java
+++ b/src/jcgp/parameters/IntegerParameter.java
@@ -3,30 +3,31 @@ package jcgp.parameters;
import javafx.beans.property.SimpleIntegerProperty;
public class IntegerParameter extends Parameter {
-
- private SimpleIntegerProperty value;
-
- public IntegerParameter(int value) {
-
- this.value.set(value);
+ public IntegerParameter(int value, String name) {
+ this.value = new SimpleIntegerProperty(value);
+ this.name = name;
}
- public IntegerParameter(int value, boolean managed) {
- this.value.set(value);
+ public IntegerParameter(int value, String name, boolean managed, boolean hidden) {
+ this.value = new SimpleIntegerProperty(value);
+ this.name = name;
this.managed = managed;
+ this.hidden = hidden;
}
public void setValue(int value) {
- this.value.set(value);
+ ((SimpleIntegerProperty) this.value).set(value);
}
- public int getValue() {
- return this.value.get();
+ @Override
+ public Object getValue() {
+ return ((SimpleIntegerProperty) this.value).get();
}
+ @Override
public SimpleIntegerProperty valueProperty() {
- return value;
+ return (SimpleIntegerProperty) value;
}
}
diff --git a/src/jcgp/parameters/Parameter.java b/src/jcgp/parameters/Parameter.java
index 8b63868..ffcf3b4 100644
--- a/src/jcgp/parameters/Parameter.java
+++ b/src/jcgp/parameters/Parameter.java
@@ -1,8 +1,14 @@
package jcgp.parameters;
+import javafx.beans.property.Property;
+
public abstract class Parameter {
+
protected boolean managed = false;
protected boolean hidden = false;
+ protected String name;
+ @SuppressWarnings("rawtypes")
+ protected Property value;
public void setManaged(boolean value) {
managed = value;
@@ -19,4 +25,13 @@ public abstract class Parameter {
public boolean isHidden() {
return hidden;
}
+
+ public String getName() {
+ return name;
+ }
+
+ public abstract Object getValue();
+
+ @SuppressWarnings("rawtypes")
+ public abstract Property valueProperty();
}
diff --git a/src/jcgp/parameters/Parameters.java b/src/jcgp/parameters/Parameters.java
index 54b54e3..723c5dc 100644
--- a/src/jcgp/parameters/Parameters.java
+++ b/src/jcgp/parameters/Parameters.java
@@ -4,19 +4,7 @@ import java.util.HashMap;
public class Parameters {
- private static HashMap<String, Parameter> parameters;
- public static void add(String key, Parameter value) {
- parameters.put(key, value);
- }
-
- public static Parameter get(String key) {
- return parameters.get(key);
- }
-
- public static boolean contains(String key) {
- return parameters.containsKey(key);
- }
// private static int rows = 0, columns = 0, inputs = 0, outputs = 0, levelsBack = 0,
// mutationRate = 0, totalGenerations = 0, parents = 0, offspring = 0,
@@ -25,6 +13,8 @@ public class Parameters {
// private static boolean debug = false;
+
+
diff --git a/src/jcgp/population/Chromosome.java b/src/jcgp/population/Chromosome.java
index df2a9d4..1f0b312 100644
--- a/src/jcgp/population/Chromosome.java
+++ b/src/jcgp/population/Chromosome.java
@@ -52,20 +52,20 @@ public class Chromosome {
*
*/
private void instantiateElements() {
- inputs = new Input[Parameters.getInputs()];
- for (int i = 0; i < Parameters.getInputs(); i++) {
+ inputs = new Input[((int) Parameters.get("inputs").getValue())];
+ for (int i = 0; i < ((int) Parameters.get("inputs").getValue()); i++) {
inputs[i] = new Input(i);
}
// rows first
- nodes = new Node[Parameters.getRows()][Parameters.getColumns()];
- for (int r = 0; r < Parameters.getRows(); r++) {
- for (int c = 0; c < Parameters.getColumns(); c++) {
+ nodes = new Node[((int) Parameters.get("rows").getValue())][((int) Parameters.get("columns").getValue())];
+ for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) {
+ for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) {
nodes[r][c] = new Node(this, r, c);
}
}
- outputs = new Output[Parameters.getOutputs()];
- for (int o = 0; o < Parameters.getOutputs(); o++) {
+ outputs = new Output[((int) Parameters.get("outputs").getValue())];
+ for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) {
outputs[o] = new Output(this, o);
}
}
@@ -78,7 +78,7 @@ public class Chromosome {
// initialise nodes - [rows][columns]
for (int r = 0; r < nodes.length; r++) {
for (int c = 0; c < nodes[r].length; c++) {
- Connection[] connections = new Connection[Parameters.getMaxArity()];
+ Connection[] connections = new Connection[((int) Parameters.get("maxArity").getValue())];
for (int i = 0; i < connections.length; i++) {
connections[i] = getRandomConnection(c);
}
@@ -100,7 +100,7 @@ public class Chromosome {
for (int r = 0; r < nodes.length; r++) {
for (int c = 0; c < nodes[r].length; c++) {
// make array of connections to initialise with
- Connection[] connections = new Connection[Parameters.getMaxArity()];
+ Connection[] connections = new Connection[((int) Parameters.get("maxArity").getValue())];
// populate with connections equivalent to clone
Connection copyConnection;
for (int i = 0; i < connections.length; i++) {
@@ -177,7 +177,7 @@ public class Chromosome {
*/
public MutableElement getRandomMutableElement() {
// choose output or node
- int index = Utilities.getRandomInt(outputs.length + Parameters.getNodeCount());
+ int index = Utilities.getRandomInt(outputs.length + ((int) Parameters.get("rows").getValue()) * ((int) Parameters.get("columns").getValue()));
if (index < outputs.length) {
// outputs
@@ -185,7 +185,7 @@ public class Chromosome {
} else {
// node
index -= outputs.length;
- return nodes[index / Parameters.getColumns()][index % Parameters.getColumns()];
+ return nodes[index / ((int) Parameters.get("columns").getValue())][index % ((int) Parameters.get("columns").getValue())];
}
}
@@ -199,7 +199,7 @@ public class Chromosome {
*/
public Connection getRandomConnection(int column) {
// work out the allowed range obeying levels back
- int allowedColumns = ((column >= Parameters.getLevelsBack()) ? Parameters.getLevelsBack() : column);
+ int allowedColumns = ((column >= ((int) Parameters.get("levelsBack").getValue())) ? ((int) Parameters.get("levelsBack").getValue()) : column);
int offset = ((column - allowedColumns) * nodes.length) - inputs.length;
// choose input or allowed node
@@ -226,7 +226,7 @@ public class Chromosome {
*/
public Connection getRandomConnection() {
// choose output or node
- int index = Utilities.getRandomInt(inputs.length + Parameters.getNodeCount());
+ int index = Utilities.getRandomInt(inputs.length + ((int) Parameters.get("columns").getValue()) * ((int) Parameters.get("rows").getValue()));
if (index < inputs.length) {
// outputs
@@ -234,7 +234,7 @@ public class Chromosome {
} else {
// node
index -= inputs.length;
- return nodes[index / Parameters.getColumns()][index % Parameters.getColumns()];
+ return nodes[index / ((int) Parameters.get("columns").getValue())][index % ((int) Parameters.get("columns").getValue())];
}
}
@@ -269,15 +269,15 @@ public class Chromosome {
}
public boolean compareTo(Chromosome chromosome) {
- for (int r = 0; r < Parameters.getRows(); r++) {
- for (int c = 0; c < Parameters.getColumns(); c++) {
+ for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) {
+ for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) {
if (!(nodes[r][c].copyOf(chromosome.getNode(r, c)))) {
return false;
}
}
}
- for (int o = 0; o < Parameters.getOutputs(); o++) {
+ for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) {
if (!(outputs[o].copyOf(chromosome.getOutput(o)))) {
return false;
}
@@ -302,11 +302,11 @@ public class Chromosome {
}
public void printNodes() {
- for (int r = 0; r < Parameters.getRows(); r++) {
+ for (int r = 0; r < ((int) Parameters.get("rows").getValue()); r++) {
System.out.print("r: " + r + "\t");
- for (int c = 0; c < Parameters.getColumns(); c++) {
+ for (int c = 0; c < ((int) Parameters.get("columns").getValue()); c++) {
System.out.print("N: (" + r + ", " + c + ") ");
- for (int i = 0; i < Parameters.getMaxArity(); i++) {
+ for (int i = 0; i < ((int) Parameters.get("maxArity").getValue()); i++) {
System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).getDescription() + ") ");
}
System.out.print("F: " + nodes[r][c].getFunction().toString() + "\t");
@@ -314,7 +314,7 @@ public class Chromosome {
System.out.print("\n");
}
- for (int o = 0; o < Parameters.getOutputs(); o++) {
+ for (int o = 0; o < ((int) Parameters.get("outputs").getValue()); o++) {
System.out.print("o: " + o + " (" + outputs[o].getSource().getDescription() + ")\t");
}
}
diff --git a/src/jcgp/population/Node.java b/src/jcgp/population/Node.java
index 4203f25..9fafe32 100644
--- a/src/jcgp/population/Node.java
+++ b/src/jcgp/population/Node.java
@@ -20,7 +20,7 @@ public class Node implements MutableElement, Connection {
this.chromosome = chromosome;
this.column = column;
this.row = row;
- this.connections = new Connection[Parameters.getMaxArity()];
+ this.connections = new Connection[((int) Parameters.get("maxArity").getValue())];
}
@Override
@@ -44,7 +44,7 @@ public class Node implements MutableElement, Connection {
function = newFunction;
- if (newConnections.length >= Parameters.getMaxArity()) {
+ if (newConnections.length >= ((int) Parameters.get("maxArity").getValue())) {
connections = newConnections;
} else {
throw new InsufficientConnectionsException();
diff --git a/src/jcgp/population/Output.java b/src/jcgp/population/Output.java
index 89ca13c..ccecae0 100644
--- a/src/jcgp/population/Output.java
+++ b/src/jcgp/population/Output.java
@@ -2,8 +2,6 @@ package jcgp.population;
import java.util.ArrayList;
-import jcgp.parameters.Parameters;
-
public class Output implements MutableElement {
private Connection source;
@@ -17,9 +15,6 @@ public class Output implements MutableElement {
public Object calculate() {
Object result = source.getValue();
- if (Parameters.getDebug()) {
- System.out.println("Output " + index + ": " + result);
- }
return result;
}
diff --git a/src/jcgp/population/Population.java b/src/jcgp/population/Population.java
index 3e7b590..5718515 100644
--- a/src/jcgp/population/Population.java
+++ b/src/jcgp/population/Population.java
@@ -4,46 +4,26 @@ import jcgp.parameters.Parameters;
public class Population {
- private Chromosome[] parents;
- private Chromosome[] offspring;
+ private Chromosome[] chromosomes;
private Chromosome bestIndividual;
public Population(Chromosome parent) {
- parents = new Chromosome[Parameters.getParentCount()];
+ chromosomes = new Chromosome[((int) Parameters.get("population").getValue())];
// make a clone for safety
- this.parents[0] = new Chromosome(parent);
- // generate the rest of parents
- for (int c = 1; c < parents.length; c++) {
- parents[c] = new Chromosome();
- }
-
- offspring = new Chromosome[Parameters.getOffspringCount()];
- for (int c = 0; c < offspring.length; c++) {
- // completely random offspring? depending on EA, this means the given parent won't be selected
- offspring[c] = new Chromosome();
+ this.chromosomes[0] = new Chromosome(parent);
+ // generate the rest of the individuals
+ for (int c = 1; c < chromosomes.length; c++) {
+ chromosomes[c] = new Chromosome(chromosomes[0]);
}
}
public Population() {
- parents = new Chromosome[Parameters.getParentCount()];
- for (int c = 0; c < parents.length; c++) {
- parents[c] = new Chromosome();
- }
-
- offspring = new Chromosome[Parameters.getOffspringCount()];
- for (int c = 0; c < offspring.length; c++) {
- offspring[c] = new Chromosome();
+ chromosomes = new Chromosome[((int) Parameters.get("population").getValue())];
+ for (int c = 0; c < chromosomes.length; c++) {
+ chromosomes[c] = new Chromosome();
}
}
- public Chromosome getOffspring(int index) {
- return offspring[index];
- }
-
- public Chromosome getParent(int index) {
- return parents[index];
- }
-
/**
* Returns all chromosomes, parents first, then offspring.
*
@@ -51,19 +31,11 @@ public class Population {
* @return
*/
public Chromosome getChromosome(int index) {
- if (index < parents.length) {
- return parents[index];
- } else {
- return offspring[index - parents.length];
- }
+ return chromosomes[index];
}
public void setBestIndividual(int index) {
- if (index < parents.length) {
- bestIndividual = parents[index];
- } else {
- bestIndividual = offspring[index - parents.length];
- }
+ bestIndividual = chromosomes[index];
}
public Chromosome getBestIndividual() {