示例#1
0
        private static void PathPlanning(string fileName)
        {
            INamedDirectedGraph <ICoordinateNode, IWeightedNamedDirectedEdge <ICoordinateNode> > graph;

            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                graph = GraphFactory.NamedDirectedFromInput(fs);
            }

            Console.WriteLine("Map loaded, it contains " + graph.NodeCount + " nodes and " + graph.EdgeCount + " edges");
            var heuristic  = new StraightLineHeuristic();
            var pathFinder = new AStarPathFinder <ICoordinateNode, IWeightedNamedDirectedEdge <ICoordinateNode> > (heuristic);

            Console.WriteLine("Type exit to quit the program.");
            Console.WriteLine();

            while (true)
            {
                ICollection <ICoordinateNode> startNodes = null;

                string read;
                while (startNodes == null || startNodes.Count < 1)
                {
                    Console.Write("Enter street names for starting point: ");
                    read = Console.ReadLine();
                    if (string.IsNullOrEmpty(read))
                    {
                        continue;
                    }
                    if (read == "quit")
                    {
                        ExitWithMsg(null);
                    }

                    // SktPedersStraede Larsbjoernsstraede
                    var parts = read.Split(new char[] { ' ' }, 2);
                    if (parts.Any(part => graph.GetEdgesByName(part).Count < 1))
                    {
                        Console.WriteLine("Invalid street name supplied");
                        continue;
                    }
                    startNodes = graph.GetNodesByEdgeNames(parts);
                }

                ICollection <ICoordinateNode> endNodes = null;
                while (endNodes == null || endNodes.Count < 1)
                {
                    Console.Write("Enter street names for ending point: ");
                    read = Console.ReadLine();
                    if (string.IsNullOrEmpty(read))
                    {
                        continue;
                    }
                    if (read == "quit")
                    {
                        ExitWithMsg(null);
                    }

                    // Studiestraede Larsbjoernsstraede
                    var parts = read.Split(new char[] { ' ' }, 2);
                    if (parts.Any(part => graph.GetEdgesByName(part).Count < 1))
                    {
                        Console.WriteLine("Invalid street name supplied");
                        continue;
                    }
                    endNodes = graph.GetNodesByEdgeNames(parts);
                }

                var path = pathFinder.ShortestPath(graph, startNodes.First(), endNodes.First());
                if (path.Count == 1)
                {
                    Console.WriteLine("Path found: Just stand still, you're already there");
                }
                else if (path.Count > 1)
                {
                    var prev     = path [0];
                    var prevName = string.Empty;

                    Console.WriteLine("Path found:");
                    for (var i = 1; i < path.Count; i++)
                    {
                        var edge = graph.GetEdge(prev, path [i]);
                        if (edge.Name != prevName)
                        {
                            Console.Write(edge.Name);
                            if (i + 1 < path.Count)
                            {
                                Console.Write(" -> ");
                            }
                        }
                        prev     = path [i];
                        prevName = edge.Name;
                    }
                }
                else
                {
                    Console.WriteLine("No path found");
                }
                Console.WriteLine();
            }
        }