aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/mutator
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/modules/mutator
parent67ace66f66ffaa00e1bd1495c0d406c801e59c5c (diff)
Refactoring resources for (hopefully) the last time
Diffstat (limited to 'src/jcgp/backend/modules/mutator')
-rw-r--r--src/jcgp/backend/modules/mutator/Mutator.java2
-rw-r--r--src/jcgp/backend/modules/mutator/PointMutator.java32
2 files changed, 28 insertions, 6 deletions
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