Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/AStarAlgorithmV2.cs @ 8408

Last change on this file since 8408 was 8408, checked in by spimming, 12 years ago

#1894

  • restructured test program
  • new, faster version of AStar algorithm
  • moved method to obtain edge max speed to way
File size: 3.7 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using HeuristicLab.Problems.RoutePlanning.Graph;
5namespace HeuristicLab.Algorithms.GraphRouting {
6  public class AStarAlgorithmV2 : IRouter {
7    private Graph graph;
8
9    private Dictionary<long, float> distances;
10    private Dictionary<long, long> predecessors;
11    private List<long> closedList;
12    private HashSet<long> closedListLookup;
13    private SortedSet<NodeData> openList;
14    private Dictionary<long, NodeData> openListLookup;
15
16    public AStarAlgorithmV2(Graph graph) {
17      this.graph = graph;
18    }
19
20    #region IRouter Members
21
22    public long[] Calculate(long sourceNodeId, long targetNodeId) {
23      distances = new Dictionary<long, float>();
24      predecessors = new Dictionary<long, long>();
25      closedList = new List<long>();
26      closedListLookup = new HashSet<long>();
27      openList = new SortedSet<NodeData>();
28      openListLookup = new Dictionary<long, NodeData>();
29
30      long currentNode = sourceNodeId;
31
32      NodeData nd = new NodeData(currentNode, 0);
33      openList.Add(nd);
34      openListLookup.Add(currentNode, nd);
35      distances.Add(currentNode, 0);
36
37      while (openList.Count > 0) {
38        NodeData data = openList.Max;
39        openList.Remove(data);
40        openListLookup.Remove(data.Node);
41        currentNode = data.Node;
42        if (currentNode == targetNodeId) {
43          return ReconstructPath(currentNode).ToArray();
44        }
45
46        Dictionary<long, float> currentNeighbors = graph.GetNeighbors(currentNode);
47        foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
48          if (closedListLookup.Contains(neighbor.Key))
49            continue;
50
51          float costsFromStart = GetDistance(currentNode) + neighbor.Value;
52
53          if (openListLookup.ContainsKey(neighbor.Key) && costsFromStart > GetDistance(neighbor.Key))
54            continue;
55
56          distances[neighbor.Key] = costsFromStart;
57          predecessors[neighbor.Key] = currentNode;
58
59          float f = costsFromStart + graph.EstimatedCosts(neighbor.Key, targetNodeId);
60
61          if (openListLookup.ContainsKey(neighbor.Key)) {
62            openList.Remove(openListLookup[neighbor.Key]);
63            openListLookup.Remove(neighbor.Key);
64          }
65
66          NodeData newNode = new NodeData(neighbor.Key, f);
67          openList.Add(newNode);
68          openListLookup.Add(newNode.Node, newNode);
69        }
70        closedList.Add(currentNode);
71        closedListLookup.Add(currentNode);
72      }
73      throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
74    }
75
76    #endregion
77
78    private float GetDistance(long id) {
79      float d;
80      if (distances.TryGetValue(id, out d)) {
81        return d;
82      } else { return float.MaxValue; }
83    }
84
85    private List<long> ReconstructPath(long nodeId) {
86      List<long> route = new List<long>();
87      long current = nodeId;
88      long next;
89
90      if (!predecessors.TryGetValue(current, out next)) {
91        return null;
92      }
93      route.Add(current);
94      while (predecessors.TryGetValue(current, out next)) {
95        current = predecessors[current];
96        route.Add(current);
97      }
98      route.Reverse();
99      return route;
100    }
101  }
102
103  class NodeData : IComparable<NodeData> {
104    public long Node { get; set; }
105    public float Costs { get; set; }
106
107    public NodeData(long nodeId, float f) {
108      Node = nodeId;
109      Costs = f;
110    }
111
112    #region IComparable<NodeData> Members
113
114    public int CompareTo(NodeData other) {
115      return this.Costs > other.Costs ? 1 : this.Costs < other.Costs ? -1 : 0;
116    }
117
118    #endregion
119  }
120}
Note: See TracBrowser for help on using the repository browser.