fix a really nasty bug: We need the actual city the ant is on not just the step count
[aco.git] / Environment.java
blob7510b1d9a9dca639481a03a61673834e50a5bf04
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;
13 protected Ant[] ants;
14 protected Graph graph;
16 protected AntView av;
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];
36 this.graph = graph;
38 for (int i = 0; i < NumOfAnts; i++)
39 ants[i] = new Ant(graph);
41 if (graph.hasCoordinates()) {
42 if (GuiSize == 0)
43 av = new AntView(this);
44 else
45 av = new AntView(GuiSize, this);
46 this.addObserver(av);
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 */
54 System.gc();
57 /* d'tor */
58 protected void finalize() {
59 deleteObservers();
62 public void run() {
63 int i = 0;
64 while (! terminateCondition(i++)) {
65 constructSolutions();
66 graph.pheromoneUpdate(ants);
67 updateStatistics(i);
68 /* localSearch();
69 * updatePheromoneTrails(); */
73 protected void constructSolutions() {
75 /* clear ants memory and ..
76 * assign an initial random city to ant */
77 for (Ant ant : ants) {
78 ant.clearData();
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++) {
85 for (Ant ant : ants)
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()) {
108 this.setChanged();
109 this.notifyObservers(this);
114 /* since we are already at it.. run the garbage collector */
115 System.gc();
118 protected Boolean terminateCondition(int iteration) {
119 /* 1) solution within predefined distance
120 * 2) max number of tour constructions or algorithm iterations
121 * 3) max cpu time
122 * 4) algorithm stagnation behaviour */
123 if (iteration >= MaxNumOfTours) {
124 return true;
125 } else {
126 return false;
130 @Override
131 public String toString() {
132 StringBuilder result = new StringBuilder();
133 result.append("Shortest tour at iteration " +
134 MinTourIteration +
135 " with length " +
136 MinTourLength +
137 "\n");
139 for (int t : MinTour) {
140 if (graph.getIds(t) != null)
141 result.append(graph.ids[t] + "\t");
142 else
143 result.append(t + "\t");
145 if (graph.getIds(0) != null)
146 result.append(graph.ids[MinTour[0]]);
147 else
148 result.append(MinTour[0]);
149 return result.toString();
152 public int[] getMinTour() {
153 return this.MinTour;
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() {
185 return this.graph;
188 public void setGraph(Graph graph) {
189 this.graph = graph;