aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/es/MuPlusLambda.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/modules/es/MuPlusLambda.java')
-rw-r--r--src/jcgp/backend/modules/es/MuPlusLambda.java67
1 files changed, 42 insertions, 25 deletions
diff --git a/src/jcgp/backend/modules/es/MuPlusLambda.java b/src/jcgp/backend/modules/es/MuPlusLambda.java
index 6a3883b..50bf265 100644
--- a/src/jcgp/backend/modules/es/MuPlusLambda.java
+++ b/src/jcgp/backend/modules/es/MuPlusLambda.java
@@ -9,41 +9,59 @@ import jcgp.backend.resources.parameters.Parameter;
import jcgp.backend.resources.parameters.ParameterStatus;
/**
- * (μ + λ) EA.
- *
+ * (μ + λ)-ES
+ * <br><br>
+ * This strategy selects the μ fittest chromosomes from the population.
+ * The promoted individuals are copied into the new population and mutated
+ * λ times, but also carried forward unchanged. The total population size
+ * is μ + λ.
+ * <br><br>
+ * Two integer parameters are used to control this strategy: parents
+ * and offspring. They are constrained in that they must always add up to
+ * the population size, and must never be smaller than 1.
+ * <br>
+ * One additional parameter, report, controls whether a detailed log of the
+ * algorithm's operation is to be printed or not. Reports respect the report
+ * interval base parameter.
*
+ * @see EvolutionaryStrategy
* @author Eduardo Pedroni
*
*/
public class MuPlusLambda implements EvolutionaryStrategy {
- private IntegerParameter parents, offspring;
+ 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) {
- parents = new IntegerParameter(1, "Parents") {
+ mu = new IntegerParameter(1, "Parents (μ)") {
@Override
public void validate(Number newValue) {
- if (newValue.intValue() + offspring.get() != resources.populationSize()) {
+ if (newValue.intValue() + lambda.get() != resources.populationSize()) {
status = ParameterStatus.INVALID;
status.setDetails("Parents + offspring must equal population size.");
} else if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
- status.setDetails("EA needs at least 1 parent.");
+ status.setDetails("ES needs at least 1 parent.");
} else {
status = ParameterStatus.VALID;
}
}
};
- offspring = new IntegerParameter(4, "Offspring") {
+ lambda = new IntegerParameter(4, "Offspring (λ)") {
@Override
public void validate(Number newValue) {
- if (newValue.intValue() + parents.get() != resources.populationSize()) {
+ if (newValue.intValue() + mu.get() != resources.populationSize()) {
status = ParameterStatus.INVALID;
status.setDetails("Parents + offspring must equal population size.");
} else if (newValue.intValue() <= 0) {
status = ParameterStatus.INVALID;
- status.setDetails("EA needs at least 1 offspring.");
+ status.setDetails("ES needs at least 1 offspring.");
} else {
status = ParameterStatus.VALID;
}
@@ -58,32 +76,31 @@ public class MuPlusLambda implements EvolutionaryStrategy {
}
@Override
- public void evolve(Population population, Mutator mutator, Resources resources) {
- // select fittest chromosomes
- if (report.get()) resources.reportln("[ES] Selected chromosome: " + population.getFittestIndex());
-
- // create copies of fittest chromosome, mutate them
- for (int i = 0; i < resources.populationSize(); i++) {
- if (i != population.getFittestIndex()) {
- if (report.get()) resources.reportln("[ES] Copying fittest chromosome to population position " + i);
- population.copyChromosome(population.getFittestIndex(), i);
- if (report.get()) resources.reportln("[ES] Mutating copied chromosome");
- mutator.mutate(population.getChromosome(i), resources);
- }
+ 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++) {
+ // 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);
+ 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()) resources.reportln("[ES] Generation is complete.");
-
+ if (report.get()) resources.reportln("[ES] Generation is complete");
}
@Override
public Parameter<?>[] getLocalParameters() {
- return new Parameter[] {parents, offspring, report};
+ return new Parameter[] {mu, lambda, report};
}
@Override
public String toString() {
return "(μ + λ)";
}
-
}