aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/mutator
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-04-01 23:00:53 +0100
committerEduardo Pedroni <ep625@york.ac.uk>2014-04-01 23:00:53 +0100
commit02fd2bc7059da416937beb1abe67e5ca60379030 (patch)
tree609341fe10aaa0f2dc45a1e72eba20bd24fb1281 /src/jcgp/backend/modules/mutator
parenta757deacded0d7357a9f68462d3f2051e16004ee (diff)
Settings pane now actually controls the parameters, not much left to do.
Diffstat (limited to 'src/jcgp/backend/modules/mutator')
-rw-r--r--src/jcgp/backend/modules/mutator/Mutator.java11
-rw-r--r--src/jcgp/backend/modules/mutator/PointMutator.java73
2 files changed, 84 insertions, 0 deletions
diff --git a/src/jcgp/backend/modules/mutator/Mutator.java b/src/jcgp/backend/modules/mutator/Mutator.java
new file mode 100644
index 0000000..fd7cd27
--- /dev/null
+++ b/src/jcgp/backend/modules/mutator/Mutator.java
@@ -0,0 +1,11 @@
+package jcgp.backend.modules.mutator;
+
+import jcgp.JCGP.Resources;
+import jcgp.backend.modules.Module;
+import jcgp.backend.population.Chromosome;
+
+public interface Mutator extends Module {
+
+ void mutate(Chromosome chromosome, Resources parameters);
+
+}
diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java
new file mode 100644
index 0000000..62d827d
--- /dev/null
+++ b/src/jcgp/backend/modules/mutator/PointMutator.java
@@ -0,0 +1,73 @@
+package jcgp.backend.modules.mutator;
+
+import java.util.HashMap;
+
+import jcgp.backend.function.Function;
+import jcgp.backend.modules.ModuleStatus;
+import jcgp.backend.parameters.DoubleParameter;
+import jcgp.backend.parameters.Parameter;
+import jcgp.backend.population.Chromosome;
+import jcgp.backend.population.MutableElement;
+import jcgp.backend.population.Node;
+import jcgp.backend.population.Output;
+import jcgp.JCGP.Resources;
+
+public class PointMutator implements Mutator {
+
+ private DoubleParameter mutationRate;
+ private HashMap<String, Parameter> localParameters;
+
+ private ModuleStatus status = ModuleStatus.READY;
+
+ public PointMutator() {
+ mutationRate = new DoubleParameter(0.5, "Percent mutation", false, false, false);
+
+ localParameters = new HashMap<String, Parameter>();
+ localParameters.put("mutRate", mutationRate);
+ }
+
+ @Override
+ public void mutate(Chromosome chromosome, Resources resources) {
+ int mutations = (int) Math.ceil(((mutationRate.get()) * ((((resources.getDouble("nodes")) + (resources.getDouble("outputs")))) / (double) 100)));
+ for (int i = 0; i < mutations; i++) {
+ MutableElement m = chromosome.getRandomMutableElement();
+
+ if (m instanceof Output) {
+ m.setConnection(0, chromosome.getRandomConnection());
+ } else if (m instanceof Node) {
+ int geneType = resources.getRandomInt(1 + resources.getInt("arity"));
+ if (geneType < 1) {
+ Function f = resources.getRandomFunction();
+ ((Node) m).setFunction(f);
+ } else {
+ m.setConnection(resources.getRandomInt(resources.getInt("arity")), chromosome.getRandomConnection(((Node) m).getColumn()));
+ }
+ }
+ }
+ }
+
+ @Override
+ public HashMap<String, Parameter> getLocalParameters() {
+ return localParameters;
+ }
+
+ @Override
+ public String toString() {
+ return "Point mutation";
+ }
+
+ @Override
+ public ModuleStatus getStatus(Resources resources) {
+ if (mutationRate.get() <= 0 || mutationRate.get() > 100) {
+ status = ModuleStatus.ERROR;
+ status.setDetails("Mutation rate must be > 0 and <= 100");
+ } else if ((int) ((mutationRate.get() / 100) * resources.getDouble("nodes")) > 0) {
+ status = ModuleStatus.WARNING;
+ status.setDetails("With mutation rate " + mutationRate.get() + ", no mutations will occur.");
+ } else {
+ status = ModuleStatus.READY;
+ status.setDetails("");
+ }
+ return status;
+ }
+}