-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoute.java
55 lines (48 loc) · 1.49 KB
/
Route.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
package tsp;
public class Route {
private City route[];
private double distance = 0;
/**
* Initialize Route
*
* @param individual A GA individual
* @param cities The cities referenced
*/
public Route(Individual individual, City cities[]){
// Get individual's chromosome
int chromosome[] = individual.getChromosome();
// Create route
this.route = new City[cities.length];
for(int geneIndex=0; geneIndex < chromosome.length; geneIndex++){
this.route[geneIndex] = cities[chromosome[geneIndex]];
}
}
/**
* Get route distance
*
* @return distance The route's distance
*/
public double getDistance(){
if (this.distance > 0) {
return this.distance;
}
// Loop over cities in route and calculate route distance
double totalDistance = 0;
for(int cityIndex=0; cityIndex+1<this.route.length; cityIndex++){
totalDistance += this.route[cityIndex].distanceFrom(this.route[cityIndex+1]);
}
totalDistance += this.route[this.route.length-1].distanceFrom(this.route[0]);
this.distance = totalDistance;
return totalDistance;
}
/**
* Print route
*/
public String toString(){
String route = "";
for (City route1 : this.route) {
route += "(" + route1.getX() + ", " + route1.getY() + ")";
}
return route;
}
}