-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ant.java
112 lines (94 loc) · 2.07 KB
/
Ant.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package acoroutine;
/**
*
* @author Evan Harris
*/
public class Ant
{
//Declare Ant data structures according to Dorigo & Stutzle p103.
private int tour[];
private int tourLength;
int tourStep;
private boolean visited[];
public int totalCities;
public Ant(int totalCities)
{
this.totalCities = totalCities;
tour = new int[totalCities+1];
for (int i = 0; i < tour.length; i++)
{
tour[i] = 0;
}
visited = new boolean[totalCities];
for (int i = 0; i < totalCities; i++)
{
visited[i] = false;
}
tourLength = 0;
tourStep = 0;
}
//Lines 1 to 5 pg 106
public void emptyMemory()
{
tourLength = 0;
for (int i = 0; i < visited.length; i++)
{
visited[i] = false;
}
for (int i = 0; i < tour.length; i++)
{
tour[i] = 0;
}
tourStep = 0;
}
public void setStartCity(int cityID)
{
tour[0] = cityID;
tour[totalCities] = cityID;
visited[cityID] = true;
tourStep++;
}
public boolean setNextCity(int cityID, int stepLength)
{
if(visited[cityID])
{
return false;
}
else
{
tour[tourStep] = cityID;
visited[cityID] = true;
tourStep++;
tourLength = tourLength + stepLength;
return true;
}
}
public int getCity(int step)
{
return tour[step];
}
public boolean hasVisited(int city)
{
return visited[city];
}
public int getLastCity()
{
return tour[tourStep-1];
}
public int getFirstCity()
{
return tour[0];
}
public int getTourLength()
{
return tourLength;
}
public int[] getTour()
{
return tour;
}
public void addFinalDistance(int distance)
{
tourLength += distance;
}
}