diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-04 16:20:33 +0000 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-02-04 16:20:33 +0000 |
commit | 1fd678821cf133c7c431daea687f3467bb0be2dd (patch) | |
tree | c23e7fe6a4076c6748d03b2cee9d41ff6e9734b2 /src/jcgp/ea | |
parent | d9671c354080de20bba4f70438af9242c8ecd675 (diff) |
Added fitness evaluation mechanism, though it might not be a very good design. Tests will be done to verify that.
Diffstat (limited to 'src/jcgp/ea')
-rw-r--r-- | src/jcgp/ea/EvolutionaryAlgorithm.java | 14 | ||||
-rw-r--r-- | src/jcgp/ea/StandardEA.java | 11 | ||||
-rw-r--r-- | src/jcgp/ea/StandardMutator.java | 22 |
3 files changed, 31 insertions, 16 deletions
diff --git a/src/jcgp/ea/EvolutionaryAlgorithm.java b/src/jcgp/ea/EvolutionaryAlgorithm.java index 6e264aa..9e72a1b 100644 --- a/src/jcgp/ea/EvolutionaryAlgorithm.java +++ b/src/jcgp/ea/EvolutionaryAlgorithm.java @@ -2,14 +2,14 @@ package jcgp.ea; import jcgp.population.Population; -public abstract class EvolutionaryAlgorithm { +public interface EvolutionaryAlgorithm { - protected Mutator mutator; +// protected Mutator mutator; +// +// public EvolutionaryAlgorithm(Mutator mutator) { +// this.mutator = mutator; +// } - public EvolutionaryAlgorithm(Mutator mutator) { - this.mutator = mutator; - } - - public abstract void evolve(Population population); + public abstract void evolve(Population population, Mutator mutator); } diff --git a/src/jcgp/ea/StandardEA.java b/src/jcgp/ea/StandardEA.java index fe0b3f9..b98fe9e 100644 --- a/src/jcgp/ea/StandardEA.java +++ b/src/jcgp/ea/StandardEA.java @@ -3,15 +3,12 @@ package jcgp.ea; import jcgp.population.Chromosome; import jcgp.population.Population; -public class StandardEA extends EvolutionaryAlgorithm { - - public StandardEA(Mutator mutator) { - super(mutator); - - } +public class StandardEA implements EvolutionaryAlgorithm { @Override - public void evolve(Population population) { + public void evolve(Population population, Mutator mutator) { + + for (Chromosome chromosome : population) { mutator.mutate(chromosome); } diff --git a/src/jcgp/ea/StandardMutator.java b/src/jcgp/ea/StandardMutator.java index 731215d..cab5c76 100644 --- a/src/jcgp/ea/StandardMutator.java +++ b/src/jcgp/ea/StandardMutator.java @@ -1,13 +1,31 @@ package jcgp.ea; +import jcgp.CGP.Parameters; +import jcgp.CGP.Utilities; import jcgp.population.Chromosome; +import jcgp.population.MutableElement; +import jcgp.population.Node; +import jcgp.population.Output; public class StandardMutator implements Mutator { @Override public void mutate(Chromosome chromosome) { + int mutations = (int) (Parameters.getMutationRate() * ((double) Parameters.getNodeNumber() / 100)); - + for (int i = 0; i < mutations; i++) { + MutableElement m = Utilities.getRandomMutable(chromosome); + + if (m instanceof Output) { + m.setConnection(Utilities.getRandomConnection(chromosome, m.getColumn())); + } else if (m instanceof Node) { + int geneType = Utilities.getRandomInt(1 + Parameters.getMaxArity()); + if (geneType < 1) { + ((Node) m).setFunction(Utilities.getRandomFunction()); + } else { + m.setConnection(Utilities.getRandomConnection(chromosome, m.getColumn())); + } + } + } } - } |