/* Water jugs problem in Picat. This is a port from B-Prologs example planning/water.pl Original note: "the only thing changed is from "top" -> "go", otherwise it's exactly the same as the B-Prolog code. " Later note: Added the current water levels in the actions. The solution is: fill1 = (8,0) transfer_1_to_2 = (3,5) empty2 = (3,0) transfer_1_to_2 = (0,3) fill1 = (8,3) transfer_1_to_2 = (6,5) empty2 = (6,0) transfer_1_to_2 = (1,5) empty2 = (1,0) transfer_1_to_2 = (0,1) fill1 = (8,1) transfer_1_to_2 = (4,5) This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import planner. main => go. go => nolog, best_plan([0,0],Plan), foreach(P in Plan) writeln(P) end, writeln(len=Plan.len). final([4,_]) ?=> true. final([_,4]) => true. % fill1 action([_V1,V2],S1,Action,Cost) ?=> Cost=1, capacity(1,C), Action = $fill1=(C,V2), S1 = [C,V2]. % fill2 action([V1,_V2],S1,Action,Cost) ?=> Cost=1, capacity(2,C), Action = $fill2=(V1,C), S1 = [V1,C]. % empty1 action([V1,V2],S1,Action,Cost),V1>0 ?=> Action = $empty1=(0,V2), Cost=1, S1 = [0,V2]. % empty2 action([V1,V2],S1,Action,Cost),V2>0 ?=> Action = $empty2=(V1,0), Cost=1, S1 = [V1,0]. % transfer_2_to_1 action([V1,V2],S1,Action,Cost),V2>0 ?=> Cost=1, capcity(1,C1), Liquid is V1+V2, Excess is Liquid-C1, (Excess=<0-> W1=Liquid,W2=0 ; W1=C1,W2=Excess ), Action = $transfer_2_to_1=(W1,W2), S1=[W1,W2]. % transfer_1_to_2 action([V1,V2],S1,Action,Cost),V1>0 => Cost=1, capcity(2,C2), Liquid is V1+V2, Excess is Liquid-C2, (Excess=<0-> W2=Liquid,W1=0 ; W2=C2,W1=Excess ), Action = $transfer_1_to_2=(W1,W2), S1=[W1,W2]. capacity(1,C1) => C1=8. capcity(2,C2) => C2=5.