aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/modules/mutator
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/modules/mutator')
-rw-r--r--src/jcgp/modules/mutator/Mutator.java6
-rw-r--r--src/jcgp/modules/mutator/PointMutator.java39
2 files changed, 30 insertions, 15 deletions
diff --git a/src/jcgp/modules/mutator/Mutator.java b/src/jcgp/modules/mutator/Mutator.java
index 10df8cd..5234f45 100644
--- a/src/jcgp/modules/mutator/Mutator.java
+++ b/src/jcgp/modules/mutator/Mutator.java
@@ -1,9 +1,11 @@
package jcgp.modules.mutator;
+import jcgp.CGP.Resources;
+import jcgp.modules.Module;
import jcgp.population.Chromosome;
-public interface Mutator {
+public interface Mutator extends Module {
- void mutate(Chromosome chromosome);
+ void mutate(Chromosome chromosome, Resources parameters);
}
diff --git a/src/jcgp/modules/mutator/PointMutator.java b/src/jcgp/modules/mutator/PointMutator.java
index b06d678..ea1ed32 100644
--- a/src/jcgp/modules/mutator/PointMutator.java
+++ b/src/jcgp/modules/mutator/PointMutator.java
@@ -1,36 +1,49 @@
package jcgp.modules.mutator;
-import jcgp.Utilities;
-import jcgp.parameters.IntegerParameter;
-import jcgp.parameters.Parameters;
+import java.util.HashMap;
+
+import jcgp.parameters.DoubleParameter;
+import jcgp.parameters.Parameter;
+import jcgp.CGP.Resources;
import jcgp.population.Chromosome;
import jcgp.population.MutableElement;
import jcgp.population.Node;
import jcgp.population.Output;
public class PointMutator implements Mutator {
-
+
+ private Parameter mutationRate;
+ private HashMap<String, Parameter> localParameters;
+
public PointMutator() {
- Parameters.add("mutRate", new IntegerParameter(10, "Mutation rate"));
+ mutationRate = new DoubleParameter(0.5, "Percent mutation");
+
+ localParameters = new HashMap<String, Parameter>();
+ localParameters.put("mutRate", mutationRate);
}
-
+
@Override
- public void mutate(Chromosome chromosome) {
- int mutations = (int) (((int) Parameters.get("mutRate").getValue()) * ((((double) Parameters.get("nodes").getValue()) + ((double) Parameters.get("outputs").getValue())) / 100));
-
+ public void mutate(Chromosome chromosome, Resources resources) {
+ int mutations = (int) Math.ceil((((double) mutationRate.getValue()) * (((((Integer) resources.get("nodes")).doubleValue() + ((Integer) resources.get("outputs")).doubleValue())) / (double) 100)));
for (int i = 0; i < mutations; i++) {
MutableElement m = chromosome.getRandomMutableElement();
if (m instanceof Output) {
- m.setConnection(chromosome.getRandomConnection());
+ m.setConnection(0, chromosome.getRandomConnection());
} else if (m instanceof Node) {
- int geneType = Utilities.getRandomInt(1 + ((int) Parameters.get("Max arity").getValue()));
+ int geneType = resources.getRandomInt(1 + ((int) resources.get("arity")));
if (geneType < 1) {
- ((Node) m).setFunction(Utilities.getRandomFunction());
+ ((Node) m).setFunction(resources.getRandomFunction());
} else {
- m.setConnection(chromosome.getRandomConnection(((Node) m).getColumn()));
+ m.setConnection(resources.getRandomInt((int) resources.get("arity")), chromosome.getRandomConnection(((Node) m).getColumn()));
}
}
}
}
+
+ @Override
+ public HashMap<String, Parameter> activate(Resources parameters) {
+ return localParameters;
+ }
+
}