fix a really nasty bug: We need the actual city the ant is on not just the step count
[aco.git] / Pheromone.java
blob9354349b3f19f2876443e99ff93fca029751765a
1 class Pheromone {
3 protected Graph graph;
4 protected double[][] Pheromone;
6 Pheromone(Graph graph) {
7 this.graph = graph;
8 this.Pheromone = new double[graph.getNumOfCities()][graph.getNumOfCities()];
10 for (int i = 0; i < graph.getNumOfCities(); i++) {
11 for (int j = 0; j < graph.getNumOfCities(); j++) {
12 setPheromone(i,j, 0.1);
18 public void pheromoneUpdate(Ant[] ants) {
19 evaporate();
21 for (Ant ant : ants) {
22 depositPheromone(ant);
25 graph.computeChoiceInformation();
28 public void evaporate() {
29 for (int i = 1; i < graph.getNumOfCities(); i++) {
30 for (int j = 1; j < graph.getNumOfCities(); j++) {
31 Pheromone[i][j] = (1.0 - graph.getROH()) * Pheromone[i][j];
32 Pheromone[j][i] = Pheromone[i][j];
37 public void depositPheromone(Ant ant) {
38 double dt = 1.0/ant.getTourLength();
39 for (int i = 0; i < graph.getNumOfCities() - 1; i++) {
40 int j = ant.getTour(i);
41 int l = ant.getTour(i+1);
42 Pheromone[j][l] = Pheromone[j][l] + dt;
43 Pheromone[l][j] = Pheromone[j][l];
47 public double[][] getPheromone() {
48 return this.Pheromone;
51 public double getPheromone(int x, int y) {
52 return this.Pheromone[x][y];
55 public void setPheromone(double[][] Pheromone) {
56 this.Pheromone = Pheromone;
59 public void setPheromone(int x, int y, double Pheromone) {
60 this.Pheromone[x][y] = Pheromone;