implementation of the gui coupling with AntView
[aco.git] / src / aco / AntFactory.java
blobb3d39fa52705d5cf090eb566a3a1b74bc804a699
1 package aco;
3 import java.io.IOException;
5 import aco.graph.*;
6 import aco.graph.data.*;
7 import aco.strategy.*;
8 import aco.strategy.acs.*;
9 import aco.strategy.as.*;
10 import aco.strategy.as.eas.*;
11 import aco.strategy.as.asrank.*;
12 import aco.strategy.as.simpleas.*;
13 import aco.mediator.*;
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 AntFactory {
28 public static ACOMediator acs(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 null;
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 ACSEnvironment env = new ACSEnvironment(acom);
84 acom.setEnvironment(env);
86 return (ACOMediator)acom;
89 public static ACOMediator as(String filename,
90 double alpha,
91 double beta,
92 double tauzero,
93 double roh,
94 int numofants,
95 int runs) {
96 SimpleASMediator acom = new SimpleASMediator(new SimpleASParameter());
97 ACOGraph acog = new ACOGraph(acom);
98 acom.setACOGraph(acog);
100 /* these are the most important */
101 acom.setACOStrategy(new ASStrategy(acom));
102 acom.setTauZeroStrategy(new SimpleASTauZeroStrategy(acom));
103 acom.setPheromoneStrategy(new SimpleASPheromoneStrategy(acom));
104 acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom));
106 /* these are not very likely to change */
107 acom.setGraphStrategy(new GraphStrategy(acom));
108 acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom));
110 acom.setGlobalBestTourMapSize(1);
111 acom.setMaxNumOfTours(runs);
112 acom.setAlpha(alpha);
113 acom.setBeta(beta);
114 acom.setRoh(roh);
116 try {
117 TSPLibReader tsplr = new TSPLibReader(filename);
118 CoordinateData coordinates = new CoordinateData(tsplr);
120 acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom));
121 acog.initializeFromCoordinates(coordinates);
123 } catch (IOException e) {
124 System.out.println(e.toString() + "(" + e.getClass() + "): " + e);
125 return null;
128 /* after the graph is initialized we can set the Number of Ants
129 * or leave it to the method parameter */
130 if (numofants == 0) {
131 acom.setNumOfAnts(acom.getNumOfCities());
132 } else {
133 acom.setNumOfAnts(numofants);
135 /* after the graph is initialized we can compute TauZero.. */
136 acom.computeTauZero();
137 /* ..then we can set the initial pheromone value.. */
138 acom.setInitialPheromones(acom.getTauZero());
139 /* ..and finally compute the initial Choice Information */
140 acom.computeChoiceInformation();
142 ASEnvironment env = new ASEnvironment(acom);
143 acom.setEnvironment(env);
145 return (ACOMediator)acom;
148 public static ACOMediator eas(String filename,
149 double alpha,
150 double beta,
151 double tauzero,
152 double roh,
153 double e,
154 int numofants,
155 int runs) {
156 EASMediator acom = new EASMediator(new EASParameter());
157 ACOGraph acog = new ACOGraph(acom);
158 acom.setACOGraph(acog);
160 /* these are the most important */
161 acom.setACOStrategy(new ASStrategy(acom));
162 acom.setTauZeroStrategy(new EASTauZeroStrategy(acom));
163 acom.setPheromoneStrategy(new EASPheromoneStrategy(acom));
164 acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom));
166 /* these are not very likely to change */
167 acom.setGraphStrategy(new GraphStrategy(acom));
168 acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom));
170 acom.setGlobalBestTourMapSize(1);
171 acom.setMaxNumOfTours(runs);
172 acom.setAlpha(alpha);
173 acom.setBeta(beta);
174 acom.setRoh(roh);
176 try {
177 TSPLibReader tsplr = new TSPLibReader(filename);
178 CoordinateData coordinates = new CoordinateData(tsplr);
180 acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom));
181 acog.initializeFromCoordinates(coordinates);
183 } catch (IOException exception) {
184 System.out.println(exception.toString() +
185 "(" + exception.getClass() + "): " + exception);
186 return null;
189 if (e == 0.0) {
190 acom.setE((double)acom.getNumOfCities());
191 } else {
192 acom.setE(e);
195 /* after the graph is initialized we can set the Number of Ants
196 * or leave it to the method parameter */
197 if (numofants == 0) {
198 acom.setNumOfAnts(acom.getNumOfCities());
199 } else {
200 acom.setNumOfAnts(numofants);
203 /* after the graph is initialized we can compute TauZero.. */
204 acom.computeTauZero();
205 /* ..then we can set the initial pheromone value.. */
206 acom.setInitialPheromones(acom.getTauZero());
207 /* ..and finally compute the initial Choice Information */
208 acom.computeChoiceInformation();
210 ASEnvironment env = new ASEnvironment(acom);
211 acom.setEnvironment(env);
213 return (ACOMediator)acom;
216 public static ACOMediator asrank(String filename,
217 double alpha,
218 double beta,
219 double tauzero,
220 double roh,
221 double w,
222 int numofants,
223 int runs) {
224 ASRankMediator acom = new ASRankMediator(new ASRankParameter());
225 ACOGraph acog = new ACOGraph(acom);
226 acom.setACOGraph(acog);
228 /* these are the most important */
229 acom.setACOStrategy(new ASStrategy(acom));
231 acom.setTauZeroStrategy(new ASRankTauZeroStrategy(acom));
232 acom.setPheromoneStrategy(new ASRankPheromoneStrategy(acom));
234 /* these are not very likely to change */
235 acom.setGraphStrategy(new GraphStrategy(acom));
236 acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom));
237 acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom));
239 acom.setGlobalBestTourMapSize(1);
240 acom.setMaxNumOfTours(runs);
241 acom.setAlpha(alpha);
242 acom.setBeta(beta);
243 acom.setRoh(roh);
245 try {
246 TSPLibReader tsplr = new TSPLibReader(filename);
247 CoordinateData coordinates = new CoordinateData(tsplr);
249 acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom));
250 acog.initializeFromCoordinates(coordinates);
252 } catch (IOException e) {
253 System.out.println(e.toString() + "(" + e.getClass() + "): " + e);
254 return null;
257 if (w == 0.0) {
258 acom.setW((double)acom.getNumOfCities());
259 } else {
260 acom.setW(w);
263 /* after the graph is initialized we can set the Number of Ants
264 * or leave it to the method parameter */
265 if (numofants == 0) {
266 acom.setNumOfAnts(acom.getNumOfCities());
267 } else {
268 acom.setNumOfAnts(numofants);
271 /* after the graph is initialized we can compute TauZero.. */
272 acom.computeTauZero();
273 /* ..then we can set the initial pheromone value.. */
274 acom.setInitialPheromones(acom.getTauZero());
275 /* ..and finally compute the initial Choice Information */
276 acom.computeChoiceInformation();
278 ASEnvironment env = new ASEnvironment(acom);
279 acom.setEnvironment(env);
281 return (ACOMediator)acom;