0% found this document useful (0 votes)
16 views31 pages

Ai Lab

Ai lab
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)
16 views31 pages

Ai Lab

Ai lab
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/ 31

Dr. A.P.J.

Abdul Kalam Technical University, Lucknow

LAB MANUAL

Subject Code: KCS-751


Subject Name: Artificial Intelligence Lab

Submitted By: Submitted To:


List of Experiments

Exp. Experiment Name Signature


No.

1. Study of Prolog programming language

2 Write a program in prolog to implement simple facts


and Queries

3 Write a program in prolog to implement simple


arithmetic

4 Write a program in prolog to solve Monkey banana


problem

5 Write a program in prolog to solve Tower of Hanoi

6 Write a program in prolog to solve 8 Puzzle problems

7 Write a program in prolog to solve 4-Queens problem

8 Write a program in prolog to solve Traveling salesman


problem

9 Write a program in prolog for Water jug problem

10 Write a program to implement Hill climbing Algorithm.


PROLOG IN DISGINING EXPERT SYSTEMS

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.

Reason with Meta-knowledge-


META PROGRAMMING- A meta-program is a program that takes other programs as data.
Interpreters and compilers are examples of mela-programs. Meta-interpreter is a particular kind
of meta-program: an interpreter for a language written in that language. So a PROLOG
interpreter is an interpreter for PROLOG, itself written in PROLOG. Due to its symbol-
manipulation capabilities, PROLOG is a powerful language for meta-programming. Therefore, it
is often used as an implementation language for other languages. PROLOG is particularly
suitable as a language for rapid prototyping where we are interested in implementing new ideas
quickly. New ideas are rapidly implemented and experimented
With AI.
Artificial Intelligence (AI) is the study of computer science focusing on developing software or
machines that exhibit human intelligence. A simple enough definition, right Obviously, there is a
lot more to it. AI is a broad topic ranging from simple calculators to self-steering technology to
something that might radically change the future.

Goals and Applications of AI


The primary goals of AI include deduction and reasoning, knowledge representation, planning,
natural language processing (NLP), learning, perception, and the ability to manipulate and move
objects. Long-term goals of AI research include achieving Creativity, Social Intelligence, and
General (human level) Intelligence.
AI has heavily influenced different sectors that we may not recognize. Ray Kurzweil says
"Many thousands of AI applications are deeply embedded in the infrastructure of every
industry." John McCarthy, one of the founders of AI, once said that “as soon as it works, no one
calls it Al anymore."

Type of Artificial Intelligence


Artificial intelligence can be divided into three subfields:
Artificial intelligence
Machine learning
Deep learning
AI vs. Machine Learning

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

Aim:- Study of Prolog programming language.

PROLOG-PROGRAMMING IN LOGIC PROLOG stands for Programming, In Logic an idea


that emerged in the early 1970's to use logic as programming language. The early developers of
this idea included Robert Kowaiski at Edinburgh (on the theoretical side), Marrten van Emden at
Edinburgh (experimental demonstration) and Alian Colmerauer at Marseilles (implementation).
David D.H. Warren's efficient implementation at Edinburgh in the mid-1970's greatly
contributed to the popularity of PROLOG. PROLOG is a programming language centred around
a small set of basic mechanisms, Including pattern matching, tree based data structuring and
automatic backtracking. This Small set constitutes a surprisingly powerful and flexible
programming framework. PROLOG is especially well suited for problems that involve objects-
in particular, structured objects- and relations between them. SYMBOLIC LANGUAGE
PROLOG is a programming language for symbolic, non-numeric computation. It is especially
well suited for solving problems that involve objects and relations between objects. For
example, it is an easy exercise in prolog to express spatial relationship between objects, such as
the blue sphere is behind the green one. It is also easy to state a more general rule: if object X is
closer to the observer than object Y. and object Y is closer than Z, then X must be closer than Z.
PROLOG can reason about the spatial relationships and their consistency with respect to the
general rule. Features like this make PROLOG a powerful language for Artificial Language A1,)
and non- numerical programming. There are well-known examples of symbolic computation
whose implementation in other standard languages took tens of pages of indigestible code, when
the same algorithms were implemented in PROLOG, the result was a crystal-clear program
easily fitting on one page.

Progmmming in PROIOG is accomplished by creating a database of facts and rules about


objects, their properties, and their relationships to other objects. Queries then can be posed about
the objects and valid conclusions will be determined and returned by the program Responses to
user queries are determined through a form of inference control known as resolution.
FOR EXAMPLE:
a) FACTS: Some facts about family relationships could be written as: sister( sue,bill) parent(
ann.sam) male(jo) female( riya)
b) RULES
To represent the general rule for grandfather, we write: grand f.gher( X2) parent(X,Y) parent(
Y,Z) male(X) c) QUERIES: Given a database of facts and rules such as that above, we may
make queries by typing after a query a symbol”?' statements such as: ?-parent(X,sam) Xann
?grandfather(X,Y) X=jo, Y=sam.
EXPERIMENT NO. 2

AIM: Write a program in prolog to implement simple facts and Queries


1. Ram likes mango.
2. Seema is a girl.
3. Bill likes Cindy.
4. Rose is red.
5. John owns gold.

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.

Here is how to use Prolog as a calculator.

?- X is 2 + 2.
X=4

?- X is 3 * 4 + 2.
X = 14

Parentheses clarify precedence.

?- 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

Here are a few examples of their use.

?- 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

Aim:- Write a program to solve the Monkey Banana problem.

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.

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)

clever(monkey) get_on(A,C) under(C,B)

A=monkey can_climb(A,C) nroom(X) inroom(C) inroom(B )

can_climb(X,C,B) C=chair X=monkey C=chair

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

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)

move(N,left,middle,right) move(1,A,_,C) inform(A,C),!


N=3

Move top Move top disk Move top disk


disk from left to right from right to center from center to left

Move top disk


right from left to center

Move top disk


from left to right

Move top disk


from left to right
Solution:-

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

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 :-

h_function(Puzz,H) p_fcn(Puzz,P), s_fcn(Puzz,S),H is P + 3*S.

The 'move' productions are 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).
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_fcn(A/B/C/D/E/F/G/H/I, S) 1 s_aux(A,B,S1), s_aux(B,C,S2),


s_aux(C,F,S3), s_aux(F,I,S4),
s_aux(I,H,S5), s_aux(H,G,S6),
s_aux(G,D,S7),
s_aux(D,A,S8), s_aux(E,S9),
S is S1+S2+S3+S4+S5+S6+S7+S8+S9.

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.

a(0,0). a(1,0). a(2,1). a(3,2). a(4,3). a(5,4). a(6,3). a(7,2). a(8,1).


b(0,0). b(1,0). b(2,0). b(3,1). b(4,2). b(5,3). b(6,2). b(7,3). b(8,2).
c(0,0). c(1,2). c(2,1). c(3,0). c(4,1). c(5,2). c(6,3). c(7,4). c(8,3).
d(0,0). d(1,1). d(2,2). d(3,3). d(4,2). d(5,3). d(6,2). d(7,2). d(8,0).
e(0,0). e(1,2). e(2,1). e(3,2). e(4,1). e(5,2). e(6,1). e(7,2). e(8,1).
f(0,0). f(1,3). f(2,2). f(3,1). f(4,0). f(5,1). f(6,2). f(7,3). f(8,2).
g(0,0). g(1,2). g(2,3). g(3,4). g(4,3). g(5,2). g(6,2). g(7,0). g(8,1).
h(0,0). h(1,3). h(2,3). h(3,3). h(4,2). h(5,1). h(6,0). h(7,1). h(8,2).
i(0,0). i(1,4). i(2,3). i(3,2). i(4,1). i(5,0). i(6,1). i(7,2). i(8,3).

%%% the out-of-cycle


function
s_fcn(A/B/C/D/E/F/G/H/I, S) :-
s_aux(A,B,S1), s_aux(B,C,S2), s_aux(C,F,S3),
s_aux(F,I,S4), s_aux(I,H,S5), s_aux(H,G,S6),
s_aux(G,D,S7), s_aux(D,A,S8), s_aux(E,S9),
S is S1+S2+S3+S4+S5+S6+S7+S8+S9.

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:

left( A/0/C/D/E/F/H/I/J , 0/A/C/D/E/F/H/I/J ).


left( A/B/C/D/0/F/H/I/J , A/B/C/0/D/F/H/I/J ).
left( A/B/C/D/E/F/H/0/J , A/B/C/D/E/F/0/H/J
). left( A/B/0/D/E/F/H/I/J , A/0/B/D/E/F/H/I/J
). left( A/B/C/D/E/0/H/I/J , A/B/C/D/0/E/H/I/J
). left( A/B/C/D/E/F/H/I/0 , A/B/C/D/E/F/H/0/I
).
up( A/B/C/0/E/F/H/I/J , 0/B/C/A/E/F/H/I/J ).
up( A/B/C/D/0/F/H/I/J , A/0/C/D/B/F/H/I/J ).
up( A/B/C/D/E/0/H/I/J , A/B/0/D/E/C/H/I/J ).
up( A/B/C/D/E/F/0/I/J , A/B/C/0/E/F/D/I/J ).
up( A/B/C/D/E/F/H/0/J , A/B/C/D/0/F/H/E/J
). up( A/B/C/D/E/F/H/I/0 , A/B/C/D/E/0/H/I/F
).

right( A/0/C/D/E/F/H/I/J , A/C/0/D/E/F/H/I/J ).


right( A/B/C/D/0/F/H/I/J , A/B/C/D/F/0/H/I/J ).
right( A/B/C/D/E/F/H/0/J , A/B/C/D/E/F/H/J/0
). right( 0/B/C/D/E/F/H/I/J , B/0/C/D/E/F/H/I/J
). right( A/B/C/0/E/F/H/I/J , A/B/C/E/0/F/H/I/J
). right( A/B/C/D/E/F/0/I/J , A/B/C/D/E/F/I/0/J
).

down( A/B/C/0/E/F/H/I/J , A/B/C/H/E/F/0/I/J ).


down( A/B/C/D/0/F/H/I/J , A/B/C/D/I/F/H/0/J ).
down( A/B/C/D/E/0/H/I/J , A/B/C/D/E/J/H/I/0
). down( 0/B/C/D/E/F/H/I/J , D/B/C/0/E/F/H/I/J
). down( A/0/C/D/E/F/H/I/J , A/E/C/D/0/F/H/I/J
). down( A/B/0/D/E/F/H/I/J , A/B/F/D/E/0/H/I/J
).
EXPERIMENT NO. 7

Aim:- Write a program to solve 4-Queen problem.


In the 4 Queens problem the object is to place 4 queens on a chessboard in such a way that no
queens can capture a piece. This means that no two queens may be placed on the same row,
column, or diagonal.

The N Queens Chessboard

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

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.

Distance from Tampa to Kansas City is


320 X=320
1 Solution
EXPERIMENT NO. 9

Aim:- Write a program for water jug problem.


"You are given two jugs, a 4-gallon one and a 3-gallon one. Neither have any measuring markers
on it. There is a tap that can be used to fill the jugs with water. How can you get exactly 2
gallons of water into the 4-gallon jug?".
Production Rules:-
R1: (x,y) --> (4,y) if x < 4
R2: (x,y) --> (x,3) if y < 3
R3: (x,y) --> (x-d,y) if x > 0
R4: (x,y) --> (x,y-d) if y > 0
R5: (x,y) --> (0,y) if x > 0
R6: (x,y) --> (x,0) if y > 0
R7: (x,y) --> (4,y-(4-x)) if x+y >= 4 and y > 0
R8: (x,y) --> (x-(3-y),y) if x+y >= 3
and x > 0 R9: (x,y) --> (x+y,0) if x+y =< 4
and y > 0 R10: (x,y) --> (0,x+y)
if x+y =< 3 and x > 0

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

Breadth First Search Algorithm

Depth First Search Algorithm

Best First Search Algorithm

Successor,
successor (Node, Capacities, Visited,
Successor), Successors
),
append( Nodes, Successors, NewNodes ),
solve_jugs( NewNodes, Capacities, [State|Visited], End, Solution )
).

successor( Node, Capacities, Visited, Successor )


:- node_state( Node, State ),
Successor = node(Action,State1,Node),
jug_transition( State, Capacities, Action, State1 ),
\+ member( State1, Visited ).
jug_transition( State0, Capacities, empty_into(Source,Target), State1 ) :-
volume( Source, State0, Content0 ),
Content0 > 0,
jug_permutation( Source, Target, Unused ),
volume( Target, State0, Content1 ),
volume( Target, Capacities, Capacity ),
Content0 + Content1 =< Capacity,
volume( Source, State1, 0 ),
volume( Target, State1, Content2 ),
Content2 is Content0 + Content1,
volume( Unused, State0, Unchanged ),
volume( Unused, State1, Unchanged ).
jug_transition( State0, Capacities, fill_from(Source,Target), State1 )
:- volume( Source, State0, Content0 ),
Content0 > 0,
jug_permutation( Source, Target, Unused ),
volume( Target, State0, Content1 ),
volume( Target, Capacities, Capacity ),
Content1 < Capacity,
Content0 + Content1 > Capacity,
volume( Source, State1, Content2 ),
volume( Target, State1, Capacity ),
Content2 is Content0 + Content1 - Capacity,
volume( Unused, State0, Unchanged ),
volume( Unused, State1, Unchanged ).

volume( small, jugs(Small, _Large, _Reservoir), Small ).


volume( large, jugs(_Small, Large, _Reservoir), Large ).
volume( reservoir, jugs(_Small, _Large, Reservoir), Reservoir
).

jug_permutation( Source, Target, Unused ) :-


select( Source, [small, large, reservoir], Residue
), select( Target, Residue, [Unused] ).

node_state( start(State), State ).


node_state( node(_Transition, State, _Predecessor), State ).

narrative( start(Start), Capacities, End ) -->


"Given three jugs with capacities of:", newline,
literal_volumes( Capacities ),
"To obtain the result:", newline,
literal_volumes( End ),
"Starting with:", newline,
literal_volumes( Start ),
"Do the following:", newline.
narrative( node(Transition, Result, Predecessor), Capacities, End ) --
> narrative( Predecessor, Capacities, End ),
literal_action( Transition, Result ).
literal_volumes( Volumes ) -->
indent, literal( Volumes ), ";", newline.

literal_action( Transition, Result ) -->


indent, "- ", literal( Transition ), " giving:",
newline, indent, indent, literal( Result ), newline.

literal( empty_into(From,To) ) -->


"Empty the ", literal( From ), " into the ",
literal( To ).
literal( fill_from(From,To) ) -->
"Fill the ", literal( To ), " from the
", literal( From ).
literal( jugs(Small,Large,Reservoir) ) -->
literal_number( Small ), " gallons in the small jug, ",
literal_number( Large ), " gallons in the large jug and
", literal_number( Reservoir ), " gallons in the
reservoir".
literal( small ) --> "small jug".
literal( large ) --> "large jug".
literal( reservoir ) -->
"reservoir".

literal_number( Number, Plus, Minus ) :-


number( Number ),
number_chars( Number, Chars ),
append( Chars, Minus, Plus ).

indent --> " ".

newline --> " ".

Goal

?- water_jugs.

Given three jugs with capacities of:


3 gallons in the small jug, 4 gallons in the large jug and 8 gallons in the reservoir;
To obtain the result:
0 gallons in the small jug, 2 gallons in the large jug and 6 gallons in the reservoir;
Starting with:
0 gallons in the small jug, 0 gallons in the large jug and 8 gallons in the reservoir;
Do the following:
- Fill the small jug from the reservoir giving:
3 gallons in the small jug, 0 gallons in the large jug and 5 gallons in the reservoir
- Empty the small jug into the large jug giving:
0 gallons in the small jug, 3 gallons in the large jug and 5 gallons in the reservoir
- Fill the small jug from the reservoir giving:
3 gallons in the small jug, 3 gallons in the large jug and 2 gallons in the reservoir
- Fill the large jug from the small jug giving:
2 gallons in the small jug, 4 gallons in the large jug and 2 gallons in the reservoir
- Empty the large jug into the reservoir giving:
2 gallons in the small jug, 0 gallons in the large jug and 6 gallons in the reservoir
- Empty the small jug into the large jug giving:
0 gallons in the small jug, 2 gallons in the large jug and 6 gallons in the reservoir
EXPERIMENT NO. 10

Aim:- Write a program to implement Hill climbing Algorithm.


Introduction: In computer science, hill climbing is a mathematical optimization technique which
belongs to the family of local search. It is relatively simple to implement, making it a popular first
choice. Although more advanced algorithms may give better results, in some situations hill
climbing works just as well. Hill climbing can be used to solve problems that have many
solutions, some of which are better than others. It starts with a random (potentially poor) solution,
and iteratively makes small changes to the solution, each time improving it a little. When the
algorithm cannot see any improvement anymore, it terminates. Ideally, at that point the current
solution is close to optimal, but it is not guaranteed that hill climbing will ever come close to the
optimal solution.
For example, hill climbing can be applied to the traveling salesman problem. It is easy to find a
solution that visits all the cities but will be very poor compared to the optimal solution. The
algorithm starts with such a solution and makes small improvements to it, such as switching the
order in which two cities are visited. Eventually, a much better route is obtained.
Hill climbing is used widely in artificial intelligence, for reaching a goal state from a starting
node. Choice of next node and starting node can be varied to give a list of related algorithms.

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).*.

f(x,y) =e-(x2+ y2)


-2
-1
0
X
22
Variants In simple hill climbing, the first closer node is chosen, whereas in steepest ascent hill
climbing all successors are compared and the closest to the solution is chosen. Both forms fail if
there is no closer node, which may happen if there are local maxima in the search space which are
not solutions.
Steepest ascent hill climbing is similar to best-first search, which tries all possible extensions of
the current path instead of only one. Stochastic hill climbing does not examine all neighbors
before deciding how to move. Rather, it selects a neighbor at random, and decides (based on the
amount of improvement in that neighbor) whether to move to that neighbor or to examine
another.
Random-restart hill climbing is a meta-algorithm built on top of the hill climbing algorithm. It is
also known as Shotgun hill climbing. It iteratively does hill-climbing, each time with a random
initial condition x0. The best xm is kept: if a new run of hill climbing produces a better xm than
the stored state, it replaces the stored state. Random-restart hill climbing is a surprisingly effective
algorithm in many cases. It turns out that it is often better to spend CPU time exploring the space,
than carefully optimizing from an initial condition. Problems:
Local maxima :A problem with hill climbing is that it will find only local maxima. Unless the
heuristic is convex, it may not reach a global maximum. Other local search algorithms try to
overcome this problem such as stochastic hill climbing, random walks and simulated annealing.
This problem of hill climbing can be solved by using random hill climbing search technique.
Ridges: A ridge is a curve in the search place that leads to a maximum, but the orientation of the
ridge compared to the available moves that are used to climb is such that each move will lead to a
smaller point. In other words, each point on a ridge looks to the algorithm like a local maximum,
even though the point is part of a curve leading to a better optimum. Plateau : Another problem
with hill climbing is that of a plateau, which occurs when we get to a "flat" part of the search
space, i.e. we have a path where the heuristics are all very close together. This kind of flatness can
cause the algorithm to cease progress and wander aimlessly.
Pseudocode:
Hill Climbing Algorithm:
currentNode = startNode; loop do
L = NEIGHBORS(currentNode); nextEval = -INF;
nextNode = NULL;
for all x in L
if (EVAL(x) > nextEval)
nextNode = x;
nextEval = EVAL(x);
if nextEval <= EVAL(currentNode)
//Return current node since no better neighbors exist
return currentNode;
currentNode = nextNode;
J
Result: The Hill Climbing algorithm is implemented. The algorithm terminates when goal state is
reached from the starting node.

You might also like