From 69d75de295f42a88d3717861a2ed24bf6fa963c9 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 30 Mar 2013 14:40:44 +0100 Subject: [PATCH] additional handler for LazyGraph algorithms that search for paths --- lazyGraph.ml | 11 ++++++++--- lazyGraph.mli | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lazyGraph.ml b/lazyGraph.ml index b6ec8fce..318914c6 100644 --- a/lazyGraph.ml +++ b/lazyGraph.ml @@ -350,7 +350,9 @@ and ('id, 'e) came_from_edge = - consistent (ie, h(X) <= dist(X,Y) + h(Y)). Both the distance and the heuristic must always 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.)) ?(distance=(fun v1 e v2 -> 1.)) ~goal @@ -373,6 +375,8 @@ let a_star graph ?(ignore=fun v -> false) (* data for this vertex *) let cell = nodes.map_get v' in if (not cell.cf_explored) || ignore v' then begin + (* 'explore' the node *) + on_explore v'; cell.cf_explored <- true; match graph.force v' with | 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 to the given (positive!) distance function. The path is reversed, 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 = - 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 let paths = Gen.start paths in try diff --git a/lazyGraph.mli b/lazyGraph.mli index 34f56bc1..652b15d8 100644 --- a/lazyGraph.mli +++ b/lazyGraph.mli @@ -130,6 +130,7 @@ val dfs : ('id, 'v, 'e) t -> 'id -> ('id * 'v * int) Gen.t (** Lazy traversal in depth first *) val a_star : ('id, 'v, 'e) t -> + ?on_explore:('id -> unit) -> ?ignore:('id -> bool) -> ?heuristic:('id -> float) -> ?distance:('id -> 'e -> 'id -> float) -> @@ -146,6 +147,7 @@ val a_star : ('id, 'v, 'e) t -> be positive or null. *) val dijkstra : ('id, 'v, 'e) t -> + ?on_explore:('id -> unit) -> ?ignore:('id -> bool) -> ?distance:('id -> 'e -> 'id -> float) -> 'id -> 'id ->