3 public int TourLength
; // the ant's tour length
4 public int[] Tour
; // N+1; ant's memory storing (partial) tours
5 public boolean[] Visited
; // N; visited cities
15 Tour
= new int[graph
.NumOfCities
];
16 Visited
= new boolean[graph
.NumOfCities
];
22 public void computeTourLength() {
23 for (int i
= 0; i
< graph
.getNumOfCities() - 1; i
++) {
24 TourLength
+= graph
.getDistance(Tour
[i
], Tour
[i
+1]);
26 TourLength
+= graph
.getDistance(Tour
[graph
.NumOfCities
- 1], Tour
[0]);
29 public void pickInitialRandomCity() {
30 int r
= (int)(Math
.random() * graph
.NumOfCities
);
35 public void neighbourListASDecisionRule(int Step
) {
37 int CurrentCity
= Step
- 1;
39 double SumProbabilities
= 0.0;
40 double SelectionProbability
[] = new double[graph
.NearestNeighboursDepth
];
42 for (int j
= 0; j
< graph
.NearestNeighboursDepth
; j
++) {
44 if ( Visited
[ graph
.getNearestNeighbour(CurrentCity
,j
) ] ) {
45 SelectionProbability
[j
] = 0.0;
47 SelectionProbability
[j
] =
48 graph
.getChoiceInformation( CurrentCity
, graph
.getNearestNeighbour(CurrentCity
,j
) );
49 SumProbabilities
+= SelectionProbability
[j
];
54 if (SumProbabilities
== 0.0) {
59 double r
= Math
.random() * SumProbabilities
;
60 double p
= SelectionProbability
[j
];
63 p
+= SelectionProbability
[++j
];
66 Tour
[Step
] = graph
.getNearestNeighbour(CurrentCity
,j
);
67 Visited
[ graph
.getNearestNeighbour(CurrentCity
,j
) ] = true;
72 public void chooseBestNext(int Step
) {
75 int CurrentCity
= Tour
[Step
- 1];
77 for (int j
= 0; j
< graph
.NumOfCities
; j
++) {
79 if (graph
.getChoiceInformation(CurrentCity
,j
) > v
) {
81 v
= graph
.getChoiceInformation(CurrentCity
,j
);
90 public int[] getTour() {
94 public int getTour(int Tour
) {
95 return this.Tour
[Tour
];
98 public int getTourLength() {
99 return this.TourLength
;
102 public void setTourLength(int TourLength
) {
103 this.TourLength
= TourLength
;
106 public void clearData() {
112 protected void clearTourLength() {
116 protected void clearTour() {
117 for (int i
= 0; i
< graph
.NumOfCities
; i
++)
121 protected void clearMemory() {
122 for (int i
= 0; i
< graph
.NumOfCities
; i
++)
127 public String
toString() {
128 StringBuilder result
= new StringBuilder();
130 result
.append("Tour with length: " + TourLength
+ "\n");
133 if (graph
.ids
[t
] != null)
134 result
.append(graph
.ids
[t
] + "\t");
136 result
.append(t
+ "\t");
138 if (graph
.ids
[0] != null)
139 result
.append(graph
.ids
[Tour
[0]]);
141 result
.append(Tour
[0]);
143 return result
.toString();