run the gc only after a new global best solution has been found
[aco.git] / src / aco / environment / Environment.java
blob430e7b21481c8d879d2baad7786b78b40eb17d0f
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 abstract class Environment
13 extends Observable
14 implements Runnable {
16 protected Ant[] ants;
17 protected ACOMediator acom;
18 protected EnvironmentData ed;
20 public Environment(ACOMediator acom) {
21 this.acom = 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 */
34 System.gc();
37 /* d'tor */
38 protected void finalize() {
39 deleteObservers();
42 protected abstract void constructSolutions();
44 protected abstract boolean terminateCondition(int iteration);
46 public void run() {
47 int i = 0;
48 while ( !terminateCondition(i++) &&
49 !interruptCondition() ) {
50 constructSolutions();
51 updateStatistics(i);
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());
74 this.setChanged();
75 this.notifyObservers();
77 /* since we are already at it.. run the garbage collector */
78 System.gc();
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);
124 @Override
125 public String toString() {
126 StringBuilder result = new StringBuilder();
128 result.append("Shortest tour at iteration " +
129 acom.getGlobalBestTourIteration() +
130 " with length " +
131 acom.getGlobalBestTourLength() +
132 "\n");
134 for (int t : acom.getGlobalBestTour())
135 result.append(t + " -- ");
137 result.delete(result.lastIndexOf(" -- "), result.length());
139 int i = 0;
140 for (int t : acom.getGlobalBestTour()) {
141 i = 0;
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();