1 package aco
.environment
;
3 import java
.lang
.Runtime
;
4 import java
.util
.SortedMap
;
5 import java
.util
.Observable
;
10 import aco
.environment
.data
.*;
12 public class Environment
16 protected ACOMediator acom
;
17 protected EnvironmentData ed
;
19 public Environment(ACOMediator acom
) {
22 this.ants
= new Ant
[acom
.getNumOfAnts()];
24 for (int i
= 0; i
< acom
.getNumOfAnts(); i
++)
25 ants
[i
] = new Ant(acom
);
27 this.ed
= new EnvironmentData(acom
);
29 /* add a shutdown hook to print out the last best solution */
30 Runtime
.getRuntime().addShutdownHook(new EnvironmentShutdownHook(this));
32 /* run the garbage collector after all the initialization is done */
37 protected void finalize() {
43 while (! terminateCondition(i
++)) {
46 acom
.pheromoneUpdate(ants
);
48 * updatePheromoneTrails(); */
52 protected void constructSolutions() {
54 /* clear ants memory and ..
55 * assign an initial random city to ant */
57 for (Ant ant
: ants
) {
59 acom
.pickInitialRandomCity(ant
);
62 /* let each ant construct a complete tour
63 * start a 1 since every ant already has an initial city */
65 for (int step
= 1; step
< acom
.getNumOfCities(); step
++) {
67 acom
.neighbourListASDecisionRule(ant
, step
);
70 /* compute tour length of each ant */
72 for (Ant ant
: ants
) {
73 acom
.computeTourLength(ant
);
79 protected void updateStatistics(int iteration
) {
80 for (Ant ant
: ants
) {
82 if (ant
.getTourLength() < acom
.getGlobalBestTourLength()) {
83 acom
.setGlobalBestTourLength(ant
.getTourLength());
84 acom
.setGlobalBestTourIteration(iteration
);
86 for (int i
= 0; i
< acom
.getNumOfCities() + 1; i
++) {
87 acom
.setGlobalBestTour(i
, ant
.getTour(i
));
90 acom
.addGlobalBestTour((Integer
)acom
.getGlobalBestTourLength(),
91 acom
.getGlobalBestTour());
93 this.notifyObservers();
98 /* since we are already at it.. run the garbage collector */
103 protected boolean stagnationBehaviour() {
104 for (Ant antOne : ants) {
105 for (Ant antTwo : ants) {
106 for (int i : antOne.getTour()) {
107 for (int j : antTwo.getTour()) {
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
>= acom
.getMaxNumOfTours()) {
125 //} else if (stagnationBehaviour()) {
133 public String
toString() {
134 StringBuilder result
= new StringBuilder();
136 result
.append("Shortest tour at iteration " +
137 acom
.getGlobalBestTourIteration() +
139 acom
.getGlobalBestTourLength() +
142 for (int t
: acom
.getGlobalBestTour())
143 result
.append(t
+ " -- ");
145 result
.delete(result
.lastIndexOf(" -- "), result
.length());
146 return result
.toString();
149 public int[] getGlobalBestTour() {
150 return ed
.getGlobalBestTour();
153 public int getGlobalBestTour(int index
) {
154 return ed
.getGlobalBestTour(index
);
157 public void setGlobalBestTour(int[] GlobalBestTour
) {
158 ed
.setGlobalBestTour(GlobalBestTour
);
161 public void setGlobalBestTour(int index
, int GlobalBestTour
) {
162 ed
.setGlobalBestTour(index
, GlobalBestTour
);
165 public int getGlobalBestTourLength() {
166 return ed
.getGlobalBestTourLength();
169 public void setGlobalBestTourLength(int GlobalBestTourLength
) {
170 ed
.setGlobalBestTourLength(GlobalBestTourLength
);
173 public int getGlobalBestTourIteration() {
174 return ed
.getGlobalBestTourIteration();
177 public void setGlobalBestTourIteration(int GlobalBestTourIteration
) {
178 ed
.setGlobalBestTourIteration(GlobalBestTourIteration
);
181 public SortedMap
<Integer
, int[]> getGlobalBestTourMap() {
182 return ed
.getGlobalBestTourMap();
185 public void addGlobalBestTour(Integer TourLength
, int[] Tour
) {
186 ed
.addGlobalBestTour(TourLength
, Tour
);