1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Services related to pathfinding of graphs using A*
18 # A single graph may have different properties according to the `PathContext` used
24 # # Weighted graph (letters are nodes, digits are weights):
32 # var graph = new Graph[Node,WeightedLink]
34 # var na = new Node(graph)
35 # var nb = new Node(graph)
36 # var nc = new Node(graph)
37 # var nd = new Node(graph)
38 # var ne = new Node(graph)
40 # var lab = new WeightedLink(graph, na, nb, 2)
41 # var lac = new WeightedLink(graph, na, nc, 3)
42 # var lbd = new WeightedLink(graph, nb, nd, 1)
43 # var lcd = new WeightedLink(graph, nc, nd, 3)
44 # var lde = new WeightedLink(graph, nd, ne, 8)
46 # var context = new WeightedPathContext(graph)
48 # var path = na.path_to(ne, 100, context)
49 # assert path != null else print "No possible path"
51 # assert path.step == nb
52 # assert path.step == nd
53 # assert path.step == ne
54 # assert path.at_end_of_path
59 protected fun debug_a_star
: Bool do return false
60 private fun debug
(msg
: String) do if debug_a_star
then
61 sys
.stderr
.write
"a_star debug: {msg}\n"
70 var graph
: Graph[N
, Link]
72 init(graph
: Graph[N
, Link])
79 var links
: Set[Link] = new HashSet[Link]
81 # used to check if node has been searched in one pathfinding
82 private var last_pathfinding_evocation
: Int = 0
84 # cost up to in current evocation
85 # lifetime limited to evocation of `path_to`
86 private var best_cost_up_to
: Int = 0
89 # lifetime limited to evocation of `path_to`
90 private var best_source
: nullable N
= null
92 # is in frontier or buckets
93 # lifetime limited to evocation of `path_to`
94 private var open
: Bool = false
96 # Main functionnality, returns path from `self` to `dest`
97 fun path_to
(dest
: N
, max_cost
: Int, context
: PathContext): nullable Path[N
]
101 var nbr_buckets
= context
.worst_cost
+ context
.worst_heuristic_cost
+ 1
102 var buckets
= new Array[List[N
]].with_capacity
(nbr_buckets
)
104 for i
in [0 .. nbr_buckets
[ do
105 buckets
.add
(new List[N
])
108 graph
.pathfinding_current_evocation
+= 1
113 self.last_pathfinding_evocation
= graph
.pathfinding_current_evocation
114 self.best_cost_up_to
= 0
117 var frontier_node
: nullable N
= null
119 var bucket_searched
: Int = 0
121 # find next valid node in frontier/buckets
123 var current_bucket
= buckets
[cost
% nbr_buckets
]
125 if current_bucket
.is_empty
then # move to next bucket
126 debug
"b {cost} {cost % nbr_buckets} {buckets[cost % nbr_buckets].hash}"
128 if cost
> max_cost
then return null
131 if bucket_searched
> nbr_buckets
then break
134 frontier_node
= current_bucket
.pop
136 if frontier_node
.open
then break
141 if frontier_node
== null then
145 else if frontier_node
== dest
then
146 debug
"picked {frontier_node}, is destination"
148 var path
= new Path[N
](cost
)
150 while frontier_node
!= self do
151 path
.nodes
.unshift
(frontier_node
)
152 frontier_node
= frontier_node
.best_source
.as(not null)
157 # adds all next nodes to frontier/buckets
159 frontier_node
.open
= false
161 debug
"w exploring adjacents of {frontier_node}"
163 for link
in frontier_node
.links
do
164 var peek_node
= link
.to
165 debug
"v {context.is_blocked(link)} {peek_node.last_pathfinding_evocation != graph.pathfinding_current_evocation} {peek_node.best_cost_up_to > frontier_node.best_cost_up_to + context.cost(link)}, {peek_node.best_cost_up_to} > {frontier_node.best_cost_up_to} + {context.cost(link)}"
166 if not context
.is_blocked
(link
) and
167 (peek_node
.last_pathfinding_evocation
!= graph
.pathfinding_current_evocation
or
169 peek_node
.best_cost_up_to
> cost
+ context
.cost
(link
)))
172 peek_node
.open
= true
173 peek_node
.last_pathfinding_evocation
= graph
.pathfinding_current_evocation
174 peek_node
.best_cost_up_to
= cost
+ context
.cost
(link
)
175 peek_node
.best_source
= frontier_node
177 var est_cost
= peek_node
.best_cost_up_to
+context
.heuristic_cost
(link
.from
, peek_node
)
178 var at_bucket
= buckets
[est_cost
% nbr_buckets
]
179 at_bucket
.add
(peek_node
)
181 debug
"u putting {peek_node} at {est_cost} -> {est_cost % nbr_buckets} {at_bucket.hash}, {cost}+{context.cost(link)}"
189 # Link between two nodes and associated to a graph
194 var graph
: Graph[N
, L
]
199 init(graph
: Graph[N
, L
], from
, to
: N
)
210 class Graph[N
: Node, L
: Link]
211 var nodes
: Set[N
] = new HashSet[N
]
212 var links
: Set[L
] = new HashSet[L
]
214 fun add_node
(node
: N
): N
221 fun add_link
(link
: L
): L
225 link
.from
.links
.add
(link
)
230 # used to check if nodes have been searched in one pathfinding
231 var pathfinding_current_evocation
: Int = 0
234 # Result from pathfinding, a walkable path
239 var nodes
= new List[N
]
241 init (cost
: Int) do total_cost
= cost
246 assert nodes
.length
>= at
else print
"a_star::Path::step failed, is at_end_of_path"
254 fun peek_step
: N
do return nodes
[at
]
256 fun at_end_of_path
: Bool do return at
>= nodes
.length
259 # Context related to an evocation of pathfinding
264 var graph
: Graph[N
, L
]
266 # Worst cost of all the link's costs
267 fun worst_cost
: Int is abstract
270 fun cost
(link
: L
): Int is abstract
272 # Is that link blocked?
273 fun is_blocked
(link
: L
): Bool is abstract
276 fun heuristic_cost
(a
, b
: N
): Int is abstract
278 fun worst_heuristic_cost
: Int is abstract
282 ### Additionnal classes, may be useful
285 # Simple context with constant cost on each links
286 # Warning: A* is not optimize for such a case
287 class ConstantPathContext
290 redef fun worst_cost
do return 1
291 redef fun cost
(l
) do return 1
292 redef fun is_blocked
(l
) do return false
293 redef fun heuristic_cost
(a
, b
) do return 0
294 redef fun worst_heuristic_cost
do return 0
297 class WeightedPathContext
300 redef type L
: WeightedLink
302 init(graph
: Graph[N
, L
])
307 for l
in graph
.links
do
309 if cost
>= worst_cost
then worst_cost
= cost
+ 1
311 self.worst_cost
= worst_cost
314 redef var worst_cost
: Int
319 redef fun is_blocked
(l
) do return false
320 redef fun heuristic_cost
(a
, b
) do return 0
321 redef fun worst_heuristic_cost
do return 0
329 init(graph
: Graph[N
, L
], from
, to
: N
, weight
: Int)