1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Algorithms.GraphRouting.Interfaces;
|
---|
5 | using HeuristicLab.Problems.RoutePlanning.Interfaces;
|
---|
6 | namespace HeuristicLab.Algorithms.GraphRouting {
|
---|
7 | public class AStarAlgorithmV3 : IRouter {
|
---|
8 | private IGraph graph;
|
---|
9 |
|
---|
10 | private Dictionary<long, float> distances;
|
---|
11 | private Dictionary<long, long> predecessors;
|
---|
12 | private List<long> closedList;
|
---|
13 | private HashSet<long> closedListLookup;
|
---|
14 | private SortedSet<NodeData> openList;
|
---|
15 | private Dictionary<long, NodeData> openListLookup;
|
---|
16 |
|
---|
17 | public AStarAlgorithmV3(IGraph graph) {
|
---|
18 | this.graph = graph;
|
---|
19 | }
|
---|
20 |
|
---|
21 | #region IRouter Members
|
---|
22 |
|
---|
23 | public long[] Calculate(long sourceNodeId, long targetNodeId) {
|
---|
24 | distances = new Dictionary<long, float>();
|
---|
25 | predecessors = new Dictionary<long, long>();
|
---|
26 | closedList = new List<long>();
|
---|
27 | closedListLookup = new HashSet<long>();
|
---|
28 | openList = new SortedSet<NodeData>();
|
---|
29 | openListLookup = new Dictionary<long, NodeData>();
|
---|
30 |
|
---|
31 | long currentNode = sourceNodeId;
|
---|
32 |
|
---|
33 | NodeData nd = new NodeData(currentNode, 0);
|
---|
34 | openList.Add(nd);
|
---|
35 | openListLookup.Add(currentNode, nd);
|
---|
36 | distances.Add(currentNode, 0);
|
---|
37 |
|
---|
38 | while (openList.Count > 0) {
|
---|
39 | NodeData data = openList.Min;
|
---|
40 | openList.Remove(data);
|
---|
41 | openListLookup.Remove(data.Node);
|
---|
42 | currentNode = data.Node;
|
---|
43 | if (currentNode == targetNodeId) {
|
---|
44 | return ReconstructPath(currentNode).ToArray();
|
---|
45 | }
|
---|
46 |
|
---|
47 | Dictionary<long, float> currentNeighbors = graph.GetNeighborsWithWeight(currentNode);
|
---|
48 | foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
|
---|
49 | if (closedListLookup.Contains(neighbor.Key))
|
---|
50 | continue;
|
---|
51 |
|
---|
52 | float costsFromStart = GetDistance(currentNode) + neighbor.Value;
|
---|
53 |
|
---|
54 | if (openListLookup.ContainsKey(neighbor.Key) && costsFromStart > GetDistance(neighbor.Key))
|
---|
55 | continue;
|
---|
56 |
|
---|
57 | distances[neighbor.Key] = costsFromStart;
|
---|
58 | predecessors[neighbor.Key] = currentNode;
|
---|
59 |
|
---|
60 | float f = costsFromStart + graph.EstimatedCosts(neighbor.Key, targetNodeId);
|
---|
61 |
|
---|
62 | if (openListLookup.ContainsKey(neighbor.Key)) {
|
---|
63 | openList.Remove(openListLookup[neighbor.Key]);
|
---|
64 | openListLookup.Remove(neighbor.Key);
|
---|
65 | }
|
---|
66 |
|
---|
67 | NodeData newNode = new NodeData(neighbor.Key, f);
|
---|
68 | openList.Add(newNode);
|
---|
69 | openListLookup.Add(newNode.Node, newNode);
|
---|
70 | }
|
---|
71 | closedList.Add(currentNode);
|
---|
72 | closedListLookup.Add(currentNode);
|
---|
73 | }
|
---|
74 | throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
|
---|
75 | }
|
---|
76 |
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | private float GetDistance(long id) {
|
---|
80 | float d;
|
---|
81 | if (distances.TryGetValue(id, out d)) {
|
---|
82 | return d;
|
---|
83 | } else { return float.MaxValue; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private List<long> ReconstructPath(long nodeId) {
|
---|
87 | List<long> route = new List<long>();
|
---|
88 | long current = nodeId;
|
---|
89 | long next;
|
---|
90 |
|
---|
91 | if (!predecessors.TryGetValue(current, out next)) {
|
---|
92 | return null;
|
---|
93 | }
|
---|
94 | route.Add(current);
|
---|
95 | while (predecessors.TryGetValue(current, out next)) {
|
---|
96 | current = predecessors[current];
|
---|
97 | route.Add(current);
|
---|
98 | }
|
---|
99 | route.Reverse();
|
---|
100 | return route;
|
---|
101 | }
|
---|
102 |
|
---|
103 | private 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 | }
|
---|
121 | }
|
---|