-
Notifications
You must be signed in to change notification settings - Fork 1
/
Population.java
68 lines (54 loc) · 1.24 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
67
68
package tsp;
public class Population
{
Tour[] tours;
public Population(int getSize, boolean initialize)
{
tours = new Tour[getSize];
if (initialize)
{
for (int i = 0; i < getSize(); i++)
{
Tour newTour = new Tour();
newTour.generateIndividual();
saveTour(i, newTour);
}
}
}
// Saves a tour
public void saveTour(int index, Tour tour)
{
tours[index] = tour;
}
//returns tour at given index
public Tour getTour(int index)
{
return tours[index];
}
public Tour getFittest()
{
Tour fittest = tours[0];
for (int i = 1; i < getSize(); i++)
{
if (fittest.getFitness() <= getTour(i).getFitness())
{
fittest = getTour(i);
}
}
return fittest;
}
@Override
public String toString()
{
String out = new String();
for(int i = 0; i<getSize(); i++)
{
out += "\n" + tours[i].toString();
}
return out;
}
public int getSize()
{
return tours.length;
}
}