public virtual object Clone()
        {
            AntColonyContext result = new AntColonyContext();

            CopyInto(result);
            return(result);
        }
Esempio n. 2
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
        }
        public void Validate(object contextObject)
        {
            AntColonyContext context = contextObject as AntColonyContext;

            if (Edges.Count != (context.Steps.Count - 1))
            {
                InvalidPath = true;
            }
        }
Esempio n. 4
0
        private void InitialisePathPheremones(IAntColonyContext context, IDictionary <IEdge, Pheremone> map, decimal initialPheremoneLevel)
        {
            AntColonyContext c = context as AntColonyContext;

            foreach (IStep step in c.Steps)
            {
                //if (!map.ContainsKey(step))
                //    map[step] = new Pheremone(initialPheremoneLevel);
            }
        }
Esempio n. 5
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
        }
        public virtual void CopyInto <T>(T parentObject)
            where T : class
        {
            AntColonyContext context = parentObject as AntColonyContext;

            if (context == null)
            {
                throw new InvalidCastException(string.Format("Cannot CopyInto type of [{0}]", parentObject.GetType().FullName));
            }

            if (Path != null)
            {
                context.Path = Path.Clone() as IPath;
            }
            if (Steps != null)
            {
                context.Steps = Steps.CloneDeepCollection();
            }
            if (BestPaths != null)
            {
                context.BestPaths = BestPaths.CloneDeepCollection();
            }
        }