diff options
Diffstat (limited to 'src/jcgp/modules/mutator')
-rw-r--r-- | src/jcgp/modules/mutator/PointMutator.java | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/jcgp/modules/mutator/PointMutator.java b/src/jcgp/modules/mutator/PointMutator.java index e9be153..2298368 100644 --- a/src/jcgp/modules/mutator/PointMutator.java +++ b/src/jcgp/modules/mutator/PointMutator.java @@ -2,6 +2,7 @@ package jcgp.modules.mutator; import java.util.HashMap; +import jcgp.modules.ModuleStatus; import jcgp.parameters.DoubleParameter; import jcgp.parameters.Parameter; import jcgp.JCGP.Resources; @@ -12,9 +13,11 @@ import jcgp.population.Output; public class PointMutator implements Mutator { - private Parameter mutationRate; + private DoubleParameter mutationRate; private HashMap<String, Parameter> localParameters; + private ModuleStatus status = ModuleStatus.READY; + public PointMutator() { mutationRate = new DoubleParameter(0.5, "Percent mutation"); @@ -24,29 +27,24 @@ public class PointMutator implements Mutator { @Override 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))); + 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 + ((int) resources.get("arity"))); + int geneType = resources.getRandomInt(1 + resources.getInt("arity")); if (geneType < 1) { ((Node) m).setFunction(resources.getRandomFunction()); } else { - m.setConnection(resources.getRandomInt((int) resources.get("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); + m.setConnection(resources.getRandomInt(resources.getInt("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); } } } } @Override - public void activate(Resources parameters) { - // nothing - } - - @Override public HashMap<String, Parameter> getLocalParameters() { return localParameters; } @@ -56,4 +54,18 @@ public class PointMutator implements Mutator { 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; + } } |