private void DoMainProcessingLoop() { int startTime = GetTime(); int iterations = 0; int lastSynchronisation = GetTime(); C.log.Info("Entering UpdateAnts - UpdatePheromones loop"); while (GetTime() - startTime < maxTime) { List <WorkerStatus> statuses = GetStatusesWaitingForProcessing(); if (statuses.Count >= 0) { ProcessWorkerStatuses(statuses); } Thread.Sleep(1000); iterations += 1; } C.log.Info("Ending..."); CommunicatorWithChildren.Stop(); KillWorkers(); C.log.Info("Best path:"); if (bestTrail != null) { ACOExample.Display(bestTrail); } C.log.Info("Best path length: " + bestLength); C.log.Info("Stopping. Waiting for stdin..."); Console.ReadKey(); }
private void ProcessReceivedPheromones(List <WorkerStatus> statuses) { IList <double[][]> pheromones = statuses.Select((status) => status.pheromones).ToList(); var sumPheromones = ACOExample.InitPheromones(pheromones[0].Length, 0.0); for (int k = 0; k < pheromones.Count; k++) { for (int i = 0; i <= pheromones[k].Length - 1; i++) { for (int j = i + 1; j <= pheromones[k][i].Length - 1; j++) { sumPheromones[i][j] += pheromones[k][i][j]; if (sumPheromones[i][j] < 0.0001) { sumPheromones[i][j] = 0.0001; } else if (sumPheromones[i][j] > 100000.0) { sumPheromones[i][j] = 100000.0; } } } } NotifyWorkersAboutNewPheromones(sumPheromones); }
private void UpdatePheromenes() { for (int i = 0; i <= pheromones.Length - 1; i++) { for (int j = i + 1; j <= pheromones[i].Length - 1; j++) { for (int k = 0; k <= ants.Length - 1; k++) { // length of ant k trail double decrease = (1.0 - ACOExample.rho) * pheromones[i][j]; double increase = 0.0; if (ACOExample.EdgeInTrail(i, j, ants[k]) == true) { double length = ACOExample.Length(ants[k], dists); increase = (ACOExample.Q / length); } pheromones[i][j] = decrease + increase; if (pheromones[i][j] < 0.0001) { pheromones[i][j] = 0.0001; } else if (pheromones[i][j] > 100000.0) { pheromones[i][j] = 100000.0; } pheromones[j][i] = pheromones[i][j]; } } } }
private void UpdateAnts() { for (int k = 0; k < ants.Length; k++) { int start = random.Next(0, numCities); int[] newTrail = ACOExample.BuildTrail(k, start, pheromones, dists, random); ants[k] = newTrail; } }
private double Length(int[] path) { return(ACOExample.Length(path, dists)); }