aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-04-07 15:58:30 +0100
committerEduardo Pedroni <ep625@york.ac.uk>2014-04-07 15:58:30 +0100
commit226365bd6a0f56143ffbce7379857ff3e126fbaf (patch)
tree4582b16a5519c94b121a369104157859dc97cba1 /src/jcgp/backend
parent67ace66f66ffaa00e1bd1495c0d406c801e59c5c (diff)
Refactoring resources for (hopefully) the last time
Diffstat (limited to 'src/jcgp/backend')
-rw-r--r--src/jcgp/backend/modules/Module.java2
-rw-r--r--src/jcgp/backend/modules/es/EvolutionaryStrategy.java (renamed from src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java)6
-rw-r--r--src/jcgp/backend/modules/es/MuPlusLambda.java (renamed from src/jcgp/backend/modules/ea/MuPlusLambda.java)13
-rw-r--r--src/jcgp/backend/modules/es/TournamentSelection.java (renamed from src/jcgp/backend/modules/ea/TournamentSelection.java)4
-rw-r--r--src/jcgp/backend/modules/fitness/DigitalCircuit.java5
-rw-r--r--src/jcgp/backend/modules/fitness/SymbolicRegression.java5
-rw-r--r--src/jcgp/backend/modules/fitness/TestCaseProblem.java4
-rw-r--r--src/jcgp/backend/modules/mutator/Mutator.java2
-rw-r--r--src/jcgp/backend/modules/mutator/PointMutator.java32
-rw-r--r--src/jcgp/backend/population/Chromosome.java4
-rw-r--r--src/jcgp/backend/population/Connection.java2
-rw-r--r--src/jcgp/backend/population/Input.java4
-rw-r--r--src/jcgp/backend/population/Node.java4
-rw-r--r--src/jcgp/backend/population/Output.java10
-rw-r--r--src/jcgp/backend/resources/ModifiableResources.java17
-rw-r--r--src/jcgp/backend/resources/Resources.java36
-rw-r--r--src/jcgp/backend/tests/NodeTests.java34
-rw-r--r--src/jcgp/backend/tests/OutputTests.java12
18 files changed, 97 insertions, 99 deletions
diff --git a/src/jcgp/backend/modules/Module.java b/src/jcgp/backend/modules/Module.java
index f2b91b0..8a95737 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 Parameter[] getLocalParameters();
+ public abstract Parameter[] getLocalParameters();
}
diff --git a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
index 3aca104..1117e99 100644
--- a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java
+++ b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
@@ -1,13 +1,13 @@
-package jcgp.backend.modules.ea;
+package jcgp.backend.modules.es;
import jcgp.backend.modules.Module;
import jcgp.backend.modules.mutator.Mutator;
import jcgp.backend.population.Population;
import jcgp.backend.resources.Resources;
-public interface EvolutionaryAlgorithm extends Module {
+public interface EvolutionaryStrategy extends Module {
- public abstract void evolve(Population population, Mutator mutator, Resources parameters);
+ public abstract void evolve(Population population, Mutator mutator, Resources resources);
public abstract int getFittestChromosome();
diff --git a/src/jcgp/backend/modules/ea/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java
index 0d16111..53fb932 100644
--- a/src/jcgp/backend/modules/ea/MuPlusLambda.java
+++ b/src/jcgp/backend/modules/es/MuPlusLambda.java
@@ -1,4 +1,4 @@
-package jcgp.backend.modules.ea;
+package jcgp.backend.modules.es;
import jcgp.backend.modules.mutator.Mutator;
import jcgp.backend.population.Population;
@@ -15,7 +15,7 @@ import jcgp.backend.resources.parameters.ParameterStatus;
* @author Eduardo Pedroni
*
*/
-public class MuPlusLambda implements EvolutionaryAlgorithm {
+public class MuPlusLambda implements EvolutionaryStrategy {
private int fittestChromosome;
@@ -58,22 +58,27 @@ public class MuPlusLambda implements EvolutionaryAlgorithm {
}
};
}
-
+
@Override
public void evolve(Population population, Mutator mutator, Resources resources) {
// select fittest chromosomes
fittestChromosome = 0;
- for (int i = 1; i < resources.getInt("popSize"); i++) {
+ if (report.get()) resources.reportln("[ES] Selecting fittest chromosome...");
+ for (int i = 0; i < resources.getInt("popSize"); 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;
}
}
+ 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++) {
if (i != fittestChromosome) {
+ if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i);
population.copyChromosome(fittestChromosome, i);
+ if (report.get()) resources.reportln("[ES] Mutating copied chromosome");
mutator.mutate(population.getChromosome(i), resources);
}
}
diff --git a/src/jcgp/backend/modules/ea/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java
index baf6704..3954de8 100644
--- a/src/jcgp/backend/modules/ea/TournamentSelection.java
+++ b/src/jcgp/backend/modules/es/TournamentSelection.java
@@ -1,4 +1,4 @@
-package jcgp.backend.modules.ea;
+package jcgp.backend.modules.es;
import java.util.HashMap;
@@ -8,7 +8,7 @@ import jcgp.backend.resources.Resources;
import jcgp.backend.resources.parameters.IntegerParameter;
import jcgp.backend.resources.parameters.Parameter;
-public class TournamentSelection implements EvolutionaryAlgorithm {
+public class TournamentSelection implements EvolutionaryStrategy {
private int fittestChromosome;
diff --git a/src/jcgp/backend/modules/fitness/DigitalCircuit.java b/src/jcgp/backend/modules/fitness/DigitalCircuit.java
index b01bdc5..8677d5f 100644
--- a/src/jcgp/backend/modules/fitness/DigitalCircuit.java
+++ b/src/jcgp/backend/modules/fitness/DigitalCircuit.java
@@ -1,11 +1,12 @@
package jcgp.backend.modules.fitness;
import jcgp.backend.function.BitwiseLogic;
+import jcgp.backend.resources.Resources;
public class DigitalCircuit extends TestCaseProblem<Integer> {
- public DigitalCircuit() {
- super();
+ public DigitalCircuit(Resources resources) {
+ super(resources);
functionSet = new BitwiseLogic();
}
diff --git a/src/jcgp/backend/modules/fitness/SymbolicRegression.java b/src/jcgp/backend/modules/fitness/SymbolicRegression.java
index da2e69e..cb9d1a7 100644
--- a/src/jcgp/backend/modules/fitness/SymbolicRegression.java
+++ b/src/jcgp/backend/modules/fitness/SymbolicRegression.java
@@ -1,11 +1,12 @@
package jcgp.backend.modules.fitness;
import jcgp.backend.function.IntegerArithmetic;
+import jcgp.backend.resources.Resources;
public class SymbolicRegression extends TestCaseProblem<Integer> {
- public SymbolicRegression() {
- super();
+ public SymbolicRegression(Resources resources) {
+ super(resources);
functionSet = new IntegerArithmetic();
}
diff --git a/src/jcgp/backend/modules/fitness/TestCaseProblem.java b/src/jcgp/backend/modules/fitness/TestCaseProblem.java
index 7753e26..4259285 100644
--- a/src/jcgp/backend/modules/fitness/TestCaseProblem.java
+++ b/src/jcgp/backend/modules/fitness/TestCaseProblem.java
@@ -51,7 +51,9 @@ public abstract class TestCaseProblem<U> extends Problem {
private ArrayList<TestCase<U>> testCases;
private IntegerParameter maxFitness;
- public TestCaseProblem() {
+ public TestCaseProblem(Resources resources) {
+ super();
+
maxFitness = new IntegerParameter(0, "Max fitness", true, false) {
@Override
public void validate(int newValue) {
diff --git a/src/jcgp/backend/modules/mutator/Mutator.java b/src/jcgp/backend/modules/mutator/Mutator.java
index ffdd35c..1d5b99a 100644
--- a/src/jcgp/backend/modules/mutator/Mutator.java
+++ b/src/jcgp/backend/modules/mutator/Mutator.java
@@ -6,6 +6,6 @@ import jcgp.backend.resources.Resources;
public interface Mutator extends Module {
- void mutate(Chromosome chromosome, Resources parameters);
+ void mutate(Chromosome chromosome, Resources resources);
}
diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java
index 54d5f3d..1f38cfe 100644
--- a/src/jcgp/backend/modules/mutator/PointMutator.java
+++ b/src/jcgp/backend/modules/mutator/PointMutator.java
@@ -1,11 +1,11 @@
package jcgp.backend.modules.mutator;
-import jcgp.backend.function.Function;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.MutableElement;
import jcgp.backend.population.Node;
import jcgp.backend.population.Output;
import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.parameters.BooleanParameter;
import jcgp.backend.resources.parameters.DoubleParameter;
import jcgp.backend.resources.parameters.Parameter;
import jcgp.backend.resources.parameters.ParameterStatus;
@@ -13,6 +13,7 @@ import jcgp.backend.resources.parameters.ParameterStatus;
public class PointMutator implements Mutator {
private DoubleParameter mutationRate;
+ private BooleanParameter report;
public PointMutator(final Resources resources) {
mutationRate = new DoubleParameter(50, "Percent mutation", false, false) {
@@ -29,23 +30,44 @@ public class PointMutator implements Mutator {
}
}
};
+
+ report = new BooleanParameter(false, "Report") {
+ @Override
+ public void validate(boolean newValue) {
+ // blank
+ }
+ };
}
@Override
public void mutate(Chromosome chromosome, Resources resources) {
int mutations = (int) ((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("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();
+ if (report.get()) resources.report("[Mutator] Mutation " + i + " selected " + m.toString() + ", ");
if (m instanceof Output) {
+ if (report.get()) resources.report("changed source from " + ((Output) m).getSource().toString() + " ");
+
m.setConnection(0, chromosome.getRandomConnection());
+
+ if (report.get()) resources.reportln("to " + ((Output) m).getSource().toString());
} else if (m instanceof Node) {
int geneType = resources.getRandomInt(1 + resources.getInt("arity"));
if (geneType < 1) {
- Function f = resources.getRandomFunction();
- ((Node) m).setFunction(f);
+ if (report.get()) resources.report("changed function from " + ((Node) m).getFunction().getName() + " ");
+
+ ((Node) m).setFunction(resources.getRandomFunction());
+
+ if (report.get()) resources.reportln("to " + ((Node) m).getFunction().getName());
} else {
- m.setConnection(resources.getRandomInt(resources.getInt("arity")), chromosome.getRandomConnection(((Node) m).getColumn()));
+ int connection = resources.getRandomInt(resources.getInt("arity"));
+ if (report.get()) resources.report("changed connection " + connection + " from " + ((Node) m).getConnection(connection) + " ");
+
+ m.setConnection(connection, chromosome.getRandomConnection(((Node) m).getColumn()));
+
+ if (report.get()) resources.reportln("to " + ((Node) m).getConnection(connection));
}
}
}
@@ -53,7 +75,7 @@ public class PointMutator implements Mutator {
@Override
public Parameter[] getLocalParameters() {
- return new Parameter[] {mutationRate};
+ return new Parameter[] {mutationRate, report};
}
@Override
diff --git a/src/jcgp/backend/population/Chromosome.java b/src/jcgp/backend/population/Chromosome.java
index d438375..bbada14 100644
--- a/src/jcgp/backend/population/Chromosome.java
+++ b/src/jcgp/backend/population/Chromosome.java
@@ -318,7 +318,7 @@ public class Chromosome {
for (int c = 0; c < (resources.getInt("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).getDescription() + ") ");
+ System.out.print("C" + i + ": (" + nodes[r][c].getConnection(i).toString() + ") ");
}
System.out.print("F: " + nodes[r][c].getFunction().getName() + "\t");
}
@@ -326,7 +326,7 @@ public class Chromosome {
}
for (int o = 0; o < (resources.getInt("outputs")); o++) {
- System.out.print("o: " + o + " (" + outputs[o].getSource().getDescription() + ")\t");
+ System.out.print("o: " + o + " (" + outputs[o].getSource().toString() + ")\t");
}
}
diff --git a/src/jcgp/backend/population/Connection.java b/src/jcgp/backend/population/Connection.java
index ea4f10f..cd0f5ce 100644
--- a/src/jcgp/backend/population/Connection.java
+++ b/src/jcgp/backend/population/Connection.java
@@ -3,7 +3,5 @@ package jcgp.backend.population;
public interface Connection {
public Object getValue();
-
- public String getDescription();
}
diff --git a/src/jcgp/backend/population/Input.java b/src/jcgp/backend/population/Input.java
index 83fba07..2661f8c 100644
--- a/src/jcgp/backend/population/Input.java
+++ b/src/jcgp/backend/population/Input.java
@@ -23,7 +23,7 @@ public class Input extends Gene implements Connection {
}
@Override
- public String getDescription() {
- return "i: " + index;
+ public String toString() {
+ return "Input " + index;
}
}
diff --git a/src/jcgp/backend/population/Node.java b/src/jcgp/backend/population/Node.java
index 6960ded..87a2f99 100644
--- a/src/jcgp/backend/population/Node.java
+++ b/src/jcgp/backend/population/Node.java
@@ -106,7 +106,7 @@ public class Node extends Gene implements MutableElement, Connection {
}
@Override
- public String getDescription() {
- return "n: " + row + ", " + column;
+ public String toString() {
+ return "Node [" + row + ", " + column + "]";
}
}
diff --git a/src/jcgp/backend/population/Output.java b/src/jcgp/backend/population/Output.java
index aa94ab6..d876951 100644
--- a/src/jcgp/backend/population/Output.java
+++ b/src/jcgp/backend/population/Output.java
@@ -11,7 +11,6 @@ public class Output extends Gene implements MutableElement {
public Output(Chromosome chromosome, int index) {
this.chromosome = chromosome;
this.index = index;
- //this.source = new SimpleObjectProperty<Connection>();
}
public Object calculate() {
@@ -32,10 +31,6 @@ public class Output extends Gene implements MutableElement {
public Connection getSource() {
return source;
}
-
-// public SimpleObjectProperty<Connection> sourceProperty() {
-// return source;
-// }
public void getActiveNodes(ArrayList<Node> activeNodes) {
if (source instanceof Node) {
@@ -66,4 +61,9 @@ public class Output extends Gene implements MutableElement {
}
return false;
}
+
+ @Override
+ public String toString() {
+ return "Output " + index;
+ }
}
diff --git a/src/jcgp/backend/resources/ModifiableResources.java b/src/jcgp/backend/resources/ModifiableResources.java
index 90c2f03..a221f73 100644
--- a/src/jcgp/backend/resources/ModifiableResources.java
+++ b/src/jcgp/backend/resources/ModifiableResources.java
@@ -40,4 +40,21 @@ public class ModifiableResources extends Resources {
this.console = console;
}
+ /*
+ * Console functionality
+ */
+ public void println(String s) {
+ System.out.println(s);
+ if (console != null) {
+ console.println(s);
+ }
+ }
+
+ public void print(String s) {
+ System.out.print(s);
+ if (console != null) {
+ console.print(s);
+ }
+ }
+
}
diff --git a/src/jcgp/backend/resources/Resources.java b/src/jcgp/backend/resources/Resources.java
index d1f396a..13e0c51 100644
--- a/src/jcgp/backend/resources/Resources.java
+++ b/src/jcgp/backend/resources/Resources.java
@@ -266,31 +266,29 @@ public class Resources {
return functionSet;
}
-// /*
-// * Test cases
-// */
-// public TestCase getTestCase(int index) {
-// return testCases[index];
-// }
-//
-// public int getTestCaseCount() {
-// return testCases.length;
-// }
-
/*
* Console functionality
+ * These are affected by parameter report
*/
- public void println(String s) {
- System.out.println(s);
- if (console != null) {
- console.println(s);
+ public void reportln(String s) {
+ if (getInt("report") > 0) {
+ if (getInt("currentGen") % getInt("report") == 0) {
+ System.out.println(s);
+ if (console != null) {
+ console.println(s);
+ }
+ }
}
}
- public void print(String s) {
- System.out.print(s);
- if (console != null) {
- console.print(s);
+ public void report(String s) {
+ if (getInt("report") > 0) {
+ if (getInt("currentGen") % getInt("report") == 0) {
+ System.out.print(s);
+ if (console != null) {
+ console.print(s);
+ }
+ }
}
}
} \ No newline at end of file
diff --git a/src/jcgp/backend/tests/NodeTests.java b/src/jcgp/backend/tests/NodeTests.java
index 2294816..e8fe02f 100644
--- a/src/jcgp/backend/tests/NodeTests.java
+++ b/src/jcgp/backend/tests/NodeTests.java
@@ -57,13 +57,6 @@ public class NodeTests {
// hardcode a value
return arg1;
}
-
- @Override
- public String getDescription() {
- // blank
- return null;
- }
-
},
new Connection() {
@@ -72,13 +65,6 @@ public class NodeTests {
// hardcode a value
return arg2;
}
-
- @Override
- public String getDescription() {
- // blank
- return null;
- }
-
}});
}
@@ -144,13 +130,6 @@ public class NodeTests {
// blank
return 0;
}
-
- @Override
- public String getDescription() {
- // blank
- return null;
- }
-
};
conn1 = new Connection() {
@@ -159,13 +138,6 @@ public class NodeTests {
// blank
return 0;
}
-
- @Override
- public String getDescription() {
- // blank
- return null;
- }
-
};
Function function = new Function() {
@@ -201,12 +173,6 @@ public class NodeTests {
// blank
return 0;
}
-
- @Override
- public String getDescription() {
- // blank
- return null;
- }
};
node.setConnection(resources.getRandomInt(resources.getInt("arity")), conn2);
diff --git a/src/jcgp/backend/tests/OutputTests.java b/src/jcgp/backend/tests/OutputTests.java
index b2bc7ec..afe44a7 100644
--- a/src/jcgp/backend/tests/OutputTests.java
+++ b/src/jcgp/backend/tests/OutputTests.java
@@ -52,12 +52,6 @@ public class OutputTests {
// test value
return outputValue;
}
-
- @Override
- public String getDescription() {
- // blank
- return null;
- }
});
assertTrue("Incorrect evaluation.", ((Integer) output.calculate()) == outputValue);
@@ -73,12 +67,6 @@ public class OutputTests {
// blank
return 0;
}
-
- @Override
- public String getDescription() {
- // blank
- return null;
- }
};
output.setConnection(0, newConn);