-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulation.java
66 lines (54 loc) · 1.7 KB
/
Population.java
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
57
58
59
60
61
62
63
64
65
66
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
public class Population {
private Hexagon population[];
private double popoulationFitness = -1;
Population(int populationSize) {
this.population = new Hexagon[populationSize];
for (int hexCount = 0; hexCount < populationSize; hexCount++) {
this.population[hexCount] = new Hexagon();
}
}
public Hexagon[] getHexagons() {
return this.population;
}
public Hexagon getFittest(int offset) {
Arrays.sort(population, new Comparator<Hexagon>() {
@Override
public int compare(Hexagon o1, Hexagon o2) {
if (o1.getFitness() > o2.getFitness()) {
return 1;
} else if (o1.getFitness() < o2.getFitness()) {
return -1;
}
return 0;
}
});
return population[offset];
}
public void setPopulationFitness(double fittnes) {
this.popoulationFitness = fittnes;
}
public double getPopoulationFitness() {
return popoulationFitness;
}
public int size() {
return this.population.length;
}
public void setHexagon(int offset, Hexagon hexagon) {
population[offset] = hexagon;
}
public Hexagon getHexagon(int offset) {
return population[offset];
}
public void shuffle() {
Random rnd = new Random();
for (int i = population.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
Hexagon temp = population[index];
population[index] = population[i];
population[i] = temp;
}
}
}