aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/population/Population.java
blob: 586f55662304e5e95606a7ba5df43f2ae52191e0 (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.population;

import jcgp.JCGP.Resources;


public class Population {
	
	private Chromosome[] chromosomes;
	private int fittest;
	
	public Population(Resources parameters) {
		chromosomes = new Chromosome[((int) parameters.get("popSize"))];
		for (int c = 0; c < chromosomes.length; c++) {
			chromosomes[c] = new Chromosome(parameters);
		}
	}
	
	public Population(Chromosome parent, Resources parameters) {
		chromosomes = new Chromosome[((int) parameters.get("popSize"))];
		// make a clone for safety
		this.chromosomes[0] = new Chromosome(parent);
		// generate the rest of the individuals
		for (int c = 1; c < chromosomes.length; c++) {
			chromosomes[c] = new Chromosome(chromosomes[0]);
		}
	}

	/**
	 * Returns all chromosomes, parents first, then offspring.
	 * 
	 * @param index
	 * @return
	 */
	public Chromosome getChromosome(int index) {
		return chromosomes[index];
	}
	
	public void setBestIndividual(int index) {
		fittest = index;
	}
	
	public Chromosome getBestIndividual() {
		return chromosomes[fittest];
	}
}