0% found this document useful (0 votes)
12 views34 pages

Ai Lab Updated

The document outlines the Bonafide Certificate for the M.Tech Computer Science and Engineering program, specifically for the Artificial Intelligence Laboratory course during the academic year 2024-2025. It includes an index of experiments conducted, such as Prolog studies, temperature conversion, and various problem-solving exercises like the N-Queen and 8-Puzzle problems. Additionally, it provides a detailed description of each experiment's aim, procedure, and results, demonstrating the application of Prolog in solving these problems.

Uploaded by

sec22cj032
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)
12 views34 pages

Ai Lab Updated

The document outlines the Bonafide Certificate for the M.Tech Computer Science and Engineering program, specifically for the Artificial Intelligence Laboratory course during the academic year 2024-2025. It includes an index of experiments conducted, such as Prolog studies, temperature conversion, and various problem-solving exercises like the N-Queen and 8-Puzzle problems. Additionally, it provides a detailed description of each experiment's aim, procedure, and results, demonstrating the application of Prolog in solving these problems.

Uploaded by

sec22cj032
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/ 34

NAME :

REGISTER NUMBER :

20CSPL601-ARTIFICIAL INTELLIGENCE
LABORATORY

III YEAR/ VI SEMESTER


(BATCH: 2022 – 2027)
M.TECH COMPUTER SCIENCE AND
ENGINEERING (5 YEARS INTEGRATED)
ACADEMIC YEAR: 2024 – 2025
Bonafide Certificate

Register No.:

Certified that this is the Bonafide Record of work done by


Mr./Ms. in the M.Tech Degree
Course Computer Science and Engineering in the 20CSPL601 –
ARTIFICIAL INTELLIGENCE laboratory during the academic year
2024-2025.

Station : Chennai –600 044


Date :

STAFF IN-CHARGE HEAD OF THE DEPARTMENT

Submitted for University Practical Examination held on _ at Sri

Sai Ram Engineering College, Chennai – 600 044.

INTERNAL EXAMINER EXTERNAL EXAMINER


INDEX

EXP PAGE
S/NO DATE TITLEOFTHEEXPERIMENT SIGNATURE
NO NO

STUDY OF PROLOG
1 1

SIMPLE FACT FOR THE STATEMENTS


2 2

TEMPERATURE CONVERSION
3 3

N-QUEEN PROBLEM
4 4

8-PUZZLE PROBLEM
5 5

WATER JUG PROBLEM


6 6

DEPTH FIRST SEARCH


7 7

8 8 BREATH FIRST SEARCH

9 9 MISSIONARIES AND CANNIBAL PROBLEM

10 10 TRAVELLING SALESMAN PROBLEM

11 11 LIBRARY MANAGEMENT SYSTEM

CONTENT BEYOND SYLLABUS

12 12 TOWERS OF HANOI

13 13 CRYPT ARITHMETIC PROBLEM


EXP NO: 1 STUDY OF PROLOG
DATE:

AIM:

To Study about Prolog

STUDY:

symbolic, non-numeric computation. suited for solving problems that involve objects and relations between objects

Tom is a parent of Bob can be written in Prolog as: parent(tom, bob).

parent as the name of a relation; tom and bob are its arguments.

The whole Family Tree parent(pam, bob).

parent(tom, bob).

parent(tom, liz).

parent(bob, ann).

parent(bob, pat).

parent(pat, jim).
This program consists of six clauses.

Prolog can be posed some questions about the parent relation

OUTPUT:

The GRANDPARENT relation expressed as a


composition of two parent relations.

(1) Who is a parent of Jim? Assume that this is some


Y.

(2) Who is a parent of Y? Assume that this is some X.

?- parent( Y, jim), parent( X, Y).

Y = pat,

X = bob

?̍- parent( tom, X), parent( X, Y).

X = bob,

Y = ann ;
relations can be used to declare simple yes/no
X = bob,
properties of objects
Y = pat ;
OUTPUT:
Adding grandparent relation to the existing program
?- male(jim).
grandparent(X,Y):-parent(X,Z),parent(Z,Y).
true.
?- grandparent(pam,pat).
?- female(pam).
true.

true.
?- grandparent(pam,ann).
?- female(liz).
true.
true.
?- grandparent(tom,ann).
?- male(X).
true .
X = tom ;
EXERCISES
X = bob ;

X = jim.
1. parent(jim,X)
let us introduce the CHILDREN relation as the inverse
2. parent(X,jim) of the parent relation.

3. parent(pam,X),parent(X,pat)

4. parent(pam,X),parent(X,Y),parent(Y,jim) For all X and Y, Y is a child of X if X is a parent of Y.

5. Who is Pat's parent? child( Y, X) :- parent( X, Y). ? Rules

6. Does Liz have a child? Output:

7. Who is Pat's grandparent? ?- child(liz,tom).

EXTENDING THE EXAMPLES: true.

female( pam).

male( tom). ?- child(X,tom).

male( bob). X = bob ;

female( liz). X = liz.

female( pat). Rules specify things that may be true if some condition
is satisfied.
female( ann).

Rules have
male( jim).
a condition part (the right-hand side of the rule)(body)
The relations introduced here are MALE and and
FEMALE. These relations are unary relations. unary
true .
a conclusion part (the left-hand side of the rule). (head)
?- mother(X,bob).
Let us Introduce MOTHER relation
X = pam ;
For all X and Y.

X is the mother of Y
false.
if X is a parent of Y and X is a female.
?- mother(X,Y).

X = pam,
mother( X, Y) :- parent( X, Y), female( X).
Y = bob ;
OUTPUT:
X = pat,

Y = jim.
?- mother(pam,bob).

RESULT: Thus, Basics of Prolog studied successfully.


EXP NO: 2 SIMPLE FACT FOR THE STATEMENTS
DATE:

AIM: To write simple fact for the statements using prolog

REPRESENTATION IN PROLOG: Prolog expressions are comprised of the following truth-functional symbols,
which have the same interpretation as in the predicate calculus.

VARIABLES AND NAMES: Variables begin with an uppercase letter. Predicate names, function names, and the
names for objects must begin with a lowercase letter. Rules for forming names are the same as for the predicate calculus.

mother_of

male

female

greater_than

Socrates

FACTS: A fact is a predicate expression that makes a declarative statement about the problem domain. Whenever a
variable occurs in a Prolog expression, it is assumed to be universally quantified. Note that all Prolog sentences must
end with a period.

likes (John, Susie). /* John likes Susie */

likes (X, Susie). /* Everyone likes Susie */

likes (John, Y). /* John likes everybody */

likes (John, Y), likes (Y, John). /* John likes everybody and everybody likes John */

likes (John, Susie); likes(John, Mary). /* John likes Susie or John likes Mary */

not (likes (John, Pizza)). /* John does not like pizza */

likes (John, Susie):- likes (John, Mary)./* John likes Susie if John likes Mary.

RULES: A rule is a predicate expression that uses logical implication (:-) to describe a relationship among facts.
Thus, a Prolog rule takes the form

left_hand_side :- right_hand_side .
This sentence is interpreted as: left_hand_side if right_hand_side. The left_hand_side is restricted to a single,
positive, literal, which means it must consist of a positive atomic expression. It cannot be negated and it cannot contain
logical connectives.

This notation is known as a Horn clause. In Horn clause logic, the left-hand side of the clause is the conclusion, and
must be a single positive literal. The right-hand side contains the premises. The Horn clause calculus is equivalent to
the first-order predicate calculus.

Statements:
1. The Cakes are delicious.
2. The Pickles are delicious.
3. The Pickles are spicy.
4. Priya relishes coffee.
5. Priya likes food if they are delicious.
6. Prakash likes food if they are spicy and delicious.

Statements in Prolog:
1. delicious(cakes).
2. delicious(pickles).
3. spicy( pickles).
4. relishes(priya, coffee).
5. likes(priya, Food) if delicious(Food). %Here Food is a variable
6. likes(prakash,Food) if spicy(Food) and delicious(Food).

OUTPUT:

Result: Thus, simple facts are represented by using prolog


EXP NO: 3 TEMPERATURE CONVERSION
DATE:

AIM:

To write predicates one converts centigrade temperature to Fahrenheit, other checks if a temperature is below

freezing.

Formula for Centigrade (C) temperatures to Fahrenheit (F) -


F = C * 9 / 5 + 32

PROGRAM
%ex3.pl

%ctof converts C to F

ctof(C,F):-F is C*9/5+32.

%freezing function check the temperature

freezing(F):-F=<32.

OUTPUT:

Result: Thus, prolog program to convert centigrade temperature to Fahrenheit is executed successfully
EXP NO: 4 4-QUEEN PROBLEM
DATE:

AIM:

To Write a Prolog program to solve 4-Queens Problem

PROCEDURE:

* The problem here is to place eight queens on the empty chessboard in such a way that no queen attacks any
other queen.
* Represent the position by a list of eight items, each of them corresponding to one queen.
* Each item in the list will specify a square of the board on which the corresponding queen is sitting.
* Example: 1/4 ◻ Queen 1 in 4th Position

* Having chosen this representation, the problem is to find such a list of the form [X1/Y1, X2N2, X3/Y3,
X4/Y4] which satisfies the no-attack requirement.
The solution relation can then be formulated by considering two cases

Case 1: The list of queens is empty: the empty because there is no attack.

Case 2: The list of queens is non-empty: then it looks like this: [X/Y | Othersl

In case 2, the first queen is at some square X/Y and the other queens are at squares specified by the list others. If this is
to be a solution then the following conditions must hold:

There must be no attack between the queens in the list others; that is, Others itself must also be a solution.

X and Y must be integers between 1 and 8.

A queen at square X/Y must not attack any of the queens in the list Others

PROGRAM:

solution([]).

solution([X/Y | Others]):-

solution(Others),

mem(Y,[1,2,3,4]),

noattack(X/Y,Others).

noattack(_,[]).

noattack(X/Y,[X1/Y1 | Others]):-

Y =\= Y1,

Y1-Y =\= X1-X,

Y1-Y =\= X-X1,


noattack(X/Y,Others).

mem(X,[X|_]).

mem(X,[_|T]):-mem(X,T).

template([1/Y1,2/Y2,3/Y3,4/Y4]).

OUTPUT:

RESULT: Thus, 4-Queen’s Problem solved by using prolog program.


EXP NO: 5 8-PUZZLE PROBLEM
DATE:

AIM:

To write a prolog program to solve 8-Puzzle problem.

PROCEDURE:

* Initialize the problem states using predicate.

* Determine whether current and destination tiles are a valid move.

* Define the solve predicate to produce the plan.

* Once the Goal list is a subset of the current State the plan is complete and it is written to the screen using
write_sol.

* The problem has three operators,

• 1st arg =name

• 2nd arg=preconditions

• 3rd arg=deletelist

• 4th arg=addlist.

* The tile can move to new position only if the destination tile is empty & Manhattan distance= 1. Check if first
list is a subset of the second.

* Remove all elements of 1st list from second to create third.


PROGRAM:

test(Plan):- write('Initialstate:'),nl,

Init=[at(tile4,1),at(tile3,2),at(tile8,3),at(empty,4),at(tile2,5),at(tile6,6),at(tile5,7),
at(tile1,8), at(tile7,9)],

write_sol(Init),

Goal=[at(tile1,1),at(tile2,2),at(tile3,3),at(tile4,4),at(empty,5),at(tile5,6),at(tile6,7),
at(tile7,8), at(tile8,9)],

nl,write('Goalstate:'),n l,

write(Goal),nl,nl,

solve(Init,Goal,Plan).

solve(State, Goal, Plan):-

solve(State,Goal,[],Plan).

is_movable(X1,Y1):-(1isX1-Y1);(-1is X1-Y1);(3is X1-Y1);(-3is X1-Y1).

solve(State,Goal,Plan,Plan):-

is_subset(Goal,State),nl,

write_sol(Plan).

solve(State, Goal, Sofar, Plan):- act(Action,Preconditions,Delete,Add),

is_subset(Preconditions, State),

\+ member(Action, Sofar), delete_list(Delete, State,

Remainder), append(Add, Remainder, NewState),

solve(NewState,Goal,[Action|Sofar],Plan).

act(move(X,Y,Z),

[at(X,Y),at(empty,Z),is_movable(Y,Z)],

[at(X,Y),at(empty,Z)],

[at(X,Z),at(empty,Y)]).

is_subset([H|T], Set):-

member(H,Set),

is_subset(T,Set).
is_subset([],_).

delete_list([H|T],Curstate,Newstate):-

remove(H, Curstate, Remainder),


delete_list(T,Remainder,Newstate).

delete_list([],Curstate,Curstate).

remove(X, [X|T], T).

remove(X,[H|T],[H|R]):-

remove(X,T,R).
write_sol([]).

write_sol([H|T]):- write_sol(T),

write(H), nl.

append([H|T],L1,[H|L2]):- append(T, L1,L2).

append([],L,L).

member(X, [X|_]).

member(X,[_|T]):-

member( X, T).

OUTPUT:

RESULT: Thus, 8-Puzzle problem is solved by using prolog program.


EXP NO: 6 BREADTH FIRST SEARCH
DATE:

AIM:

To write a prolog program to implement Breadth First Search.

PROCEDURE:

Breadth-First Search (BFS) is a graph traversal algorithm that explores all the vertices of a graph in
breadth-first order, i.e., it visits all the nodes at the same depth before moving on to the nodes at the next depth.

PROGRAM:

s(a,b).

s(a,c).

s(b,d).

s(b,e).

s(c,f).

s(c,g).

s(d,h).

s(e,i).

s(e,j).

s(f,k).

goal(f).

goal(j).

solve(Start,Solution):-

bfs([[Start]],Solution).

bfs([[Node|Path]|_],[Node|Path]):- goal(Node).

bfs([Path|Paths],Solution):-

extend(Path,NewPaths),

write(NewPaths),
nl, conc(Paths,NewPaths,Paths1),bfs(Paths1,Solution).

extend([Node|Path],NewPaths):-
bagof([NewNode,Node|Path],(s(Node,NewNode),not(member(NewNode,[Node|Path]))),NewPaths),!.

extend(_,[]).

conc([],L,L).

conc([X|L1],L2,[X|L3]):-nl,write('conc'),write(X),write(' '),write(L1),write(L2),conc(L1,L2,L3).

OUTPUT:

RESULT: Thus, the prolog program to implement BFS was executed successfully.
EXP NO: 7 DEPTH FIRST SEARCH
DATE:

AIM:

To write a prolog program to implement Depth First Search.

PROCEDURE:

• Depth-first search is an algorithm used for traversing or searching a tree or graph data
structure.

• The algorithm starts at the root node and explores as far as possible along each branch before
backtracking.

• It works by maintaining a stack of nodes to visit, starting with the root node.

• At each step, the algorithm pops the top node from the stack and visits its unvisited neighbors,
pushing them onto the stack.

• This process continues until the stack is empty or the goal node is found.

PROGRAM:

s(a,b).

s(a,c).

s(b,d).

s(b,e).

s(c,f).

s(c,g).

s(d,h).

s(e,i).

s(e,j).

s(f,k).

goal(f).

goal(j). mem(X,[X|_]).

mem(X,[_|Tail]):-mem(X,Tail).

solve(Node,Solution):-

dfs([],Node,Solution).

dfs(Path,Node,[Node|Path]):-
goal(Node).

dfs(Path,Node,Sol):-

s(Node,Node1), not(mem(Node1,Path)),

dfs([Node|Path],Node1,Sol).

OUTPUT:

RESULT: Thus, the prolog program to implement DFS was executed Successfully.
EXP NO: 8 TRAVELLING SALESMAN PROBLEM
DATE:

AIM:

To Write a Prolog program to solve Travelling Salesman Problem.

PROCEDURE:

The Traveling Salesman Problem (TSP) is a classic optimization problem in which a salesman wants to visit a
number of cities exactly once, and return to the starting city. The objective is to find the shortest possible route that
visits every city.

PROGRAM:

edge(a, b, 3).

edge(a, c, 5).

edge(a, d, 7).

edge(a, e, 8).

edge(b, c, 9).

edge(b, d, 10).

edge(b, e, 3).

edge(c, d, 5).

edge(c, e, 8).

edge(d, e, 6).

edge(b, a, 3).

edge(c, a, 4).

edge(d, a, 2).

edge(e, a, 7).

edge(c, b, 4).

edge(d, b, 6).

edge(e, b, 3).

edge(d, c, 5).

edge(e, c, 8).
edge(e, d, 6).

edge(a, h, 2).

edge(h, d, 1).

/* Finds the length of a list, while there is something in the list it increments N when there is nothing left it
returns.*/

len([], 0).

len([H|T], N):- len(T, X), N is X+1 .

/*Best path, is called by shortest_path. It sends it the paths found in a path, distance format*/

best_path(Visited, Total):- path(a, a, Visited, Total).

/*Path is expanded to take in distance so far and the nodes visited */ path(Start,

Fin, Visited, Total) :- path(Start, Fin, [Start], Visited, 0, Total).

/*This adds the stopping location to the visited list, adds the distance and then calls recursive to the next
stopping location along the path */

path(Start, Fin, CurrentLoc, Visited, Costn, Total) :-

edge(Start, StopLoc, Distance), NewCostn is Costn + Distance, \+ member(StopLoc, CurrentLoc), path(StopLoc, Fin,

[StopLoc|CurrentLoc], Visited, NewCostn, Total).

/*When we find a path back to the starting point, make that the total distance and make sure the graph has
touch every node*/

path(Start, Fin, CurrentLoc, Visited, Costn, Total) :-

edge(Start, Fin, Distance), reverse([Fin|CurrentLoc], Visited), len(Visited, Q), (Q\=7 ->

Total is 100000; Total is Costn + Distance).

/*This is called to find the shortest path, takes all the paths, collects them in holder. Then calls pick on that
holder which picks the shortest path and returns it*/

shortest_path(Path):-setof(Cost-Path, best_path(Path,Cost), Holder),pick(Holder,Path).

/* Is called, compares 2 distances. If cost is smaller than bcost, no need to go on. Cut it.*/

best(Cost-Holder,Bcost-_,Cost-Holder):- Cost<Bcost,!.

best(_,X,X).

/*Takes the top path and distance off of the holder and recursively calls it.*/

pick([Cost-Holder|R],X):- pick(R,Bcost-Bholder),best(Cost-Holder,Bcost-Bholder,X),!. pick([X],X).


OUTPUT:

RESULT: Thus, the Travelling Salesperson Problem was solved by using Prolog
EXP NO: 9 WATER JUG PROBLEM
DATE:

AIM:

To Write a Prolog program to solve Water Jug Problem

PROCEDURE:

• You are given two jugs, a 4-gallon one and a 3-gallon one. Neither has any measuring mark on it.
There is a pump that can be used to fill the jugs with water. Get exactly 2 gallons of water into the 4-
gallon jug.

• The state space for this problem can be described as the set of ordered pairs of integers (x,y)

Where,

• X represents the quantity of water in the 4-gallon jug X= 0,1,2,3,4

• Y represents the quantity of water in 3-gallon jug Y=0,1,2,3

Start State: (0,0)

• Goal State: (2,0)

• Generate production rules for the water jug problem

Production Rules:

Rule State Process

1 (X,Y | X<4) (4,Y) {Fill 4-gallon jug}

2 (X,Y |Y<3) (X,3) {Fill 3-gallon jug}

3 (X,Y |X>0) (0,Y) {Empty 4-gallon jug}

4 (X,Y | Y>0) (X,0) {Empty 3-gallon jug}

(X,Y | X+Y>=4 , Y>0) (4,Y-(4-X)) {Pour water from 3-gallon jug into 4-gallon jug until
5
4-gallon jug is full}

(X,Y | X+Y>=3 , X>0) (X-(3-Y),3) {Pour water from 4-gallon jug into 3-gallon jug until
6
3-gallon jug is full}

7 (X,Y | X+Y<=4 ,Y>0) (X+Y,0) {Pour all water from 3-gallon jug into 4-gallon jug}

8 (X,Y | X+Y <=3^ X>0) (0,X+Y){Pour all water from 4-gallon jug into 3-gallon jug}

9 (0,2) (2,0){Pour 2 gallon water from 3 gallon jug into 4 gallon jug}
PROGRAM:

fill(x,y).

fill(2,0):- nl,

write('Goal State is Reached..................... ').

fill(X,Y):- X=0, Y=<1,

nl,

write('Fill the 4-Gallon Jug:(4,'),write(Y),write(')'),fill(4,Y).

fill(X,Y):-

Y=0, X>=3,

nl,

write('Fill the 3-Gallon Jug:('), write(X),

write(',3)'), fill(X,3).

fill(X,Y):- X+Y>=4,

Y=3, X=3,

Y1 is Y-(4-X),

nl,

write('Pour water from 3-Gallon Jug to 4-Gallon until is full:(4,'), write(Y1),

write(')'), fill(4,Y1).

fill(X,Y):- X+Y>=3,

X=4, Y=<1,

X1 is X-(3-Y),

nl,

write('Pour water from 4-Gallon Jug to 3-Gallon until is full:'), write(X1),

write(',3)'), fill(X1,3).

fill(X,Y):- X+Y=<4,

X=0, Y>1,

X1 is X+Y,

nl,

write('pour all the water from 3-Gallon Jug to 4-Gallon:('), write(X1),

write(',0)'), fill(X1,0).

fill(X,Y):- X+Y<3,
Y=0,

Y1 is X+Y,

nl,

write('Pour all the water from 4-Gallon Jug too 3-Gallon:(0,'), write(Y1),

write(')'), fill(0,Y).

fill(X,Y):- Y>=2, X=4,

nl,

write('Empty the 4-Gallon Jug on Ground:(0,'), write(Y),

write(')'), fill(0,Y).

fill(X,Y):- Y=3, X>=1,

nl,

write('Empty the 3-Gallon Jug on Ground:('), write(X),

write(',0)'), fill(X,0).

fill(X,Y):- X>4,Y<3,

write('4L Jug Overflowed.'),nl.

fill(X,Y):- X<4, Y>3,

write('3L Jug Overflowed.'),nl.

fill(X,Y):-X>4,Y>3,

write('4L3L Jug Overflowed.'),nl.


OUTPUT:

RESULT: Thus, the Water Jug problem Solved by using Prolog.


EXP NO: 10 MISSIONARIES AND CANNIBAL PROBLEM
DATE:

AIM:

To write a prolog program to solve Missionaries and Cannibal Problem.

PROCEDURE:

The Missionaries and Cannibals problem is a classic problem in Artificial Intelligence that involves three
missionaries and three cannibals on one side of a river, along with a boat that can carry at most two people at a
time.

The goal is to move all the people to the other side of the river without ever leaving more missionaries than
cannibals on either side, or else the cannibals will eat the missionaries.

PROGRAM:

% Represent a state as [CL,ML,B,CR,MR]

start([3,3,left,0,0]).

goal([0,0,right,3,3]).

legal(CL,ML,CR,MR) :-

% is this state a legal one? ML>=0, CL>=0,

MR>=0, CR>=0, (ML>=CL ; ML=0),

(MR>=CR ; MR=0).

% Possible moves:
move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):-

% Two missionaries cross left to right. MR2 is MR+2,

ML2 is ML-2, legal(CL,ML2,CR,MR2).

move([CL,ML,left,CR,MR],[CL2,ML,right,CR2,MR]):-

% Two cannibals cross left to right. CR2 is CR+2,

CL2 is CL-2, legal(CL2,ML,CR2,MR).

move([CL,ML,left,CR,MR],[CL2,ML2,right,CR2,MR2]):-

% One missionary and one cannibal cross left to right. CR2 is CR+1,

CL2 is CL-1, MR2 is MR+1, ML2

is ML-1,

legal(CL2,ML2,CR2,MR2).

move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):-

% One missionary crosses left to right. MR2 is MR+1,

ML2 is ML-1, legal(CL,ML2,CR,MR2).

move([CL,ML,left,CR,MR],[CL2,ML,right,CR2,MR]):-

% One cannibal crosses left to right. CR2 is CR+1,

CL2 is CL-1, legal(CL2,ML,CR2,MR).

move([CL,ML,right,CR,MR],[CL,ML2,left,CR,MR2]):-

% Two missionaries cross right to left. MR2 is MR-2,

ML2 is ML+2, legal(CL,ML2,CR,MR2).

move([CL,ML,right,CR,MR],[CL2,ML,left,CR2,MR]):-

% Two cannibals cross right to left. CR2 is CR-2,

CL2 is CL+2, legal(CL2,ML,CR2,MR).

move([CL,ML,right,CR,MR],[CL2,ML2,left,CR2,MR2]):-

% One missionary and one cannibal cross right to left. CR2 is CR-1,

CL2 is CL+1, MR2 is MR-1


ML2 is ML+1,

legal(CL2,ML2,CR2,MR2).

move([CL,ML,right,CR,MR],[CL,ML2,left,CR,MR2]):-

% One missionary crosses right to left. MR2 is MR-1,

ML2 is ML+1, legal(CL,ML2,CR,MR2).

move([CL,ML,right,CR,MR],[CL2,ML,left,CR2,MR]):-

% One cannibal crosses right to left. CR2 is CR-1,

CL2 is CL+1, legal(CL2,ML,CR2,MR). % Recursive call to solve the problem

path([CL1,ML1,B1,CR1,MR1],[CL2,ML2,B2,CR2,MR2],Explored,MovesList) :-

move([CL1,ML1,B1,CR1,MR1],[CL3,ML3,B3,CR3,MR3]),

not(member([CL3,ML3,B3,CR3,MR3],Explored)),

path([CL3,ML3,B3,CR3,MR3],[CL2,ML2,B2,CR2,MR2],[[CL3,ML3,B3,CR3,MR3]|Explored],

[ [[CL3,ML3,B3,CR3,MR3],[CL1,ML1,B1,CR1,MR1]] | MovesList ]).

% Solution found

path([CL,ML,B,CR,MR],[CL,ML,B,CR,MR],_,MovesList):-

output(MovesList).

% Printing output([]) :-

nl.

output([[A,B]|MovesList]) :-

output(MovesList),

write(B), write(' -> '), write(A), nl.

% Find the solution for the missionaries and cannibals problem find :-

path([3,3,left,0,0],[0,0,right,3,3],[[3,3,left,0,0]],_).
OUTPUT

RESULT: Thus, the Missionaries and Cannibals problem solved by using Prolog.
EXP NO: 11 LIBRARY MANAGEMENT SYSTEM
DATE:

AIM:

To write a Prolog program to simulate Library Management System.

PROGRAM:

% Define books with their properties book(harry_potter_1,

jk_rowling, 1997, fantasy). book(harry_potter_2, jk_rowling,

1998, fantasy). book(harry_potter_3, jk_rowling, 1999,

fantasy). book(the_hobbit, jrr_tolkien, 1937, fantasy).

book(the_lord_of_the_rings, jrr_tolkien, 1954, fantasy).

book(the_da_vinci_code, dan_brown, 2003, thriller).

book(angels_and_demons, dan_brown, 2000, thriller).

book(digital_fortress, dan_brown, 1998, thriller).

book(the_girl_with_the_dragon_tattoo, stieg_larsson, 2005, mystery).

book(the_girl_who_played_with_fire, stieg_larsson, 2006, mystery).

book(the_girl_who_kicked_the_hornets_nest, stieg_larsson, 2007, mystery).

% Define borrowers with their properties

borrower(john, doe, 12345).

borrower(jane, smith, 67890).

borrower(jack, black, 24680).

% Define the borrowed predicate

borrowed(harry_potter_1, 12345).

borrowed(the_hobbit, 67890).

borrowed(the_da_vinci_code, 24680).

borrowed(the_girl_with_the_dragon_tattoo, 12345).

borrowed(the_girl_who_played_with_fire, 67890).
% Define predicates for book search and borrower search

find_book_by_title(Title, Author, Year, Genre) :-

book(Title, Author, Year, Genre).

find_book_by_author(Author, Title, Year, Genre) :- book(Title,

Author, Year, Genre).

find_borrower_by_id(Id, FirstName, LastName) :-

borrower(FirstName, LastName, Id).

list_borrowed_books :- borrowed(BookTitle,

BorrowerId),

find_book_by_title(BookTitle, Author, Year, Genre),

find_borrower_by_id(BorrowerId, FirstName, LastName),

format('Book: ~w, Author: ~w, Year: ~w, Genre: ~w, Borrower: ~w ~w~n', [BookTitle, Author, Year,
Genre, FirstName, LastName]).

OUTPUT:

RESULT: Thus, Library Management system is simulated using Prolog.


CONTENT BEYOND SYLLABUS

EXP NO: 12 TOWERS OF HANOI


DATE:

AIM:

To Write a Prolog Program to solve Towers of Hanoi.

PROGRAM:

% Prolog program to representation of 'Tower of Hanoi'


move(1,X,Y,_) :-
write('Move 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).
% Define a predicate to solve the Towers of Hanoi puzzle stoh(N) :-
move(N,left,right,center).

OUTPUT:

RESULT: Thus, the Program to solve Towers of Hanoi was executed successfully.
EXP NO: 13 CRYPT ARITHMETIC PROBLEM
DATE:

AIM:

To Write a Prolog Program to solve Crypt Arithmetic Problem.


SEND
M ORE

MONEY

PROGRAM:

alpha1(S, E, N, D, M, O, R, Y, [S, E, N, D, M, O, R, E, M, O, N, E, Y]) :-


between(1, 9, S),
between(0, 9, E), E \=S,
between(0, 9, N), N \= E,
N \= S,
between(0, 9, D),
D \= N, D \= E, D \= S,
between(1, 9, M),
M \= D, M \= N, M \= E, M \= S,
between(0, 9, O),
O \= M, O \= D, O \= N, O \= E, O \= S,
between(0, 9, R),
R \= O, R \= M, R \= D, R \= N, R \= E, R \= S,
between(0, 9, Y),
Y \= R, Y \= O, Y \= M, Y \= D, Y \= N, Y \= E, Y \= S,
Send is S*1000 + E*100 + N*10 + D, More is
M*1000 + O*100 + R*10 + E,
Money is M*10000 + O*1000 + N*100 + E*10 + Y,
Money is Send + More.

OUTPUT:

RESULT: Thus, the Crypt Arithmetic Problem was solved by using Prolog.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy