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
60 # Type of the others nodes in the `graph`
64 var graph
: Graph[N
, Link]
72 var links
: Set[Link] = new HashSet[Link]
74 # used to check if node has been searched in one pathfinding
75 private var last_pathfinding_evocation
: Int = 0
77 # cost up to in current evocation
78 # lifetime limited to evocation of `path_to`
79 private var best_cost_up_to
: Int = 0
82 # lifetime limited to evocation of `path_to`
83 private var best_source
: nullable N
= null
85 # is in frontier or buckets
86 # lifetime limited to evocation of `path_to`
87 private var open
: Bool = false
89 # Main functionnality, returns path from `self` to `dest`
90 fun path_to
(dest
: N
, max_cost
: Int, context
: PathContext): nullable AStarPath[N
]
92 return path_to_alts
(dest
, max_cost
, context
, null)
95 # Find a path to a possible `destination` or a node accepted by `alt_targets`
96 fun path_to_alts
(destination
: nullable N
, max_cost
: Int, context
: PathContext,
97 alt_targets
: nullable TargetCondition[N
]): nullable AStarPath[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
= 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
127 if cost
> max_cost
then return null
130 if bucket_searched
> nbr_buckets
then break
132 frontier_node
= current_bucket
.pop
134 if frontier_node
.open
then break
139 if frontier_node
== null then
143 else if frontier_node
== destination
or
144 (alt_targets
!= null and alt_targets
.accept
(frontier_node
)) then
146 var path
= new AStarPath[N
](cost
)
148 while frontier_node
!= self do
149 path
.nodes
.unshift
(frontier_node
)
150 frontier_node
= frontier_node
.best_source
151 assert frontier_node
!= null
156 # adds all next nodes to frontier/buckets
158 frontier_node
.open
= false
160 for link
in frontier_node
.links
do
161 var peek_node
= link
.to
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
)))
167 peek_node
.open
= true
168 peek_node
.last_pathfinding_evocation
= graph
.pathfinding_current_evocation
169 peek_node
.best_cost_up_to
= cost
+ context
.cost
(link
)
170 peek_node
.best_source
= frontier_node
173 if destination
!= null then
174 est_cost
= peek_node
.best_cost_up_to
+ context
.heuristic_cost
(peek_node
, destination
)
175 else if alt_targets
!= null then
176 est_cost
= peek_node
.best_cost_up_to
+ alt_targets
.heuristic_cost
(peek_node
, link
)
179 var at_bucket
= buckets
[est_cost
% nbr_buckets
]
180 at_bucket
.add
(peek_node
)
188 # Link between two nodes and associated to a graph
190 # Type of the nodes in `graph`
193 # Type of the other links in `graph`
196 # The graph to which belongs `self`
197 var graph
: Graph[N
, L
]
199 # Origin of this link
202 # Endpoint of this link
212 class Graph[N
: Node, L
: Link]
213 # Nodes in this graph
214 var nodes
: Set[N
] = new HashSet[N
]
216 # Links in this graph
217 var links
: Set[L
] = new HashSet[L
]
219 # Add a `node` to this graph
220 fun add_node
(node
: N
): N
227 # Add a `link` to this graph
228 fun add_link
(link
: L
): L
232 link
.from
.links
.add
(link
)
237 # Used to check if nodes have been searched in one pathfinding
238 private var pathfinding_current_evocation
: Int = 0
241 # Result from path finding and a walkable path
244 # The total cost of this path
247 # The list of nodes composing this path
248 var nodes
= new List[N
]
250 private var at
: Int = 0
252 # Step on the path and get the next node to travel
255 assert nodes
.length
>= at
else print
"a_star::AStarPath::step failed, is at_end_of_path"
263 # Peek at the next step of the path
264 fun peek_step
: N
do return nodes
[at
]
266 # Are we at the end of this path?
267 fun at_end_of_path
: Bool do return at
>= nodes
.length
270 # Context related to an evocation of pathfinding
272 # Type of the nodes in `graph`
275 # Type of the links in `graph`
278 # Graph to which is associated `self`
279 var graph
: Graph[N
, L
]
281 # Worst cost of all the link's costs
282 fun worst_cost
: Int is abstract
285 fun cost
(link
: L
): Int is abstract
287 # Is that link blocked?
288 fun is_blocked
(link
: L
): Bool is abstract
291 fun heuristic_cost
(a
, b
: N
): Int is abstract
293 # The worst cost suggested by the heuristic
294 fun worst_heuristic_cost
: Int is abstract
298 ### Additionnal classes, may be useful
301 # Simple context with constant cost on each links
302 # Warning: A* is not optimize for such a case
303 class ConstantPathContext
306 redef fun worst_cost
do return 1
307 redef fun cost
(l
) do return 1
308 redef fun is_blocked
(l
) do return false
309 redef fun heuristic_cost
(a
, b
) do return 0
310 redef fun worst_heuristic_cost
do return 0
313 # A `PathContext` for graphs with `WeightedLink`
314 class WeightedPathContext
317 redef type L
: WeightedLink
324 for l
in graph
.links
do
326 if cost
>= worst_cost
then worst_cost
= cost
+ 1
328 self.worst_cost
= worst_cost
331 redef var worst_cost
: Int is noinit
336 redef fun is_blocked
(l
) do return false
337 redef fun heuristic_cost
(a
, b
) do return 0
338 redef fun worst_heuristic_cost
do return 0
341 # A `Link` with a `weight`
345 # The `weight`, or cost, of this link
349 # Advanced path conditions with customizable accept states
350 class TargetCondition[N
: Node]
351 # Should the pathfinding accept `node` as a goal?
352 fun accept
(node
: N
): Bool is abstract
354 # Approximate cost from `node` to an accept state
355 fun heuristic_cost
(node
: N
, link
: Link): Int is abstract