private double[] MoveProbs(int cityX, bool[] visited, Pheromone pheromone, int[][] dists) { // for ant, located at nodeX, with visited[], return the prob of moving to each city int numCities = pheromone.Size; double[] taueta = new double[numCities]; // inclues cityX and visited cities double sum = 0.0; // sum of all tauetas // i is the adjacent city for (int i = 0; i <= taueta.Length - 1; i++) { if (i == cityX) { taueta[i] = 0.0; // prob of moving to self is 0 } else if (visited[i]) { taueta[i] = 0.0; // prob of moving to a visited city is 0 } else { taueta[i] = Math.Pow(pheromone.Get(cityX, i), alpha) * Math.Pow((1.0 / Distance(cityX, i, dists)), beta); // could be huge when pheromone[][] is big if (taueta[i] < 0.0001) { taueta[i] = 0.0001; } else if (taueta[i] > (double.MaxValue / (numCities * 100))) { taueta[i] = double.MaxValue / (numCities * 100); } } sum += taueta[i]; } double[] probs = new double[numCities]; for (int i = 0; i <= probs.Length - 1; i++) { probs[i] = taueta[i] / sum; // big trouble if sum = 0.0 } return(probs); }
private void UpdatePheromones(Pheromone pheromone, Ant[] ants, int[][] dists) { for (int i = 0; i <= pheromone.Size - 1; i++) { for (int j = i + 1; j <= pheromone.Size - 1; j++) { for (int k = 0; k <= ants.Length - 1; k++) { double length = Length(ants[k].Trail, dists); // length of ant k trail double decrease = (1.0 - rho) * pheromone.Get(i, j); double increase = 0.0; if (EdgeInTrail(i, j, ants[k].Trail)) { increase = (Q / length); } pheromone.Set(i, j, decrease + increase); } } } }