diff options
Diffstat (limited to 'src/jcgp/backend/modules/mutator')
-rw-r--r-- | src/jcgp/backend/modules/mutator/Mutator.java | 11 | ||||
-rw-r--r-- | src/jcgp/backend/modules/mutator/PointMutator.java | 73 |
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; + } +} |