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

import java.util.Iterator;

import jcgp.Parameters;

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

	@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
				// since this would shift everything back one position, increment index
				index++;
			}
			
		};
	}
}