From b0c0698e5503c2506217117bf144fde31e6f6601 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 25 Apr 2014 19:38:16 +0100 Subject: Commented lots of packages. --- src/jcgp/backend/modules/mutator/PointMutator.java | 97 +++++++++------------- 1 file changed, 39 insertions(+), 58 deletions(-) (limited to 'src/jcgp/backend/modules/mutator/PointMutator.java') diff --git a/src/jcgp/backend/modules/mutator/PointMutator.java b/src/jcgp/backend/modules/mutator/PointMutator.java index 44c453a..17c6996 100644 --- a/src/jcgp/backend/modules/mutator/PointMutator.java +++ b/src/jcgp/backend/modules/mutator/PointMutator.java @@ -6,88 +6,69 @@ import jcgp.backend.population.Node; import jcgp.backend.population.Output; import jcgp.backend.resources.Resources; import jcgp.backend.resources.parameters.BooleanParameter; -import jcgp.backend.resources.parameters.DoubleParameter; import jcgp.backend.resources.parameters.IntegerParameter; -import jcgp.backend.resources.parameters.Parameter; -import jcgp.backend.resources.parameters.ParameterStatus; -public class PointMutator implements Mutator { - - private DoubleParameter mutationRate; - private IntegerParameter nodesMutated; - private BooleanParameter report; +/** + * Point mutator + *

+ * In point mutation, a number of random genes + * is picked and mutated until all required + * mutations have been performed. The actual number + * of genes to be mutated can be defined in any + * arbitrary way. + * + * @author Eduardo Pedroni + * + */ +public abstract class PointMutator implements Mutator { - public PointMutator(final Resources resources) { - mutationRate = new DoubleParameter(50, "Percent mutation", false, false) { - @Override - public void validate(Number newValue) { - nodesMutated.set((int) ((newValue.intValue()) * (((((double) resources.nodes() + resources.outputs()))) / 100))); - if (newValue.doubleValue() <= 0 || newValue.doubleValue() > 100) { - status = ParameterStatus.INVALID; - status.setDetails("Mutation rate must be > 0 and <= 100"); - } else if ((int) ((newValue.doubleValue() / 100) * (double) resources.nodes()) <= 0) { - status = ParameterStatus.WARNING; - status.setDetails("With mutation rate " + mutationRate.get() + ", 0 genes will be mutated."); - } else { - status = ParameterStatus.VALID; - } - } - }; - nodesMutated = new IntegerParameter(0, "Genes mutated", true, false) { - @Override - public void validate(Number newValue) { - // blank - } - }; - report = new BooleanParameter(false, "Report") { - @Override - public void validate(Boolean newValue) { - // blank - } - }; - } + protected IntegerParameter genesMutated; + protected BooleanParameter report; @Override public void mutate(Chromosome chromosome, Resources resources) { - if (report.get()) resources.reportln("[Mutator] Number of mutations to be performed: " + nodesMutated.get()); - for (int i = 0; i < nodesMutated.get(); i++) { + if (report.get()) resources.reportln("[Mutator] Number of mutations to be performed: " + genesMutated.get()); + + // for however many genes must be mutated + for (int i = 0; i < genesMutated.get(); i++) { + MutableElement m = chromosome.getRandomMutableElement(); - if (report.get()) resources.report("[Mutator] Mutation " + i + " selected " + m.toString() + ", "); + if (report.get()) resources.report("[Mutator] Mutation " + i + " selected " + m + ", "); + + // outputs and nodes are mutated differently if (m instanceof Output) { - if (report.get()) resources.report("changed source from " + ((Output) m).getSource().toString() + " "); + if (report.get()) resources.report("changed source from " + ((Output) m).getSource() + " "); + // outputs are easy, simply set to a different random connection, any will do m.setConnection(0, chromosome.getRandomConnection()); - if (report.get()) resources.reportln("to " + ((Output) m).getSource().toString()); + if (report.get()) resources.reportln("to " + ((Output) m).getSource()); } else if (m instanceof Node) { + /* nodes are more complicated, first we must decide whether to mutate the function + * or a connection + * we do this by generating a random int between 0 and 1 + arity + */ int geneType = resources.getRandomInt(1 + resources.arity()); + + // if the int is less than 1, mutate function, else mutate connections if (geneType < 1) { - if (report.get()) resources.report("changed function from " + ((Node) m).getFunction().getName() + " "); + if (report.get()) resources.report("changed function from " + ((Node) m).getFunction() + " "); ((Node) m).setFunction(resources.getRandomFunction()); - if (report.get()) resources.reportln("to " + ((Node) m).getFunction().getName()); + if (report.get()) resources.reportln("to " + ((Node) m).getFunction()); } else { - int connection = resources.getRandomInt(resources.arity()); - if (report.get()) resources.report("changed connection " + connection + " from " + ((Node) m).getConnection(connection) + " "); + // if we decided to mutate connection, subtract 1 from geneType so it fits into the arity range + geneType -= 1; + if (report.get()) resources.report("changed connection " + geneType + " from " + ((Node) m).getConnection(geneType) + " "); - m.setConnection(connection, chromosome.getRandomConnection(((Node) m).getColumn())); + m.setConnection(geneType, chromosome.getRandomConnection(((Node) m).getColumn())); - if (report.get()) resources.reportln("to " + ((Node) m).getConnection(connection)); + if (report.get()) resources.reportln("to " + ((Node) m).getConnection(geneType)); } } } } - @Override - public Parameter[] getLocalParameters() { - return new Parameter[] {mutationRate, nodesMutated, report}; - } - - @Override - public String toString() { - return "Point mutation"; - } - } -- cgit v1.2.3