Ai Lab
Ai Lab
LAB MANUAL
An expert system is a set of programs that manipulates encoded knowledge to solve problems in
a specialized domain that normally requires human expertise. An expert system's knowledge is
obtained from expert sources such as texts, journal articles. databases etc. and encoded in a form
suitable for the system to use in its inference or reasoning processes. Once a sufficient body of
expert knowledge has been acquired, it must be encoded in some form, loaded into knowledge
base, then tested, and refined continually throughout the life of the system PROLOG serves as a
powerful language in designing expert systems because of its following features.
Use of knowledge rather than data. Modification of the knowledge base without recompilation
of the control programs. Capable of explaining conclusion. Symbolic computations resembling
manipulations of natural language.
Most of our smartphone, daily device or even the internet uses Artificial intelligence. Very
often, AI and machine learning are used interchangeably by big companies that want to
announce their latest innovation. However, Machine learning and AI are different in some ways.
AI- artificial intelligence- is the science of training machines to perform human tasks. The term
was invented in the 1950s when scientists began exploring how computers could solve problems
on their own.
Prolog is a simple but powerful programming language developed at the University of
Marseilles as a practical tool for programming in logic. From a user's point of view the major
attraction of the language is ease of programming. The system comprises an interpreter and a
compiler, both written largely in Prolog itself. At the user level the compiler is viewed as a built-
in procedure which may be called from the interpreter. When compiled, a procedure will run 10
to 20 times faster and use store more economically.
EXPERIMENT NO. 1
Clauses
likes(ram ,mango).
girl(seema).
red(rose).
likes(bill ,cindy).
owns(john ,gold).
Goal
?- likes (ram,What).
What = mango.
1 solution.
Assignment:
Aim: Write facts for following:
1. Ram likes apple.
2. Ram is taller then Mohan.
3. My name is Subodh.
4. Apple is fruit.
5. Orange is fruit.
6. Ram is male.
Simple Queries
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
EXPERIMENT NO. 3
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>
YX
<Y
X >=
YX
=< 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
EXPERIMENT NO. 4
Imagine a room containing a monkey, chair and some bananas. That have been hanged from the
center of ceiling. If the monkey is clever enough he can reach the bananas by placing the chair
directly below the bananas and climb on the chair .
The problem is to prove the monkey can reach the bananas.
Production Rules
can_reach clever,close
. get_on: can_climb.
Parse Tree
can_reach(A, B)
clever(A) close(A,B)
B=banana
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. 5
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. 6
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),Fi
nal), 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. 8
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
R
7
<4,2>
/ |...\...
/ |... \... R2
R3
R
5
<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.
Mathematical description: Hill climbing attempts to maximize (or minimize) a function f(x),
where x are discrete states. These states are typically represented by vertices in a graph, where
edges in the graph encode nearness or similarity of a graph. Hill climbing will follow the graph
from vertex to vertex, always locally increasing (or decreasing) the value of ƒ, until a local
maximum (or local minimum) xm is reached. Hill climbing can also operate on a continuous
space: in that case, the algorithm is called gradient ascent (or gradient descent if the function is
minimized).*.