aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/modules/es/MuPlusLambda.java
blob: 3743ee69080953168e03efbfd82d0ba3e3eaf519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package jcgp.backend.modules.es;

import jcgp.backend.modules.mutator.Mutator;
import jcgp.backend.parameters.BooleanParameter;
import jcgp.backend.parameters.IntegerParameter;
import jcgp.backend.parameters.ParameterStatus;
import jcgp.backend.population.Population;
import jcgp.backend.resources.Resources;

/**
 * (μ + λ)-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 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(resources);
		mu = new IntegerParameter(1, "Parents (μ)") {
			@Override
			public void validate(Number newValue) {
				if (newValue.intValue() + lambda.get() != getResources().populationSize()) {
					status = ParameterStatus.INVALID;
					status.setDetails("Parents + offspring must equal population size.");
				} else if (newValue.intValue() <= 0) {
					status = ParameterStatus.INVALID;
					status.setDetails("ES needs at least 1 parent.");
				} else {
					status = ParameterStatus.VALID;
				}
			}
		};

		lambda = new IntegerParameter(4, "Offspring (λ)") {
			@Override
			public void validate(Number newValue) {
				if (newValue.intValue() + mu.get() != getResources().populationSize()) {
					status = ParameterStatus.INVALID;
					status.setDetails("Parents + offspring must equal population size.");
				} else if (newValue.intValue() <= 0) {
					status = ParameterStatus.INVALID;
					status.setDetails("ES needs at least 1 offspring.");
				} else {
					status = ParameterStatus.VALID;
				}
			}
		};

		report = new BooleanParameter(false, "Report");

		setName("(μ + λ)");
		registerParameters(mu, lambda, report);
	}

	@Override
	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 = 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()) getResources().reportln("[ES] Mutating copied chromosome");
			mutator.mutate(population.get(i));
		}

		if (report.get()) getResources().reportln("[ES] Generation is complete");
	}
}