diff options
Diffstat (limited to 'src/jcgp/backend/modules/es/MuPlusLambda.java')
-rw-r--r-- | src/jcgp/backend/modules/es/MuPlusLambda.java | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java index 8186b11..3743ee6 100644 --- a/src/jcgp/backend/modules/es/MuPlusLambda.java +++ b/src/jcgp/backend/modules/es/MuPlusLambda.java @@ -28,21 +28,21 @@ import jcgp.backend.resources.Resources; * */ public class MuPlusLambda extends EvolutionaryStrategy { - + private IntegerParameter mu, lambda; private BooleanParameter report; - + /** * Creates a new instance of MuPlusLambda. * * @param resources a reference to the experiment's resources. */ public MuPlusLambda(final Resources resources) { - super(); + super(resources); mu = new IntegerParameter(1, "Parents (μ)") { @Override public void validate(Number newValue) { - if (newValue.intValue() + lambda.get() != resources.populationSize()) { + if (newValue.intValue() + lambda.get() != getResources().populationSize()) { status = ParameterStatus.INVALID; status.setDetails("Parents + offspring must equal population size."); } else if (newValue.intValue() <= 0) { @@ -53,11 +53,11 @@ public class MuPlusLambda extends EvolutionaryStrategy { } } }; - + lambda = new IntegerParameter(4, "Offspring (λ)") { @Override public void validate(Number newValue) { - if (newValue.intValue() + mu.get() != resources.populationSize()) { + if (newValue.intValue() + mu.get() != getResources().populationSize()) { status = ParameterStatus.INVALID; status.setDetails("Parents + offspring must equal population size."); } else if (newValue.intValue() <= 0) { @@ -68,29 +68,30 @@ public class MuPlusLambda extends EvolutionaryStrategy { } } }; - + report = new BooleanParameter(false, "Report"); - + setName("(μ + λ)"); registerParameters(mu, lambda, report); } - + @Override - public void evolve(Population population, Mutator mutator, Resources resources) { - /* Population is sorted in ascending order of fitness, so leave the last - * mu chromosomes intact - */ - for (int i = 0; i < resources.populationSize() - mu.get(); i++) { + public void evolve(Population population, Mutator mutator) { + // sort the population in order of best fitness + population.sort(); + + // population is now sorted in ascending order of fitness, the last chromosomes are the fittest + for (int i = 0; i < getResources().populationSize() - mu.get(); i++) { // select a random parent out of the lambda offspring individuals - int randomParent = resources.populationSize() - 1 - resources.getRandomInt(mu.get()); - if (report.get()) resources.reportln("[ES] Copying Chr " + randomParent + " to population position " + i); + int randomParent = getResources().populationSize() - 1 - getResources().getRandomInt(mu.get()); + if (report.get()) getResources().reportln("[ES] Copying Chr " + randomParent + " to population position " + i); population.copyChromosome(randomParent, i); - + // mutate selected chromosome - if (report.get()) resources.reportln("[ES] Mutating copied chromosome"); - mutator.mutate(population.getChromosome(i), resources); + if (report.get()) getResources().reportln("[ES] Mutating copied chromosome"); + mutator.mutate(population.get(i)); } - - if (report.get()) resources.reportln("[ES] Generation is complete"); + + if (report.get()) getResources().reportln("[ES] Generation is complete"); } } |