1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Algorithms.GraphRouting.Interfaces;
|
---|
5 | using HeuristicLab.Problems.RoutePlanning.Interfaces;
|
---|
6 | using HeuristicLab.Algorithms.GraphRouting.PriorityQueues;
|
---|
7 | namespace HeuristicLab.Algorithms.GraphRouting {
|
---|
8 | public class DijkstraNoDecAlgorithm : IRouter {
|
---|
9 | private IGraph graph;
|
---|
10 | private IHeap<float, long> unvisitedNodes;
|
---|
11 | private Dictionary<long, float> distances;
|
---|
12 | private Dictionary<long, long> predecessors;
|
---|
13 |
|
---|
14 | public DijkstraNoDecAlgorithm(IGraph graph) {
|
---|
15 | this.graph = graph;
|
---|
16 | }
|
---|
17 |
|
---|
18 | #region IRouter Members
|
---|
19 |
|
---|
20 | public long[] Calculate(long sourceNodeId, long targetNodeId) {
|
---|
21 | unvisitedNodes = new NaivePriorityQueue<float, long>();
|
---|
22 | //unvisitedNodes = new BinaryHeap<float, long>(1000000); // unvisited
|
---|
23 | //unvisitedNodes = new BinomialHeap<float, long>(); // unvisited
|
---|
24 | //unvisitedNodes = new BinaryHeapV2<float, long>(float.MaxValue, float.MinValue, 1000000); // unvisited
|
---|
25 | //unvisitedNodes = new BinaryHeapV3<float, long>(float.MaxValue, float.MinValue, 1000000); // unvisited
|
---|
26 | //unvisitedNodes = new Heap4<float, long>(float.MaxValue, float.MinValue, 1000000); // unvisited
|
---|
27 | //unvisitedNodes = new FibonacciHeap<float, long>(); // unvisited
|
---|
28 | distances = new Dictionary<long, float>();
|
---|
29 | predecessors = new Dictionary<long, long>();
|
---|
30 |
|
---|
31 | List<long> resultRoute = new List<long>();
|
---|
32 | if (sourceNodeId == targetNodeId) {
|
---|
33 | resultRoute.Add(sourceNodeId);
|
---|
34 | return resultRoute.ToArray();
|
---|
35 | }
|
---|
36 |
|
---|
37 | long currentNode = sourceNodeId;
|
---|
38 | float currentWeight = 0;
|
---|
39 | unvisitedNodes.Insert(currentWeight, currentNode);
|
---|
40 |
|
---|
41 | while (unvisitedNodes.Size > 0) {
|
---|
42 | KeyValuePair<float, long> data = unvisitedNodes.PeekMin();
|
---|
43 | unvisitedNodes.RemoveMin();
|
---|
44 | currentNode = data.Value;
|
---|
45 | currentWeight = data.Key;
|
---|
46 |
|
---|
47 | //--------------
|
---|
48 | //Logger.LogToFile("cn = " + currentNode + " (" + currentWeight + ")");
|
---|
49 | //--------------
|
---|
50 |
|
---|
51 | if (currentNode == targetNodeId) {
|
---|
52 | break;
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (currentWeight <= GetDistance(currentNode)) {
|
---|
56 | distances[currentNode] = currentWeight;
|
---|
57 | RelaxNeighbors(currentNode, currentWeight);
|
---|
58 | }
|
---|
59 |
|
---|
60 | //--------------
|
---|
61 | //Logger.LogToFile("");
|
---|
62 | //--------------
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (currentNode != targetNodeId) {
|
---|
66 | throw new Exception(string.Format("No route found from {0} to {1}", sourceNodeId, targetNodeId));
|
---|
67 | }
|
---|
68 | return GetRoute(targetNodeId).ToArray();
|
---|
69 | }
|
---|
70 |
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | public void RelaxNeighbors(long nodeId, float weight) {
|
---|
74 | Dictionary<long, float> currentNeighbors = graph.GetNeighborsWithWeight(nodeId);
|
---|
75 | foreach (KeyValuePair<long, float> neighbor in currentNeighbors) {
|
---|
76 | float totalWeight = weight + neighbor.Value;
|
---|
77 | if (totalWeight < GetDistance(neighbor.Key)) {
|
---|
78 | distances[neighbor.Key] = totalWeight;
|
---|
79 | predecessors[neighbor.Key] = nodeId;
|
---|
80 | unvisitedNodes.Insert(totalWeight, neighbor.Key);
|
---|
81 |
|
---|
82 | //--------------
|
---|
83 | //Logger.LogToFile(" neigb:" + neighbor.Key + " (" + totalWeight + ")");
|
---|
84 | //--------------
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | private float GetDistance(long id) {
|
---|
90 | float d;
|
---|
91 | if (distances.TryGetValue(id, out d)) {
|
---|
92 | return d;
|
---|
93 | } else { return float.MaxValue; }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private List<long> GetRoute(long nodeId) {
|
---|
97 | List<long> route = new List<long>();
|
---|
98 | long current = nodeId;
|
---|
99 | long next;
|
---|
100 |
|
---|
101 | if (!predecessors.TryGetValue(current, out next)) {
|
---|
102 | return null;
|
---|
103 | }
|
---|
104 | route.Add(current);
|
---|
105 | while (predecessors.TryGetValue(current, out next)) {
|
---|
106 | current = predecessors[current];
|
---|
107 | route.Add(current);
|
---|
108 | }
|
---|
109 | route.Reverse();
|
---|
110 | return route;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|