Skip to content

Commit

Permalink
a2Q1 finished
Browse files Browse the repository at this point in the history
  • Loading branch information
QiaosongDeng committed Oct 10, 2022
1 parent 9189423 commit 8da7179
Showing 1 changed file with 148 additions and 13 deletions.
161 changes: 148 additions & 13 deletions assignment2/a2.lp
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
%Instruction:
%type clingo a2.lp -c n="steps constraint" in the command line.
%special rule:
%monkey can open door or close door when carrying the chair.


%Test case
room(r1,p1). room(r1,p2). room(r1,p3). room(r1,p4). % 4 locations in room r1
room(r2,q1). room(r2,q2). % 2 locatoins in room r2
at(monkey,r1,p1,0). %monkey is in room r1 at location p2 initially
%at(chair, r2,q1,0). %chair is in room r2 at location q1 initially
%at(bananas,r1,p4,0). %bananas are in room r1 at p4 initially
at(monkey,r1,p2,0). %monkey is in room r1 at location p2 initially
at(chair, r2,q1,0). %chair is in room r2 at location q1 initially
at(bananas,r1,p4,0). %bananas are in room r1 at p4 initially
door(r1,p2,r2,q1,closed,0). %there is a door adjacent p2 of r1 and q1 of r2 which is closed initially (p2 and q1 are "door locations")

#show walk/5.
#show walk/6.
#show at/4.
%#show at/4.
#show openDoor/6.
#show closeDoor/6.
#show pick_up/4.
#show put_down/4.
#show climb/4.
#show bananas/2.
%#show door/6.
%#show goal/1.
%#show goal/0.
%#show chair/3.
%#show chair/2.

time(0..5).
time(0..n).%type clingo a2.lp -c n="steps constraint" in the command line.


% actions
Expand All @@ -30,6 +41,9 @@ time(0..5).
at(monkey,Room,P1,T),
not opening(monkey,T), %monkey cant move when he is busy
not closing(monkey,T),
not pickingUp(monkey,T),
not puttingDown(monkey,T),
not climbing(monkey,T),
not goal(T).

{openDoor(monkey, Room1, P1, Room2, P2, T)}:-
Expand All @@ -41,18 +55,25 @@ time(0..5).
door(Room1, P1, Room2, P2, closed, T),
at(monkey,Room1,P1,T),
not closing(monkey,T),
not pickingUp(monkey,T),
not puttingDown(monkey,T),
not climbing(monkey,T),
not goal(T).

{walk(monkey,Room1, P1, Room2, P2, T)}:-
%monkey walk from (Room1, P1) to (Room2, P2)
room(Room1, P1),
room(Room2, P2),
Room1 != Room2,
time(T),

door(Room1, P1, Room2, P2,opened,T),
at(monkey,Room1,P1,T),
not opening(monkey,T), %monkey cant move when he is busy
not closing(monkey,T),
not pickingUp(monkey,T),
not puttingDown(monkey,T),
not climbing(monkey,T),
not goal(T).

{closeDoor(monkey, Room1, P1, Room2, P2, T)}:-
Expand All @@ -64,18 +85,56 @@ time(0..5).
door(Room1, P1, Room2, P2, opened, T),
at(monkey,Room1,P1,T),
not opening(monkey,T),
not pickingUp(monkey,T),
not puttingDown(monkey,T),
not climbing(monkey,T),
not goal(T).


%pick_up(monkey,Room,P,T):-
{pick_up(monkey,Room,P,T)}:-
%monkey picks up the chair, both of which are at position P of Room, at T.
room(Room, P),
time(T),

at(monkey, Room, P, T),
at(chair, Room, P, T),
chair(Room,P,T),
not opening(monkey,T),
not closing(monkey,T),
not puttingDown(monkey,T),
not climbing(monkey,T),
not goal(T).


%put_down(monkey,Room,P,T):-
{put_down(monkey,Room,P,T)}:-
%monkey puts down the chair at position P of Room, at T.
room(Room, P),
time(T),

%climb(monkey, Room,P, T):-
%monkey climbs to the chair, both of which are at position P of Room at T.
at(monkey, Room, P, T),
at(chair, Room, P, T),
chair(on_monkey,T),
not opening(monkey,T),
not closing(monkey,T),
not pickingUp(monkey,T),
not climbing(monkey,T),
not goal(T).

{climb(monkey, Room,P, T)}:-
%monkey climbs to the chair, both of which are at position P of Room at T.
room(Room,P),
time(T),

at(monkey, Room, P, T),
at(chair, Room, P, T),
chair(Room,P,T),
at(bananas, Room, P,T),
not opening(monkey,T),
not closing(monkey,T),
not puttingDown(monkey,T),
not pickingUp(monkey,T),
not surroundingDoorsOpened(Room,T),
not goal(T).



Expand All @@ -85,7 +144,16 @@ opening(monkey,T):-
openDoor(monkey, _, _, _, _, T).

closing(monkey,T):-
closeDoor(monkey, _, _, _, _, T).
closeDoor(monkey, _, _, _, _, T).

pickingUp(monkey,T):-
pick_up(monkey, _, _, T).

puttingDown(monkey,T):-
put_down(monkey, _, _, T).

climbing(monkey,T):-
climb(monkey, _, _, T).

at(monkey,Room,P2,T+1):-
%change's monkey position when monkey walks from p1 to p2 at the same room
Expand Down Expand Up @@ -142,6 +210,35 @@ at(monkey, Room, Place, T+1):-
at(monkey, Room, Place, T),
closing(monkey,T).

at(monkey, Room, Place, T+1):-
%monkey stays at the same place when picking up chair.
at(monkey, Room, Place, T),
pickingUp(monkey,T).

at(monkey, Room, Place, T+1):-
%monkey stays at the same place when putting Down chair.
at(monkey, Room, Place, T),
puttingDown(monkey,T).

at(chair, Room, Place, T):-
chair(on_monkey, T),
at(monkey, Room, Place, T).

at(chair, Room, Place, T):-
chair(Room, Place, T).

at(monkey, Room, Place, T+1):-
%monkey stays at the same place when climbing.
at(monkey, Room, Place, T),
climbing(monkey,T).

at(bananas, Room, Place, T+1):-
%bananas stays at the same place if monkey doesn't reach it.
time(T),
at(bananas, Room, Place, T).



door(Room1, P1, Room2, P2, closed, T+1):-
%change the door's property if the door is closed.
room(Room1,P1),
Expand All @@ -164,7 +261,7 @@ door(Room1, P1, Room2, P2, opened, T+1):-
not goal(T).%if goal is achieved, dont keep anymore

door(Room1, P1, Room2, P2, closed, T+1):-
%keep door closed in the Time(T+1) if it is not openend now.
%keep door closed in the Time(T+1) if it is not opened now.
room(Room1,P1),
room(Room2,P2),
time(T),
Expand All @@ -174,10 +271,48 @@ door(Room1, P1, Room2, P2, closed, T+1):-
not openDoor(monkey, Room2, P2, Room1, P1, T),
not goal(T).%if goal is achieved, dont keep anymore

chair(on_monkey,T+1):-
time(T),

pick_up(monkey, _, _,T).

chair(on_monkey,T+1):-
time(T),

chair(on_monkey, T),
not put_down(monkey, _, _,T).

chair(Room, P, T+1):-
time(T),

put_down(monkey, Room, P, T).

chair(Room, P, T+1):-
time(T),

chair(Room, P, T),
not pick_up(monkey, Room, P,T).

chair(Room, P, T):-
time(T),

at(chair, Room, P,T),
not chair(on_monkey, T).

bananas(on_monkey, T+1):-

climbing(monkey,T).

surroundingDoorsOpened(Room,T):-
door(Room, _, _, _, opened, T).


%dont set a action as a goal.
goal(T) :-
at(monkey,r2,q2,T).
%at(monkey,r1,p4,T),
%chair(r1,p4,T).
%door(r1, p2, r2, q1, opened, T).
bananas(on_monkey,T).
goal(T+1):- time(T), goal(T).
goal :- time(T), goal(T).
:- not goal.
Expand Down

0 comments on commit 8da7179

Please sign in to comment.