aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/ea/StandardEA.java
blob: 901333b32a56a9b64e7dac793a8aba6a7b401dbd (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
package jcgp.ea;

import jcgp.Parameters;
import jcgp.population.Chromosome;
import jcgp.population.Population;

/**
 * (1 + λ) EA.
 * 
 * 
 * @author Eduardo Pedroni
 *
 */
public class StandardEA implements EvolutionaryAlgorithm {
	
	private int bestFitness = 0;

	@Override
	public void evolve(Population population, Mutator mutator) {
		// select fittest chromosome
		int fittest = 0;
		
		for (int i = 1; i < Parameters.getPopulationSize(); i++) {
			if (population.getChromosome(i).getFitness() >= population.getChromosome(fittest).getFitness()) {
				fittest = i;
			}
		}
		bestFitness = population.getChromosome(fittest).getFitness();
		population.setBestIndividual(fittest);
		System.out.println("Best fitness: " + bestFitness);
		// create copies of fittest chromosome, mutate them
		Chromosome fc = population.getChromosome(fittest);
		for (int i = 0; i < Parameters.getPopulationSize(); i++) {
			if (i != fittest) {
				population.getChromosome(i).copyConnections(fc);
				mutator.mutate(population.getChromosome(i));
			}
		}
	}
	
	@Override
	public int getBestFitness() {
		return bestFitness;
	}
}