additional handler for LazyGraph algorithms that search for paths

This commit is contained in:
Simon Cruanes 2013-03-30 14:40:44 +01:00
parent ddd3175185
commit 69d75de295
2 changed files with 10 additions and 3 deletions

View file

@ -350,7 +350,9 @@ and ('id, 'e) came_from_edge =
- consistent (ie, h(X) <= dist(X,Y) + h(Y)). - consistent (ie, h(X) <= dist(X,Y) + h(Y)).
Both the distance and the heuristic must always Both the distance and the heuristic must always
be positive or null. *) be positive or null. *)
let a_star graph ?(ignore=fun v -> false) let a_star graph
?(on_explore=fun v -> ())
?(ignore=fun v -> false)
?(heuristic=(fun v -> 0.)) ?(heuristic=(fun v -> 0.))
?(distance=(fun v1 e v2 -> 1.)) ?(distance=(fun v1 e v2 -> 1.))
~goal ~goal
@ -373,6 +375,8 @@ let a_star graph ?(ignore=fun v -> false)
(* data for this vertex *) (* data for this vertex *)
let cell = nodes.map_get v' in let cell = nodes.map_get v' in
if (not cell.cf_explored) || ignore v' then begin if (not cell.cf_explored) || ignore v' then begin
(* 'explore' the node *)
on_explore v';
cell.cf_explored <- true; cell.cf_explored <- true;
match graph.force v' with match graph.force v' with
| Empty -> next () | Empty -> next ()
@ -420,9 +424,10 @@ let a_star graph ?(ignore=fun v -> false)
(** Shortest path from the first node to the second one, according (** Shortest path from the first node to the second one, according
to the given (positive!) distance function. The path is reversed, to the given (positive!) distance function. The path is reversed,
ie, from the destination to the source. The int is the distance. *) ie, from the destination to the source. The int is the distance. *)
let dijkstra graph ?(ignore=fun v -> false) ?(distance=fun v1 e v2 -> 1.) v1 v2 = let dijkstra graph ?on_explore ?(ignore=fun v -> false)
?(distance=fun v1 e v2 -> 1.) v1 v2 =
let paths = let paths =
a_star graph ~ignore ~distance ~heuristic:(fun _ -> 0.) a_star graph ?on_explore ~ignore ~distance ~heuristic:(fun _ -> 0.)
~goal:(fun v -> graph.eq v v2) ~start:v1 in ~goal:(fun v -> graph.eq v v2) ~start:v1 in
let paths = Gen.start paths in let paths = Gen.start paths in
try try

View file

@ -130,6 +130,7 @@ val dfs : ('id, 'v, 'e) t -> 'id -> ('id * 'v * int) Gen.t
(** Lazy traversal in depth first *) (** Lazy traversal in depth first *)
val a_star : ('id, 'v, 'e) t -> val a_star : ('id, 'v, 'e) t ->
?on_explore:('id -> unit) ->
?ignore:('id -> bool) -> ?ignore:('id -> bool) ->
?heuristic:('id -> float) -> ?heuristic:('id -> float) ->
?distance:('id -> 'e -> 'id -> float) -> ?distance:('id -> 'e -> 'id -> float) ->
@ -146,6 +147,7 @@ val a_star : ('id, 'v, 'e) t ->
be positive or null. *) be positive or null. *)
val dijkstra : ('id, 'v, 'e) t -> val dijkstra : ('id, 'v, 'e) t ->
?on_explore:('id -> unit) ->
?ignore:('id -> bool) -> ?ignore:('id -> bool) ->
?distance:('id -> 'e -> 'id -> float) -> ?distance:('id -> 'e -> 'id -> float) ->
'id -> 'id -> 'id -> 'id ->