Пример #1
0
        private void BuildGraph(StarSystem start, StarSystem goal, double maxJump)
        {
            logger.Trace("Building graph for route from '{0}' to '{1}'", start.Name, goal.Name);
                        
            List<StarSystem> systems = GetLocalSystems(start, goal).ToList();
            logger.Trace("Building edges for route from '{0}' to '{1}'", start.Name, goal.Name);
            for (int i = 0; i < systems.Count; i++)
            {
                StarSystem a = systems[i];
                for (int j = 0; j < systems.Count; j++)
                {
                    StarSystem b = systems[j];
                    if (a.Equals(b))
                        continue;

                    if (a.DistanceFrom(b) <= maxJump)
                    {
                        var edge = new Edge<StarSystem>(a, b);
                        double edgeWeight = CalculateEdgeWeight(a, b);
                        
                        graph.AddVerticesAndEdge(edge);
                        weights.Add(edge, edgeWeight);
                    }
                }
            }
            logger.Trace("{2} edges added for route from '{0}' to '{1}'", start.Name, goal.Name, graph.Edges.Count());          
        }
Пример #2
0
        private IEnumerable<Edge<StarSystem>> FindPath(StarSystem start, StarSystem goal, double maxJump)
        {
            BuildGraph(start, goal, maxJump);

            // if we only have one edge, then that is the shortest path
            if (graph.Edges.Count() == 1)
                return graph.Edges;

            // setup the shortest path algorithm
            var edgeCost = AlgorithmExtensions.GetIndexer(weights);
            var tryGetPath = graph.ShortestPathsBellmanFord(edgeCost, start);
            //var tryGetPath = graph.ShortestPathsDijkstra(edgeCost, start);

            logger.Trace("Searching for route from '{0}' to '{1}'", start.Name, goal.Name);
            IEnumerable<Edge<StarSystem>> path;
            if (tryGetPath(goal, out path))
                return path;
            else
                throw new RouteNotFoundException(string.Format("Unable to find route from '{0}' to '{1}'", start.Name, goal.Name));
        }
Пример #3
0
 public Route(StarSystem start, StarSystem end)
 {
     this.start = start;
     this.end = end;
     hops = new List<Edge<StarSystem>>();
 }
Пример #4
0
 public Route(IEnumerable<Edge<StarSystem>> path)
 {
     hops = path.ToList();            
     start = hops[0].Source;
     end = hops[hops.Count - 1].Target;
 }
Пример #5
0
 public double DistanceFrom(StarSystem other)
 {
     return Location.DistanceFrom(other.Location);
 }
Пример #6
0
 /// <summary>
 /// Gets a list of StarSystems that surround the starting system.  The distance between the
 /// starting system and the ending system defines the radius of the search circle.
 /// </summary>
 /// <param name="start">The starting point</param>
 /// <param name="end">The goal system</param>
 /// <returns>A list of systems inside the radius of the search circle</returns>
 private IEnumerable<StarSystem> GetLocalSystems(StarSystem start, StarSystem end)
 {
     double radius = start.DistanceFrom(end);
     return provider.Systems.Where(s => s.DistanceFrom(start) <= radius);
 }
Пример #7
0
 private double CalculateEdgeWeight(StarSystem a, StarSystem b)
 {
     return a.DistanceFrom(b);
 }
Пример #8
0
 public Route Find(StarSystem start, StarSystem end, double maxJump)
 {
     IEnumerable<Edge<StarSystem>> path = FindPath(start, end, maxJump);
     return new Route(path);
 }
Пример #9
0
 public Route Find(StarSystem start, StarSystem end)
 {
     return Find(start, end, 15);
 }