| 1 | /* |
|---|
| 2 | Salt state-space search problem |
|---|
| 3 | |
|---|
| 4 | 2003 Portuguese National Logical Programming Contest problem |
|---|
| 5 | http://paginas.fe.up.pt/~eol/LP/0304/documents/Exercicios_CNPL.PDF |
|---|
| 6 | |
|---|
| 7 | Introduction: |
|---|
| 8 | Mr Silva sells salt. He has to measure the quantity requested by his |
|---|
| 9 | customers by using two measures and an accumulator. Neither has any |
|---|
| 10 | measuring markers. Those measures can easily be broken and he has to |
|---|
| 11 | replace them each time it happens. More, a substitution can be made |
|---|
| 12 | by a measure with a different capacity than the one being replaced. |
|---|
| 13 | |
|---|
| 14 | Objective: |
|---|
| 15 | To produce a program, given the capacity of two measures and the |
|---|
| 16 | intended quantity, which helps Mr. Silva knowing if it is possible |
|---|
| 17 | to obtain the amount requested by his customer, and if so, measuring |
|---|
| 18 | the intended quantity in the least amount of steps. |
|---|
| 19 | |
|---|
| 20 | Remarks: |
|---|
| 21 | This problem is similar to the Water Jug's' problem. It is more general, |
|---|
| 22 | seeing that the Water Jug's problem uses static values for the jugs |
|---|
| 23 | capacities and the final goal. |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | :- object(salt(_Acumulator, _Measure1, _Measure2), |
|---|
| 28 | instantiates(heuristic_state_space)). |
|---|
| 29 | |
|---|
| 30 | :- info([ |
|---|
| 31 | version is 1.1, |
|---|
| 32 | author is 'Paula Marisa Sampaio', |
|---|
| 33 | date is 2008/6/9, |
|---|
| 34 | comment is 'Salt state-space search problem (updated from the original 1.0 version to support heuristics).']). |
|---|
| 35 | |
|---|
| 36 | % each state is represented by a compound term with four arguments: (Acumulator, Measure1, Measure2, Step) |
|---|
| 37 | initial_state(initial, (0, 0, 0, all_empty)). |
|---|
| 38 | |
|---|
| 39 | % the intended salt quantity must end up on the acumulator |
|---|
| 40 | goal_state(acumulator, (Acumulator, _, _, _)) :- |
|---|
| 41 | parameter(1, Acumulator). |
|---|
| 42 | |
|---|
| 43 | % state transitions: |
|---|
| 44 | |
|---|
| 45 | % emptying a measure into the accumulator |
|---|
| 46 | next_state((Acc, X, Y, _), (NewAcc, 0, Y, transfer(m1, acc)), 1) :- |
|---|
| 47 | X > 0, |
|---|
| 48 | NewAcc is Acc + X. |
|---|
| 49 | next_state((Acc, X, Y, _), (NewAcc, X, 0, transfer(m2, acc)), 1) :- |
|---|
| 50 | Y > 0, |
|---|
| 51 | NewAcc is Acc + Y. |
|---|
| 52 | |
|---|
| 53 | % filling up of one of the measures |
|---|
| 54 | next_state((Acc, X, Y, Step), (Acc, MaxX, Y, fill(m1)), 1) :- |
|---|
| 55 | parameter(2, MaxX), |
|---|
| 56 | X < MaxX, |
|---|
| 57 | Step \= empty(m1). |
|---|
| 58 | next_state((Acc, X, Y, Step), (Acc, X, MaxY, fill(m2)), 1) :- |
|---|
| 59 | parameter(3, MaxY), |
|---|
| 60 | Y < MaxY, |
|---|
| 61 | Step \= empty(m2). |
|---|
| 62 | |
|---|
| 63 | % either pouring of a measure into the other till it is filled up |
|---|
| 64 | % or all content of a measure into the other one |
|---|
| 65 | next_state((Acc, X, Y, _), (Acc, W, Z, transfer(m2, m1)), 1) :- |
|---|
| 66 | parameter(2, MaxX), |
|---|
| 67 | Y > 0, |
|---|
| 68 | X < MaxX, |
|---|
| 69 | (X + Y >= MaxX -> |
|---|
| 70 | W = MaxX, |
|---|
| 71 | Z is Y - (MaxX - X) |
|---|
| 72 | ; |
|---|
| 73 | W is X + Y, |
|---|
| 74 | Z = 0 |
|---|
| 75 | ). |
|---|
| 76 | next_state((Acc, X, Y, _), (Acc, W, Z, transfer(m1, m2)), 1) :- |
|---|
| 77 | parameter(3, MaxY), |
|---|
| 78 | X > 0, |
|---|
| 79 | Y < MaxY, |
|---|
| 80 | (X + Y >= MaxY -> |
|---|
| 81 | W is X - (MaxY - Y), |
|---|
| 82 | Z = MaxY |
|---|
| 83 | ; |
|---|
| 84 | W = 0, |
|---|
| 85 | Z is X + Y |
|---|
| 86 | ). |
|---|
| 87 | |
|---|
| 88 | % throwing out the contents of a measure; does not afect the accumulator |
|---|
| 89 | next_state((Acc, X, Y, Step), (Acc, 0, Y, empty(m1)), 1) :- |
|---|
| 90 | X > 0, |
|---|
| 91 | Step \= fill(m1). |
|---|
| 92 | next_state((Acc, X, Y, Step), (Acc, X, 0, empty(m2)), 1) :- |
|---|
| 93 | Y > 0, |
|---|
| 94 | Step \= fill(m2). |
|---|
| 95 | |
|---|
| 96 | heuristic((Acc, Acc, _, _), 0.1) :- |
|---|
| 97 | parameter(1, Acc), |
|---|
| 98 | !. |
|---|
| 99 | heuristic((Acc, _, Acc, _), 0.1) :- |
|---|
| 100 | parameter(1, Acc), |
|---|
| 101 | !. |
|---|
| 102 | heuristic((Acc, X, Y, _), 0.2) :- |
|---|
| 103 | parameter(1, Acc), |
|---|
| 104 | Acc is abs(X - Y), |
|---|
| 105 | !. |
|---|
| 106 | heuristic((Acc, X, _, _), 0.3) :- |
|---|
| 107 | parameter(1, Acc), |
|---|
| 108 | ( X mod Acc =:= 0 -> |
|---|
| 109 | Cost is X // Acc |
|---|
| 110 | ; Acc mod X =:= 0 -> |
|---|
| 111 | Cost is Acc // X |
|---|
| 112 | ), |
|---|
| 113 | !. |
|---|
| 114 | heuristic((Acc, _, Y, _), 0.3) :- |
|---|
| 115 | parameter(1, Acc), |
|---|
| 116 | ( Y mod Acc =:= 0 -> |
|---|
| 117 | Cost is Y // Acc |
|---|
| 118 | ; Acc mod Y =:= 0 -> |
|---|
| 119 | Cost is Acc // Y |
|---|
| 120 | ), |
|---|
| 121 | !. |
|---|
| 122 | heuristic((Acc, X, Y, _), 0.4) :- |
|---|
| 123 | parameter(1, Acc), |
|---|
| 124 | Diff is abs(X - Y), |
|---|
| 125 | ( Diff mod Acc =:= 0 -> |
|---|
| 126 | Cost is Diff // Acc |
|---|
| 127 | ; Acc mod Diff =:= 0 -> |
|---|
| 128 | Cost is Acc // Diff |
|---|
| 129 | ), |
|---|
| 130 | !. |
|---|
| 131 | heuristic((_, _, _, _), 0.5). |
|---|
| 132 | |
|---|
| 133 | member_path((Acc, X, Y, _), [(Acc, X, Y, _)| _]) :- |
|---|
| 134 | !. |
|---|
| 135 | member_path(State, [_| Path]) :- |
|---|
| 136 | member_path(State, Path). |
|---|
| 137 | |
|---|
| 138 | print_state((Acc, X, Y, Step)) :- |
|---|
| 139 | write('('), write((Acc, X, Y)), write(') '), write(Step), nl. |
|---|
| 140 | |
|---|
| 141 | :- end_object. |
|---|