Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
1
Indexable
Never
% WORLD
#const t=20.
time(1..t).
item(cannibal(1..3); priest(1..3)).
item(empty). 
boat. 
side(left).
side(right).
otherside(S1,S2) :- side(S1), side(S2).

% INITIAL WORLD
at(1, I, left) :- item(I).
at(1, boat, left) :- boat.
at(T, item(empty), left) :- item(empty), time(T). 
at(T, item(empty), right) :- item(empty), time(T). 

% ACTIONS
action(move(I1,I2)) :- item(I1), item(I2).
action(wait). 

% ALL POSSIBLE ACTIONS
1 { do(A,T) : action(A) } 1 :- time(T), T < t.

% WHAT HAPPENS TO THINGS WHEN ACTIONS ARE DONE
being_transferred(T, I) :- do(move(I, _), T).
being_transferred(T, I) :- do(move(_, I), T).
% MOVE
at(T+1, I1, S1) :- being_transferred(T,I1), time(T), item(I1), at(T, I1, S2), otherside(S1, S2).
at(T+1, boat, S1) :- being_transferred(T,I1), time(T), item(I1), at(T, boat, S2), otherside(S1, S2).
% WAIT
at(T+1, I, S1) :- do(wait, T), time(T), item(I), at(T, I, S1).
at(T+1, boat, S1) :- do(wait, T), time(T), at(T, boat, S1).

% NUMBER OF CANIBALS/PRIESTS AT EACH SIDE
num_C(T,S,N) :- time(T), side(S), N = #count {I : at(T, item(cannibal(I)), S) }.
num_P(T,S,N) :- time(T), side(S), N = #count {I : at(T, item(priest(I)), S) }.
% CONSTRAINTS 
:- being_transferred(T,I), at(T, I, S1), item(I), at(T, boat, S2), otherside(S1,S2).
:- side(S), num_C(T,S,N), num_P(T,S,N), time(T), num_P>0, num_C>num_P.

% GOAL REACHED
goal_reached(T) :- time(T), at(T,boat,right), at(T,I,right) : item(I).
:- do (T , wait ) , not do ( T +1 , wait ) , time ( T ) , time ( T +1) , T +1 < t.
:- not goal_reached(t). 

#show do/2.