From 02fd2bc7059da416937beb1abe67e5ca60379030 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Tue, 1 Apr 2014 23:00:53 +0100 Subject: Settings pane now actually controls the parameters, not much left to do. --- .../backend/modules/ea/EvolutionaryAlgorithm.java | 15 ++++ src/jcgp/backend/modules/ea/MuPlusLambda.java | 80 ++++++++++++++++++++++ .../backend/modules/ea/TournamentSelection.java | 55 +++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java create mode 100644 src/jcgp/backend/modules/ea/MuPlusLambda.java create mode 100644 src/jcgp/backend/modules/ea/TournamentSelection.java (limited to 'src/jcgp/backend/modules/ea') diff --git a/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java b/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java new file mode 100644 index 0000000..ce457ef --- /dev/null +++ b/src/jcgp/backend/modules/ea/EvolutionaryAlgorithm.java @@ -0,0 +1,15 @@ +package jcgp.backend.modules.ea; + +import jcgp.JCGP.Resources; +import jcgp.backend.modules.Module; +import jcgp.backend.modules.mutator.Mutator; +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.Population; + +public interface EvolutionaryAlgorithm extends Module { + + public abstract void evolve(Population population, Mutator mutator, Resources parameters); + + public abstract Chromosome getFittestChromosome(); + +} diff --git a/src/jcgp/backend/modules/ea/MuPlusLambda.java b/src/jcgp/backend/modules/ea/MuPlusLambda.java new file mode 100644 index 0000000..fcfba05 --- /dev/null +++ b/src/jcgp/backend/modules/ea/MuPlusLambda.java @@ -0,0 +1,80 @@ +package jcgp.backend.modules.ea; + +import java.util.HashMap; + +import jcgp.JCGP.Resources; +import jcgp.backend.modules.ModuleStatus; +import jcgp.backend.modules.mutator.Mutator; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.Parameter; +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.Population; + +/** + * (μ + λ) EA. + * + * + * @author Eduardo Pedroni + * + */ +public class MuPlusLambda implements EvolutionaryAlgorithm { + + private Chromosome fittestChromosome; + + private IntegerParameter parents, offspring; + + private HashMap localParameters; + + public MuPlusLambda() { + parents = new IntegerParameter(1, "Parents"); + offspring = new IntegerParameter(4, "Offspring"); + + localParameters = new HashMap(); + + localParameters.put("mu", parents); + localParameters.put("lambda", offspring); + } + + @Override + public void evolve(Population population, Mutator mutator, Resources parameters) { + // select fittest chromosome + int fittest = 0; + + for (int i = 1; i < parameters.getInt("popSize"); i++) { + if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) { + fittest = i; + } + } + fittestChromosome = population.getChromosome(fittest); + population.setBestIndividual(fittest); + // create copies of fittest chromosome, mutate them + Chromosome fc = population.getChromosome(fittest); + for (int i = 0; i < parameters.getInt("popSize"); i++) { + if (i != fittest) { + population.getChromosome(i).copyConnections(fc); + mutator.mutate(population.getChromosome(i), parameters); + } + } + } + + @Override + public Chromosome getFittestChromosome() { + return fittestChromosome; + } + + @Override + public HashMap getLocalParameters() { + return localParameters; + } + + @Override + public String toString() { + return "(μ + λ)"; + } + + @Override + public ModuleStatus getStatus(Resources resources) { + return ModuleStatus.READY; + } + +} diff --git a/src/jcgp/backend/modules/ea/TournamentSelection.java b/src/jcgp/backend/modules/ea/TournamentSelection.java new file mode 100644 index 0000000..6cbaa45 --- /dev/null +++ b/src/jcgp/backend/modules/ea/TournamentSelection.java @@ -0,0 +1,55 @@ +package jcgp.backend.modules.ea; + +import java.util.HashMap; + +import jcgp.JCGP.Resources; +import jcgp.backend.modules.ModuleStatus; +import jcgp.backend.modules.mutator.Mutator; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.Parameter; +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.Population; + +public class TournamentSelection implements EvolutionaryAlgorithm { + + private Chromosome fittestChromosome; + + private IntegerParameter tournament; + private HashMap localParameters; + + public TournamentSelection() { + tournament = new IntegerParameter(1, "Tournament size"); + + localParameters = new HashMap(); + localParameters.put("tournament", tournament); + } + + @Override + public HashMap getLocalParameters() { + return localParameters; + } + + @Override + public void evolve(Population population, Mutator mutator, + Resources parameters) { + tournament.set(tournament.get() + 1); + fittestChromosome = population.getChromosome(0); + // TODO implement this + + } + + @Override + public Chromosome getFittestChromosome() { + return fittestChromosome; + } + + @Override + public String toString() { + return "Tournament"; + } + + @Override + public ModuleStatus getStatus(Resources resources) { + return null; + } +} -- cgit v1.2.3