package jcgp.backend.modules.ea; 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.Population; /** * (μ + λ) EA. * * * @author Eduardo Pedroni * */ public class MuPlusLambda implements EvolutionaryAlgorithm { private int fittestChromosome; private IntegerParameter parents, offspring; public MuPlusLambda() { parents = new IntegerParameter(1, "Parents") { @Override protected void validate(int newValue) { } }; offspring = new IntegerParameter(4, "Offspring") { @Override protected void validate(int newValue) { } }; } @Override public void evolve(Population population, Mutator mutator, Resources resources) { // TODO actually use parents and offspring // select fittest chromosome fittestChromosome = 0; for (int i = 1; i < resources.getInt("popSize"); i++) { if (population.getChromosome(i).getFitness() >= population.getChromosome(fittestChromosome).getFitness()) { fittestChromosome = i; } } // create copies of fittest chromosome, mutate them for (int i = 0; i < resources.getInt("popSize"); i++) { if (i != fittestChromosome) { population.copyChromosome(fittestChromosome, i); mutator.mutate(population.getChromosome(i), resources); } } } @Override public int getFittestChromosome() { return fittestChromosome; } @Override public Parameter[] getLocalParameters() { return new Parameter[] {parents, offspring}; } @Override public String toString() { return "(μ + λ)"; } @Override public ModuleStatus getStatus(Resources resources) { return ModuleStatus.READY; } }