apply the observer pattern for the gui to get notifications from AntView
[aco.git] / src / aco / EnvironmentFactory.java
blobe5fea24e57f3c8d454ae5a3866d566f777e57083
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.*;
23 import aco.environment.as.*;
24 import aco.environment.acs.*;
25 import aco.tsplibreader.*;
27 public class EnvironmentFactory {
29 public static Environment acs(String filename,
30 double beta,
31 double qzero,
32 double roh,
33 int cl,
34 int numofants,
35 int runs) {
37 ACSMediator acom = new ACSMediator(new ACSParameter());
38 ACOGraph acog = new ACOGraph(acom);
39 acom.setACOGraph(acog);
41 /* these are the most important */
42 acom.setACOStrategy(new ACSStrategy(acom));
43 acom.setTauZeroStrategy(new ACSTauZeroStrategy(acom));
44 acom.setPheromoneStrategy(new ACSPheromoneStrategy(acom));
45 acom.setChoiceInformationStrategy(new ACSChoiceInformationStrategy(acom));
47 /* these are not very likely to change */
48 acom.setGraphStrategy(new GraphStrategy(acom));
49 acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom));
51 acom.setGlobalBestTourMapSize(1);
52 acom.setMaxNumOfTours(runs);
53 acom.setBeta(beta);
54 acom.setRoh(roh);
55 acom.setQZero(qzero);
56 acom.setNearestNeighbourListDepth(cl);
58 try {
59 TSPLibReader tsplr = new TSPLibReader(filename);
60 CoordinateData coordinates = new CoordinateData(tsplr);
62 acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom));
63 acog.initializeFromCoordinates(coordinates);
65 } catch (IOException e) {
66 System.out.println(e.toString() + "(" + e.getClass() + "): " + e);
67 return null;
70 /* after the graph is initialized we can set the Number of Ants
71 * or leave it to the method parameter */
72 if (numofants == 0) {
73 acom.setNumOfAnts(acom.getNumOfCities());
74 } else {
75 acom.setNumOfAnts(numofants);
77 /* after the graph is initialized we can compute TauZero.. */
78 acom.computeTauZero();
79 /* ..then we can set the initial pheromone value.. */
80 acom.setInitialPheromones(acom.getTauZero());
81 /* ..and finally compute the initial Choice Information */
82 acom.computeChoiceInformation();
84 System.out.println(acom.getACOParameter());
86 ACSEnvironment env = new ACSEnvironment(acom);
87 acom.setEnvironment(env);
89 AntView av = new AntView(acom);
90 new Thread(av).start();
91 env.addObserver(av);
93 return (Environment)env;
96 public static Environment as(String filename,
97 double alpha,
98 double beta,
99 double tauzero,
100 double roh,
101 int numofants,
102 int runs) {
103 SimpleASMediator acom = new SimpleASMediator(new SimpleASParameter());
104 ACOGraph acog = new ACOGraph(acom);
105 acom.setACOGraph(acog);
107 /* these are the most important */
108 acom.setACOStrategy(new ASStrategy(acom));
109 acom.setTauZeroStrategy(new SimpleASTauZeroStrategy(acom));
110 acom.setPheromoneStrategy(new SimpleASPheromoneStrategy(acom));
111 acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom));
113 /* these are not very likely to change */
114 acom.setGraphStrategy(new GraphStrategy(acom));
115 acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom));
117 acom.setGlobalBestTourMapSize(1);
118 acom.setMaxNumOfTours(runs);
119 acom.setAlpha(alpha);
120 acom.setBeta(beta);
121 acom.setRoh(roh);
123 try {
124 TSPLibReader tsplr = new TSPLibReader(filename);
125 CoordinateData coordinates = new CoordinateData(tsplr);
127 acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom));
128 acog.initializeFromCoordinates(coordinates);
130 } catch (IOException e) {
131 System.out.println(e.toString() + "(" + e.getClass() + "): " + e);
132 return null;
135 /* after the graph is initialized we can set the Number of Ants
136 * or leave it to the method parameter */
137 if (numofants == 0) {
138 acom.setNumOfAnts(acom.getNumOfCities());
139 } else {
140 acom.setNumOfAnts(numofants);
142 /* after the graph is initialized we can compute TauZero.. */
143 acom.computeTauZero();
144 /* ..then we can set the initial pheromone value.. */
145 acom.setInitialPheromones(acom.getTauZero());
146 /* ..and finally compute the initial Choice Information */
147 acom.computeChoiceInformation();
149 System.out.println(acom.getACOParameter());
151 ASEnvironment env = new ASEnvironment(acom);
152 acom.setEnvironment(env);
154 AntView av = new AntView(acom);
155 new Thread(av).start();
156 env.addObserver(av);
158 return (Environment)env;
161 public static Environment eas(String filename,
162 double alpha,
163 double beta,
164 double tauzero,
165 double roh,
166 double e,
167 int numofants,
168 int runs) {
169 EASMediator acom = new EASMediator(new EASParameter());
170 ACOGraph acog = new ACOGraph(acom);
171 acom.setACOGraph(acog);
173 /* these are the most important */
174 acom.setACOStrategy(new ASStrategy(acom));
175 acom.setTauZeroStrategy(new EASTauZeroStrategy(acom));
176 acom.setPheromoneStrategy(new EASPheromoneStrategy(acom));
177 acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom));
179 /* these are not very likely to change */
180 acom.setGraphStrategy(new GraphStrategy(acom));
181 acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom));
183 acom.setGlobalBestTourMapSize(1);
184 acom.setMaxNumOfTours(runs);
185 acom.setAlpha(alpha);
186 acom.setBeta(beta);
187 acom.setRoh(roh);
189 try {
190 TSPLibReader tsplr = new TSPLibReader(filename);
191 CoordinateData coordinates = new CoordinateData(tsplr);
193 acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom));
194 acog.initializeFromCoordinates(coordinates);
196 } catch (IOException exception) {
197 System.out.println(exception.toString() +
198 "(" + exception.getClass() + "): " + exception);
199 return null;
202 if (e == 0.0) {
203 acom.setE((double)acom.getNumOfCities());
204 } else {
205 acom.setE(e);
208 /* after the graph is initialized we can set the Number of Ants
209 * or leave it to the method parameter */
210 if (numofants == 0) {
211 acom.setNumOfAnts(acom.getNumOfCities());
212 } else {
213 acom.setNumOfAnts(numofants);
216 /* after the graph is initialized we can compute TauZero.. */
217 acom.computeTauZero();
218 /* ..then we can set the initial pheromone value.. */
219 acom.setInitialPheromones(acom.getTauZero());
220 /* ..and finally compute the initial Choice Information */
221 acom.computeChoiceInformation();
223 System.out.println(acom.getACOParameter());
225 ASEnvironment env = new ASEnvironment(acom);
226 acom.setEnvironment(env);
228 AntView av = new AntView(acom);
229 new Thread(av).start();
230 env.addObserver(av);
232 return (Environment)env;
235 public static Environment asrank(String filename,
236 double alpha,
237 double beta,
238 double tauzero,
239 double roh,
240 double w,
241 int numofants,
242 int runs) {
243 ASRankMediator acom = new ASRankMediator(new ASRankParameter());
244 ACOGraph acog = new ACOGraph(acom);
245 acom.setACOGraph(acog);
247 /* these are the most important */
248 acom.setACOStrategy(new ASStrategy(acom));
250 acom.setTauZeroStrategy(new ASRankTauZeroStrategy(acom));
251 acom.setPheromoneStrategy(new ASRankPheromoneStrategy(acom));
253 /* these are not very likely to change */
254 acom.setGraphStrategy(new GraphStrategy(acom));
255 acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom));
256 acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom));
258 acom.setGlobalBestTourMapSize(1);
259 acom.setMaxNumOfTours(runs);
260 acom.setAlpha(alpha);
261 acom.setBeta(beta);
262 acom.setRoh(roh);
264 try {
265 TSPLibReader tsplr = new TSPLibReader(filename);
266 CoordinateData coordinates = new CoordinateData(tsplr);
268 acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom));
269 acog.initializeFromCoordinates(coordinates);
271 } catch (IOException e) {
272 System.out.println(e.toString() + "(" + e.getClass() + "): " + e);
273 return null;
276 if (w == 0.0) {
277 acom.setW((double)acom.getNumOfCities());
278 } else {
279 acom.setW(w);
282 /* after the graph is initialized we can set the Number of Ants
283 * or leave it to the method parameter */
284 if (numofants == 0) {
285 acom.setNumOfAnts(acom.getNumOfCities());
286 } else {
287 acom.setNumOfAnts(numofants);
290 /* after the graph is initialized we can compute TauZero.. */
291 acom.computeTauZero();
292 /* ..then we can set the initial pheromone value.. */
293 acom.setInitialPheromones(acom.getTauZero());
294 /* ..and finally compute the initial Choice Information */
295 acom.computeChoiceInformation();
297 System.out.println(acom.getACOParameter());
299 ASEnvironment env = new ASEnvironment(acom);
300 acom.setEnvironment(env);
302 AntView av = new AntView(acom);
303 new Thread(av).start();
304 env.addObserver(av);
306 return (Environment)env;