From: Tim Vieira Date: Tue, 2 Jul 2013 21:48:27 +0000 (-0400) Subject: added example of optimal path extraction for shortest path. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=caf91193b7f473d08b9e03f1b031e0dc5432497b;p=dyna2 added example of optimal path extraction for shortest path. --- diff --git a/examples/dijkstra-backpointers.dyna b/examples/dijkstra-backpointers.dyna new file mode 100644 index 0000000..8c950b6 --- /dev/null +++ b/examples/dijkstra-backpointers.dyna @@ -0,0 +1,37 @@ +% single source shortest path with optimal path extraction. +path(start) min= 0. +path(B) min= path(A) + edge(A,B). +goal min= path(end). + +% expensive path +edge("a","b") := 1. +edge("b","c") := 1. +edge("c","d") := 1. + +% cheap path +edge("a","e") := 1. +edge("e","d") := 1. + +% define begining and end. +start := "a". +end := "d". + +% use argmin to determine backpointers +b(V) argmin= [path(U) + edge(U,V), U]. + +% extract cheapest path by following backpointers from each vertex. +bestpath(start) := [start]. +bestpath(V) := U is b(V), [V | bestpath(U)]. + +% the optimal path is the one from the `end`. +optimalpath = reverse(bestpath(end)). + +% --------------------------------------- +% list utilities +:- backchain reverse/1. +reverse([]) = []. +reverse([X|Xs]) = append(reverse(Xs), X). + +:- backchain append/2. +append([], Y) = [Y]. +append([X|Xs], Y) = [X|append(Xs, Y)].