/* Railroad switching problem in Picat. Problem fromStefan Edelkamp, Stefan Schrödl: "Heuristic Search: Theory and Applications, page ff" Note: This use a graph representation. Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import planner. import bplan. main => go. % planner go => initial_state(Init), time(planner.best_plan(Init,Plan)), print_plan(Plan), write(len=Plan.length),nl. % % planner for optimal path % and then bplan for all optimal paths % go2 => % first find the optimal plan initial_state(Init), time(planner.best_plan(Init,Plan1)), % then find all optimal plans P = new_list(Plan1.length), All=findall(P,bplan.plan(P)), foreach(Plan in All) print_plan(Plan) end, writeln(len=All.length), nl. print_plan(Plan) => foreach([From,To] in Plan) printf("%w -> %w\n", From,To) end, nl. index(-) initial_state([e,a,b]). index(-) final([e,b,a]). % for bplan index(-) goal_state([e,b,a]). action(From,To,Move,Cost) ?=> graph2(From,To,_), Move = [From,To], Cost = 1. % for bplan legal_move(From,Move,To) => action(From,To,Move,_). % % This is the graph from figure 1.4 (page 13) in "Heuristic Search..." % (This is an directed graph.) index(-,-,-) graph([e,a,b],[b,a,e],eab_bae). graph([b,a,e],[b,e,a],bae_bea). graph([b,a,e],[a,e,b],bae_aeb). graph([b,e,a],[a,b,e],bea_abe). graph([a,b,e],[a,e,b],abe_aeb). graph([a,b,e],[e,b,a],abe_eba). % Make the graph undirected graph2(From,To,Move) ?=> graph(From,To,Move). graph2(From,To,Move) ?=> graph(To,From, Move).