move initial ant system algorithm to protoas package
[aco.git] / aco / AntMain.java
blob2ca49c2904b3734da1ffdf74795788d4a70c404e
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.mediator.*;
10 import aco.parameter.*;
11 import aco.environment.*;
12 import aco.tsplibreader.*;
14 public class AntMain {
16 public static void runACSAlgorithm(String filename,
17 double beta,
18 double qzero,
19 double roh,
20 int cl,
21 int numofants,
22 int runs) {
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);
43 acom.setBeta(beta);
44 acom.setRoh(roh);
45 acom.setQZero(qzero);
46 acom.setNearestNeighbourListDepth(cl);
48 try {
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);
54 return;
57 /* after the graph is initialized we can set the Number of Ants
58 * or leave it to the method parameter */
59 if (numofants == 0) {
60 acom.setNumOfAnts(acom.getNumOfCities());
61 } else {
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();
78 env.addObserver(av);
80 env.run();
83 public static void runASAlgorithm(String filename,
84 double alpha,
85 double beta,
86 double tauzero,
87 double roh,
88 int numofants,
89 int runs) {
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);
110 acom.setBeta(beta);
111 acom.setRoh(roh);
113 try {
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);
121 return;
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());
128 } else {
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();
145 env.addObserver(av);
147 env.run();
150 public static void runEASAlgorithm(String filename,
151 double alpha,
152 double beta,
153 double tauzero,
154 double roh,
155 double e,
156 int numofants,
157 int runs) {
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);
180 acom.setBeta(beta);
181 acom.setRoh(roh);
183 try {
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);
192 return;
195 if (e == 0.0) {
196 acom.setE((double)acom.getNumOfCities());
197 } else {
198 acom.setE(e);
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());
205 } else {
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();
223 env.addObserver(av);
225 env.run();
228 public static void runASRankAlgorithm(String filename,
229 double alpha,
230 double beta,
231 double tauzero,
232 double roh,
233 double w,
234 int numofants,
235 int runs) {
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);
258 acom.setBeta(beta);
259 acom.setRoh(roh);
261 try {
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);
269 return;
272 if (w == 0.0) {
273 acom.setW((double)acom.getNumOfCities());
274 } else {
275 acom.setW(w);
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());
282 } else {
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();
300 env.addObserver(av);
302 env.run();
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])) {
311 switch (i) {
312 case 0:
313 /* filename, alpha, beta, tauzero, roh, numofants, iterations */
314 runASAlgorithm(args[2], 1.0, 5.0, 0, 0.5, 0, Integer.parseInt(args[1]));
315 break;
316 case 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]));
319 break;
320 case 2:
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]));
323 break;
324 case 3:
325 /* filename, beta, qzero, roh, cl, numofants, runs */
326 runACSAlgorithm(args[2], 2.0, 0.7, 0.1, 15, 15, Integer.parseInt(args[1]));
327 break;
332 System.out.print("Computation finished..");
333 /* wait for a keypress before exit */
334 try {
335 System.in.read();
337 } catch (IOException e) {
338 System.out.println(e.toString() + "(" + e.getClass() + "): " + e);
341 System.exit(0);