Пример #1
0
        public void AntColonyPathAntTest(Int32 numberOfAnts, Int32 numberOfIterations, decimal initialPheremone, decimal evaporationRate,
                                         IPathPath path,
                                         ICollection <IStep> steps,
                                         ICollection <IPathPath> expectedResult)
        {
            #region Set up test data
            IAntColonyContext context = new AntColonyContext();
            context.Path  = path;
            context.Steps = steps;
            #endregion

            #region Run Test
            Colony <PathAnt, IEdge> colony = new Colony <PathAnt, IEdge>(numberOfAnts, numberOfIterations, initialPheremone, evaporationRate, InitialisePathPheremones);
            colony.InitialiseAnts(context);
            colony.InitialisePheremones();
            colony.RunAnts();
            #endregion

            #region Verify Results
            Assert.IsNotNull(context.BestPaths);
            context.BestPaths.ForEach(p => Console.WriteLine(string.Format("Best Path: [{0}] {1},{2}", (p as IPathPath).Score, (p as IPathPath).Edges.First().StartStep.StepName, string.Join(",", (p as IPathPath).Edges.Select(e => e.EndStep.StepName)))));
            Assert.AreEqual(expectedResult.Count, context.BestPaths.Count);
            CollectionAssert.AreEquivalent(expectedResult, context.BestPaths);
            #endregion
        }
Пример #2
0
        /// <summary>
        /// Leave pheremones on the edges traversed along the considered best path.
        /// </summary>
        /// <param name="PheremoneMap">The map of pheremone levels for all edges</param>
        /// <param name="path">The path travelled by the ant</param>
        private void DepositPheremones(IDictionary <TPheremoneKey, Pheremone> PheremoneMap, IPathPath path)
        {
            decimal pathScore = path.Score;
            decimal delta     = (1.0m / pathScore);

            foreach (TPheremoneKey steps in path.Edges)
            {
                //if (!PheremoneMap.ContainsKey(steps))
                //    PheremoneMap[steps] = new Pheremone(InitialPheremoneLevel);
                PheremoneMap[steps].PheremoneLevel += delta;
            }
        }