3 import java
.io
.IOException
;
7 import aco
.graph
.data
.*;
10 import aco
.parameter
.*;
11 import aco
.environment
.*;
12 import aco
.tsplibreader
.*;
14 public class AntMain
{
16 public static void runACSAlgorithm(String filename
,
24 ACOMediator acom
= new ACOMediator(new ACSParameter());
25 ACOGraph acog
= new ACOGraph(acom
);
26 acom
.setACOGraph(acog
);
28 /* these are the most important */
29 acom
.setACOStrategy(new ACSStrategy(acom
));
30 /* setDistanceStrategy should be a flag from TSPLibReader */
31 //acom.setDistanceStrategy(new ATT_DistanceStrategy(acom));
32 acom
.setDistanceStrategy(new EUC_2D_DistanceStrategy(acom
));
33 acom
.setPheromoneStrategy(new ACSPheromoneStrategy(acom
));
34 acom
.setChoiceInformationStrategy(new ACSChoiceInformationStrategy(acom
));
36 /* these are not very likely to change */
37 acom
.setGraphStrategy(new GraphStrategy(acom
));
38 acom
.setTauZeroStrategy(new ACSTauZeroStrategy(acom
));
39 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
41 acom
.setGlobalBestTourMapSize(1);
42 acom
.setMaxNumOfTours(runs
);
46 acom
.setNearestNeighbourListDepth(cl
);
49 TSPLibReader tsplr
= new TSPLibReader(filename
);
50 CoordinateData coordinates
= new CoordinateData(tsplr
);
51 acog
.initializeFromCoordinates(coordinates
);
52 } catch (IOException e
) {
53 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
57 /* after the graph is initialized we can set the Number of Ants
58 * or leave it to the method parameter */
60 acom
.setNumOfAnts(acom
.getNumOfCities());
62 acom
.setNumOfAnts(numofants
);
64 /* after the graph is initialized we can compute TauZero.. */
65 acom
.computeTauZero();
66 /* ..then we can set the initial pheromone value.. */
67 acom
.setInitialPheromones(acom
.getTauZero());
68 /* ..and finally compute the initial Choice Information */
69 acom
.computeChoiceInformation();
71 System
.out
.println(acom
.getACOParameter());
73 Environment env
= new Environment(acom
);
74 acom
.setEnvironment(env
);
76 AntView av
= new AntView(acom
);
77 new Thread(av
).start();
83 public static void runASAlgorithm(String filename
,
90 ACOMediator acom
= new ACOMediator(new ASParameter());
91 ACOGraph acog
= new ACOGraph(acom
);
92 acom
.setACOGraph(acog
);
94 acom
.setGraphStrategy(new GraphStrategy(acom
));
95 /* these are the most important */
96 acom
.setACOStrategy(new ASStrategy(acom
));
97 /* setDistanceStrategy should be a flag from TSPLibReader */
98 //acom.setDistanceStrategy(new ATT_DistanceStrategy(acom));
99 acom
.setDistanceStrategy(new EUC_2D_DistanceStrategy(acom
));
101 acom
.setPheromoneStrategy(new ASPheromoneStrategy(acom
));
102 acom
.setTauZeroStrategy(new ASTauZeroStrategy(acom
));
103 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
104 /* these are not very likely to change */
105 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
107 acom
.setGlobalBestTourMapSize(1);
108 acom
.setMaxNumOfTours(runs
);
109 acom
.setAlpha(alpha
);
114 TSPLibReader tsplr
= new TSPLibReader(filename
);
115 CoordinateData coordinates
= new CoordinateData(tsplr
);
117 acog
.initializeFromCoordinates(coordinates
);
119 } catch (IOException e
) {
120 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
124 /* after the graph is initialized we can set the Number of Ants
125 * or leave it to the method parameter */
126 if (numofants
== 0) {
127 acom
.setNumOfAnts(acom
.getNumOfCities());
129 acom
.setNumOfAnts(numofants
);
131 /* after the graph is initialized we can compute TauZero.. */
132 acom
.computeTauZero();
133 /* ..then we can set the initial pheromone value.. */
134 acom
.setInitialPheromones(acom
.getTauZero());
135 /* ..and finally compute the initial Choice Information */
136 acom
.computeChoiceInformation();
138 System
.out
.println(acom
.getACOParameter());
140 Environment env
= new Environment(acom
);
141 acom
.setEnvironment(env
);
143 AntView av
= new AntView(acom
);
144 new Thread(av
).start();
150 public static void runEASAlgorithm(String filename
,
158 ACOMediator acom
= new ACOMediator(new EASParameter());
159 ACOGraph acog
= new ACOGraph(acom
);
160 acom
.setACOGraph(acog
);
162 /* these are the most important */
163 acom
.setACOStrategy(new ASStrategy(acom
));
165 /* setDistanceStrategy should be a flag from TSPLibReader */
166 //acom.setDistanceStrategy(new ATT_DistanceStrategy(acom));
167 acom
.setDistanceStrategy(new EUC_2D_DistanceStrategy(acom
));
169 acom
.setPheromoneStrategy(new EASPheromoneStrategy(acom
));
170 acom
.setTauZeroStrategy(new EASTauZeroStrategy(acom
));
172 /* these are not very likely to change */
173 acom
.setGraphStrategy(new GraphStrategy(acom
));
174 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
175 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
177 acom
.setGlobalBestTourMapSize(1);
178 acom
.setMaxNumOfTours(runs
);
179 acom
.setAlpha(alpha
);
184 TSPLibReader tsplr
= new TSPLibReader(filename
);
185 CoordinateData coordinates
= new CoordinateData(tsplr
);
187 acog
.initializeFromCoordinates(coordinates
);
189 } catch (IOException exception
) {
190 System
.out
.println(exception
.toString() +
191 "(" + exception
.getClass() + "): " + exception
);
196 acom
.setE((double)acom
.getNumOfCities());
201 /* after the graph is initialized we can set the Number of Ants
202 * or leave it to the method parameter */
203 if (numofants
== 0) {
204 acom
.setNumOfAnts(acom
.getNumOfCities());
206 acom
.setNumOfAnts(numofants
);
209 /* after the graph is initialized we can compute TauZero.. */
210 acom
.computeTauZero();
211 /* ..then we can set the initial pheromone value.. */
212 acom
.setInitialPheromones(acom
.getTauZero());
213 /* ..and finally compute the initial Choice Information */
214 acom
.computeChoiceInformation();
216 System
.out
.println(acom
.getACOParameter());
218 Environment env
= new Environment(acom
);
219 acom
.setEnvironment(env
);
221 AntView av
= new AntView(acom
);
222 new Thread(av
).start();
228 public static void runASRankAlgorithm(String filename
,
236 ACOMediator acom
= new ACOMediator(new ASRankParameter());
237 ACOGraph acog
= new ACOGraph(acom
);
238 acom
.setACOGraph(acog
);
240 /* these are the most important */
241 acom
.setACOStrategy(new ASStrategy(acom
));
243 /* setDistanceStrategy should be a flag from TSPLibReader */
244 //acom.setDistanceStrategy(new ATT_DistanceStrategy(acom));
245 acom
.setDistanceStrategy(new EUC_2D_DistanceStrategy(acom
));
247 acom
.setTauZeroStrategy(new ASRankTauZeroStrategy(acom
));
248 acom
.setPheromoneStrategy(new ASRankPheromoneStrategy(acom
));
250 /* these are not very likely to change */
251 acom
.setGraphStrategy(new GraphStrategy(acom
));
252 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
253 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
255 acom
.setGlobalBestTourMapSize(1);
256 acom
.setMaxNumOfTours(runs
);
257 acom
.setAlpha(alpha
);
262 TSPLibReader tsplr
= new TSPLibReader(filename
);
263 CoordinateData coordinates
= new CoordinateData(tsplr
);
265 acog
.initializeFromCoordinates(coordinates
);
267 } catch (IOException e
) {
268 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
273 acom
.setW((double)acom
.getNumOfCities());
278 /* after the graph is initialized we can set the Number of Ants
279 * or leave it to the method parameter */
280 if (numofants
== 0) {
281 acom
.setNumOfAnts(acom
.getNumOfCities());
283 acom
.setNumOfAnts(numofants
);
286 /* after the graph is initialized we can compute TauZero.. */
287 acom
.computeTauZero();
288 /* ..then we can set the initial pheromone value.. */
289 acom
.setInitialPheromones(acom
.getTauZero());
290 /* ..and finally compute the initial Choice Information */
291 acom
.computeChoiceInformation();
293 System
.out
.println(acom
.getACOParameter());
295 Environment env
= new Environment(acom
);
296 acom
.setEnvironment(env
);
298 AntView av
= new AntView(acom
);
299 new Thread(av
).start();
305 public static void main(String
[] args
) {
307 String
[] algorithms
= { "as", "eas", "asrank", "acs" };
309 for (int i
= 0; i
< algorithms
.length
; i
++) {
310 if (args
[0].equalsIgnoreCase(algorithms
[i
])) {
313 /* filename, alpha, beta, tauzero, roh, numofants, iterations */
314 runASAlgorithm(args
[2], 1.0, 5.0, 0, 0.5, 0, Integer
.parseInt(args
[1]));
317 /* filename, alpha, beta, tauzero, roh, e, numofants, iterations */
318 runEASAlgorithm(args
[2], 1.0, 5.0, 0, 0.5, 0, 0, Integer
.parseInt(args
[1]));
321 /* filename, alpha, beta, tauzero, roh, w, numofants, iterations */
322 runASRankAlgorithm(args
[2], 1.0, 5.0, 0, 0.5, 6.0, 0, Integer
.parseInt(args
[1]));
325 /* filename, beta, qzero, roh, cl, numofants, runs */
326 runACSAlgorithm(args
[2], 2.0, 0.7, 0.1, 15, 15, Integer
.parseInt(args
[1]));
332 System
.out
.print("Computation finished..");
333 /* wait for a keypress before exit */
337 } catch (IOException e
) {
338 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);