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
22 # # Weighted graph (letters are nodes, digits are weights):
30 # var graph = new Graph[Node,WeigthedLink[Node]]
32 # var na = new Node(graph)
33 # var nb = new Node(graph)
34 # var nc = new Node(graph)
35 # var nd = new Node(graph)
36 # var ne = new Node(graph)
38 # var lab = new WeightedLink(graph, na, nb, 2)
39 # var lac = new WeightedLink(graph, na, nc, 3)
40 # var lbd = new WeightedLink(graph, nb, nd, 1)
41 # var lcd = new WeightedLink(graph, nc, nd, 3)
42 # var lde = new WeightedLink(graph, nd, ne, 8)
44 # var context = new WeightedPathContext(graph)
46 # var path = na.path_to(ne, 100, context)
47 # assert path != null else print "No possible path"
49 # while not path.at_end_of_path do
55 protected fun debug_a_star
: Bool do return false
56 private fun debug
(msg
: String) do if debug_a_star
then
57 stderr
.write
"a_star debug: {msg}\n"
66 var graph
: Graph[N
, Link]
68 init(graph
: Graph[N
, Link])
75 var links
: Set[Link] = new HashSet[Link]
77 # used to check if node has been searched in one pathfinding
78 private var last_pathfinding_evocation
: Int = 0
80 # cost up to in current evocation
81 # lifetime limited to evocation of path_to
82 private var best_cost_up_to
: Int = 0
85 # lifetime limited to evocation of path_to
86 private var best_source
: nullable N
= null
88 # is in frontier or buckets
89 # lifetime limited to evocation of path_to
90 private var open
: Bool = false
93 # Main functionnality, returns path from `self` to `dest`
94 fun path_to
(dest
: Node, max_cost
: Int, context
: PathContext): nullable Path[N
]
98 var nbr_buckets
= context
.worst_cost
+ context
.worst_heuristic_cost
+ 1
99 var buckets
= new Array[List[Node]].with_capacity
(nbr_buckets
)
101 for i
in [0 .. nbr_buckets
[ do
102 buckets
.add
(new List[Node])
105 graph
.pathfinding_current_evocation
+= 1
110 self.last_pathfinding_evocation
= graph
.pathfinding_current_evocation
111 self.best_cost_up_to
= 0
113 while cost
< max_cost
do
114 var frontier_node
: nullable Node = null
116 var bucket_searched
: Int = 0
118 # find next valid node in frontier/buckets
120 var current_bucket
= buckets
[cost
% nbr_buckets
]
122 if current_bucket
.is_empty
then # move to next bucket
123 debug
"b {cost} {cost % nbr_buckets} {buckets[cost % nbr_buckets].hash}"
127 if bucket_searched
> nbr_buckets
then break
130 frontier_node
= current_bucket
.pop
132 if frontier_node
.open
then break
137 if frontier_node
== null then
141 else if frontier_node
== dest
then
142 debug
"picked {frontier_node}, is destination"
144 var path
= new Path[N
](cost
)
146 while frontier_node
!= self do
147 path
.nodes
.unshift
(frontier_node
)
148 frontier_node
= frontier_node
.best_source
.as(not null)
153 # adds all next nodes to frontier/buckets
155 frontier_node
.open
= false
157 debug
"w exploring adjacents of {frontier_node}"
159 for link
in frontier_node
.links
do
160 var peek_node
= link
.to
161 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)}"
162 if not context
.is_blocked
(link
) and
163 (peek_node
.last_pathfinding_evocation
!= graph
.pathfinding_current_evocation
or
165 peek_node
.best_cost_up_to
> cost
+ context
.cost
(link
)))
168 peek_node
.open
= true
169 peek_node
.last_pathfinding_evocation
= graph
.pathfinding_current_evocation
170 peek_node
.best_cost_up_to
= cost
+ context
.cost
(link
)
171 peek_node
.best_source
= frontier_node
173 var est_cost
= peek_node
.best_cost_up_to
+context
.heuristic_cost
(link
.from
, peek_node
)
174 var at_bucket
= buckets
[est_cost
% nbr_buckets
]
175 at_bucket
.add
(peek_node
)
177 debug
"u putting {peek_node} at {est_cost} -> {est_cost % nbr_buckets} {at_bucket.hash}, {cost}+{context.cost(link)}"
187 # Find closes node with matching caracteristic
188 # TODO remove closures
189 fun find_closest
(max_to_search
: Int): nullable N
!with
(n
: N
): Bool
191 if with
(self) then return self
193 var frontier
= new List[N
]
194 graph
.pathfinding_current_evocation
+= 1
195 var current_evocation
= graph
.pathfinding_current_evocation
198 self.last_pathfinding_evocation
= current_evocation
201 while not frontier
.is_empty
do
202 var node
= frontier
.shift
204 for link
in node
.links
do
206 if to
.last_pathfinding_evocation
!= current_evocation
then
207 if with
(to
) then return to
210 to
.last_pathfinding_evocation
= current_evocation
215 if i
> max_to_search
then return null
226 var graph
: Graph[N
, L
]
231 init(graph
: Graph[N
, L
], from
, to
: N
)
242 class Graph[N
: Node, L
: Link]
243 var nodes
: Set[N
] = new HashSet[N
]
244 var links
: Set[L
] = new HashSet[L
]
246 fun add_node
(node
: N
): N
253 fun add_link
(link
: L
): L
257 link
.from
.links
.add
(link
)
262 # used to check if nodes have been searched in one pathfinding
263 var pathfinding_current_evocation
: Int = 0
266 # Result from pathfinding, a walkable path
271 var nodes
= new List[N
]
273 init (cost
: Int) do total_cost
= cost
278 assert nodes
.length
>= at
else print
"a_star::Path::step failed, is at_end_of_path"
286 fun peek_step
: N
do return nodes
[at
]
288 fun at_end_of_path
: Bool do return at
>= nodes
.length
291 # Context related to an evocation of pathfinding
296 var graph
: Graph[N
, L
]
298 # Worst cost of all the link's costs
299 fun worst_cost
: Int is abstract
302 fun cost
(link
: Link): Int is abstract
304 # Is that link blocked?
305 fun is_blocked
(link
: Link): Bool is abstract
308 fun heuristic_cost
(a
, b
: Node): Int is abstract
310 fun worst_heuristic_cost
: Int is abstract
314 ### Additionnal classes, may be useful
317 # Simple context with constant cost on each links
318 # Warning: A* is not optimize for such a case
319 class ConstantPathContext
322 redef fun worst_cost
do return 1
323 redef fun cost
(l
) do return 1
324 redef fun is_blocked
(l
) do return false
325 redef fun heuristic_cost
(a
, b
) do return 0
326 redef fun worst_heuristic_cost
do return 0
329 class WeightedPathContext
332 redef type L
: WeightedLink
334 init(graph
: Graph[N
, L
])
339 for l
in graph
.links
do
341 if cost
>= worst_cost
then worst_cost
= cost
+ 1
343 self.worst_cost
= worst_cost
346 redef var worst_cost
: Int
352 redef fun is_blocked
(l
) do return false
353 redef fun heuristic_cost
(a
, b
) do return 0
354 redef fun worst_heuristic_cost
do return 0
362 init(graph
: Graph[N
, L
], from
, to
: N
, weight
: Int)