예제 #1
0
 public PathController(IPyramideController pyramid)
 {
     _pyramid   = pyramid;
     _startNode = _pyramid.Nodes[0];
     _path      = new NodePath();
     CheckValidPath();
 }
예제 #2
0
        private static INodePath GetBigestValuePath(INode node, INodePath path)
        {
            path.AddNode(node);

            if (node.AvailablePath.Count == 0)
            {
                return(path);
            }

            var pathsums = (from n in node.AvailablePath let pathSum = new NodePath() select GetBigestValuePath(n, pathSum)).ToList();

            var maxValuePath = pathsums.OrderByDescending(t => t.Sum).FirstOrDefault();

            if (maxValuePath != null)
            {
                path.AddNodeRange(maxValuePath.PathNodes);
            }
            return(path);
        }
예제 #3
0
        public void AntColonyNodeAntTest(Int32 numberOfAnts, Int32 numberOfIterations, decimal initialPheremone, decimal evaporationRate,
                                         INodePath path,
                                         ICollection <INodePath> expectedResult)
        {
            #region Set up test data
            Matrix <IStep, IStep, decimal> distances = new Matrix <IStep, IStep, decimal>();
            distances.Add(AdelaideNode, BrisbaneNode, 2280);
            distances.Add(AdelaideNode, CanberraNode, 1170);
            distances.Add(AdelaideNode, DarwinNode, 3040);
            distances.Add(AdelaideNode, MelbourneNode, 750);
            distances.Add(AdelaideNode, PerthNode, 1850);

            distances.Add(BrisbaneNode, SydneyNode, 970);
            distances.Add(BrisbaneNode, DarwinNode, 3440);

            distances.Add(CanberraNode, SydneyNode, 290);
            distances.Add(CanberraNode, MelbourneNode, 650);

            distances.Add(SydneyNode, MelbourneNode, 880);

            IAntColonyContext context = new AntColonyContext();
            context.Path  = path;
            context.Steps = distances.HorizontalKeys().Union(distances.VerticalKeys()).Distinct().ToList();
            #endregion

            #region Run Test
            Colony <NodeAnt, IStep> colony = new Colony <NodeAnt, IStep>(numberOfAnts, numberOfIterations, initialPheremone, evaporationRate, InitialiseNodePheremones);
            colony.InitialiseAnts(context);
            colony.RunAnts();
            #endregion

            #region Verify Results
            Assert.IsNotNull(context.BestPaths);
            context.BestPaths.ForEach(p => Console.WriteLine(string.Format("Best Path: [{0}] {1}", (p as INodePath).Score, string.Join(",", (p as INodePath).Steps.Select(e => (e as IStep).StepName)))));
            Assert.AreEqual(expectedResult.Count, context.BestPaths.Count);
            CollectionAssert.AreEquivalent(expectedResult, context.BestPaths);
            #endregion
        }
예제 #4
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, INodePath path)
        {
            decimal pathScore = path.Score == 0 ? 1 : path.Score;
            decimal delta     = (1.0m / pathScore);

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