// -------------------------------------------------------------------------------------------- private static void UpdatePheromones(double[][] pheromones, int[][] ants, int[][] dists, object threadLock) { lock (threadLock) { 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 - rho) * pheromones[i][j]; double increase = 0.0; if (EdgeInTrail(i, j, ants[k]) == true) { double length = ACOExample.Length(ants[k], dists); increase = (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]; } } } } }
static void Main(string[] args) { int scenario = 0; switch (scenario) { case 0: WorkQueueTest.Run(); break; case 1: ACOExample.Run(); break; case 2: ACO.ACOWithShappExample.Run(args); break; case 3: ProgramForProtoTesting.MainMethod(args); break; case 4: Program main = new Program(); main.Execute(args); break; } }
public static void Run() { IList <int[][]> ants = new SynchronizedCollection <int[][]>(); IList <double[][]> pheromones = new SynchronizedCollection <double[][]>(); IList <object> locks = new SynchronizedCollection <object>(); IList <int> iterations = new SynchronizedCollection <int>(); try { C.log.Info("Begin Ant Colony Optimization demo"); C.log.Info("Number cities in problem = " + numCities); C.log.Info("Number ants = " + numAnts); C.log.Info("Maximum time = " + maxTime); C.log.Info("Alpha (pheromone influence) = " + alpha); C.log.Info("Beta (local node influence) = " + beta); C.log.Info("Rho (pheromone evaporation coefficient) = " + rho.ToString("F2")); C.log.Info("Q (pheromone deposit factor) = " + Q.ToString("F2")); C.log.Info("Initialing dummy graph distances"); int[][] dists = MakeGraphDistances(numCities); C.log.Info("Initialing ants to random trails"); for (int i = 0; i < numThreads; ++i) { ants.Add(InitAnts(numAnts, numCities)); locks.Add(new object()); iterations.Add(0); } // initialize ants to random trails ShowAnts(ants, dists); int[] bestTrail = ACOExample.BestTrail(ants, dists, locks); // determine the best initial trail double bestLength = Length(bestTrail, dists); // the length of the best trail Console.Write("Best initial trail length: " + bestLength.ToString("F1") + ""); //Display(bestTrail); C.log.Info("Initializing pheromones on trails"); for (int i = 0; i < numThreads; ++i) { pheromones.Add(InitPheromones(numCities)); } List <Thread> threads = new List <Thread>(); for (int i = 0; i < numThreads; ++i) { threads.Add(new Thread(new ParameterizedThreadStart((threadIdxParam) => { int threadIdx = (int)threadIdxParam; C.log.Info("Sarting as " + threadIdx); while (true) { UpdateAnts(ants[threadIdx], pheromones[threadIdx], dists, locks[threadIdx], randoms[threadIdx]); UpdatePheromones(pheromones[threadIdx], ants[threadIdx], dists, locks[threadIdx]); lock (locks[threadIdx]) { iterations[threadIdx]++; } } }))); } for (int i = 0; i < numThreads; ++i) { threads[i].Start(i); } int startTime = GetTime(); int iteration = 0; int lastSynchronisation = GetTime(); C.log.Info("Entering UpdateAnts - UpdatePheromones loop"); while (GetTime() - startTime < maxTime) { //for (int i = 0; i < numThreads; ++i) // UpdateAnts(ants[i], pheromones[i], dists, locks[i]); //for (int i = 0; i < numThreads; ++i) // UpdatePheromones(pheromones[i], ants[i], dists, locks[i]); if (GetTime() - lastSynchronisation >= synchronisationPeriod) { SynchronizePheromones(pheromones, locks); } if (GetTime() - bestLengthCheckPeriod >= 1) { int[] currBestTrail = ACOExample.BestTrail(ants, dists, locks); double currBestLength = Length(currBestTrail, dists); C.log.Info("length " + currBestLength.ToString("F1")); int sumOfIterations = 0; for (int i = 0; i < numThreads; ++i) { lock (locks[i]) { sumOfIterations += iterations[i]; } } C.log.Info("iterations " + sumOfIterations); if (currBestLength < bestLength) { bestLength = currBestLength; bestTrail = currBestTrail; C.log.Info("New best length of " + bestLength.ToString("F1") + " found at time " + iteration); } } Thread.Sleep(500); iteration += 1; } C.log.Info("Time complete"); C.log.Info("Best trail found:"); Display(bestTrail); C.log.Info("Length of best trail found: " + bestLength.ToString("F1")); C.log.Info("End Ant Colony Optimization demo"); Console.Read(); } catch (Exception ex) { C.log.Info(ex.Message); } }