blob: 2db8776559addda3c77378ab51f0ca8d89f9f23d (
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
|
package jcgp.modules.ea;
import jcgp.modules.mutator.Mutator;
import jcgp.parameters.Parameters;
import jcgp.parameters.IntegerParameter;
import jcgp.parameters.BooleanParameter;
import jcgp.population.Chromosome;
import jcgp.population.Population;
/**
* (1 + λ) EA.
*
*
* @author Eduardo Pedroni
*
*/
public class StandardEA implements EvolutionaryAlgorithm {
private Chromosome fittestChromosome;
@Override
public void evolve(Population population, Mutator mutator) {
// select fittest chromosome
int fittest = 0;
for (int i = 1; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) {
if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) {
fittest = i;
}
}
fittestChromosome = population.getChromosome(fittest);
population.setBestIndividual(fittest);
if (((BooleanParameter) Parameters.get("debug")).getValue()) {
System.out.println("Best fitness: " + fittestChromosome.getFitness());
}
// create copies of fittest chromosome, mutate them
Chromosome fc = population.getChromosome(fittest);
for (int i = 0; i < ((IntegerParameter) Parameters.get("population")).getValue(); i++) {
if (i != fittest) {
population.getChromosome(i).copyConnections(fc);
mutator.mutate(population.getChromosome(i));
}
}
}
@Override
public Chromosome getFittestChromosome() {
return fittestChromosome;
}
}
|