aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/ea
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/ea')
-rw-r--r--src/jcgp/ea/EvolutionaryAlgorithm.java14
-rw-r--r--src/jcgp/ea/StandardEA.java11
-rw-r--r--src/jcgp/ea/StandardMutator.java22
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()));
+ }
+ }
+ }
}
-
}