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

import java.util.Iterator;

import jcgp.Parameters;

public class Population implements Iterable<Chromosome> {
	
	private Chromosome[] population;
	
	public Population(Chromosome chromosome) {
		population = new Chromosome[Parameters.getPopulationSize()];
		for (int c = 0; c < population.length; c++) {
			population[c] = new Chromosome(chromosome);
		}
	}
	
	public Population() {
		population = new Chromosome[Parameters.getPopulationSize()];
		for (int c = 0; c < population.length; c++) {
			population[c] = new Chromosome();
		}
	}

	@Override
	public Iterator<Chromosome> iterator() {
		return new Iterator<Chromosome>() {
			
			private int index = 0;

			@Override
			public boolean hasNext() {
				if (index < population.length) {
					return true;
				} else {
					return false;
				}
			}

			@Override
			public Chromosome next() {
				Chromosome next = population[index];
				index++;
				return next;
			}

			@Override
			public void remove() {
				// not allowed
				throw new UnsupportedOperationException("Removing chromosomes from the population is not allowed. Instead, re-instantiate the chromosome.");
			}
		};
	}
}