rewrite main class file
[aco.git] / aco / AntMain.java
blob56be0b8918356c89ef5edd40517828fbc0c460f2
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 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);
43 acom.setAlpha(1.0);
44 acom.setBeta(beta);
45 acom.setRoh(roh);
46 acom.setQZero(qzero);
47 acom.setNearestNeighbourListDepth(cl);
49 try {
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);
55 return;
58 /* after the graph is initialized we can set the Number of Ants
59 * or leave it to the method parameter */
60 if (numofants == 0) {
61 acom.setNumOfAnts(acom.getNumOfCities());
62 } else {
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();
79 env.addObserver(av);
81 env.run();
84 public static void runASAlgorithm(String filename,
85 double alpha,
86 double beta,
87 double tauzero,
88 double roh,
89 int numofants,
90 int runs) {
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);
111 acom.setBeta(beta);
112 acom.setRoh(roh);
114 try {
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);
122 return;
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());
129 } else {
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();
146 env.addObserver(av);
148 env.run();
151 public static void runEASAlgorithm(String filename,
152 double alpha,
153 double beta,
154 double tauzero,
155 double roh,
156 double e,
157 int numofants,
158 int runs) {
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);
181 acom.setBeta(beta);
182 acom.setRoh(roh);
184 try {
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);
193 return;
196 if (e == 0.0) {
197 acom.setE((double)acom.getNumOfCities());
198 } else {
199 acom.setE(e);
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());
206 } else {
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();
224 env.addObserver(av);
226 env.run();
229 public static void runASRankAlgorithm(String filename,
230 double alpha,
231 double beta,
232 double tauzero,
233 double roh,
234 double w,
235 int numofants,
236 int runs) {
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);
259 acom.setBeta(beta);
260 acom.setRoh(roh);
262 try {
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);
270 return;
273 if (w == 0.0) {
274 acom.setW((double)acom.getNumOfCities());
275 } else {
276 acom.setW(w);
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());
283 } else {
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();
301 env.addObserver(av);
303 env.run();
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 */
316 try {
317 System.in.read();
319 } catch (IOException e) {
320 System.out.println(e.toString() + "(" + e.getClass() + "): " + e);
323 System.exit(0);