new constructor: instanciate an object only with strategies
[aco.git] / Environment.java
blobfa2c301bec42a407c8bc0ad42056fde5caabcbf1
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.getNumOfCities()];
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 /* TODO: threading */
78 for (Ant ant : ants) {
79 ant.clearData();
80 ant.pickInitialRandomCity();
83 /* let each ant construct a complete tour
84 * start a 1 since every ant already has an initial city */
85 /* TODO: threading */
86 for (int step = 1; step < graph.getNumOfCities(); step++) {
87 for (Ant ant : ants)
88 ant.neighbourListASDecisionRule(step);
91 /* compute tour length of each ant */
92 /* TODO: threading */
93 for (Ant ant : ants) {
94 ant.computeTourLength();
100 protected void updateStatistics(int iteration) {
101 for (Ant ant : ants) {
103 if (ant.getTourLength() < getMinTourLength()) {
104 setMinTourLength(ant.getTourLength());
105 setMinTourIteration(iteration);
107 for (int i = 0; i < graph.NumOfCities; i++) {
108 setMinTour(i, ant.getTour(i));
111 if (graph.hasCoordinates()) {
112 this.setChanged();
113 this.notifyObservers(this);
118 /* since we are already at it.. run the garbage collector */
119 System.gc();
122 protected boolean stagnationBehaviour() {
123 for (Ant antOne : ants) {
124 for (Ant antTwo : ants) {
125 for (int i : antOne.getTour()) {
126 for (int j : antTwo.getTour()) {
127 if (i != j)
128 return false;
133 return true;
136 protected boolean terminateCondition(int iteration) {
137 /* 1) solution within predefined distance
138 * 2) max number of tour constructions or algorithm iterations
139 * 3) max cpu time
140 * 4) algorithm stagnation behaviour */
141 if (iteration >= MaxNumOfTours) {
142 return true;
143 //} else if (stagnationBehaviour()) {
144 //return true;
145 } else {
146 return false;
150 @Override
151 public String toString() {
152 StringBuilder result = new StringBuilder();
153 result.append("Shortest tour at iteration " +
154 MinTourIteration +
155 " with length " +
156 MinTourLength +
157 "\n");
159 for (int t : MinTour) {
160 if (graph.getIds(t) != null)
161 result.append(graph.ids[t] + "\t");
162 else
163 result.append(t + "\t");
165 if (graph.getIds(0) != null)
166 result.append(graph.ids[MinTour[0]]);
167 else
168 result.append(MinTour[0]);
169 return result.toString();
172 public int[] getMinTour() {
173 return this.MinTour;
176 public int getMinTour(int index) {
177 return this.MinTour[index];
180 public void setMinTour(int[] MinTour) {
181 this.MinTour = MinTour;
184 public void setMinTour(int index, int MinTour) {
185 this.MinTour[index] = MinTour;
188 public int getMinTourLength() {
189 return this.MinTourLength;
192 public void setMinTourLength(int MinTourLength) {
193 this.MinTourLength = MinTourLength;
196 public int getMinTourIteration() {
197 return this.MinTourIteration;
200 public void setMinTourIteration(int MinTourIteration) {
201 this.MinTourIteration = MinTourIteration;
204 public Graph getGraph() {
205 return this.graph;
208 public void setGraph(Graph graph) {
209 this.graph = graph;