Beispiel #1
0
        public bool Step()
        {
            if (Queue.Count == 0)
            {
                return(true);
            }

            // The path being evaluated this loop.
            Path pathUnderEval = Queue.Dequeue();

            // TODO If the node that this path leads to has already been processed, it already has the best possible
            // path stored. Therefore the algoritm can skip processing this node.
            TNodeId pathNodeId = pathUnderEval.LastOfRoute;

            if (BestPaths.ContainsKey(pathNodeId) &&
                pathUnderEval.CompareTo(BestPaths[pathNodeId]) > 0)
            {
                return(false);
            }

            // Retrieve the node from the Graph instance.
            Graph <TNodeId> .Node node = Graph.Nodes[pathNodeId];
            foreach (Graph <TNodeId> .Edge edge in node.Edges)
            {
                // Calculate the length to reach the destination of this edge.
                double edgeLength = pathUnderEval.Length + edge.Cost;

                // If the destination of this edge is known, and the stored length is less (better) than the
                // length of the edge being evaluated now, this edge can be skipped. If it is not known, or this
                // path is better, the algorithm continues.
                if (BestPaths.ContainsKey(edge.Destination) &&
                    BestPaths[edge.Destination].Length < edgeLength)
                {
                    continue;
                }

                // Copy the route of the path and add the destination of the edge to it..
                var edgeRoute = new List <TNodeId>(pathUnderEval.Route);
                edgeRoute.Add(edge.Destination);

                // Initialize the better path, and store and queue it.
                var betterPath = new Path(edgeRoute, edgeLength);
                BestPaths[edge.Destination] = betterPath;
                Queue.Insert(betterPath);
            }

            Console.WriteLine($"{Graph}");
            Console.WriteLine($"{string.Join(Environment.NewLine, BestPaths)}");

            return(false);
        }
        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();
            }
        }