revert to calling static methods again
[aco.git] / aco / environment / Environment.java
blob0e0de32c798e3efd228e3abd1d5116970ef74a04
1 package aco.environment;
3 import java.lang.Runtime;
4 import java.util.SortedMap;
5 import java.util.Observable;
7 import aco.ant.*;
8 import aco.misc.*;
9 import aco.mediator.*;
10 import aco.environment.data.*;
12 public class Environment
13 extends Observable {
15 protected Ant[] ants;
16 protected ACOMediator acom;
17 protected EnvironmentData ed;
19 public Environment(ACOMediator acom) {
20 this.acom = 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 */
33 System.gc();
36 /* d'tor */
37 protected void finalize() {
38 deleteObservers();
41 public void run() {
42 int i = 0;
43 while (! terminateCondition(i++)) {
44 constructSolutions();
45 updateStatistics(i);
46 acom.pheromoneUpdate(ants);
47 /* localSearch();
48 * updatePheromoneTrails(); */
52 protected void constructSolutions() {
54 /* clear ants memory and ..
55 * assign an initial random city to ant */
56 /* TODO: threading */
57 for (Ant ant : ants) {
58 ant.reset();
59 acom.pickInitialRandomCity(ant);
62 /* let each ant construct a complete tour
63 * start a 1 since every ant already has an initial city */
64 /* TODO: threading */
65 for (int step = 1; step < acom.getNumOfCities(); step++) {
66 for (Ant ant : ants)
67 acom.neighbourListASDecisionRule(ant, step);
70 /* compute tour length of each ant */
71 /* TODO: threading */
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());
92 this.setChanged();
93 this.notifyObservers();
98 /* since we are already at it.. run the garbage collector */
99 System.gc();
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()) {
108 if (i != j)
109 return false;
114 return true;
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 >= acom.getMaxNumOfTours()) {
124 return true;
125 //} else if (stagnationBehaviour()) {
126 //return true;
127 } else {
128 return false;
132 @Override
133 public String toString() {
134 StringBuilder result = new StringBuilder();
136 result.append("Shortest tour at iteration " +
137 acom.getGlobalBestTourIteration() +
138 " with length " +
139 acom.getGlobalBestTourLength() +
140 "\n");
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);