From 0c288cc1952809294c8d70d86b9f41b04878ac2e Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 23 Mar 2014 18:05:13 +0000 Subject: Majorly refactored, node grid is fully implemented. About to attempt active path locking. --- src/jcgp/modules/mutator/PointMutator.java | 39 ++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'src/jcgp/modules/mutator/PointMutator.java') 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 localParameters; + public PointMutator() { - Parameters.add("mutRate", new IntegerParameter(10, "Mutation rate")); + mutationRate = new DoubleParameter(0.5, "Percent mutation"); + + localParameters = new HashMap(); + 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 activate(Resources parameters) { + return localParameters; + } + } -- cgit v1.2.3