Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/DijkstraNoDecAlgorithm.cs @ 14604

Last change on this file since 14604 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: 3.9 KB
RevLine 
[8539]1
2using System;
3using System.Collections.Generic;
4using HeuristicLab.Algorithms.GraphRouting.Interfaces;
[8640]5using HeuristicLab.Problems.RoutePlanning.Interfaces;
[8539]6using HeuristicLab.Algorithms.GraphRouting.PriorityQueues;
7namespace 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) {
[8640]21      unvisitedNodes = new NaivePriorityQueue<float, long>();
22      //unvisitedNodes = new BinaryHeap<float, long>(1000000);      // unvisited
[8539]23      //unvisitedNodes = new BinomialHeap<float, long>();    // unvisited
[8640]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
[8539]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
[8640]47        //--------------
48        //Logger.LogToFile("cn = " + currentNode + " (" + currentWeight + ")");
49        //--------------
50
[8539]51        if (currentNode == targetNodeId) {
52          break;
53        }
54
55        if (currentWeight <= GetDistance(currentNode)) {
56          distances[currentNode] = currentWeight;
57          RelaxNeighbors(currentNode, currentWeight);
58        }
[8640]59
60        //--------------
61        //Logger.LogToFile("");
62        //--------------
[8539]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);
[8640]81
82          //--------------
83          //Logger.LogToFile("    neigb:" + neighbor.Key + " (" + totalWeight + ")");
84          //--------------
[8539]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}
Note: See TracBrowser for help on using the repository browser.