package jcgp.modules.mutator; import java.util.HashMap; import jcgp.parameters.DoubleParameter; import jcgp.parameters.Parameter; import jcgp.JCGP.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 localParameters; public PointMutator() { mutationRate = new DoubleParameter(0.5, "Percent mutation"); localParameters = new HashMap(); localParameters.put("mutRate", mutationRate); } @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))); 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"))); if (geneType < 1) { ((Node) m).setFunction(resources.getRandomFunction()); } else { m.setConnection(resources.getRandomInt((int) resources.get("arity")), chromosome.getRandomConnection(((Node) m).getColumn())); } } } } @Override public void activate(Resources parameters) { // nothing } @Override public HashMap getLocalParameters() { return localParameters; } @Override public String toString() { return "Point mutation"; } }