Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/AStarAlgorithm.cs @ 8481

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

#1894

  • renamed Graph to OsmGraph
  • generic type in edge interface
File size: 3.3 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using HeuristicLab.Problems.RoutePlanning.Graph;
5namespace HeuristicLab.Algorithms.GraphRouting {
6  public class AStarAlgorithm : IRouter {
7    private OsmGraph 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 PriorityQueueOld<float, long> openList;
14    private PriorityQueue<float, long> openList;
15
16    public AStarAlgorithm(OsmGraph 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
28      //openList = new PriorityQueueOld<float, long>();
29      openList = new PriorityQueue<float, long>(new Comparer());
30
31      long currentNode = sourceNodeId;
32
33      //openList.Enqueue(currentNode, 0);
34      openList.Enqueue(0, currentNode);
35      distances.Add(currentNode, 0);
36
37      while (openList.Count > 0) {
38        //currentNode = openList.Dequeue();
39        currentNode = openList.DequeueValue();
40        if (currentNode == targetNodeId) {
41          return ReconstructPath(currentNode).ToArray();
42        }
43
44        Dictionary<long, float> currentNeighbors = graph.GetNeighbors(currentNode);
45        foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
46          //if (closedList.Contains(neighbor.Key))
47          //  continue;
48          if (closedListLookup.Contains(neighbor.Key))
49            continue;
50
51          float costsFromStart = GetDistance(currentNode) + neighbor.Value;
52
53          if (openList.Contains(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          if (openList.Contains(neighbor.Key)) {
61            openList.Remove(neighbor.Key);
62          }
63          //openList.Enqueue(neighbor.Key, f);
64          openList.Enqueue(f, neighbor.Key);
65        }
66        closedList.Add(currentNode);
67        closedListLookup.Add(currentNode);
68      }
69      throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
70    }
71
72    #endregion
73
74    private float GetDistance(long id) {
75      float d;
76      if (distances.TryGetValue(id, out d)) {
77        return d;
78      } else { return float.MaxValue; }
79
80    }
81
82    private List<long> ReconstructPath(long nodeId) {
83      List<long> route = new List<long>();
84      long current = nodeId;
85      long next;
86
87      if (!predecessors.TryGetValue(current, out next)) {
88        return null;
89      }
90      route.Add(current);
91      while (predecessors.TryGetValue(current, out next)) {
92        current = predecessors[current];
93        route.Add(current);
94      }
95      route.Reverse();
96      return route;
97    }
98  }
99
100  class Comparer : IComparer<float> {
101    public int Compare(float item1, float item2) {
102      return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.