Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1894:

  • new implementation for priority queue
  • based on heap data structure
  • allow multiple keys
  • adapted test program
File size: 3.2 KB
RevLine 
[8321]1
2using System;
3using System.Collections.Generic;
4using HeuristicLab.Problems.RoutePlanning.Graph;
5namespace HeuristicLab.Algorithms.GraphRouting {
[8350]6  public class AStarAlgorithm : IRouter {
[8321]7    private Graph graph;
8
9    private Dictionary<long, float> distances;
10    private Dictionary<long, long> predecessors;
11    private List<float> closedList = new List<float>();
[8350]12    //private PriorityQueueOld<float, long> openList;
[8321]13    private PriorityQueue<float, long> openList;
14
15    public AStarAlgorithm(Graph graph) {
16      this.graph = graph;
17    }
18
19    #region IRouter Members
20
21    public long[] Calculate(long sourceNodeId, long targetNodeId) {
22      distances = new Dictionary<long, float>();
23      predecessors = new Dictionary<long, long>();
24      closedList = new List<float>();
[8350]25      //openList = new PriorityQueueOld<float, long>();
26      openList = new PriorityQueue<float, long>(new MyComparer());
[8321]27
28      long currentNode = sourceNodeId;
29
[8350]30      //openList.Enqueue(currentNode, 0);
31      openList.Enqueue(0, currentNode);
[8321]32      distances.Add(currentNode, 0);
33
34      while (openList.Count > 0) {
[8350]35        //currentNode = openList.Dequeue();
36        currentNode = openList.DequeueValue();
[8321]37        if (currentNode == targetNodeId) {
38          return ReconstructPath(currentNode).ToArray();
39        }
40
41        Dictionary<long, float> currentNeighbors = graph.GetNeighbors(currentNode);
42        foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
43          if (closedList.Contains(neighbor.Key))
44            continue;
45
46          float costsFromStart = GetDistance(currentNode) + neighbor.Value;
47
48          if (openList.Contains(neighbor.Key) && costsFromStart > GetDistance(neighbor.Key))
49            continue;
50
51          distances[neighbor.Key] = costsFromStart;
52          predecessors[neighbor.Key] = currentNode;
53
54          float f = costsFromStart + graph.EstimatedCosts(neighbor.Key, targetNodeId);
55          if (openList.Contains(neighbor.Key)) {
56            openList.Remove(neighbor.Key);
57          }
[8350]58          //openList.Enqueue(neighbor.Key, f);
59          openList.Enqueue(f, neighbor.Key);
[8321]60        }
61      }
62      throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
63    }
64
65    #endregion
66
67    private float GetDistance(long id) {
68      float d;
69      if (distances.TryGetValue(id, out d)) {
70        return d;
71      } else { return float.MaxValue; }
72
73    }
74
75    private List<long> ReconstructPath(long nodeId) {
76      List<long> route = new List<long>();
77      long current = nodeId;
78      long next;
79
80      if (!predecessors.TryGetValue(current, out next)) {
81        return null;
82      }
83      route.Add(current);
84      while (predecessors.TryGetValue(current, out next)) {
85        current = predecessors[current];
86        route.Add(current);
87      }
88      route.Reverse();
89      return route;
90    }
91  }
[8350]92
93  class MyComparer : IComparer<float> {
94    public int Compare(float item1, float item2) {
95      return item1 > item2 ? 1 : item1 < item2 ? -1 : 0;
96
97      // inverted comparison
98      //return item1 < item2 ? 1 : item1 > item2 ? -1 : 0;
99    }
100  }
[8321]101}
Note: See TracBrowser for help on using the repository browser.