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
.getNumOfCities()];
16 Visited
= new boolean[graph
.getNumOfCities()];
22 public void computeTourLength() {
23 for (int i
= 0; i
< graph
.getNumOfCities() - 1; i
++) {
24 TourLength
+= graph
.getDistance(Tour
[i
], Tour
[i
+1]);
27 graph
.getDistance(Tour
[graph
.getNumOfCities() - 1], Tour
[0]);
30 public void pickInitialRandomCity() {
31 int r
= (int)(Math
.random() * graph
.NumOfCities
);
36 public void neighbourListASDecisionRule(int Step
) {
38 int CurrentCity
= Tour
[Step
- 1];
40 double SumProbabilities
= 0.0;
41 double SelectionProbability
[] = new double[graph
.getNearestNeighboursDepth()];
43 for (int j
= 0; j
< graph
.getNearestNeighboursDepth(); j
++) {
45 if ( Visited
[ graph
.getNearestNeighbour(CurrentCity
,j
) ] ) {
46 SelectionProbability
[j
] = 0.0;
48 SelectionProbability
[j
] =
49 graph
.getChoiceInformation( CurrentCity
,
50 graph
.getNearestNeighbour(CurrentCity
,j
) );
51 SumProbabilities
+= SelectionProbability
[j
];
56 if (SumProbabilities
== 0.0) {
61 double r
= Math
.random() * SumProbabilities
;
62 double p
= SelectionProbability
[j
];
65 p
+= SelectionProbability
[++j
];
68 Tour
[Step
] = graph
.getNearestNeighbour(CurrentCity
,j
);
69 Visited
[ graph
.getNearestNeighbour(CurrentCity
,j
) ] = true;
74 public void chooseBestNext(int Step
) {
76 /* this seems to be neccessary because some choice information
77 * tends to become zero after a while */
79 int CurrentCity
= Tour
[Step
- 1];
81 for (int j
= 0; j
< graph
.getNumOfCities(); j
++) {
83 if (graph
.getChoiceInformation(CurrentCity
,j
) > v
) {
85 v
= graph
.getChoiceInformation(CurrentCity
,j
);
94 public int[] getTour() {
98 public int getTour(int Tour
) {
99 return this.Tour
[Tour
];
102 public int getTourLength() {
103 return this.TourLength
;
106 public void setTourLength(int TourLength
) {
107 this.TourLength
= TourLength
;
110 public void clearData() {
116 protected void clearTourLength() {
120 protected void clearTour() {
121 for (int i
= 0; i
< graph
.NumOfCities
; i
++)
125 protected void clearMemory() {
126 for (int i
= 0; i
< graph
.NumOfCities
; i
++)
131 public String
toString() {
132 StringBuilder result
= new StringBuilder();
134 result
.append("Tour with length: " + TourLength
+ "\n");
137 if (graph
.ids
[t
] != null)
138 result
.append(graph
.ids
[t
] + "\t");
140 result
.append(t
+ "\t");
142 if (graph
.ids
[0] != null)
143 result
.append(graph
.ids
[Tour
[0]]);
145 result
.append(Tour
[0]);
147 return result
.toString();