Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/AStarAlgorithmV3.cs @ 15728

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

#1894:

  • Included costCalculator in a star search
  • restructured and renamed Dijkstra algorithm
  • Added code comments to FibonacciHeap
File size: 4.6 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using HeuristicLab.Algorithms.GraphRouting.Interfaces;
5using HeuristicLab.Problems.RoutePlanning.Interfaces;
6using HeuristicLab.Problems.RoutePlanning.RoutingGraph;
7namespace HeuristicLab.Algorithms.GraphRouting {
8  public class AStarAlgorithmV3 : IRouter {
9    private IGraph graph;
10    private ICostCalculator costCalculator;
11
12    private Dictionary<long, float> distances;
13    private Dictionary<long, long> predecessors;
14    private List<long> closedList;
15    private HashSet<long> closedListLookup;
16    private SortedSet<NodeData> openList;
17    private Dictionary<long, NodeData> openListLookup;
18
19    public AStarAlgorithmV3(IGraph graph)
20      : this(graph, new TravelTimeCostCalculator()) {
21    }
22
23    public AStarAlgorithmV3(IGraph graph, ICostCalculator costCalc) {
24      this.graph = graph;
25      this.costCalculator = costCalc;
26    }
27
28    #region IRouter Members
29
30    public long[] Calculate(long sourceNodeId, long targetNodeId) {
31      distances = new Dictionary<long, float>();
32      predecessors = new Dictionary<long, long>();
33      closedList = new List<long>();
34      closedListLookup = new HashSet<long>();
35      openList = new SortedSet<NodeData>();
36      openListLookup = new Dictionary<long, NodeData>();
37
38      long currentNode = sourceNodeId;
39
40      NodeData nd = new NodeData(currentNode, 0);
41      openList.Add(nd);
42      openListLookup.Add(currentNode, nd);
43      distances.Add(currentNode, 0);
44
45      int i = 0;
46      while (openList.Count > 0) {
47
48        NodeData data = openList.Min;
49        //Console.Write(i + ": (" + openList.Count + "/");
50        openList.Remove(data);
51        //Console.Write(openList.Count + ") - (" + openListLookup.Count + "/");
52        openListLookup.Remove(data.Node);
53        //Console.WriteLine(openListLookup.Count + ")");
54        currentNode = data.Node;
55        if (currentNode == targetNodeId) {
56          return ReconstructPath(currentNode).ToArray();
57        }
58
59        //if (openList.Count != openListLookup.Count) {
60        //  Console.WriteLine();
61        //} else {
62        //  Console.Write(".");
63        //}
64
65        Dictionary<long, float> currentNeighbors = graph.GetNeighborsWithWeight(currentNode);
66        foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
67          if (closedListLookup.Contains(neighbor.Key))
68            continue;
69
70          float costsFromStart = GetDistance(currentNode) + neighbor.Value;
71
72          if (openListLookup.ContainsKey(neighbor.Key) && costsFromStart > GetDistance(neighbor.Key))
73            continue;
74
75          distances[neighbor.Key] = costsFromStart;
76          predecessors[neighbor.Key] = currentNode;
77
78          Vertex vertNeigh = graph.GetVertex(neighbor.Key);
79          Vertex vertTarget = graph.GetVertex(targetNodeId);
80          float f = costsFromStart + costCalculator.EstimatedCosts(vertNeigh, vertTarget);
81
82          if (openListLookup.ContainsKey(neighbor.Key)) {
83            openList.Remove(openListLookup[neighbor.Key]);
84            openListLookup.Remove(neighbor.Key);
85          }
86
87          NodeData newNode = new NodeData(neighbor.Key, f);
88          openList.Add(newNode);  // wenn f bereits vorhanden -> kein add: openList.Contains(new NodeData(123, 0.2973786f))
89          openListLookup.Add(newNode.Node, newNode);
90        }
91        closedList.Add(currentNode);
92        closedListLookup.Add(currentNode);
93        i++;
94      }
95      throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
96    }
97
98    #endregion
99
100    private float GetDistance(long id) {
101      float d;
102      if (distances.TryGetValue(id, out d)) {
103        return d;
104      } else { return float.MaxValue; }
105    }
106
107    private List<long> ReconstructPath(long nodeId) {
108      List<long> route = new List<long>();
109      long current = nodeId;
110      long next;
111
112      if (!predecessors.TryGetValue(current, out next)) {
113        return null;
114      }
115      route.Add(current);
116      while (predecessors.TryGetValue(current, out next)) {
117        current = predecessors[current];
118        route.Add(current);
119      }
120      route.Reverse();
121      return route;
122    }
123
124    private class NodeData : IComparable<NodeData> {
125      public long Node { get; set; }
126      public float Costs { get; set; }
127
128      public NodeData(long nodeId, float f) {
129        Node = nodeId;
130        Costs = f;
131      }
132
133      #region IComparable<NodeData> Members
134
135      public int CompareTo(NodeData other) {
136        return this.Costs > other.Costs ? 1 : this.Costs < other.Costs ? -1 : 0;
137      }
138
139      #endregion
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.