soundbyte
soundbyte

Reputation: 1

Prolog constraint solver

I'm working on the following problem in Prolog:

Five patients, all having blood tests, are waiting in the doctor's surgery and are sitting on a bench from left to right, where the leftmost position is the first. Determine the position of each patient, along with her or his blood group, age, height, and weight. Their ages are 5, 9, 30, 46 and 60. Their heights are 40, 48, 60, 65 and 74. Their weights are 40, 75, 96, 125 and 165.

  1. The person on the far right is 37 years older than Leila, and is 60 inches tall.
  2. Leila weighs 56 pounds more than her height.
  3. Alice weighs 75 pounds and is 74 inches tall.
  4. Ali is type AB and weighs 56 pounds less than Leila.
  5. The person in the centre is 9 years old, is blood type AO and weighs 96 pounds.
  6. Adam, who is the first, is 65 inches tall, and weighs 100 pounds more than his height.
  7. The person who is blood type O, is 25 years older than the person to the left of them.
  8. Farah is 60 years old.
  9. The person who is blood type A, is 55 years younger than Farah, and 17 inches taller than Farah.
  10. The person who is next to the 9 year old but not next to the person who is 65 inches tall, is blood type B, and weighs 125 pounds.

Define the predicate patients(Table) where Table is a table, where the rows from 1 to 5 describe the people sitting on the bench in left to right order. The columns are in the sequence person’s name, blood group, age, height and weight. An example value for Table could be Table = [['Adam', 'A', 5, 40, 40], ['Ali', 'AB', 9, 48, 75]], ['Alice', 'AO', 30, 60, 96], ['Farah', 'B', 46, 65, 125], ['Leila', 'O', 60, 74, 165]].

I think I am very close to the solution, though my program isn't running properly. I'll paste my code below:

:- op(100,xfy,on).

age(5). age(9). age(30). age(46). age(60).
height(40). height(48). height(60). height(65). height(74).
weight(40). weight(75). weight(96). weight(125). weight(165).

patients(Table) :-

    makebench(5, Table),
    Table = [_, _, _, _, [_, _, Age5, 60, _]],
    ['Leila', _, AgeLeila, HeightLeila, WeightLeila] on Table,
    ['Alice', _, _, 74, 75] on Table,
    ['Ali', 'AB', _, _, WeightAli] on Table,
    Table = [_, _, [_, 'AO', 9, _, 96], _, _],
    Table = [['Adam', _, _, 65, 165], _, _, _, _],
    rightof([_, 'O', A, _, _], [_, _, A1, _, _], Table),
    ['Farah', _, 60, HeightFarah, _] on Table,
    [_, 'A', 5, H, _] on Table,
    Table = [_, _, _, [_, 'B', _, _, 125], _],

{   Age5 = AgeLeila + 37,
    WeightLeila = HeightLeila + 56,
    WeightAli = WeightLeila - 56,
    A = A1 + 25,
    H = HeightFarah + 17,
    age(Age5),
    age(AgeLeila),
    weight(WeightLeila),
    height(HeightLeila),
    weight(WeightAli),
    height(HeightFarah)}.

makebench(0, []).

makebench(N, [[_, _, _, _, _]|List]) :-
    N > 0, N1 is N - 1, makebench(N1,List).

X on [X | _].
X on [_ | R] :- X on R.

sublist(S, L) :- add(S, _, L).
sublist(S, [_ | T]) :- sublist(S, T).

add([], L, L).
add([X | R], Y, [X | T]) :- add(R,Y,T).

rightof(H1, H2, L) :- sublist([H2, H1], L).

Any help is appreciated.

Upvotes: 0

Views: 164

Answers (1)

soundbyte
soundbyte

Reputation: 1

I figured out the problem: I had to load the clpr library and remove the age(), height() and weight() terms from the constraints. Actually, I removed them entirely because they weren't needed.

Upvotes: 0

Related Questions