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
.NumOfCities
];
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 */
77 for (Ant ant
: ants
) {
79 ant
.pickInitialRandomCity();
82 /* let each ant construct a complete tour
83 * start a 1 since every ant already has an initial city */
84 for (int step
= 1; step
< graph
.getNumOfCities(); step
++) {
86 ant
.neighbourListASDecisionRule(step
);
89 /* compute tour length of each ant */
90 for (Ant ant
: ants
) {
91 ant
.computeTourLength();
96 protected void updateStatistics(int iteration
) {
97 for (Ant ant
: ants
) {
99 if (ant
.getTourLength() < getMinTourLength()) {
100 setMinTourLength(ant
.getTourLength());
101 setMinTourIteration(iteration
);
103 for (int i
= 0; i
< graph
.NumOfCities
; i
++) {
104 setMinTour(i
, ant
.getTour(i
));
107 if (graph
.hasCoordinates()) {
109 this.notifyObservers(this);
114 /* since we are already at it.. run the garbage collector */
118 protected Boolean
terminateCondition(int iteration
) {
119 /* 1) solution within predefined distance
120 * 2) max number of tour constructions or algorithm iterations
122 * 4) algorithm stagnation behaviour */
123 if (iteration
>= MaxNumOfTours
) {
131 public String
toString() {
132 StringBuilder result
= new StringBuilder();
133 result
.append("Shortest tour at iteration " +
139 for (int t
: MinTour
) {
140 if (graph
.getIds(t
) != null)
141 result
.append(graph
.ids
[t
] + "\t");
143 result
.append(t
+ "\t");
145 if (graph
.getIds(0) != null)
146 result
.append(graph
.ids
[MinTour
[0]]);
148 result
.append(MinTour
[0]);
149 return result
.toString();
152 public int[] getMinTour() {
156 public int getMinTour(int index
) {
157 return this.MinTour
[index
];
160 public void setMinTour(int[] MinTour
) {
161 this.MinTour
= MinTour
;
164 public void setMinTour(int index
, int MinTour
) {
165 this.MinTour
[index
] = MinTour
;
168 public int getMinTourLength() {
169 return this.MinTourLength
;
172 public void setMinTourLength(int MinTourLength
) {
173 this.MinTourLength
= MinTourLength
;
176 public int getMinTourIteration() {
177 return this.MinTourIteration
;
180 public void setMinTourIteration(int MinTourIteration
) {
181 this.MinTourIteration
= MinTourIteration
;
184 public Graph
getGraph() {
188 public void setGraph(Graph graph
) {