| 1 | |
|---|
| 2 | :- object(miss_cann, |
|---|
| 3 | instantiates(heuristic_state_space)). |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | :- info([ |
|---|
| 7 | version is 1.1, |
|---|
| 8 | author is 'Paulo Moura', |
|---|
| 9 | date is 2000/11/21, |
|---|
| 10 | comment is 'Missionaries and cannibals heuristic state space search problem.']). |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | :- uses(loop, [forto/3]). |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | initial_state(start, ((3,3), left, (0,0))). |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | goal_state(end, ((0,0), right, (3,3))). |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | print_state(((Me,Ce), B, (Md,Cd))) :- |
|---|
| 23 | forto(1, Me, write('M')), |
|---|
| 24 | forto(1, Ce, write('C')), |
|---|
| 25 | (B = left -> |
|---|
| 26 | write('.<__>..........') |
|---|
| 27 | ; |
|---|
| 28 | write('..........<__>.')), |
|---|
| 29 | forto(1, Md, write('M')), |
|---|
| 30 | forto(1, Cd, write('C')), |
|---|
| 31 | nl. |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | next_state(((Me,Ce),left,(Md,Cd)), ((Me2,Ce2),right,(Md2,Cd2)), 1) :- %mm |
|---|
| 35 | Me >= 2, |
|---|
| 36 | once((Me - 2 =:= 0; Me - 2 >= Ce)), |
|---|
| 37 | Cd =< 2, |
|---|
| 38 | Me2 is Me - 2, |
|---|
| 39 | Ce2 is Ce, |
|---|
| 40 | Md2 is Md + 2, |
|---|
| 41 | Cd2 is Cd. |
|---|
| 42 | |
|---|
| 43 | next_state(((Me,Ce),left,(Md,Cd)), ((Me2,Ce2),right,(Md2,Cd2)), 2) :- %m |
|---|
| 44 | Me >= 1, |
|---|
| 45 | once((Me - 1 =:= 0; Me - 1 >= Ce)), |
|---|
| 46 | Cd =< 1, |
|---|
| 47 | Me2 is Me - 1, |
|---|
| 48 | Ce2 is Ce, |
|---|
| 49 | Md2 is Md + 1, |
|---|
| 50 | Cd2 is Cd. |
|---|
| 51 | |
|---|
| 52 | next_state(((Me,Ce),left,(Md,Cd)), ((Me2,Ce2),right,(Md2,Cd2)), 1) :- %cc |
|---|
| 53 | Ce >= 2, |
|---|
| 54 | once((Md >= Cd + 2; Md =:= 0)), |
|---|
| 55 | Me2 is Me, |
|---|
| 56 | Ce2 is Ce - 2, |
|---|
| 57 | Md2 is Md, |
|---|
| 58 | Cd2 is Cd + 2. |
|---|
| 59 | |
|---|
| 60 | next_state(((Me,Ce),left,(Md,Cd)), ((Me2,Ce2),right,(Md2,Cd2)), 2) :- %c |
|---|
| 61 | Ce >= 1, |
|---|
| 62 | once((Md >= Cd + 1; Md =:= 0)), |
|---|
| 63 | Me2 is Me, |
|---|
| 64 | Ce2 is Ce - 1, |
|---|
| 65 | Md2 is Md, |
|---|
| 66 | Cd2 is Cd + 1. |
|---|
| 67 | |
|---|
| 68 | next_state(((Me,Ce),left,(Md,Cd)), ((Me2,Ce2),right,(Md2,Cd2)), 1) :- %mc |
|---|
| 69 | Me >= 1, |
|---|
| 70 | Ce >= 1, |
|---|
| 71 | Md >= Cd, |
|---|
| 72 | Me2 is Me - 1, |
|---|
| 73 | Ce2 is Ce - 1, |
|---|
| 74 | Md2 is Md + 1, |
|---|
| 75 | Cd2 is Cd + 1. |
|---|
| 76 | |
|---|
| 77 | next_state(((Me,Ce),right,(Md,Cd)), ((Me2,Ce2),left,(Md2,Cd2)), Cost) :- |
|---|
| 78 | next_state(((Md,Cd),left,(Me,Ce)), ((Md2,Cd2),right,(Me2,Ce2)), Cost). |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | heuristic(((_, _), _, (Md, Cd)), Cost) :- |
|---|
| 82 | Cost is 6 - (Md + Cd). |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | :- end_object. |
|---|