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 abstract class Environment
17 protected ACOMediator acom
;
18 protected EnvironmentData ed
;
20 public Environment(ACOMediator acom
) {
23 this.ants
= new Ant
[acom
.getNumOfAnts()];
25 for (int i
= 0; i
< acom
.getNumOfAnts(); i
++)
26 ants
[i
] = new Ant(acom
);
28 this.ed
= new EnvironmentData(acom
);
30 /* add a shutdown hook to print out the last best solution */
31 Runtime
.getRuntime().addShutdownHook(new EnvironmentShutdownHook(this));
33 /* run the garbage collector after all the initialization is done */
38 protected void finalize() {
42 protected abstract void constructSolutions();
44 protected abstract boolean terminateCondition(int iteration
);
48 while ( !terminateCondition(i
++) &&
49 !interruptCondition() ) {
52 acom
.pheromoneUpdate(ants
);
56 protected boolean interruptCondition() {
57 return Thread
.interrupted();
60 protected void updateStatistics(int iteration
) {
61 for (Ant ant
: ants
) {
63 if (ant
.getTourLength() < acom
.getGlobalBestTourLength()) {
64 acom
.setGlobalBestTourLength(ant
.getTourLength());
65 acom
.setGlobalBestTourIteration(iteration
);
67 for (int i
= 0; i
< acom
.getNumOfCities() + 1; i
++) {
68 acom
.setGlobalBestTour(i
, ant
.getTour(i
));
71 acom
.addGlobalBestTour((Integer
)acom
.getGlobalBestTourLength(),
72 acom
.getGlobalBestTour());
75 this.notifyObservers();
77 /* since we are already at it.. run the garbage collector */
84 public int[] getGlobalBestTour() {
85 return ed
.getGlobalBestTour();
88 public int getGlobalBestTour(int index
) {
89 return ed
.getGlobalBestTour(index
);
92 public void setGlobalBestTour(int[] GlobalBestTour
) {
93 ed
.setGlobalBestTour(GlobalBestTour
);
96 public void setGlobalBestTour(int index
, int GlobalBestTour
) {
97 ed
.setGlobalBestTour(index
, GlobalBestTour
);
100 public int getGlobalBestTourLength() {
101 return ed
.getGlobalBestTourLength();
104 public void setGlobalBestTourLength(int GlobalBestTourLength
) {
105 ed
.setGlobalBestTourLength(GlobalBestTourLength
);
108 public int getGlobalBestTourIteration() {
109 return ed
.getGlobalBestTourIteration();
112 public void setGlobalBestTourIteration(int GlobalBestTourIteration
) {
113 ed
.setGlobalBestTourIteration(GlobalBestTourIteration
);
116 public SortedMap
<Integer
, int[]> getGlobalBestTourMap() {
117 return ed
.getGlobalBestTourMap();
120 public void addGlobalBestTour(Integer TourLength
, int[] Tour
) {
121 ed
.addGlobalBestTour(TourLength
, Tour
);
125 public String
toString() {
126 StringBuilder result
= new StringBuilder();
128 result
.append("Shortest tour at iteration " +
129 acom
.getGlobalBestTourIteration() +
131 acom
.getGlobalBestTourLength() +
134 for (int t
: acom
.getGlobalBestTour())
135 result
.append(t
+ " -- ");
137 result
.delete(result
.lastIndexOf(" -- "), result
.length());
140 for (int t
: acom
.getGlobalBestTour()) {
142 /* necessary for loop so that we don't check the last city
143 * which is equal to the first one */
144 for (int c
= 0; c
< acom
.getNumOfCities(); c
++)
145 if (acom
.getGlobalBestTour(c
) == t
&& i
++ > 0)
146 result
.append("\n" + "crossed " + c
+ " more than once!");
149 return result
.toString();