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 ASChoiceInformationStrategy(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
);
47 acom
.setNearestNeighbourListDepth(cl
);
50 TSPLibReader tsplr
= new TSPLibReader(filename
);
51 CoordinateData coordinates
= new CoordinateData(tsplr
);
52 acog
.initializeFromCoordinates(coordinates
);
53 } catch (IOException e
) {
54 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
58 /* after the graph is initialized we can set the Number of Ants
59 * or leave it to the method parameter */
61 acom
.setNumOfAnts(acom
.getNumOfCities());
63 acom
.setNumOfAnts(numofants
);
65 /* after the graph is initialized we can compute TauZero.. */
66 acom
.computeTauZero();
67 /* ..then we can set the initial pheromone value.. */
68 acom
.setInitialPheromones(acom
.getTauZero());
69 /* ..and finally compute the initial Choice Information */
70 acom
.computeChoiceInformation();
72 System
.out
.println(acom
.getACOParameter());
74 Environment env
= new Environment(acom
);
75 acom
.setEnvironment(env
);
77 AntView av
= new AntView(acom
);
78 new Thread(av
).start();
84 public static void runASAlgorithm(String filename
,
91 ACOMediator acom
= new ACOMediator(new ASParameter());
92 ACOGraph acog
= new ACOGraph(acom
);
93 acom
.setACOGraph(acog
);
95 acom
.setGraphStrategy(new GraphStrategy(acom
));
96 /* these are the most important */
97 acom
.setACOStrategy(new ASStrategy(acom
));
98 /* setDistanceStrategy should be a flag from TSPLibReader */
99 //acom.setDistanceStrategy(new ATT_DistanceStrategy(acom));
100 acom
.setDistanceStrategy(new EUC_2D_DistanceStrategy(acom
));
102 acom
.setPheromoneStrategy(new ASPheromoneStrategy(acom
));
103 acom
.setTauZeroStrategy(new ASTauZeroStrategy(acom
));
104 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
105 /* these are not very likely to change */
106 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
108 acom
.setGlobalBestTourMapSize(1);
109 acom
.setMaxNumOfTours(runs
);
110 acom
.setAlpha(alpha
);
115 TSPLibReader tsplr
= new TSPLibReader(filename
);
116 CoordinateData coordinates
= new CoordinateData(tsplr
);
118 acog
.initializeFromCoordinates(coordinates
);
120 } catch (IOException e
) {
121 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
125 /* after the graph is initialized we can set the Number of Ants
126 * or leave it to the method parameter */
127 if (numofants
== 0) {
128 acom
.setNumOfAnts(acom
.getNumOfCities());
130 acom
.setNumOfAnts(numofants
);
132 /* after the graph is initialized we can compute TauZero.. */
133 acom
.computeTauZero();
134 /* ..then we can set the initial pheromone value.. */
135 acom
.setInitialPheromones(acom
.getTauZero());
136 /* ..and finally compute the initial Choice Information */
137 acom
.computeChoiceInformation();
139 System
.out
.println(acom
.getACOParameter());
141 Environment env
= new Environment(acom
);
142 acom
.setEnvironment(env
);
144 AntView av
= new AntView(acom
);
145 new Thread(av
).start();
151 public static void runEASAlgorithm(String filename
,
159 ACOMediator acom
= new ACOMediator(new EASParameter());
160 ACOGraph acog
= new ACOGraph(acom
);
161 acom
.setACOGraph(acog
);
163 /* these are the most important */
164 acom
.setACOStrategy(new ASStrategy(acom
));
166 /* setDistanceStrategy should be a flag from TSPLibReader */
167 //acom.setDistanceStrategy(new ATT_DistanceStrategy(acom));
168 acom
.setDistanceStrategy(new EUC_2D_DistanceStrategy(acom
));
170 acom
.setPheromoneStrategy(new EASPheromoneStrategy(acom
));
171 acom
.setTauZeroStrategy(new EASTauZeroStrategy(acom
));
173 /* these are not very likely to change */
174 acom
.setGraphStrategy(new GraphStrategy(acom
));
175 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
176 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
178 acom
.setGlobalBestTourMapSize(1);
179 acom
.setMaxNumOfTours(runs
);
180 acom
.setAlpha(alpha
);
185 TSPLibReader tsplr
= new TSPLibReader(filename
);
186 CoordinateData coordinates
= new CoordinateData(tsplr
);
188 acog
.initializeFromCoordinates(coordinates
);
190 } catch (IOException exception
) {
191 System
.out
.println(exception
.toString() +
192 "(" + exception
.getClass() + "): " + exception
);
197 acom
.setE((double)acom
.getNumOfCities());
202 /* after the graph is initialized we can set the Number of Ants
203 * or leave it to the method parameter */
204 if (numofants
== 0) {
205 acom
.setNumOfAnts(acom
.getNumOfCities());
207 acom
.setNumOfAnts(numofants
);
210 /* after the graph is initialized we can compute TauZero.. */
211 acom
.computeTauZero();
212 /* ..then we can set the initial pheromone value.. */
213 acom
.setInitialPheromones(acom
.getTauZero());
214 /* ..and finally compute the initial Choice Information */
215 acom
.computeChoiceInformation();
217 System
.out
.println(acom
.getACOParameter());
219 Environment env
= new Environment(acom
);
220 acom
.setEnvironment(env
);
222 AntView av
= new AntView(acom
);
223 new Thread(av
).start();
229 public static void runASRankAlgorithm(String filename
,
237 ACOMediator acom
= new ACOMediator(new ASRankParameter());
238 ACOGraph acog
= new ACOGraph(acom
);
239 acom
.setACOGraph(acog
);
241 /* these are the most important */
242 acom
.setACOStrategy(new ASStrategy(acom
));
244 /* setDistanceStrategy should be a flag from TSPLibReader */
245 //acom.setDistanceStrategy(new ATT_DistanceStrategy(acom));
246 acom
.setDistanceStrategy(new EUC_2D_DistanceStrategy(acom
));
248 acom
.setTauZeroStrategy(new ASRankTauZeroStrategy(acom
));
249 acom
.setPheromoneStrategy(new ASRankPheromoneStrategy(acom
));
251 /* these are not very likely to change */
252 acom
.setGraphStrategy(new GraphStrategy(acom
));
253 acom
.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom
));
254 acom
.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom
));
256 acom
.setGlobalBestTourMapSize(1);
257 acom
.setMaxNumOfTours(runs
);
258 acom
.setAlpha(alpha
);
263 TSPLibReader tsplr
= new TSPLibReader(filename
);
264 CoordinateData coordinates
= new CoordinateData(tsplr
);
266 acog
.initializeFromCoordinates(coordinates
);
268 } catch (IOException e
) {
269 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);
274 acom
.setW((double)acom
.getNumOfCities());
279 /* after the graph is initialized we can set the Number of Ants
280 * or leave it to the method parameter */
281 if (numofants
== 0) {
282 acom
.setNumOfAnts(acom
.getNumOfCities());
284 acom
.setNumOfAnts(numofants
);
287 /* after the graph is initialized we can compute TauZero.. */
288 acom
.computeTauZero();
289 /* ..then we can set the initial pheromone value.. */
290 acom
.setInitialPheromones(acom
.getTauZero());
291 /* ..and finally compute the initial Choice Information */
292 acom
.computeChoiceInformation();
294 System
.out
.println(acom
.getACOParameter());
296 Environment env
= new Environment(acom
);
297 acom
.setEnvironment(env
);
299 AntView av
= new AntView(acom
);
300 new Thread(av
).start();
306 public static void main(String
[] args
) {
308 /* filename, alpha, beta, tauzero, roh {w/e}, numofants, iterations */
309 //runASAlgorithm(args[1], 1.0, 5.0, 0, 0.5, 0, Integer.parseInt(args[0]));
310 runEASAlgorithm(args
[1], 1.0, 5.0, 0, 0.5, 0, 0, Integer
.parseInt(args
[0]));
311 //runASRankAlgorithm(args[1], 1.0, 5.0, 0, 0.5, 6.0, 0, Integer.parseInt(args[0]));
312 /* filename, beta, qzero, roh, cl, numofants, runs */
313 //runACSAlgorithm(args[1], 2.0, 0.7, 0.1, 15, 15, Integer.parseInt(args[0]));
314 System
.out
.print("Computation finished..");
315 /* wait for a keypress before exit */
319 } catch (IOException e
) {
320 System
.out
.println(e
.toString() + "(" + e
.getClass() + "): " + e
);