Module: Paths
- Defined in:
- lib/rbbt/network/paths.rb
Class Method Summary collapse
- .dijkstra(adjacency, start_node, end_node = nil, max_steps = nil) ⇒ Object
- .extract_path(parents, start_node, end_node) ⇒ Object
- .random_weighted_dijkstra(adjacency, l, start_node, end_node = nil) ⇒ Object
- .weighted_dijkstra(adjacency, start_node, end_node = nil, threshold = nil, max_steps = nil) ⇒ Object
Class Method Details
.dijkstra(adjacency, start_node, end_node = nil, max_steps = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rbbt/network/paths.rb', line 5 def self.dijkstra(adjacency, start_node, end_node = nil, max_steps = nil) return nil unless adjacency.include? start_node active = PriorityQueue.new distances = Hash.new { 1.0 / 0.0 } parents = Hash.new active[start_node] << 0 best = 1.0 / 0.0 until active.empty? u = active.priorities.first distance = active.shift distances[u] = distance d = distance + 1 path = extract_path(parents, start_node, u) next if path.length > max_steps if max_steps adjacency[u].each do |v| next unless d < distances[v] and d < best # we can't relax this one best = d if (String === end_node ? end_node == v : end_node.include?(v)) active[v] << d if adjacency.include? v distances[v] = d parents[v] = u end end if end_node end_node = end_node.select{|n| parents.keys.include? n}.first unless String === end_node return nil if not parents.include? end_node extract_path(parents, start_node, u) else parents end end |
.extract_path(parents, start_node, end_node) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/rbbt/network/paths.rb', line 40 def self.extract_path(parents, start_node, end_node) path = [end_node] while not path.last === start_node path << parents[path.last] end path end |
.random_weighted_dijkstra(adjacency, l, start_node, end_node = nil) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rbbt/network/paths.rb', line 90 def self.random_weighted_dijkstra(adjacency, l, start_node, end_node = nil) return nil unless adjacency.include? start_node active = PriorityQueue.new distances = Hash.new { 1.0 / 0.0 } parents = Hash.new active[start_node] << 0 best = 1.0 / 0.0 until active.empty? u = active.priorities.first distance = active.shift distances[u] = distance next if not adjacency.include?(u) or adjacency[u].nil? or adjacency[u].empty? Misc.zip_fields(adjacency[u]).each do |v,node_dist| next if node_dist.nil? d = distance + (node_dist * (l + rand)) next unless d < distances[v] and d < best # we can't relax this one active[v] << distances[v] = d parents[v] = u best = d if (String === end_node ? end_node == v : end_node.include?(v)) end end if end_node end_node = (end_node & parents.keys).first unless String === end_node return nil if not parents.include? end_node path = [end_node] while not path.last === start_node path << parents[path.last] end path else parents end end |
.weighted_dijkstra(adjacency, start_node, end_node = nil, threshold = nil, max_steps = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rbbt/network/paths.rb', line 48 def self.weighted_dijkstra(adjacency, start_node, end_node = nil, threshold = nil, max_steps = nil) return nil unless adjacency.include? start_node active = PriorityQueue.new distances = Hash.new { 1.0 / 0.0 } parents = Hash.new active[start_node] << 0 best = 1.0 / 0.0 found = false until active.empty? u = active.priorities.first distance = active.shift distances[u] = distance path = extract_path(parents, start_node, u) next if path.length > max_steps if max_steps next if not adjacency.include?(u) or (adjacency[u].nil? or adjacency[u].empty? ) Misc.zip_fields(adjacency[u]).each do |v,node_dist| next if node_dist.nil? or (threshold and node_dist > threshold) d = distance + node_dist next unless d < distances[v] and d < best # we can't relax this one active[v] << d distances[v] = d parents[v] = u if (String === end_node ? end_node == v : end_node.include?(v)) best = d found = true end end end return nil unless found if end_node end_node = (end_node & parents.keys).first unless String === end_node return nil if not parents.include? end_node extract_path(parents, start_node, end_node) else parents end end |