3 import java
.lang
.Runtime
;
4 import java
.util
.Observable
;
6 class Environment
extends Observable
{
8 public final int NumOfAnts
;
9 public final int MaxNumOfTours
;
11 protected int[] MinTour
;
12 protected int MinTourLength
;
13 protected int MinTourIteration
;
16 protected Graph graph
;
20 Environment(int MaxNumOfTours
, Graph graph
) {
21 this(MaxNumOfTours
, 0, graph
.NumOfCities
, graph
);
24 Environment(int MaxNumOfTours
, int GuiSize
, Graph graph
) {
25 this(MaxNumOfTours
, GuiSize
, graph
.NumOfCities
, graph
);
28 Environment(int MaxNumOfTours
, int GuiSize
, int NumOfAnts
, Graph graph
) {
30 this.MaxNumOfTours
= MaxNumOfTours
;
32 this.MinTour
= new int[graph
.getNumOfCities()];
33 this.MinTourLength
= graph
.getINFINITY();
34 this.MinTourIteration
= 0;
36 this.NumOfAnts
= NumOfAnts
;
37 this.ants
= new Ant
[NumOfAnts
];
40 for (int i
= 0; i
< NumOfAnts
; i
++)
41 ants
[i
] = new Ant(graph
);
43 if (graph
.hasCoordinates()) {
45 av
= new AntView(this);
47 av
= new AntView(GuiSize
, this);
49 new Thread(av
).start();
52 /* add a shutdown hook to print out the last best solution */
53 Runtime
.getRuntime().addShutdownHook(new EnvironmentShutdownHook(this));
55 /* run the garbage collector after all the initialization is done */
60 protected void finalize() {
66 while (! terminateCondition(i
++)) {
68 graph
.pheromoneUpdate(ants
);
71 * updatePheromoneTrails(); */
75 protected void constructSolutions() {
77 /* clear ants memory and ..
78 * assign an initial random city to ant */
80 for (Ant ant
: ants
) {
82 ant
.pickInitialRandomCity();
85 /* let each ant construct a complete tour
86 * start a 1 since every ant already has an initial city */
88 for (int step
= 1; step
< graph
.getNumOfCities(); step
++) {
90 ant
.neighbourListASDecisionRule(step
);
93 /* compute tour length of each ant */
95 for (Ant ant
: ants
) {
96 ant
.computeTourLength();
102 protected void updateStatistics(int iteration
) {
103 for (Ant ant
: ants
) {
105 if (ant
.getTourLength() < getMinTourLength()) {
106 setMinTourLength(ant
.getTourLength());
107 setMinTourIteration(iteration
);
109 for (int i
= 0; i
< graph
.NumOfCities
; i
++) {
110 setMinTour(i
, ant
.getTour(i
));
113 if (graph
.hasCoordinates()) {
115 this.notifyObservers(this);
120 /* since we are already at it.. run the garbage collector */
124 protected boolean stagnationBehaviour() {
125 for (Ant antOne
: ants
) {
126 for (Ant antTwo
: ants
) {
127 for (int i
: antOne
.getTour()) {
128 for (int j
: antTwo
.getTour()) {
138 protected boolean terminateCondition(int iteration
) {
139 /* 1) solution within predefined distance
140 * 2) max number of tour constructions or algorithm iterations
142 * 4) algorithm stagnation behaviour */
143 if (iteration
>= MaxNumOfTours
) {
145 //} else if (stagnationBehaviour()) {
153 public String
toString() {
154 StringBuilder result
= new StringBuilder();
155 result
.append("Shortest tour at iteration " +
161 for (int t
: MinTour
) {
162 if (graph
.getIds(t
) != null)
163 result
.append(graph
.ids
[t
] + "\t");
165 result
.append(t
+ "\t");
167 if (graph
.getIds(0) != null)
168 result
.append(graph
.ids
[MinTour
[0]]);
170 result
.append(MinTour
[0]);
171 return result
.toString();
174 public int[] getMinTour() {
178 public int getMinTour(int index
) {
179 return this.MinTour
[index
];
182 public void setMinTour(int[] MinTour
) {
183 this.MinTour
= MinTour
;
186 public void setMinTour(int index
, int MinTour
) {
187 this.MinTour
[index
] = MinTour
;
190 public int getMinTourLength() {
191 return this.MinTourLength
;
194 public void setMinTourLength(int MinTourLength
) {
195 this.MinTourLength
= MinTourLength
;
198 public int getMinTourIteration() {
199 return this.MinTourIteration
;
202 public void setMinTourIteration(int MinTourIteration
) {
203 this.MinTourIteration
= MinTourIteration
;
206 public Graph
getGraph() {
210 public void setGraph(Graph graph
) {