apply the observer pattern for the gui to get notifications from AntView
[aco.git] / src / aco / AntMain.java
bloba8fb46ec3fbfa4baf774c771641ca20c5f78f7f2
1 package aco;
3 import java.io.IOException;
5 import aco.antview.*;
6 import aco.graph.*;
7 import aco.graph.data.*;
8 import aco.strategy.*;
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,
29 double beta,
30 double qzero,
31 double roh,
32 int cl,
33 int numofants,
34 int runs) {
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);
52 acom.setBeta(beta);
53 acom.setRoh(roh);
54 acom.setQZero(qzero);
55 acom.setNearestNeighbourListDepth(cl);
57 try {
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);
66 return;
69 /* after the graph is initialized we can set the Number of Ants
70 * or leave it to the method parameter */
71 if (numofants == 0) {
72 acom.setNumOfAnts(acom.getNumOfCities());
73 } else {
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();
90 env.addObserver(av);
92 env.run();
95 public static void runASAlgorithm(String filename,
96 double alpha,
97 double beta,
98 double tauzero,
99 double roh,
100 int numofants,
101 int runs) {
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);
119 acom.setBeta(beta);
120 acom.setRoh(roh);
122 try {
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);
131 return;
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());
138 } else {
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();
155 env.addObserver(av);
157 env.run();
160 public static void runEASAlgorithm(String filename,
161 double alpha,
162 double beta,
163 double tauzero,
164 double roh,
165 double e,
166 int numofants,
167 int runs) {
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);
185 acom.setBeta(beta);
186 acom.setRoh(roh);
188 try {
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);
198 return;
201 if (e == 0.0) {
202 acom.setE((double)acom.getNumOfCities());
203 } else {
204 acom.setE(e);
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());
211 } else {
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();
229 env.addObserver(av);
231 env.run();
234 public static void runASRankAlgorithm(String filename,
235 double alpha,
236 double beta,
237 double tauzero,
238 double roh,
239 double w,
240 int numofants,
241 int runs) {
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);
260 acom.setBeta(beta);
261 acom.setRoh(roh);
263 try {
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);
272 return;
275 if (w == 0.0) {
276 acom.setW((double)acom.getNumOfCities());
277 } else {
278 acom.setW(w);
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());
285 } else {
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();
303 env.addObserver(av);
305 env.run();
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])) {
314 switch (i) {
315 case 0:
316 /* filename, alpha, beta, tauzero, roh, numofants, iterations */
317 runASAlgorithm(args[2], 1.0, 2.0, 0, 0.5, 0, Integer.parseInt(args[1]));
318 break;
319 case 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]));
322 break;
323 case 2:
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]));
326 break;
327 case 3:
328 /* filename, beta, qzero, roh, cl, numofants, runs */
329 runACSAlgorithm(args[2], 2.0, 0.7, 0.1, 15, 15, Integer.parseInt(args[1]));
330 break;
335 System.out.print("Computation finished..");
336 /* wait for a keypress before exit */
337 try {
338 System.in.read();
340 } catch (IOException e) {
341 System.out.println(e.toString() + "(" + e.getClass() + "): " + e);
344 System.exit(0);