Aashi Goel AI Lab 3rd Sem
Aashi Goel AI Lab 3rd Sem
Aashi Goel AI Lab 3rd Sem
S INSTITUTE OF
ENGINEERING & TECHNOLOGY
KCA351: AI LAB
Submitted By:
Aashi Goel
(2003110140002)
INDEX
S.No Contents Page No.
1 Institute Vision and Mission 3
2 Department Vision, Mission and PEO 4
3 Program Outcomes and Program Specific Outcomes. 5
4 Course outcomes 6
5 List of Experiment 7
6 Experiment Description 8-36
Vision of the Institute
To be an excellent department that imparts value based quality education and uplifts
innovative research in the ever-changing field of technology.
Mission of Department
1. Students will have the successful careers in the field of computer science
and allied sectors as an innovative engineer.
2. Students will continue to learn and advance their careers through
participation in professional activities, attainment of professional
certification and seeking advance studies.
3. Students will be able to demonstrate a commitment to life-long learning.
4. Students will be ready to serve society in any manner and become a
responsible and aware citizen.
5. Establishing students in a leadership role in any field.
Program Outcomes
1. Engineering knowledge: Apply the knowledge of mathematics, science,
engineering fundamentals, and an engineering specialization to the solution of
complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first
principles of mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified needs
with appropriate consideration for the public health and safety, and the cultural,
societal, and environmental considerations
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of
data, and synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources,
and modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge
to assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member
or leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with
the engineering community and with society at large, such as, being able to
comprehend and write effective reports and design documentation, make effective
presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary
environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of
technological change.
Program Specific Outcomes
1. Ability to apply and analyze computational concepts in the areas related to
algorithms, machine learning, cloud computing, web designing and web
services.
2. Ability to apply standard practices and methodologies in software
development and project management.
3. Ability to employ fundamental concepts and emerging technologies for
innovative research activities, carrier opportunities & zeal for higher
studies.
Course Outcomes
CO-1 To Understand the concept of Artificial intelligence.
CO-4 To understand & apply different types of machine learning and models.
To understand the design principles of pattern recognition with estimation and apply
CO-5 classification technique.
List of Experiments
Exp. Experiment Name Course
No. Outcome
1 Write a program in prolog to implement simple facts and Queries CO1
2 Write a program in prolog to implement simple arithmetic CO1
3 Write a program in prolog to solve Monkey banana problem CO4
4 Write a program in prolog to solve Tower of Hanoi CO2
5 Write a program in prolog to solve 8 Puzzle problems CO2
6 Write a program in prolog to solve 4-Queens problem CO3
7 Write a program in prolog to solve Traveling salesman problem CO4
8 Write a program in prolog for Water jug problem CO3
Value added Programs
9. Write a program to implement a Tic-Tac-Toe game.
CO3
10. Write a python program to implement simple Chatbot?
CO4
EXPERIMENT NO. 1
Goal
?- likes (ram,What).
What = mango.
1 solution.
Viva Questions:
1- First create a source file for the genealogical logicbase application. Start by adding a few
members of your family tree. It is important to be accurate, since we will be exploring family
relationships. Your own knowledge of who your relatives are will verify the correctness of your
Prolog programs.
2- Enter a two-argument predicate that records the parent-child relationship. One argument
represents the parent, and the other the child. It doesn't matter in which order you enter the
arguments, as long as you are consistent. Often Prolog programmers adopt the convention that
parent(A,B) is interpreted "A is the parent of B".
3- Create a source file for the customer order entry program. We will begin it with three record
types (predicates). The first is customer/3 where the three arguments are
arg1
Customer name
arg2
City
arg3
Credit rating (aaa, bbb, etc)
4- Next add clauses that define the items that are for sale. It should also have three arguments
arg1
Item identification number
arg2
Item name
arg3
The reorder point for inventory (when at or below this level, reorder)
5- Next add an inventory record for each item. It has two arguments.
arg1
Item identification number (same as in the item record)
arg2
Amount in stock
Assignment:
Now that we have some facts in our Prolog program, we can consult the program in the listener
and query, or call, the facts. This chapter, and the next, will assume the Prolog program
contains only facts. Queries against programs with rules will be covered in a later chapter.
Prolog queries work by pattern matching. The query pattern is called a goal. If there is a fact
that matches the goal, then the query succeeds and the listener responds with 'yes.' If there is
no matching fact, then the query fails and the listener responds with 'no.'
Prolog's pattern matching is called unification. In the case where the logicbase contains only
facts, unification succeeds if the following three conditions hold.
● The predicate named in the goal and logicbase are the same.
● Both predicates have the same arity.
● All of the arguments are the same.
Before proceeding, review figure 3.1, which has a listing of the program so far.
The first query we will look at asks if the office is a room in the game. To pose this, we would
enter that goal followed by a period at the listener prompt.
?- room(office).
yes
Prolog will respond with a 'yes' if a match was found. If we wanted to know if the attic was a
room, we would enter that goal.
?- room(attic).
no
Solution:-
clauses
likes(ram ,mango).
girl(seema).
red(rose).
likes(bill ,cindy).
owns(john ,gold).
queries
?-likes(ram,What).
What= mango
?-likes(Who,cindy).
Who= cindy
?-red(What).
What= rose
?-owns(Who,What).
Who= john
What= gold
Viva Questions:
easy(1).
easy(2).
easy(3).
gizmo(a,1).
gizmo(b,3).
gizmo(a,2).
gizmo(d,5).
gizmo(c,3).
gizmo(a,3).
gizmo(c,4).
and predict the answers to the queries below, including all alternatives when the semicolon (;)
is entered after an answer.
?- easy(2).
?- easy(X).
?- gizmo(a,X).
?- gizmo(X,3).
?- gizmo(d,Y).
?- gizmo(X,X).
2- Consider this
logicbase, harder(a,1).
harder(c,X).
harder(b,4).
harder(d,2).
?- harder(a,X).
?- harder(c,X).
?- harder(X,1).
?- harder(X,4).
3- Enter the listener and reproduce some of the example queries you have seen against
location/2. List or print location/2 for reference if you need it. Remember to respond with a
semicolon (;) for multiple answers. Trace the query.
5- If parent/2 seems to be working, you can add additional family members to get a larger
logicbase. Remember to include the corresponding male/1 or female/1 predicate for each
individual added.
EXPERIMENT NO. 2
AIM: Write a program in prolog to implement simple arithmetic.
Arithmetic
Prolog must be able to handle arithmetic in order to be a useful general purpose programming
language. However, arithmetic does not fit nicely into the logical scheme of things.
That is, the concept of evaluating an arithmetic expression is in contrast to the straight pattern
matching we have seen so far. For this reason, Prolog provides the built-in predicate 'is' that
evaluates arithmetic expressions. Its syntax calls for the use of operators, which will be
described in more detail in chapter 12.
X is <arithmetic expression>
The variable X is set to the value of the arithmetic expression. On backtracking it is unassigned.
The arithmetic expression looks like an arithmetic expression in any other programming
language.
?- X is 2 + 2.
X=4
?- X is 3 * 4 + 2.
X = 14
?- X is 3 * (4 + 2).
X = 18
?- X is (8 / 4) / 2. X=1
In addition to 'is,' Prolog provides a number of operators that compare two numbers. These
include 'greater than', 'less than', 'greater or equal than', and 'less or equal than.' They behave
more logically, and succeed or fail according to whether the comparison is true or false. Notice
the order of the symbols in the greater or equal than and less than or equal operators. They are
specifically constructed not to look like an arrow, so that you can use arrow symbols in your
programs without confusion.
X>Y
X<Y
X >= Y
X =< Y
?- 4 > 3.
Yes
?- 4 < 3.
No
?- X is 2 + 2, X > 3.
X=4
?- X is 2 + 2, 3 >= X.
No
?- 3+4 > 3*2.
Yes
Production rules:
c_to_f f is c * 9 / 5 +32
freezing f < = 32
Rules:
c_to_f(C,F) :-
F is C * 9 / 5 +
32. freezing(F) :-
F =< 32.
Queries :
?- c_to_f(100,X).
X = 212
Yes
?- freezing(15).
Yes
?- freezing(45).
No
Viva Questions:
1- Write a predicate valid_order/3 that checks whether a customer order is valid. The
arguments should be customer, item, and quantity. The predicate should succeed only if the
customer is a valid customer with a good credit rating, the item is in stock, and the quantity
ordered is less than the quantity in stock.
2- Write a reorder/1 predicate which checks inventory levels in the inventory record against the
reorder quantity in the item record. It should write a message indicating whether or not it's
time to reorder.
Assignment:
Production Rules
can_reach🡪clever,close.
get_on:🡪 can_climb.
under🡪in room,in_room,
in_room,can_climb. Close🡪get_on,under|
tall
Parse Tree
can_reach(A, B)
clever(A) close(A,B)
So Can_climb(monkey,chair) close(monkey,banana)
A=monkey B=banana
Solution:-
Clauses:
in_room(bananas).
in_room(chair).
in_room(monkey).
clever(monkey).
can_climb(monkey, chair).
tall(chair).
can_move(monkey, chair, bananas).
can_reach(X, Y):-clever(X),close(X, Y).
get_on(X,Y):- can_climb(X,Y).
under(Y,Z):-in_room(X),in_room(Y),
in_room(Z),can_climb(X,Y,Z).
close(X,Z):-get_on(X,Y), under(Y,Z);
tall(Y).
Queries:
?- can_reach(A,
B). A = monkey.
B = banana.
?- can_reach(monkey,
banana). Yes.
EXPERIMENT NO. 4
Aim:- Write a program to solve Tower of Hanoi.
This object of this famous puzzle is to move N disks from the left peg to the right peg using the
center peg as an auxiliary holding peg. At no time can a larger disk be placed upon a smaller
disk. The following diagram depicts the starting setup for N=3 disks.
Production Rules
hanoi(N)🡪move(N,left,middle,right).
move(1,A,_,C)🡪inform(A,C),fail.
move(N,A,B,C)🡪N1=N-1,move(N1,A,C,B),inform(A,C),move(N1,B,A,C).
Diagram:-
Parse Tree:-
hanoi(3)
domains
loc =right;middle;left
predicates
hanoi(integer)
move(integer,loc,loc,loc)
inform(loc,loc)
clauses
hanoi(N):-
move(N,left,middle,right).
move(1,A,_,C):-
inform(A,C),!.
move(N,A,B,C):-
N1=N-1,
move(N1,A,C,B),
inform(A,C),
move(N1,B,A,C).
inform(Loc1, Loc2):-
write("\nMove a disk from ", Loc1, " to ", Loc2).
goal
hanoi(3).
Move(3,left,right,center).
Move top disk from left to
right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
Yes
EXPERIMENT NO. 5
Aim:- Write a program to solve 8-Puzzle problem.
The title of this section refers to a familiar and popular sliding tile puzzle that has been around
for at least forty years. The most frequent older versions of this puzzle have numbers or letters
an the sliding tiles, and the player is supposed to slide tiles into new positions in order to
realign a scrambled puzzle back into a goal alignment. For illustration, we use the 3 x 3 8-tile
version, which is depicted here in goal configuration
7 2 3
4 6 5
1 8
To represent these puzzle "states" we will use a Prolog term representation employing '/' as a
separator. The positions of the tiles are listed (separated by '/') from top to bottom, and from
left to right. Use "0" to represent the empty tile (space). For example, the goal is ...
goal(1/2/3/8/0/4/7/6/5).
Production Rules :-
s_aux(0,0) 🡪 cut
s_aux(X,Y,0) :- Y is X+1,
!.
s_aux(8,1,0) :- !.
The heuristic function we use here is a combination of two other estimators: p_fcn, the
Manhattan distance function, and s_fcn, the sequence function, all as explained in Nilsson
(1980), which estimates how badly out-of-sequence the tiles are (around the outside).
h_function(Puzz,H) :- p_fcn(Puzz,P),
s_fcn(Puzz,S),
H is P + 3*S.
The 'move' predicate is defined as follows.
move(P,C,left) :- left(P,C).
move(P,C,up) :- up(P,C).
move(P,C,right) :- right(P,C).
move(P,C,down) :- down(P,C).
Here is the code for p and s.
%%% Manhattan distance
p_fcn(A/B/C/D/E/F/G/H/I, P) :-
a(A,Pa), b(B,Pb), c(C,Pc),
d(D,Pd), e(E,Pe), f(F,Pf),
g(G,Pg), h(H,Ph), i(I,Pi),
P is Pa+Pb+Pc+Pd+Pe+Pf+Pg+Ph+Pg+Pi.
s_aux(0,0) :- !.
s_aux(_,1).
s_aux(X,Y,0) :- Y is X+1, !.
s_aux(8,1,0) :- !.
s_aux(_,_,2).
The Prolog program from the previous section and the program outlined in this section can be
used as an 8-puzzle solver.
?- solve(0/8/1/2/4/3/7/6/5, S).
Solution:
domains
queen = q(integer,
integer) queens = queen*
freelist = integer*
board = board(queens, freelist, freelist, freelist, freelist)
predicates
nondeterm placeN(integer, board, board)
nondeterm place_a_queen(integer, board, board)
nondeterm nqueens(integer)
nondeterm makelist(integer, freelist)
nondeterm findandremove(integer, freelist, freelist)
nextrow(integer, freelist, freelist)
clauses
nqueens(N):-
makelist(N,L),
Diagonal=N*2-1,
makelist(Diagonal,LL),
placeN(N,board([],L,L,LL,LL),Final),
write(Final).
placeN(_,board(D,[],[],D1,D2),board(D,[],[],D1,D2)):-!.
placeN(N,Board1,Result):-
place_a_queen(N,Board1,Board2),
placeN(N,Board2,Result).
place_a_queen(N,
board(Queens,Rows,Columns,Diag1,Diag2),
board([q(R,C)|Queens],NewR,NewC,NewD1,NewD2)):-
nextrow(R,Rows,NewR),
findandremove(C,Columns,NewC),
D1=N+C-R,findandremove(D1,Diag1,NewD1),
D2=R+C-1,findandremove(D2,Diag2,NewD2).
findandremove(X,[X|Rest],Rest).
findandremove(X,[Y|Rest],[Y|Tail]):-
findandremove(X,Rest,Tail).
makelist(1,[1]).
makelist(N,[N|Rest]) :-
N1=N-1,makelist(N1,Rest).
nextrow(Row,[Row|Rest],Rest).
goal
nqueens(4),nl.
board([q(1,2),q(2,4),q(3,1),q(4,3),[],[],[7,4,1],[7,4,1])
yes
EXPERIMENT NO. 7
Aim:-Write a program to solve traveling salesman problem.
The following is the simplified map used for the prototype:
Production Rules:-
route(Town1,Town2,Distance)🡪 road(Town1,Town2,Distance).
route(Town1,Town2,Distance)🡪 road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,
domains
town = symbol
distance =
integer
predicates
nondeterm road(town,town,distance)
nondeterm route(town,town,distance)
clauses
road("tampa","houston",200).
road("gordon","tampa",300).
road("houston","gordon",100).
road("houston","kansas_city",120).
road("gordon","kansas_city",130).
route(Town1,Town2,Distance):-
road(Town1,Town2,Distance).
route(Town1,Town2,Distance):-
road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,
!.
goal
route("tampa", "kansas_city", X),
write("Distance from Tampa to Kansas City is ",X),nl.
Parse Tree
<0,0>
/\
R1 R2
<0,3>
/ |...\
/ |... \
R1 R4... R9
<3,0>
/ |\
/ | \
R2 R3 R5
<3,3>
/ |...\...
/ |... \...
R1 R3 R7
<4,2>
/ |...\...
/ |... \...
R2 R3 R5
<0,2>
/ |\
/ | \
R1 R7 R9
<2,0>
Algorithms
Successor,
successor (Node, Capacities, Visited, Successor),
Successors
),
append( Nodes, Successors, NewNodes ),
solve_jugs( NewNodes, Capacities, [State|Visited], End, Solution )
).
Goal
?- water_jugs.
In the Tic Tac Toe computer program the player chooses if they want to be X or O. Who
takes the first turn is randomly chosen. Then the player and computer take turns making
moves. The boxes on the left side of the flowchart are what happens during the player’s
turn. The right side shows what happens on the computer's turn. After the player or
computer makes a move, the program checks if they won or caused a tie, and then the
game switches turns. After the game is over, the program asks the player if they want to
play again.
EXPERIMENT NO 10
pattern_where_is(S, X), !,
print_prompt(me),
responses_db(location, D),
random_pick(D, R),
write_list(R),
n'), get_location(2),
loc(Y),
find_route(Y, D, R), !,
responses_db(location, [