Ai Lab Updated
Ai Lab Updated
REGISTER NUMBER :
20CSPL601-ARTIFICIAL INTELLIGENCE
LABORATORY
Register No.:
EXP PAGE
S/NO DATE TITLEOFTHEEXPERIMENT SIGNATURE
NO NO
STUDY OF PROLOG
1 1
TEMPERATURE CONVERSION
3 3
N-QUEEN PROBLEM
4 4
8-PUZZLE PROBLEM
5 5
12 12 TOWERS OF HANOI
AIM:
STUDY:
symbolic, non-numeric computation. suited for solving problems that involve objects and relations between objects
parent as the name of a relation; tom and bob are its arguments.
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
This program consists of six clauses.
OUTPUT:
Y = pat,
X = bob
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)
female( pam).
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).
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, Y), likes (Y, John). /* John likes everybody and everybody likes John */
likes (John, Susie); likes(John, Mary). /* John likes Susie or John likes Mary */
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:
AIM:
To write predicates one converts centigrade temperature to Fahrenheit, other checks if a temperature is below
freezing.
PROGRAM
%ex3.pl
%ctof converts C to F
ctof(C,F):-F is C*9/5+32.
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:
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.
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,
mem(X,[X|_]).
mem(X,[_|T]):-mem(X,T).
template([1/Y1,2/Y2,3/Y3,4/Y4]).
OUTPUT:
AIM:
PROCEDURE:
* 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.
• 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.
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,Plan):-
is_subset(Goal,State),nl,
write_sol(Plan).
is_subset(Preconditions, State),
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):-
delete_list([],Curstate,Curstate).
remove(X,[H|T],[H|R]):-
remove(X,T,R).
write_sol([]).
write_sol([H|T]):- write_sol(T),
write(H), nl.
append([],L,L).
member(X, [X|_]).
member(X,[_|T]):-
member( X, T).
OUTPUT:
AIM:
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:
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:
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).
/*Best path, is called by shortest_path. It sends it the paths found in a path, distance format*/
/*Path is expanded to take in distance so far and the nodes visited */ path(Start,
/*This adds the stopping location to the visited list, adds the distance and then calls recursive to the next
stopping location along the path */
edge(Start, StopLoc, Distance), NewCostn is Costn + Distance, \+ member(StopLoc, CurrentLoc), path(StopLoc, Fin,
/*When we find a path back to the starting point, make that the total distance and make sure the graph has
touch every node*/
/*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*/
/* 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.*/
RESULT: Thus, the Travelling Salesperson Problem was solved by using Prolog
EXP NO: 9 WATER JUG PROBLEM
DATE:
AIM:
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,
Production Rules:
(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,
nl,
fill(X,Y):-
Y=0, X>=3,
nl,
write(',3)'), fill(X,3).
fill(X,Y):- X+Y>=4,
Y=3, X=3,
Y1 is Y-(4-X),
nl,
write(')'), fill(4,Y1).
fill(X,Y):- X+Y>=3,
X=4, Y=<1,
X1 is X-(3-Y),
nl,
write(',3)'), fill(X1,3).
fill(X,Y):- X+Y=<4,
X=0, Y>1,
X1 is X+Y,
nl,
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).
nl,
write(')'), fill(0,Y).
nl,
write(',0)'), fill(X,0).
fill(X,Y):- X>4,Y<3,
fill(X,Y):-X>4,Y>3,
AIM:
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:
start([3,3,left,0,0]).
goal([0,0,right,3,3]).
legal(CL,ML,CR,MR) :-
(MR>=CR ; MR=0).
% Possible moves:
move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):-
move([CL,ML,left,CR,MR],[CL2,ML,right,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,
is ML-1,
legal(CL2,ML2,CR2,MR2).
move([CL,ML,left,CR,MR],[CL,ML2,right,CR,MR2]):-
move([CL,ML,left,CR,MR],[CL2,ML,right,CR2,MR]):-
move([CL,ML,right,CR,MR],[CL,ML2,left,CR,MR2]):-
move([CL,ML,right,CR,MR],[CL2,ML,left,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,
legal(CL2,ML2,CR2,MR2).
move([CL,ML,right,CR,MR],[CL,ML2,left,CR,MR2]):-
move([CL,ML,right,CR,MR],[CL2,ML,left,CR2,MR]):-
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],
% 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),
% 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:
PROGRAM:
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
list_borrowed_books :- borrowed(BookTitle,
BorrowerId),
format('Book: ~w, Author: ~w, Year: ~w, Genre: ~w, Borrower: ~w ~w~n', [BookTitle, Author, Year,
Genre, FirstName, LastName]).
OUTPUT:
AIM:
PROGRAM:
OUTPUT:
RESULT: Thus, the Program to solve Towers of Hanoi was executed successfully.
EXP NO: 13 CRYPT ARITHMETIC PROBLEM
DATE:
AIM:
MONEY
PROGRAM:
OUTPUT:
RESULT: Thus, the Crypt Arithmetic Problem was solved by using Prolog.