0% found this document useful (0 votes)
20 views5 pages

AI File

The document contains Prolog code for solving various problems, including a water jug problem, graph traversal using BFS and DFS, a family tree structure, and a monkey and banana problem. It defines rules and predicates for managing states and actions related to these problems. The code demonstrates logical relationships and actions that can be performed based on the current state of the entities involved.

Uploaded by

amartya0327
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

AI File

The document contains Prolog code for solving various problems, including a water jug problem, graph traversal using BFS and DFS, a family tree structure, and a monkey and banana problem. It defines rules and predicates for managing states and actions related to these problems. The code demonstrates logical relationships and actions that can be performed based on the current state of the entities involved.

Uploaded by

amartya0327
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Water Jug

water_jug(X, Y):-X>4, Y<3, write('4L water jug overflowed.'),nl.


water_jug(X, Y):-X<4, Y>3, write('3L water jug overflowed.'),nl.
water_jug(X, Y):-X>4, Y>3, write('Both water jugs are overflowed.'),nl.

water_jug(X,Y):-(X=:=0, Y=:=0,nl,write('4L:0 & 3L:3 (Action: Fill 3L jug.)'), XY is


3,water_jug(X,YY));
(X=:=0, Y=:=0,nl,write('4L:4 & 3L:0 (Action: Fill 4L jug.)'), XX is
4,water_jug(XX,Y));
(X=:=2, Y=:=0,nl,write('4L:2 & 3L:0 (Action: Goal State
Reached....)'));
(X=:=4, Y=:=0,nl,write('4L:1 & 3L:3 (Action: Pour Water from 4L to
3L jug.)'), XX is X-3,YY is 3, water_jug(XX,YY));
(X=:=0, Y=:=3,nl,write('4L:3 & 3L:0 (Action: Pour Water from 3L to
4L jug.)'), XX is 3,YY is 0, water_jug(XX,YY));
(X=:=1, Y=:=3,nl,write('4L:1 & 3L:0 (Action: Empty 3L jug.)'), YY is
0,water_jug(X,YY));
(X=:=3, Y=:=0,nl,write('4L:3 & 3L:3 (Action: Fill 3L jug.)'), YY is
3,water_jug(X,YY));
(X=:=3, Y=:=3,nl,write('4L:4 & 3L:2 (Action: Pour Water from 3L to
4L jug until 4L jug is full.)'), XX is X+1, YY is Y-1, water_jug(XX,YY));
(X=:=1, Y=:=0,nl,write('4L:0 & 3L:1 (Action: Pour Water from 4L to
3L jug.)'), XX is Y, YY is X, water_jug(XX,YY));
(X=:=0, Y=:=3,nl,write('4L:4 & 3L:1 (Action: Fill 4L jug.)'), XX is
4,water_jug(XX,Y));
(X=:=4, Y=:=1,nl,write('4L:2 & 3L:3 (Action: Pour Water from 4L to
3L jug until 3L jug is full.)'), XX is X-2, YY is Y+2, water_jug(XX,YY));
(X=:=2, Y=:=3,nl,write('4L:2 & 3L:0 (Action: Empty 3L jug.)'), YY is
0,water_jug(X,YY));
(X=:=4, Y=:=2,nl,write('4L:0 & 3L:2 (Action: Empty 4L jug.)'), XX is
0,water_jug(XX,Y));
(X=:=0, Y=:=2,nl,write('4L:2 & 3L:3 (Action: Pour water from 3L jug
to 4L jug.)'), XX is Y, YY is X, water_jug(XX,YY));
BFS and DFS

%connected(+Start, +Goal, -Weight)


connected(1,7,1).
connected(1,8,1).
connected(1,3,1).
connected(7,4,1).
connected(7,20,1).
connected(7,17,1).
connected(8,6,1).
connected(3,9,1).
connected(3,12,1).
connected(9,19,1).
connected(4,42,1).
connected(20,28,1).
connected(17,10,1).

connected2(X,Y,D) :- connected(X,Y,D).
connected2(X,Y,D) :- connected(Y,X,D).

next_node(Current, Next, Path) :-


connected2(Current, Next, _),
not(member(Next, Path)).

breadth_first(Goal, Goal, _,[Goal]).


breadth_first(Start, Goal, Visited, Path) :-
findall(X,
(connected2(X,Start,_),not(member(X,Visited))),
[T|Extend]),
write(Visited), nl,
append(Visited, [T|Extend], Visited2),
append(Path, [T|Extend], [Next|Path2]),
breadth_first(Next, Goal, Visited2, Path2).

depth_first(Goal, Goal, _, [Goal]).


depth_first(Start, Goal, Visited, [Start|Path]) :-
next_node(Start, Next_node, Visited),
write(Visited), nl,
depth_first(Next_node, Goal, [Next_node|Visited], Path).

Family tree

parent(Z,Y) :- father(Z,Y) ; mother(Z,Y).

sibling(bob,bill).
sibling(sue,bill).
sibling(nancy,jeff).
sibling(nancy,ron).
sibling(jell,ron).

sibling(X,Y) :-
parent(A,X),
parent(A,Y),
not(X = Y).

sister(X, Y) :-
sibling(X, Y),
female(X),
not(X = Y).
brother(X, Y) :-
sibling(X, Y),
male(X),
not(X = Y).

grandparent(C,D) :- parent(C,E), parent(E,D).

aunt(X,Y) :-
parent(Z,Y), sister(X,Z).
aunt(X, Y) :- female(X), sibling(X, Z), parent(Z, Y).
% aunt(X, Y) :- female(X), spouse(X, W), sibling(W, Z), parent(Z, Y).

uncle(X,Y) :-
parent(Z,Y), brother(X,Z).

male(john).
male(bob).
male(bill).
male(ron).
male(jeff).

female(mary).
female(sue).
female(nancy).
female(jane).

mother(mary, sue).
mother(mary, bill).
mother(sue, nancy).
mother(sue, jeff).
mother(jane, ron).

father(john, sue).
father(john, bill).
father(bob, nancy).
father(bob, jeff).
father(bill, ron).
Monkey banana

move(state(middle,onbox,middle,hasnot),
grasp,
state(middle,onbox,middle,has)).
move(state(P,onfloor,P,H),
climb,
state(P,onbox,P,H)).
move(state(P1,onfloor,P1,H),
drag(P1,P2),
state(P2,onfloor,P2,H)).
move(state(P1,onfloor,B,H),
walk(P1,P2),
state(P2,onfloor,B,H)).
canget(state(_,_,_,has)).
canget(State1) :-
move(State1,_,State2),
canget(State2).

You might also like