0% found this document useful (0 votes)
1 views

AI practicals

The document outlines several experiments implemented in Prolog, including Fibonacci series, Quick sort, Depth First Search, Breadth First Search, the Monkey Banana Problem, and the Tower of Hanoi. Each experiment includes the aim, program code, query, and output. The programs demonstrate various algorithms and problem-solving techniques using Prolog.

Uploaded by

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

AI practicals

The document outlines several experiments implemented in Prolog, including Fibonacci series, Quick sort, Depth First Search, Breadth First Search, the Monkey Banana Problem, and the Tower of Hanoi. Each experiment includes the aim, program code, query, and output. The programs demonstrate various algorithms and problem-solving techniques using Prolog.

Uploaded by

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

Experiment -3

Aim: WAP in prolog to implement a fibonacci series


Program:-

fibonacci(0, 0).

fibonacci(1, 1).
fibonacci(N, Result) :-
fibonacci(N, Result, [0, 1]).
fibonacci(0, 0, _).

fibonacci(1,
Query:-
fibonacci(5,N).
Output:-

fibonacci(5,N).
N=5
Experiment -4
Aim: WAP in prolog to implement a Quick sort
Program:-
quicksort([], []).
quicksort([X],[X])

quicksort([Pivot | Tail], Sorted) :-


partition(Pivot, Tail, Less, Greater),
quicksort(Less, SortedLess),
quicksort(Greater, SortedGreater),

append(SortedLess, [Pivot | SortedGreater], Sorted).


partition(_, [], [], []).
partition(Pivot, [Head | Tail], [Head | Less], Greater) :-
Head =< Pivot,

partition(Pivot, Tail, Less, Greater).


partition(Pivot, [Head | Tail], Less, [Head | Greater]) :-
Head > Pivot,
Partition(Pivot, Tail, Less, Greater).
Query:-

quicksort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], Sorted).


Output:-
Sorted=[1,1,2,3,3,5,5,5,6,9]
Experiment -4
Aim: WAP in Prolog to implement Depth First Search
Program:-

childnode(a,b).

childnode(a,c).

childnode(c,d).

childnode(c,e).

path(A,B,[A|L]):-child(A,B,L).

child(A,B,[B|[]]):- childnode(A,B),!.

child(A,B,[X|L1]):- childnode(A,X), child(X,B,L1).

Query:-

path(a,e,L).

Output:-

path(a,e,L)

L = [a, c, e]
Experiment -5
Aim: WAP in Prolog to implement Breath First Search
Program:-

state_record(State, Parent, [State, Parent]).

go(Start, Goal) :-

empty_queue(Empty_open),
state_record(Start, nil, State),
add_to_queue(State, Empty_open, Open),
empty_set(Closed),
path(Open, Closed, Goal).
path(Open,_,_) :- empty_queue(Open),
write('graph searched, no solution found').

path(Open, Closed, Goal) :-


remove_from_queue(Next_record, Open, _),
state_record(State, _, Next_record),
State = Goal,
write('Solution path is: '), nl,
printsolution(Next_record, Closed).

path(Open, Closed, Goal) :-


remove_from_queue(Next_record, Open, Rest_of_open),
(bagof(Child, moves(Next_record, Open, Closed, Child), Children);Children = []),
add_list_to_queue(Children, Rest_of_open, New_open),

add_to_set(Next_record, Closed, New_closed),


path(New_open, New_closed, Goal),!.
moves(State_record, Open, Closed, Child_record) :-
state_record(State, _, State_record),

mov(State, Next),
% not (unsafe(Next)),
state_record(Next, _, Test),
not(member_queue(Test, Open)),
not(member_set(Test, Closed)),

state_record(Next, State, Child_record).


printsolution(State_record, _):-
state_record(State,nil, State_record),
write(State), nl.

printsolution(State_record, Closed) :-
state_record(State, Parent, State_record),
state_record(Parent, _, Parent_record),
member(Parent_record, Closed),

printsolution(Parent_record, Closed),
write(State), nl.

add_list_to_queue([], Queue, Queue).

add_list_to_queue([H|T], Queue, New_queue) :-


add_to_queue(H, Queue, Temp_queue),
add_list_to_queue(T, Temp_queue, New_queue)
Experiment -6
Aim:- WAP in Prolog to implement Monkey Banana Problem
Program:-

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),push(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,Move,State2), canget(State2).

Query:-

canget(state(atdoor,onfloor,atwindow,hasnot)).

canget(state(atdoor,onbox,atwindow,hasnot)).

Ourput:-

canget(state(atdoor,onfloor,atwindow,hasnot)).

true

canget(state(atdoor,onbox,atwindow,hasnot)).

false
Experiment -7
Aim:- WAP in Prolog to implement Tower of Hanoi
Program:-

move(1,X,Y,_) :-
write('Move top disk from '),
write(X),

write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :-

N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),

move(M,Z,Y,X).
Query:-
move(3,x,y,z).
move(4,x,y,z).
Output:-

move(3,x,y,z).
Move top disk from x to y
Move top disk from x to z
Move top disk from y to z
Move top disk from x to y
Move top disk from z to x
Move top disk from z to y
Move top disk from x to y

true
move(4,x,y,z).

Move top disk from x to z


Move top disk from x to y
Move top disk from z to y
Move top disk from x to z
Move top disk from y to x
Move top disk from y to z
Move top disk from x to z
Move top disk from x to y
Move top disk from z to y
Move top disk from z to x
Move top disk from y to x
Move top disk from z to y
Move top disk from x to z
Move top disk from x to y
Move top disk from z to y
true

You might also like