Finding Explanations
%%John take book problem:Finding Explanations
const n=2.
step(0..n).
#domain step(T).
fluent(i,locked(room)).
fluent(i,dark(room)).
fluent(i,at_hand(book)).
action(use_key_unlock(room)).
action(take(book)).
action(turn_on(light)).
x_action(brk).
x_action(inspect).
act(A):- action(A).
act(A):- x_action(A).
goal(T):-
h(at_hand(book),T).
goal:-
goal(T).
%:- not goal.
%%Initial state
h(locked(room),0).
h(dark(room),0).
-h(at_hand(book),0).
%%Dynamic causal law
-h(locked(room),T+1):-
o(use_key_unlock(room),T),
T<n.
h(at_hand(book),T+1):-
o(take(book),T),
T<n.
-h(dark(room),T+1):-
o(turn_on(light),T),
T<n.
%This rule is added to find the EXPLANATION
h(dark(room),T+1):-
o(brk,T),
T<n.
h(dark(room),T+1):-
o(inspect,T),
T<n.
%%Executability condition
%%I try to make sure that all that actions happen in the reasonalable order.
%%Therefore, I add the following rules to guarantee the right order of actions.
:- -h(locked(room),T),
o(use_key_unlock(room),T).
:- h(locked(room),T),
o(take(book),T).
:- h(at_hand(book),T),
o(take(book),T).
:- -h(dark(room),T),
o(turn_on(light),T).
:- h(dark(room),T),
o(take(book),T).
%%Inertial Axiom/Default
h(F,T+1):-
fluent(i,F),
h(F,T),
not -h(F,T+1).
-h(F,T+1):-
fluent(i,F),
-h(F,T),
not h(F,T+1).
%suppose the room was still dark after John turned on the light.
%suppose the possible reason is 1)that the light was broken by someone2)that the operator was inspecting
%the wire and cable, leading to no electricity
%reality check
:-obs(F,false,T),
h(F,T),
fluent(F).
:-obs(F,true,T),
-h(F,T),
fluent(F).
%h(F,T):- obs(F,true,T).% SHOULD BE 0 !!
%-h(F,T):- obs(F,false,T).
%Full awareness axiom
h(F,0)|-h(F,0):- fluent(i,F).
o(A,T):- hpd(A,T),
act(A).
%observe the world, update the knowledge base
hpd(use_key_unlock(room),0).
hpd(turn_on(light),1).
obs(dark(room),true,2).
const m=1.
step(0..n+m).
%{o(A,T): action(A)}:- T<n+m. %planning module
{o(A,T): x_action(A)}:- T<n. %diagnostic module
explain(A,T):- x_action(A),
o(A,T),
not hpd(A,T).
hide. show explain(A,T).
