3 import java
.io
.IOException
;
7 import aco
.graph
.data
.*;
9 import aco
.strategy
.acs
.*;
10 import aco
.strategy
.as
.*;
11 import aco
.strategy
.as
.eas
.*;
12 import aco
.strategy
.as
.asrank
.*;
13 import aco
.strategy
.as
.simpleas
.*;
14 import aco
.mediator
.acs
.*;
15 import aco
.mediator
.as
.eas
.*;
16 import aco
.mediator
.as
.asrank
.*;
17 import aco
.mediator
.as
.simpleas
.*;
18 import aco
.parameter
.acs
.*;
19 import aco
.parameter
.as
.eas
.*;
20 import aco
.parameter
.as
.asrank
.*;
21 import aco
.parameter
.as
.simpleas
.*;
22 import aco
.environment
.as
.*;
23 import aco
.environment
.acs
.*;
24 import aco
.tsplibreader
.*;
26 public class AntMain
{
28 public static void runACSAlgorithm(String filename
,
36 ACSMediator acom
= new ACSMediator(new ACSParameter());
37 ACOGraph acog
= new ACOGraph(acom
);
38 acom
.setACOGraph(acog
);
40 /* these are the most important */
41 acom
.setACOStrategy(new ACSStrategy(acom
));
42 acom
.setTauZeroStrategy(new ACSTauZeroStrategy(acom
));
43 acom
.setPheromoneStrategy(new ACSPheromoneStrategy(acom
));
44 acom
.setChoiceInformationStrategy(new ACSChoiceInformationStrategy(acom
));
46 /* these are not very likely to change */
47 acom
.setGraphStrategy(new GraphStrategy(acom
));
48 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
50 acom
.setGlobalBestTourMapSize(1);
51 acom
.setMaxNumOfTours(runs
);
55 acom
.setNearestNeighbourListDepth(cl
);
58 TSPLibReader tsplr
= new TSPLibReader(filename
);
59 CoordinateData coordinates
= new CoordinateData(tsplr
);
61 acom
.setDistanceStrategy(DistanceStrategy
.getStrategyFromTSPLibReader(tsplr
, acom
));
62 acog
.initializeFromCoordinates(coordinates
);
64 } catch (IOException e
) {
65 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
69 /* after the graph is initialized we can set the Number of Ants
70 * or leave it to the method parameter */
72 acom
.setNumOfAnts(acom
.getNumOfCities());
74 acom
.setNumOfAnts(numofants
);
76 /* after the graph is initialized we can compute TauZero.. */
77 acom
.computeTauZero();
78 /* ..then we can set the initial pheromone value.. */
79 acom
.setInitialPheromones(acom
.getTauZero());
80 /* ..and finally compute the initial Choice Information */
81 acom
.computeChoiceInformation();
83 System
.out
.println(acom
.getACOParameter());
85 ACSEnvironment env
= new ACSEnvironment(acom
);
86 acom
.setEnvironment(env
);
88 AntView av
= new AntView(acom
);
89 new Thread(av
).start();
95 public static void runASAlgorithm(String filename
,
102 SimpleASMediator acom
= new SimpleASMediator(new SimpleASParameter());
103 ACOGraph acog
= new ACOGraph(acom
);
104 acom
.setACOGraph(acog
);
106 /* these are the most important */
107 acom
.setACOStrategy(new ASStrategy(acom
));
108 acom
.setTauZeroStrategy(new SimpleASTauZeroStrategy(acom
));
109 acom
.setPheromoneStrategy(new SimpleASPheromoneStrategy(acom
));
110 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
112 /* these are not very likely to change */
113 acom
.setGraphStrategy(new GraphStrategy(acom
));
114 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
116 acom
.setGlobalBestTourMapSize(1);
117 acom
.setMaxNumOfTours(runs
);
118 acom
.setAlpha(alpha
);
123 TSPLibReader tsplr
= new TSPLibReader(filename
);
124 CoordinateData coordinates
= new CoordinateData(tsplr
);
126 acom
.setDistanceStrategy(DistanceStrategy
.getStrategyFromTSPLibReader(tsplr
, acom
));
127 acog
.initializeFromCoordinates(coordinates
);
129 } catch (IOException e
) {
130 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
134 /* after the graph is initialized we can set the Number of Ants
135 * or leave it to the method parameter */
136 if (numofants
== 0) {
137 acom
.setNumOfAnts(acom
.getNumOfCities());
139 acom
.setNumOfAnts(numofants
);
141 /* after the graph is initialized we can compute TauZero.. */
142 acom
.computeTauZero();
143 /* ..then we can set the initial pheromone value.. */
144 acom
.setInitialPheromones(acom
.getTauZero());
145 /* ..and finally compute the initial Choice Information */
146 acom
.computeChoiceInformation();
148 System
.out
.println(acom
.getACOParameter());
150 ASEnvironment env
= new ASEnvironment(acom
);
151 acom
.setEnvironment(env
);
153 AntView av
= new AntView(acom
);
154 new Thread(av
).start();
160 public static void runEASAlgorithm(String filename
,
168 EASMediator acom
= new EASMediator(new EASParameter());
169 ACOGraph acog
= new ACOGraph(acom
);
170 acom
.setACOGraph(acog
);
172 /* these are the most important */
173 acom
.setACOStrategy(new ASStrategy(acom
));
174 acom
.setTauZeroStrategy(new EASTauZeroStrategy(acom
));
175 acom
.setPheromoneStrategy(new EASPheromoneStrategy(acom
));
176 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
178 /* these are not very likely to change */
179 acom
.setGraphStrategy(new GraphStrategy(acom
));
180 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
182 acom
.setGlobalBestTourMapSize(1);
183 acom
.setMaxNumOfTours(runs
);
184 acom
.setAlpha(alpha
);
189 TSPLibReader tsplr
= new TSPLibReader(filename
);
190 CoordinateData coordinates
= new CoordinateData(tsplr
);
192 acom
.setDistanceStrategy(DistanceStrategy
.getStrategyFromTSPLibReader(tsplr
, acom
));
193 acog
.initializeFromCoordinates(coordinates
);
195 } catch (IOException exception
) {
196 System
.out
.println(exception
.toString() +
197 "(" + exception
.getClass() + "): " + exception
);
202 acom
.setE((double)acom
.getNumOfCities());
207 /* after the graph is initialized we can set the Number of Ants
208 * or leave it to the method parameter */
209 if (numofants
== 0) {
210 acom
.setNumOfAnts(acom
.getNumOfCities());
212 acom
.setNumOfAnts(numofants
);
215 /* after the graph is initialized we can compute TauZero.. */
216 acom
.computeTauZero();
217 /* ..then we can set the initial pheromone value.. */
218 acom
.setInitialPheromones(acom
.getTauZero());
219 /* ..and finally compute the initial Choice Information */
220 acom
.computeChoiceInformation();
222 System
.out
.println(acom
.getACOParameter());
224 ASEnvironment env
= new ASEnvironment(acom
);
225 acom
.setEnvironment(env
);
227 AntView av
= new AntView(acom
);
228 new Thread(av
).start();
234 public static void runASRankAlgorithm(String filename
,
242 ASRankMediator acom
= new ASRankMediator(new ASRankParameter());
243 ACOGraph acog
= new ACOGraph(acom
);
244 acom
.setACOGraph(acog
);
246 /* these are the most important */
247 acom
.setACOStrategy(new ASStrategy(acom
));
249 acom
.setTauZeroStrategy(new ASRankTauZeroStrategy(acom
));
250 acom
.setPheromoneStrategy(new ASRankPheromoneStrategy(acom
));
252 /* these are not very likely to change */
253 acom
.setGraphStrategy(new GraphStrategy(acom
));
254 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
255 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
257 acom
.setGlobalBestTourMapSize(1);
258 acom
.setMaxNumOfTours(runs
);
259 acom
.setAlpha(alpha
);
264 TSPLibReader tsplr
= new TSPLibReader(filename
);
265 CoordinateData coordinates
= new CoordinateData(tsplr
);
267 acom
.setDistanceStrategy(DistanceStrategy
.getStrategyFromTSPLibReader(tsplr
, acom
));
268 acog
.initializeFromCoordinates(coordinates
);
270 } catch (IOException e
) {
271 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
276 acom
.setW((double)acom
.getNumOfCities());
281 /* after the graph is initialized we can set the Number of Ants
282 * or leave it to the method parameter */
283 if (numofants
== 0) {
284 acom
.setNumOfAnts(acom
.getNumOfCities());
286 acom
.setNumOfAnts(numofants
);
289 /* after the graph is initialized we can compute TauZero.. */
290 acom
.computeTauZero();
291 /* ..then we can set the initial pheromone value.. */
292 acom
.setInitialPheromones(acom
.getTauZero());
293 /* ..and finally compute the initial Choice Information */
294 acom
.computeChoiceInformation();
296 System
.out
.println(acom
.getACOParameter());
298 ASEnvironment env
= new ASEnvironment(acom
);
299 acom
.setEnvironment(env
);
301 AntView av
= new AntView(acom
);
302 new Thread(av
).start();
308 public static void main(String
[] args
) {
310 String
[] algorithms
= { "as", "eas", "asrank", "acs" };
312 for (int i
= 0; i
< algorithms
.length
; i
++) {
313 if (args
[0].equalsIgnoreCase(algorithms
[i
])) {
316 /* filename, alpha, beta, tauzero, roh, numofants, iterations */
317 runASAlgorithm(args
[2], 1.0, 2.0, 0, 0.5, 0, Integer
.parseInt(args
[1]));
320 /* filename, alpha, beta, tauzero, roh, e, numofants, iterations */
321 runEASAlgorithm(args
[2], 1.0, 2.0, 0, 0.5, 0, 0, Integer
.parseInt(args
[1]));
324 /* filename, alpha, beta, tauzero, roh, w, numofants, iterations */
325 runASRankAlgorithm(args
[2], 1.0, 2.0, 0, 0.5, 6.0, 0, Integer
.parseInt(args
[1]));
328 /* filename, beta, qzero, roh, cl, numofants, runs */
329 runACSAlgorithm(args
[2], 2.0, 0.7, 0.1, 15, 15, Integer
.parseInt(args
[1]));
335 System
.out
.print("Computation finished..");
336 /* wait for a keypress before exit */
340 } catch (IOException e
) {
341 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);