1 import java
.lang
.Runtime
;
2 import java
.util
.Observable
;
4 class Environment
extends Observable
{
6 public final int NumOfAnts
;
7 public final int MaxNumOfTours
;
9 protected int[] MinTour
;
10 protected int MinTourLength
;
11 protected int MinTourIteration
;
14 protected Graph graph
;
18 Environment(int MaxNumOfTours
, Graph graph
) {
19 this(MaxNumOfTours
, 0, graph
.NumOfCities
, graph
);
22 Environment(int MaxNumOfTours
, int GuiSize
, Graph graph
) {
23 this(MaxNumOfTours
, GuiSize
, graph
.NumOfCities
, graph
);
26 Environment(int MaxNumOfTours
, int GuiSize
, int NumOfAnts
, Graph graph
) {
28 this.MaxNumOfTours
= MaxNumOfTours
;
30 this.MinTour
= new int[graph
.getNumOfCities()];
31 this.MinTourLength
= graph
.getINFINITY();
32 this.MinTourIteration
= 0;
34 this.NumOfAnts
= NumOfAnts
;
35 this.ants
= new Ant
[NumOfAnts
];
38 for (int i
= 0; i
< NumOfAnts
; i
++)
39 ants
[i
] = new Ant(graph
);
41 if (graph
.hasCoordinates()) {
43 av
= new AntView(this);
45 av
= new AntView(GuiSize
, this);
47 new Thread(av
).start();
50 /* add a shutdown hook to print out the last best solution */
51 Runtime
.getRuntime().addShutdownHook(new EnvironmentShutdownHook(this));
53 /* run the garbage collector after all the initialization is done */
58 protected void finalize() {
64 while (! terminateCondition(i
++)) {
66 graph
.pheromoneUpdate(ants
);
69 * updatePheromoneTrails(); */
73 protected void constructSolutions() {
75 /* clear ants memory and ..
76 * assign an initial random city to ant */
78 for (Ant ant
: ants
) {
80 ant
.pickInitialRandomCity();
83 /* let each ant construct a complete tour
84 * start a 1 since every ant already has an initial city */
86 for (int step
= 1; step
< graph
.getNumOfCities(); step
++) {
88 ant
.neighbourListASDecisionRule(step
);
91 /* compute tour length of each ant */
93 for (Ant ant
: ants
) {
94 ant
.computeTourLength();
100 protected void updateStatistics(int iteration
) {
101 for (Ant ant
: ants
) {
103 if (ant
.getTourLength() < getMinTourLength()) {
104 setMinTourLength(ant
.getTourLength());
105 setMinTourIteration(iteration
);
107 for (int i
= 0; i
< graph
.NumOfCities
; i
++) {
108 setMinTour(i
, ant
.getTour(i
));
111 if (graph
.hasCoordinates()) {
113 this.notifyObservers(this);
118 /* since we are already at it.. run the garbage collector */
122 protected boolean stagnationBehaviour() {
123 for (Ant antOne
: ants
) {
124 for (Ant antTwo
: ants
) {
125 for (int i
: antOne
.getTour()) {
126 for (int j
: antTwo
.getTour()) {
136 protected boolean terminateCondition(int iteration
) {
137 /* 1) solution within predefined distance
138 * 2) max number of tour constructions or algorithm iterations
140 * 4) algorithm stagnation behaviour */
141 if (iteration
>= MaxNumOfTours
) {
143 //} else if (stagnationBehaviour()) {
151 public String
toString() {
152 StringBuilder result
= new StringBuilder();
153 result
.append("Shortest tour at iteration " +
159 for (int t
: MinTour
) {
160 if (graph
.getIds(t
) != null)
161 result
.append(graph
.ids
[t
] + "\t");
163 result
.append(t
+ "\t");
165 if (graph
.getIds(0) != null)
166 result
.append(graph
.ids
[MinTour
[0]]);
168 result
.append(MinTour
[0]);
169 return result
.toString();
172 public int[] getMinTour() {
176 public int getMinTour(int index
) {
177 return this.MinTour
[index
];
180 public void setMinTour(int[] MinTour
) {
181 this.MinTour
= MinTour
;
184 public void setMinTour(int index
, int MinTour
) {
185 this.MinTour
[index
] = MinTour
;
188 public int getMinTourLength() {
189 return this.MinTourLength
;
192 public void setMinTourLength(int MinTourLength
) {
193 this.MinTourLength
= MinTourLength
;
196 public int getMinTourIteration() {
197 return this.MinTourIteration
;
200 public void setMinTourIteration(int MinTourIteration
) {
201 this.MinTourIteration
= MinTourIteration
;
204 public Graph
getGraph() {
208 public void setGraph(Graph graph
) {