A Unit of S.V.S.U: Chhatrapati Shahuji Subharti Institute of Technology & Engineering

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

CHHATRAPATI SHAHUJI SUBHARTI

INSTITUTE
OF TECHNOLOGY &
ENGINEERING

A UNIT OF S.V.S.U
SUBHARTIPURAM, N.H 58, DELHI-HARIDWAR- BY PASS ROAD
MEERUT 250005

DEPARTMENT OF COMPUTER SCIENCE


& INFORMATION TECHNOLOGY
LAB MANUAL- ARTIFICIAL INTELLIGENCE LAB

INDEX

S.NO

1.

LIST OF EXPERIMENTS

STUDY OF PROLOG
A program in prolog to find the factorial of a given no.

2.

3.

A program in prolog to generate the fibonicci series.

4.

A program in prolog for concatenation of two strings.

5.

6.

A progam in prolog to find the sum of n natural nos.

A program in prolog for sorting.

7.

The traveling salesman problem

8.

Monkey & Banana


Problem

9.

Eight Queen Problem

10.

.The

"Water-jugs Problem"

Goal of AI Lab
Prolog was invented in the early seventies at the University of
Marseille. Prolog stands for Programming in Logic. It is a logic
language that is particularly used by programs that use nonnumeric objects. For this reason it is a frequently used language in
Artificial Intelligence where manipulation of symbols is a common
task. Prolog differs from the most common programmings
languages because it is declarativre language. Traditional
programming languages are said to be procedural. This means that
the programmer specify how to solve a problem. In declarative
languages the programmers only give the problem and the language
find
himself
how
to
solve
the
problem.
Although it can be, and often is, used by itself, Prolog complements
traditional languages when used together in the same application.

Presentation of Prolog
1.1 What is a Prolog program
Programming in Prolog is very different from programming in a
traditional procedural language like Pascal. In Prolog you don't say
how the program will work.
Prolog can be separated in two parts :

1.1.1 The Program


The program, sometimes called Database is a texte file (*.pl) that
contain the facts and rules that will be used by the user of the
program. It contains all the relations that make this program.

1.1.2 The Query


When you launch a program you are in query modequery mode.
This mode is represented by the sign ? - at the begining of the line.
In query mode you ask questions about relations described in the
program.

1.2 Loading a program


loading First you have to launch your Prolog compiler, for this
report we used the SWI-Prolog wich is a freeware (if you want to
know more about SWI-Prolog go to this page). When Prolog is
launched the ?- should appear meaning you are in query mode.
The .manier to launch a program depends of your compiler. For
SWI-Prolog you can load a program by typing the cammand [file].
when the file of your program is file.pl. If you compiler is not SWIProlog you can also try the command reconsult(file). When you have
done this you can use all the facts and rules that are contained in the
program. Now let's begin to see what is a fact...?.

2 The Facts
2.1 Simple facts
In Prolog we can make some statements by using facts. Facts either
consist of a particular item or a relation between items. For example
we can represent the fact that it is sunny by writing the program :
sunny.
We can now ask a query of Prolog by asking
?- sunny.
?- is the Prolog prompt. To this query, Prolog will answer yes. sunny
is true because (from above) Prolog matches it in its database of
facts.
Facts have some simple rules of syntax. Facts should always begin
with a lowercase letter and end with a full stop. The facts themselves
can consist of any letter or number combination, as well as the
underscore _ character. However, names containing the characters -,
+,*,/, or other mathematical operators should be avoided.

2.2 Facts with arguments


More complicated facts consist of a relation and the items that this
refers to. These items are called arguments. Facts can have
arbitrary number of arguments from zero upwards. A general
model is shown below:
relation(<argument1>,<argument2>,....,<argumentN> ).
The arguments can be any legal Prolog term. The basic Prolog terms
are an integer, an atom, a variable or a structure. Various Prolog
implementation enhance this basic list with other data types, such as
floating
point
numbers,
or
strings.
Exemple :

likes(john,mary).
In the above fact john and mary are two atomes. Atoms are usally
made from letters and digits with lowercase characters. The
underscore (_) can also be used to separe 2 words but is not allowed
as the first charactere. Atoms can also be legally made from
symbols. The followings are legal atoms :atoms
hello
zz42
two_words
====>

The followings are not legal atoms :


Hello
4hello
_Hello
two words
two-words
You can use single quotes to make any character combination a legal
atom.
'two words'
'UpperCase'
'12444'
'etc...'
The fact likes(john,mary). say that there is a relation between john
and mary. It can be read as either john likes mary or mary likes
john. This reversibility can be very useful to the programmer,
however it can also be a source of mistakes. You have to be clear on
how
you
intend
to
interpret
the
relation.
The number of arguments is the arity of the predicate. A predicate

with 2 arguments will be called by predicate_name/2. You can have


differents predicats with the same name if they have a different
arity.

2.3 How to query


Once you have entered the facts in a program you can ask prolog
about it. An example program can be :
eats(fred,oranges).

/* 'Fred eats oranges' */

eats(tony,apple).

/* 'Tony eats apple' */

eats(john,apple).

/* 'John eats apple' */

If we now ask some queries we would get the followings things :


?- eats(fred,oranges).
/* does this match anything in the database? */
yes
/* yes, that matchs the first clause */
?- eats(john,apple).
yes
?- eats(mike,apple).
no
/* there is no relation between mike and apple */

3 Variables and Unification


3.1 Simples unifications
How can we ask something like ''what does Fred eat ?'' If we have
the following program :
eats(fred,oranges).
How do we ask what fred eats ? We could write something like this :
?- eats(fred,what).
But Prolog will say no. The reason is that Prolog can't find the
relation eats(fred,what) in hi database. In this case we have to use a
variable wich will be unified to match a relation given in the
program.
This
process
is
known
as
unification.
Variables are distinguished from atoms by starting with a capital
letter. Here are some exemples of variables :variables
X

/* a single capital letter

VaRiAbLe

*/

/* a word beginning with an upper case letter */

Two_words /* two words separated with an underscore */


Now that we know how to use a variables, we can ask the same
question as before using the variable What instead of an atom.
?- eats(fred,What)
What=oranges
yes
In this case Prolog try to unifie the variable with an atom.
''What=oranges'' means that the query is successfull when What is
unified with oranges.
With the same program if we ask :

?- eats(Who,oranges).
In this example we ask who eats oranges. To this query Prolog
should answer :
?- eats(Who,oranges).
Who=fred
yes
Now if we ask :
?- eats(Who,apple).
no
Prolog answer no because he can't find in his database any atom
than can match with this relation.
Now if we only want to know if something is eated by anyone and we
don't care about that person we can use the underscore. The '_' can
be used like any variable.
For example if we ask eats(fred,_) the result will be :
?- eats(fred,_).
yes
The result will be yes because Prolog can find a relation of eat
between fred and something. But Prolog will not tell use the value of
'_'.
Now if we have the following program :
eats(fred,apple).
eats(fred,oranges).
Now if we ask :
?- eats(fred,What).

The first answer will be ''What=apple'' because that is the


unification that match the first relation of eats with fred in the
database. Then prolog will be waiting for you to press a key. If you
press enter Prolog will be ready for a new query. In most
implemetation if you press the key '';'' then Prolog will try to find if
there is any other successful unification. Prolog will give the second
result ''What=orange'' because this the second one in the program.
If you press again the key '';'' then Prolog will try to find a third
unification. The result will be ''no'' because he is not able to find any
other successful unification.
?- eats(fred,What).
What=apple ;
What=oranges ;
no

4 Rules
4.1 Rules
Consider the following sentence : 'All men are mortal' We can
express this thing in Prolog by :
mortal(X) :- human(X).
The clause can be read as 'X is mortal if X is human'.
To continue with this example, let us define the fact that Socrate is a
human. Our program will be :
mortal(X) :- human(X).

human(socrate).
Now if we ask to prolog :
?- mortal(socrate).
Prolog will respond :
yes
In order to solve the query -? mortal(socrates). Prolog will use the
rule we have given. It says that in order to prove that someone is
mortal we can prove that he is human. So from the goal
mortal(socrate) Prolog generate the subgoal human(socrate).
We can still use variables. For example we might want to know who
is mortal :
?- mortal(X).
Then Prolog should respond :
P=socrate
This means that Prolog was able to succed the goal by unifying the
variable X to socrates. Again this was done by using the subgoal
human(X).
Sometimes we may wish to specify alternatives ways to provre
something. We can do this by using differents rules ands facts with
the same name. For exeample, we can represent the sentence
'Something is fun if it is a PC running UNIX or am old amiga or an
ice cream' with the following program :
fun(X) :pc(X),
unix(X).

/* something is fun if
/* it is a pc and
/* it is running unix

*/
*/
*/

fun(X) :old(X),

/* or it is fun if
/* it is old and

amiga(X). /* it is an amiga

*/
*/
*/

fun(ice_cream).
/* the ice_crean is also fun */
This program says that there are three ways to know if an object is
fun or not. Like for pure facts, Prolog will start from the first clause
(a clause can be a rule or a fact) of fun and try it. If that does not
succed Prolog will try the next clause. If there is no more clauses
then it fails and Prolog responds ' no '. We can also see in this
example that the 'and' is represented by a ',' and the 'or' by
differents clause. If needed the 'or' can also be represebted by ';'. In
the previous examples when we was asking eats(fred,What) and
pressing the key ';' to see the following results we was in fact asking
'or'.
All identically-named variables in a rule (for example X in the last
rule we've seem) are of course considered as one unic variable and
must have the same instantiation for each solution in a particular
query. Identical variables names in differents rules are considerated
as differents variables and are totally independent, this is the same
as
if
we
had
used
differents
names.
The following program :
fun(X) :pc(X),
unix(X).
fun(X) :old(X),
amiga(X).
Will be seen by Prolog as :

fun(X_1) :pc(X_1),
unix(X_1).
fun(X_2) :old(X_2),
amiga(X_2).

5 Backtracking
5.1 Fail
The fail/1 predicate is provided by Prolog. When it is called, it
causes the failure of the rule. And this will be for ever, nothing can
change the statement of this predicate. You can ask you what is its
utility. We can associate it to the Cut/1 predicate, described in the
next subsection in this report.it allow us to include the negation in a
rule.A typical use of fail is a negation of a predicate.We can resume
the fail with this sheme :
goal(X) :- failure(X),!,fail.
goal(X).
failure(X) are the conditions that make goal(X) fail.

5.2 Cut
Up to this point, we have worked with Prolog's backtracking. We
have seen how to use the backtracking to write compact predicates.
Sometimes it is desirable to selectively turn off backtracking. Prolog
provides a predicate that performs this function. It is called the
cut/1,
represented
by
an
exclamation
point
(!).

The cut/1 effectively tells Prolog to freeze all the decisions made so
far in this predicate. That is, if required to backtrack, it will
automatically fail without trying other alternatives. We will first
examine the effects of the cut/1 and then look at some practical
reasons to use it . When the cut/1 is encountered, it re-routes
backtracking. It short-circuits backtracking in the goals to its left on
its level, and in the level above, which contained the cut/1. That is,
both the parent goal and the goals of the particular rule being
execut/1ed are affected by the cut/1. The effect is undone if a new
route
is
taken
into
the
parent
goal.
We will write some simple predicates that illustrate the behavior of
the cut/1, first adding some data to backtrack over.
data(pentiumIII).
data(athlon).
Here is the first test case. It has no cut/1 and will be used for
comparison purposes.
compare_cut_1(X) :data(X).
compare_cut_1('last chip').
This is the control case, which exhibits the normal behavior.
?- compare_cut_1(X), write(X), nl, fail.
pentiumIII
athlon
last chip
no
Next, we put a cut/1 at the end of the first clause.
compare_cut_2(X) :data(X),
!.

compare_cut_2('last chip').
Note that it stops backtracking through both the data subgoal (left),
and the compare_cut_2/1 parent (above).
?- compare_cut_2(X), write(X), nl, fail.
pentiumIII
no
Next we put a cut/1 in the middle of two subgoals.
compare_cut_3(X,Y) :data(X),
!,
data(Y).
compare_cut_3('last chip').
Note that the cut inhibits backtracking in the parent
compare_cut_3/2 and in the goals to the left of (before) the cut (first
data). The second data/1 to the right of (after) the cut is still free to
backtrack.
?- compare_cut_3(X,Y), write(X-Y), nl, fail.
pentiumIII - pentiumIII
pentiumIII - athlon
no
Performance is the main reason to use the cut/1. This separates the
logical purists from the pragmatists. Various arguments can also be
made as to its effect on code readability and maintainability. It is
often called the 'goto' of logic programming. You will most often use
the cut/1 when you know that at a certain point in a given predicate,
Prolog has either found the only answer, or if it hasn't, there is no
answer. In this case you insert a cut/1 in the predicate at that point.
Similarly, you will use it when you want to force a predicate to fail in
a certain situation, and you don't want it to look any further.

5.3 Not
For logical puritey, it is always possible to rewrite the predicates
without the cut/1. This is done with the built-in predicate not/1.
Some claim this provides for clearer code, but often the explicit and
liberal use of 'not' clutters up the code, rather than clarifying it.
When using the cut/1, the order of the rules become important. Let's
try to rewrite compare_cut_2/1 with the not/1 predicate.
not_2(X) :- X = pentiumIII.
not_2(X) :- not(X = pentiumIII).
You can now see the difference between not/1 and cut/1, but the code
is here to simple to see really the difference of clarity.Try with
compare_cut_3 :
not_3(X,Y) :- X = pentiumIII,data(Y).
not_3(X,Y) :- not(X = pentiumIII),not(Y).
Now, you can imaginate what will be the difference between the
algorithms based on the cut/1 and those on the not/1. The result is
the
same,
it's
just
a
way
of
thinking.
it is interesting to note that not/1 is defined using the cut/1. It also
uses call/1, another built-in predicate that calls a predicate.
not(X) :- call(X), !, fail.
not(X).

6 Recursion
6.1 What is recursion
The recursion in any language is a function that can call itself until
the goal has been succeed.
In Prolog, recursion appears when a predicate contain a goal that
refers to itself.

As we have seen in the earlier chapters when a rule is called Prolog


create a new query with new variables. So it make no difference
wether a rule calls another rule or call itself.
In Prolog and in any language, a recursive definition always has at
least two parts. A first fact that act like a stopping condition and a
rule that call it self simplified. At each level the first fact is checked.
If the fact is true then the recursion ends. If not the recursion
continue.
A recursive rule must never call itself with the same arguments ! If
that happens then the program will never end.
7 Lists
7.1 What is a list in Prolog
Lists are powerful data structures for holding and manipulating
groups of things. In Prolog, a list is simply a collection of terms. The
terms can be any Prolog data types,including structures and other
lists.Syntactically, a list is denoted by square brackets with the terms
separated by commas. For example, a list of alcohol is represented
as:[Tequila,Whisky,Vodka,Martini,Muscat,Malibu,Soho,Epita].
This gives us an alternative way of representing the locations of
things. Rather than having separate location predicates for each
thing, we can have one location predicate per container, with a list of
things in the container.
list_where([Tequila,Whisky,Vodka], bathroom).
list_where([Martini,Muscat], kitchen).
list_where([Malibu,Soho], under_the_bed).
list_where([Epita], everywhere).

The empty list is represented by a set of empty brackets []. This is


equivalent to the nil in other programming language.For our
example in this section, it can describe the lack of things in a place :
list_where([], cave).
The Unification works on lists just as it works on other data
structure of SWI Prolog. With that, we now can ask questions about
lists to prolog:

?- [_,_,X] = [lesson, work, sleeping].


X = sleeping
?- list_where(X, under_the_bed).
X = [Malibu,Soho]
Notice that there is a impractical method of getteing a list of
elements in the first example, because of Prolog won't unify unless
both list have the same number of elements. At last, the special
notation for list structures.
[X | Y]
This structure is unified with a list, X is bound to the first element of
the list, called the head. Y is bound to the list of remaining elements,
called the tail.Note that the tail is considered as a list for Prolog and
the empty list does not unify with the standart list syntax because it
has no head. Here is an example :
?- [X|Y] = [a, b, c, d, e].
X=a
Y = [b, c, d, e]

?- [X|Y] = [].
no
The empty list does not unify with the standard list syntax because it
has no head.
?- [X|Y] = [].
no

This failure is important, because it is often used to test for the


boundary condition in a recursive routine. That is, as long as there
are elements in the list, a unification with the [X|Y] pattern will
succeed. When there are no elements in the list, that unification fails,
indicating that the boundary condition applies. We can specify more
than just the first element before the bar (|). In fact, the only rule is
that what follows it should be a list.
?- [First, Second | Q] = [water,gin,tequila,whisky].
First = water
Second = gin
Q = [tequila,whisky]
We have said a list is a special kind of structure. In a sense it is, but
in another sense it is just like any other Prolog term. The last
example gives us some insight into the true nature of the list. It is
really an ordinary two-argument predicate. The first argument is
the head and the second is the tail. This predicate is represented by a
period(.). To see this notation, we use the built-in predicate display,
which writes lits in using this syntax.

?- X = [T|Q], display(X).
.(_01,_02)

8 Others Elements of Prolog


8.1 Operators
For the arithmetic operators, prolog has already a list of predefined
predicates. These are :
=,is,<,>,=<,>=,==,=:=,/,*,+,-,mod,div
In Prolog, the calculations don't write like we have learned.They are
written as a binary tree.That is to say that :
y*5+10*x are written in Prolog : +(*(y,5),*(10,x)).
Because we don't have learned to write like this, Prolog accept our
way of writing calculations.Nevertheless, we have to say to him the
rules of priority on the operators.For instance, we have to say to
Prolog that * is prior on +.We have also to indicate to Prolog the
prior calculations with '()'.Prolog allows the programmer to define
his own operators with the relations next :
Op(P,xfy,name).
Where P is the priority of the operators(between 1 and 1200), xfy
indicates if the operator is infix(xfx,xfy,yfx) or postfix(fx,fy).The
name is, of course, the name of the operator.Note that the prior
operator has the lowest priority. Prolog has already these predefined
operators :
Op(1200,xfx,':-').
Op(1200,fx,[:-,?-]).

Op(1100,xfy,';').
Op(1000,xfy,',').
Op(700,xfx,[=,is,<,>,=<,>=,==,=:=]).
Op(500,yfx,[+.-]).
Op(500,fx,[+,-,not]).
Op(400,yfx,[*,/,div]).
Op(300,xfx,mod).

8.2 Arithmetic
Prolog use the infix operator 'is' to give the result of an arithmetic
operation to a variable.is
X is 3 + 4.
Prolog responds
X=7
yes
When the goal operator 'is' is used the variables on the right must
have been unified to numbers before. Prolog is not oriented
calculations, so, after a certain point, he approximates the
calculations and he don't answer the exact number but his
approximation Fo example :
?- X is 1000 + .0001
X = 1000
yes

9 Input and Output


9.1 Input Output commands
At this time we have seen how we can communicate with prolog
using the keyboard and the screen. We will now see how we can
communicate with prolog using files.\

Prolog can read and write in a file. The file where Prolog read is
called input and the file where Prolog write is called output. When
you run Prolog the default output is your screen (the shell) and the
input is your keyboard. If you want to use files for thatr you have to
tell it to Prolog using the commands listed in the appendix on this
page.

9.2 Read and Write


readwrite Sometimes a program will nedd to to read a term from a
file or the keybord and write a term on the screen or in a file. In this
case the goals write and read can be used.
read(X).
Read the term from the active input and unifie X with it.
write(Y).
Write the term Y on the active output.

10 SWI-Prolog
10.1 What is SWI-Prolog
If you want to use Prolog you need a compiler. There are many
compiler downloadables on internet. For this report we used SWIProlog. SWI-Prolog is free Prolog compiler licenced under the GPL,
targeting primaly the research and education.

10.2 Author
SWI-Prolog is designed and implemented by Jan Wielemaker from
the University of Amsterdam, department of Social Science
Informatics (SWI).

10.3 Platforms
SWI-Prolog is written in ANSI C and should configure for most
Unix
machines
having
an
ANSI
C
Compiler.
Ports have been made for :
1. MS Win32
2. MS Win3.1
3. MS DOS
4. OS/2

2. Write a program in prolog to find the factorial of a


given no.
factorial(0,1).
factorial(N,F) :N>0,
N1 is N-1,
factorial(N1,F1),
F is N * F1.

3. Write a program in prolog to generate the fibonicci


series.
fib(0, 0).
fib(X, Y) :- X > 0, fib(X, Y, _).

fib(1, 1, 0).
fib(X, Y1, Y2) :X > 1,
X1 is X - 1,
fib(X1, Y2, Y3),
Y1 is Y2 + Y3.

4. Write a program in prolog for concatenation of two


strings.
append ([],L,L).
append([X/L1],L2,[X/L3]):-append (L1,L2,L3).
?-append([alpha,beta],[gamma,delta],X),
Write(X).write(/n).

5. Write a progam in prolog to find the sum of n


natural nos.
Sum_to (1,1).
Sum_to(N,R):Not(N=1).
NL is N-1 , sum_to(N1,R1),R is N+R1.
?- sum_to(S,R),write(R),write(/n).

5. Write a program in prolog for sorting.


Naive sort

Naive sort is not very effective algorithm. It generates all


permutations and then it tests if the permutation is a sorted list.
naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted).
is_sorted([]).
is_sorted)[_]).
is_sorted([X,Y|T]):-X<=Y,is_sorted([Y|T]).
Insert sort

Insert sort is a traditional sort algorithm. Prolog implementation


of insert sort is based on idea of accumulator.
insert_sort(List,Sorted):-i_sort(List,[],Sorted).
i_sort([],Acc,Acc).
i_sort([H|T],Acc,Sorted):insert(H,Acc,NAcc),i_sort(T,NAcc,Sorted).
insert(X,[Y|T],[Y|NT]):-X>Y,insert(X,T,NT).
insert(X,[Y|T],[X,Y|T]:-X<=Y.
insert(X,[],[X]).
Bubble sort
Bubble sort is another traditional sort algorithm which is not
very effective. Again, we use accumulator to implement bubble
sort.
bubble_sort(List,Sorted):-b_sort(List,[],Sorted).
b_sort([],Acc,Acc).
b_sort([H|T],Acc,Sorted):-bubble(H,T,NT,Max),b_sort(NT,[Max|
Acc],Sorted).
bubble(X,[],[],X).

bubble(X,[Y|T],[Y|NT],Max):-X>Y,bubble(X,T,NT,Max).
bubble(X,[Y|T],[X|NT],Max):-X<=Y,bubble(Y,T,NT,Max).

Merge sort

Merge sort is usually used to sort large files but its idea can be
utilized to every list. If properly implemented it could be a very
efficient algorithm.
merge_sort([],[]).
merge_sort(List,Sorted):divide(List,L1,L2),merge_sort(L1,Sorted1),merge_sort(L2,Sorted
2),
merge(Sorted1,Sorted2,Sorted).
merge([],L,L).
merge(L,[],L):-L\=[].
merge([X|T1],[Y|T2],[X|T]):-X<=Y,merge(T1,[Y|T2],T).
merge([X|T1],[Y|T2],[Y|T]):-X>Y,merge([X|T1],T2,T).

Quick sort
Quick sort is one of the fastest sort algorithms. However, its
power is often overvalued. The efficiency of quick sort is sensitive
to choice of pivot which is used to distribute list into two "halfs".
quick_sort([],[]).

quick_sort([H|T],Sorted):pivoting(H,T,L1,L2),quick_sort(L1,Sorted1),quick_sort(L1,Sorte
d2),
append(Sorted1,[H|Sorted2]).
pivoting(H,[],[],[]).
pivoting(H,[X|T],[X|L],G):-X<=H,pivoting(H,T,L,G).
pivoting(H,[X|T],L,[X|G]):-X>H,pivoting(H,T,L,G).

The above implementation of quick sort using append is not very


effective. We can write better program using accumulator.
quick_sort2(List,Sorted):-q_sort(List,[],Sorted).
q_sort([],Acc,Acc).
q_sort([H|T],Acc,Sorted):pivoting(H,T,L1,L2),
quick_sort(L1,Acc,Sorted1),quick_sort(L1,[H|Sorted1],Sortd)

7.Short notes:
The traveling salesman problem
The problem is that in what order should the salesman visit all
the sites so they minimize the total distance traveled. The
salesman has to return to the starting point of the tour, so he
could choose any starting point. Each site can only be visited
once.

The query or subgoal such as vertices(X) will give X a value


consisting of a list of atoms, which represent the sites in the
problem instance. edges(X) will give X a value consisting of a
list of terms, each of the form path(A,B,C) where A and B are
the sites and C is the length of the path connecting A to B. The
distance C applies in either direction A to B or B to A.
Here are the two clauses of the problem.
vertices( [v1,v2,v3,v4,v5]).
edges([ path(v1,v2,5),
path(v2,v3,3),
path(v3,v4,2),
path(v4,v5,5),
path(v5,v1,3),
path(v1,v3,4),
path(v2,v4,5) ] ).
I need to develop a predicate tsp with one argument, that should
be satisfied if there exits a tour whose length is less that the
(numerical) value of that argument. I only need to test whether a
given number is greater than the length of the shortest tour.
The predicate tsp(N) will have a subgoal like vertices(X) that
will give X the site list, followed by some other variable say, y is a
permutation of X. Then we can test for whether the tour
associated with Y has a cost at most N.
For example
| ?- tsp(20).
The response could be
[v1,v2,v3,v4,v5]

yes
| ?8.Monkey & Banana
The Problem:
There is a monkey at the door into a room. Inthe middle of the
room a banana ishanging from the ceiling. The monkey is hungry
and
wants to get the banana, but he cannot stretch
high enough from the floor. At the window of
the room there is a box themonkey may use.
The monkey can perform the following
actions:
Walk on the floor
Climb the box
Push the box around (if it is already at the box)
Grasp the banana if standing on the box
directly under the banana.
Monkey World is described by some 'state'
that can change in time.
Current state is determined by the position of
the objects
State:
Monkey Horizontal
Monkey Vertical
Box Position
Has Banana
Initial State:
Monkey is at the door
Monkey is on floor
Box is at window
Monkey does not have banana
6. In prolog:
state(atdoor, onfloor, atwindow, hasnot).

Goal:
state(_, _, _, has). : Anonymous Variables
Allowed Moves:
Grasp banana
Climb box
Push box
Walk around
Not all moves are possible in every possible
state of the world e.g. grasp is only possible
if the monkey is standing on the box directly
under the banana and does not have the
banana yet.
Move from one state to another
In prolog:
move(State1, Move, State2)
Move :
Grasp
Climb
Push
Walk
Grasp
move(state(middle, onbox, middle, hasnot),
grasp,
state(middle, onbox, middle, has)).
Climb
move(state(P, onfloor, P, H),
climb,
state(P, onbox, P, H)).

Push
move(state(P1, onfloor, P1, H),
push(P1, P2),
state(P2, onfloor, P2, H)).
Walk
move(state(P1, onfloor, B, H),
walk(P1, P2),
state(P2, onfloor, B, H)).
Main question our program will pose:
Can the monkey in some initial state get the
banana?
Prolog predicate:
canget(State)
Canget(State)
(1) For any state in which the monkey already
has the banana the predicate is true
canget(state(_, _, _, has)).
Canget(State)
(2) In other cases one or more moves are
necessary. The monkey can get the banana in
any state (State1) if there is some move
(Move) from State1 to some state (State2),
such that the monkey can get the banana in
State2 (in zero or more moves).
canget(State1) :move(State1, Move, State2),
canget(State2).

Questions:
?- canget(state(atwindow, onfloor, atwindow, has)).
Yes
?- canget(state(atdoor, onfloor, atwindow, hasnot)).
Yes
?- canget(state(atwindow, onbox, atwindow, hasnot)).
No
Clause Order
Grasp
Climb
Push
Walk
Effectively says that the monkey prefers
grasping to climbing, climbing to pushing
etc...
This order of preferences helps the monkey
to solve the problem.
Reorder Clauses
Walk
7. Grasp
Climb
Push
This results in an infinite loop!
As the first move the monkey chooses will
always be move, therefore he moves
aimlessly around the room.
Conclusion:
A program in Prolog may be declaratively
correct, but procedurally incorrect.
i.e. Unable to find a solution when a solution

actually exists.

9.Eight Queen Problem


At first, we need to define a representation of the board. Each
square of chessboard can be represented by a pair of X and Y
coordinates. We can write such pair as X/Y, where / is only for
visual purposes. We know that each queen should be at a
different row or column thus, we can define a template as [1/Y1,
2/Y2, 3/Y3, 4/Y4, 5/Y5, 6/Y6, 7/Y7, 8/Y8]
We can generilize our solution with two cases. Case 1: THe list fo
queens is empty. This is in fact a solution since there is no attack.
Case 2. The list looks like this [X/Y|Others] Then we should look
at three conditions for this case (1) There must be no attack in
Others. Others must itself be a solution. (2) X and Y must be
integers between 1 and 8. (3) X/Y must not attack any queen in
Others. Our solution template means that all queens are in
different columns. Then a) Y-coordinates should be different, b)
they should not be in the same diagonal. Then here is the
algorithm:
solution([]).
solution([X/Y|Others]) :solution(Others),
member(Y, [1,2,3,4,5,6,7,8]),
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).

member(Item,[Item|Rest]).
member(Item,[First|Rest]) :member(Item,Rest).
template([1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8]).
After these
?- template(S), solution(S).
should give the solutions. Use ; to find more than one solution.

10.The "Water-jugs Problem"

This classic AI problem is described in Artificial Intelligence as


follows:
"You are given two jugs, a 4-gallon one and a 3-gallon one.
Neither has 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?".
E. Rich & K. Knight, Artificial Intelligence, 2nd edition, McGrawHill, 1991
This program implements an "environmentally responsible"
solution to the water jugs problem. Rather than filling and spilling

from an infinite water resource, we conserve a finite initial charge


with a third jug: (reservoir).
This approach is simpler than the traditional method, because
there are only two actions; it is more flexible than the traditional
method, because it can solve problems that are constrained by a
limited supply from the reservoir.
To simulate the infinite version, we use a filled reservoir with a
capacity greater than the combined capacities of the jugs, so that
the reservoir can never be emptied.
"Perfection is achieved not when there is nothing more to add,
but when there is nothing more to take away." Antoine de SaintExupry
Entry Point
The water_jugs solution is derived by a simple, breadth-first,
state-space search; and translated into a readable format by a
DCG.
water_jugs :SmallCapacity = 3,
LargeCapacity = 4,
Reservoir is SmallCapacity + LargeCapacity + 1,
volume( small, Capacities, SmallCapacity ),
volume( large, Capacities, LargeCapacity ),
volume( reservoir, Capacities, Reservoir ),
volume( small, Start, 0 ),
volume( large, Start, 0 ),
volume( reservoir, Start, Reservoir ),
volume( large, End, 2 ),

water_jugs_solution( Start, Capacities, End, Solution ),


phrase( narrative(Solution, Capacities, End), Chars ),
put_chars( Chars ).

11. Write a program in prolog to generate the depth


first search.
(define (DFS startState goalState)
(define (extend path moves)
(cond ((equal? (car path) goalState) path)
((null? moves) #f)
(else (or (extend (cons (car moves) path) (GET-MOVES (car
moves)))
(extend path (cdr moves))))))
(extend (list startState) (GET-MOVES startState)))

(define (GET-MOVES state)


(cond ((equal? (list-ref state 0) 'space) (list (swap state 0 1) (swap
state 0 3)))
((equal? (list-ref state 1) 'space) (list (swap state 1 0) (swap state
1 2)
(swap state 1 4)))
((equal? (list-ref state 2) 'space) (list (swap state 2 1) (swap state
2 5)))
((equal? (list-ref state 3) 'space) (list (swap state 3 0) (swap state
3 4)
(swap state 3 6)))
((equal? (list-ref state 4) 'space) (list (swap state 4 1) (swap state
4 3)
(swap state 4 5) (swap state 4 7)))
((equal? (list-ref state 5) 'space) (list (swap state 5 2) (swap state
5 4)
(swap state 5 8)))
((equal? (list-ref state 6) 'space) (list (swap state 6 3) (swap state
6 7)))

((equal? (list-ref state 7) 'space) (list (swap state 7 4) (swap state


7 6)

(swap state 7 8)))


((equal? (list-ref state 8) 'space) (list (swap state 8 5) (swap state
8 7)))))
(define (swap lst index1 index2)
(let ((val1 (list-ref lst index1)) (val2 (list-ref lst index2)))
(replace (replace lst index1 val2) index2 val1)))
(define (replace lst index item)
(if (= index 0)
(cons item (cdr lst))
(cons (car lst) (replace (cdr lst) (- index 1) item))))

11. Write a program in prolog to generate the depth


first search.
(define (BFS startState goalState)
(define (BFS-paths paths)
(cond ((null? paths) #f)
((equal? (caar paths) goalState) (car paths))
(else (BFS-paths (append (cdr paths)
(extend-all (car paths) (GET-MOVES (caar
paths))))))))

(define (extend-all path nextStates)


(if (null? nextStates)
'()
(cons (cons (car nextStates) path)
(extend-all path (cdr nextStates)))))
(BFS-paths (list (list startState))))

(0 0)

(4 0)

(4 3)

(0 3)

(4 3)

(0 0)

(0 0)

(4 0)

(3 0)

(0 3)

(1 3)

You might also like