aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/es
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/modules/es')
-rw-r--r--src/jcgp/backend/modules/es/EvolutionaryStrategy.java14
-rw-r--r--src/jcgp/backend/modules/es/MuPlusLambda.java103
-rw-r--r--src/jcgp/backend/modules/es/TournamentSelection.java53
3 files changed, 170 insertions, 0 deletions
diff --git a/src/jcgp/backend/modules/es/EvolutionaryStrategy.java b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
new file mode 100644
index 0000000..1117e99
--- /dev/null
+++ b/src/jcgp/backend/modules/es/EvolutionaryStrategy.java
@@ -0,0 +1,14 @@
+package jcgp.backend.modules.es;
+
+import jcgp.backend.modules.Module;
+import jcgp.backend.modules.mutator.Mutator;
+import jcgp.backend.population.Population;
+import jcgp.backend.resources.Resources;
+
+public interface EvolutionaryStrategy extends Module {
+
+ public abstract void evolve(Population population, Mutator mutator, Resources resources);
+
+ public abstract int getFittestChromosome();
+
+}
diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java
new file mode 100644
index 0000000..53fb932
--- /dev/null
+++ b/src/jcgp/backend/modules/es/MuPlusLambda.java
@@ -0,0 +1,103 @@
+package jcgp.backend.modules.es;
+
+import jcgp.backend.modules.mutator.Mutator;
+import jcgp.backend.population.Population;
+import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.parameters.BooleanParameter;
+import jcgp.backend.resources.parameters.IntegerParameter;
+import jcgp.backend.resources.parameters.Parameter;
+import jcgp.backend.resources.parameters.ParameterStatus;
+
+/**
+ * (μ + λ) EA.
+ *
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public class MuPlusLambda implements EvolutionaryStrategy {
+
+ private int fittestChromosome;
+
+ private IntegerParameter parents, offspring;
+ private BooleanParameter report;
+
+ public MuPlusLambda(final Resources resources) {
+ parents = new IntegerParameter(1, "Parents") {
+ @Override
+ public void validate(int newValue) {
+ if (newValue + offspring.get() != resources.getInt("popSize")) {
+ status = ParameterStatus.INVALID;
+ status.setDetails("Parents + offspring must equal population size.");
+ } else if (newValue <= 0) {
+ status = ParameterStatus.INVALID;
+ status.setDetails("EA needs at least 1 parent.");
+ } else {
+ status = ParameterStatus.VALID;
+ }
+ }
+ };
+ offspring = new IntegerParameter(4, "Offspring") {
+ @Override
+ public void validate(int newValue) {
+ if (newValue + parents.get() != resources.getInt("popSize")) {
+ status = ParameterStatus.INVALID;
+ status.setDetails("Parents + offspring must equal population size.");
+ } else if (newValue <= 0) {
+ status = ParameterStatus.INVALID;
+ status.setDetails("EA needs at least 1 offspring.");
+ } else {
+ status = ParameterStatus.VALID;
+ }
+ }
+ };
+ report = new BooleanParameter(false, "Report") {
+ @Override
+ public void validate(boolean newValue) {
+ // nothing
+ }
+ };
+ }
+
+ @Override
+ public void evolve(Population population, Mutator mutator, Resources resources) {
+ // select fittest chromosomes
+ fittestChromosome = 0;
+
+ if (report.get()) resources.reportln("[ES] Selecting fittest chromosome...");
+ for (int i = 0; i < resources.getInt("popSize"); i++) {
+ if (report.get()) resources.reportln("[ES] Chromosome " + i + ", fitness: " + population.getChromosome(i).getFitness());
+ if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) {
+ fittestChromosome = i;
+ }
+ }
+ if (report.get()) resources.reportln("[ES] Selected chromosome: " + fittestChromosome);
+
+ // create copies of fittest chromosome, mutate them
+ for (int i = 0; i < resources.getInt("popSize"); i++) {
+ if (i != fittestChromosome) {
+ if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i);
+ population.copyChromosome(fittestChromosome, i);
+ if (report.get()) resources.reportln("[ES] Mutating copied chromosome");
+ mutator.mutate(population.getChromosome(i), resources);
+ }
+ }
+
+ }
+
+ @Override
+ public int getFittestChromosome() {
+ return fittestChromosome;
+ }
+
+ @Override
+ public Parameter[] getLocalParameters() {
+ return new Parameter[] {parents, offspring, report};
+ }
+
+ @Override
+ public String toString() {
+ return "(μ + λ)";
+ }
+
+}
diff --git a/src/jcgp/backend/modules/es/TournamentSelection.java b/src/jcgp/backend/modules/es/TournamentSelection.java
new file mode 100644
index 0000000..3954de8
--- /dev/null
+++ b/src/jcgp/backend/modules/es/TournamentSelection.java
@@ -0,0 +1,53 @@
+package jcgp.backend.modules.es;
+
+import java.util.HashMap;
+
+import jcgp.backend.modules.mutator.Mutator;
+import jcgp.backend.population.Population;
+import jcgp.backend.resources.Resources;
+import jcgp.backend.resources.parameters.IntegerParameter;
+import jcgp.backend.resources.parameters.Parameter;
+
+public class TournamentSelection implements EvolutionaryStrategy {
+
+ private int fittestChromosome;
+
+ private IntegerParameter tournament;
+ private HashMap<String, Parameter> localParameters;
+
+ public TournamentSelection() {
+ tournament = new IntegerParameter(1, "Tournament size") {
+ @Override
+ public void validate(int newValue) {
+ // TODO this
+ }
+ };
+
+ localParameters = new HashMap<String, Parameter>();
+ localParameters.put("tournament", tournament);
+ }
+
+ @Override
+ public Parameter[] getLocalParameters() {
+ return new Parameter[] {tournament};
+ }
+
+ @Override
+ public void evolve(Population population, Mutator mutator,
+ Resources parameters) {
+ tournament.set(tournament.get() + 1);
+ fittestChromosome = 0;
+ // TODO implement this
+
+ }
+
+ @Override
+ public int getFittestChromosome() {
+ return fittestChromosome;
+ }
+
+ @Override
+ public String toString() {
+ return "Tournament";
+ }
+}