replace the observer pattern with a simple update function
[aco.git] / protoas / Pheromone.java
blobeeecc29f164aa5085d1a207f114eb373fe2a43fd
1 package protoas;
3 class Pheromone {
5 protected Graph graph;
6 protected double[][] Pheromone;
8 Pheromone(Graph graph) {
9 this.graph = graph;
10 this.Pheromone = new double[graph.getNumOfCities()][graph.getNumOfCities()];
12 double tauzero =
13 (double)(graph.getNumOfCities()) /
14 graph.nearestNeighbourHeuristicRandomStart();
16 for (int i = 0; i < graph.getNumOfCities(); i++) {
17 for (int j = 0; j < graph.getNumOfCities(); j++) {
18 setPheromone(i,j, tauzero);
24 public void pheromoneUpdate(Ant[] ants) {
25 evaporate();
27 for (Ant ant : ants) {
28 depositPheromone(ant);
31 graph.computeChoiceInformation();
34 public void evaporate() {
35 for (int i = 1; i < graph.getNumOfCities(); i++) {
36 for (int j = 1; j < graph.getNumOfCities(); j++) {
37 Pheromone[i][j] = (1.0 - graph.getROH()) * Pheromone[i][j];
38 Pheromone[j][i] = Pheromone[i][j];
43 public void depositPheromone(Ant ant) {
44 double dt = 1.0/ant.getTourLength();
45 int j = 0;
46 int l = 0;
47 for (int i = 0; i < graph.getNumOfCities() - 1; i++) {
48 j = ant.getTour(i);
49 l = ant.getTour(i+1);
50 Pheromone[j][l] = Pheromone[j][l] + dt;
51 Pheromone[l][j] = Pheromone[j][l];
55 public double[][] getPheromone() {
56 return this.Pheromone;
59 public double getPheromone(int x, int y) {
60 return this.Pheromone[x][y];
63 public void setPheromone(double[][] Pheromone) {
64 this.Pheromone = Pheromone;
67 public void setPheromone(int x, int y, double Pheromone) {
68 this.Pheromone[x][y] = Pheromone;