Вы находитесь на странице: 1из 23

# Compiled by: Zafar Iqbal Khan

Lab Manual
CS 4500 Artificial Intelligence

Fall Session 1434, 2013
College of Computer Engineering & Sciences

Compiled by: Zafar Iqbal Khan

Compiled by: Zafar Iqbal Khan
A. Description
Course No. CS 4500
Course Name: Artificial Intelligence
Contact Hours: 1
Pre requisite: CS2300 Programming II

B. Objectives
This lab aims to introduce the basics of fact base programming and decision making. The lab
introduces prolog as language and SWI-Prolog as tool for testing routines written during lab
sessions.

C. Outcomes
1. Learn Prolog language
2. Gain experience with the installation of Open Source product SWI-Prolog
3. Hands on Experience at factual programming

D. Syllabus
Introduction to fact base programming, Prolog, SWI-Prolog as tool, Fact: Simple facts
and facts with arguments, Variables, Unification, simple unification, variable
unification, Rules, addition of rules with program, Backtracking, Fail, Cut, not,
Recursion: Family Tree, factorial, Operators and Arithmetic, Input and Output: Read
& Write.

Compiled by: Zafar Iqbal Khan
E. Schedule

F. Evaluation Method
Students understanding of subject and their knowledge of Prolog will be evaluated
by a number of Lab exercises & Assignments.
One midterm practice session will further help to assess the understanding of
students and accordingly shape the course covering. Finally, their overall
performance is examined by final lab exam.

G. References
Text Books-
1. Ivan Bratko, Prolog Programming for Artificial Intelligence, Addison
Wesley, 4 Ed. ISBN 0321417461, 9780321417466
2. Class Notes and Other stuff given during or before lab sessions

SN Topics Week
1
Preamble
Introduction to fact base programming, Prolog,
1
2
Facts:
Simple facts and facts with arguments
2
3
Variables & Unifications
Simple Unification
Variable Unifications
3
4
Rules
Simple Predicates, How to add a rule with
program
4-5
5
Back tracking
Fail, Cut, Not
6-7
6
Recursion
Family tree, Factorial
8-10
7
Input & Output
11
8 Operators and Arithmetic 12
10
Graph Traversal
Depth First Search, Breadth First Search
13-14
Compiled by: Zafar Iqbal Khan

Reference Books-
1. William F. Clocksin, Christopher S. Mellish, Programming in Prolog,
Springer, ISBN 3540006788, 9783540006787

Websites, blogs and Wikis-

1. For getting a Copy of SWI-Prolog Compiler

2. A comprehensive treatise to learn Prolog by Paul Brna
http://homepages.inf.ed.ac.uk/pbrna/prologbook/

3. WikiBook from Wikipedia
http://en.wikibooks.org/wiki/Prolog

4. SWI-Prolog Reference Manual
http://www.swi-prolog.org/pldoc/refman/

Compiled by: Zafar Iqbal Khan

Lab Session 01

Variables
A variable is a string of upper-case letters, lower-case letters, digits and underscore characters
that starts either with an upper-case letter or with underscore. For
example, X, Y, Variable, _tag, X_526, and List, List24, _head, Tail, _input and Output are all
Prolog variables.
The variable _ (that is, a single underscore character) is rather special. It's called the anonymous
variable.

Numbers
Real numbers aren't particularly important in typical Prolog applications. So although most
Prolog implementations do support floating point numbers or floats (that is, representations of
real numbers such as 1657.3087 or ) we are not going to discuss them in this course.
But integers (that is: ... -2, -1, 0, 1, 2, 3...) are useful for such tasks as counting the elements of a
list, and we'll discuss how to manipulate them in a later lecture. Their Prolog syntax is the
obvious one: 23, 1001, 0, -365, and so on.

Atoms
An atom is either:
1. A string of characters made up of upper-case letters, lower-case letters, digits, and the
underscore character, that begins with a lower-case letter. For
example: butch, big_cheese_burger, and m_monroe2.
2. An arbitrary sequence of character enclosed in single quotes. For example 'Vincent',
'The Gimp', 'Five_Dollar_Shake', '&^%&#@\$ &*', and ' '. The character between the
single quotes is called the atom name. Note that we are allowed to use spaces in such
atoms --- in fact, a common reason for using single quotes is so we can do precisely that.
3. A string of special characters. For example: @= and ====> and ; and :- are all atoms. As
we have seen, some of these atoms, such as ; and :- have a pre-defined meaning.
1. Which of the following sequences of characters are atoms, which are variables, and
which are neither?
a. gRACIOUS
b. Landmark
c. variable123
d. Variable2000
e. big_cheese_burger
f. big_cheese_sandwitch
g. Ali
h. _Wali
i. _Saud

2. convert the following into predicates
a. Ali is a man.
b. dog barks.
c. swan flies.
d. human are mortal.
f. wali grows.
g. saeed runs.
h. pigeon is a bird.
i. all birds flies.
j. a car has four wheels.

Lab Session 02
Clauses

1. Write a program in prolog to check for basic facts related to clause man.
/* facts */
man(ali).
man(wali).
man(saud).
man(saeed).

at prolog prompt check-

?- man(ali).
yes

?-man(saud).
yes

?-man(naif).
no

2. given the following facts about clause drive/2.

/* facts */

drive(ali, corolla).
drive(wali, camry).
drive(saeed, corolla).
drive(naif, corolla).
drive(hamza, camry).

a. What car wali drives?
b. How many persons drive corolla.
c. Who drives ford?
d. List all drivers with their cars.

a. ?- drive(wali, X).
X= camry

yes

b. ?-drive(X, corolla).
X=ali
/* press ; */
X=saeed
/* press ; */
X=naif
/* press ; again */

No

c. ?- drive(X, ford).

yes

d. ?- drive(X, Y).
X= ali
Y= corolla
/* press ; */
X=wali
Y=camry
/* press ; */
Y=ford
/* press ; */
X=saeed
Y=corolla
/* press ; */
X=naif
Y=corolla
/* press ; */
X=hamza
Y=camry

Lab Session -03
Predicates matching
1. Check the following for matching
a. =(abdullah, abdullah).
b. =(ali, abdullah).
c. ali=ali.
d. ali=ali.
e. 2 = 2.
f. 2 = 2.
h. X=Y.
i. eat(X,Y) =eat(mango, banana).
j. X=ali, X=wali.

2. Write a program in Prolog to test a given pair of points, whether they form a horizontal line or
vertical line.

horizontal(line(point(X,Y),point(Z,Y))).
vertical(line(point(X,Y),point(X,Z))).

at prolog prompt, check following

?- vertical(line(point(1,1),point(1,3))).

yes
?- vertical(line(point(1,1),point(3,2))).
no

Lab Session- 4
Objective: - Disjunction and Conjugation
Problems:-
1. A person can be assumed happy in either of following conditions. If he is healthy or if he is
wealthy or if he has friends.
/*facts*/
healthy(ali).
hasfriend(mahmood).

/*rule (;) is used for disjunction i.e. logical OR*/
happy(X):- healthy(X);
wealthy(X);
hasfreind(X).

2. A person gets itminan(contentment ) if he has Iman and he does zikr(remembrance) of Allah
SWT. Write program in prolog to check whether a given person has itminan or not.
/*facts */
iman(ali).
iman(wali).
zikr(ali).
zikr(saud).
/* rule (,) is used for conjugation i.e. logical AND */
itminan(X):- iman(X), zikr(X).

Lab Session -5
Family Tree
Objective: - To deuce predicate and check it from given clause.

We have only clause father(A,B), e.g. father(ali,wali)
e.g. to deduce grandfather relation we will do as-
grandfather(A,C):- father(A,B), father(B,C).
do following-
1. Deduce and check Uncle Relationship.

2. Deduce and check Cousin Relationship.

Ali
Hamza
Wali

Solution:-
First we will create fact base to be used in both exercises.
father(ali,wali).
father(ali, hamza).
father (ali, saud).
father(ali, saeed).
father(hamza,salman).
father(hamza, sultan).

1. Uncle relationship

uncle(X,Y):- father(A,X), father(A, Z), father(Z,Y).

include above fact base into code file and run following query at prolog prompt
?-uncle(wali,salman).
yes

?-uncle(wali, saeed).
No

2. Cousin relationship

cousin(X,Y):-father(A,X), father(B,Y), father(C,A), father(C,B).
include above fact base into code file and run following query at prolog prompt
?-cousin(saeed, salman).
yes

No
Lab Session -6
Algebraic Function
1. Write a prolog program for common algebraic function e.g. +,-,* etc.

sum:-
A is 10, nl,
B is 5, nl,
C is a+b,
write(sum is ),write(C).

On prolog prompt write,
?- sum.
sum is 15
yes

2. Write a prolog program for calculation of area and circumference of a circle. It must take value
of radius from user and output value of area and circumference.
circle:-
A is 3.14*R*R,
CF is 2*3.14*R,
write(Area of Circle is ), write(A), nl,
write(Circumference of Circle is ), write(CF).

On prolog prompt write
?-circle.
enter the radius of the circle : 10
Area of Circle is 314
Circumference of Circle is 12.56

Lab Session -7
Recursion
1. Write a program in Prolog to calculate the value of factorial of an integer.

fact(0,1).
fact(X, Y) :-
X is X-1,
fact(X1, Z),
Y is X*Z,!.
Lab Session -8
assert, asserta, assertz and retract

1. Write a program to show use of assert and retract in prolog.
on prolog prompt write-
1 ?- assert(man(ali)).

Yes
2 ?- assert(man(wali)).

Yes
3 ?- man(ali).

Yes
4 ?- man(saud).

No
5 ?- retract(man(ali)).

Yes
6 ?- man(ali).

No
7 ?- retract(man(wali)).

Yes
8 ?- man(X).

No

2. Write a program in prolog to show use of asserta and assertz.
on prolog prompt write-
?- assert(fruit(mango)).

Yes
?- assert(fruit(dates)).

Yes
?- asserta(fruit(orange)).
Yes
?- assertz(fruit(kiwi)).

Yes

?- fruit(X).

X = orange ;

X = mango ;

X = dates ;

X = kiwi ;

No
Lab Session -09
List Basics

1. Validate following list operations
a. [a, b, c] = [a, c, b].
b. [X] = [a, b, c].
c. [X|Y] = [I, am, a, Muslim].
d. [X,Y|Z] = [a, b, c, d, e, f].
e. [X|Y] =[].
f. [X|Y] = [[a, [b, c]], d].
g. [X|Y] = [a].

2. Write program in prolog to print elements of a list.

printlist([]).
printlist([X|Y]):- write(X), nl, printlist(Y).
Lab Session - 10
100

Objective: Write and Execute prolog program for BFS.
% BFS program using stacks
extend(Path,NewPaths),
append(Queue,NewPaths,NewQueue),
N is M+1.

extend([Node|Path],NewPaths) :-
findall([NewNode,Node|Path],
(arc(Node,NewNode,_),
\+ member(NewNode,Path)), % for avoiding loops
NewPaths).

1
100

4
3
50
125
100
5
2
75
125
125
75
50
4
3
2
5
1
% data according to the graph (node,node,weight)
arc(1,2,100).
arc(1,3,125).
arc(1,4,100).
arc(2,3,50).
arc(2,4,75).
arc(2,5,125).
arc(3,4,100).
arc(3,5,125).
arc(4,5,50).
arc(5,1,75).

% program ends here
To execute the program do this:

For traversing to node 5 from node 1 by BFS write following command
** Here N gives the number of node traversed and P returns path traversed

Lab Session - 11
Depth First Search Program
100

Objective: Write and Execute prolog program for BFS.
% DFS program using stacks
depth_first([[Goal|Path]|_],Goal,[Goal|Path],0).
depth_first([Path|Queue],Goal,FinalPath,N) :-
extend(Path,NewPaths),
append(NewPaths,Queue,NewQueue),
depth_first(NewQueue,Goal,FinalPath,M),
N is M+1.

extend([Node|Path],NewPaths) :-
findall([NewNode,Node|Path],
(arc(Node,NewNode,_),
\+ member(NewNode,Path)), % for avoiding loops
NewPaths).

1
100

4
3
50
125
100
5
2
75
125
125
75
50
4
3
2
5
1
% data according to the graph (node,node,weight)
arc(1,2,100).
arc(1,3,125).
arc(1,4,100).
arc(2,3,50).
arc(2,4,75).
arc(2,5,125).
arc(3,4,100).
arc(3,5,125).
arc(4,5,50).
arc(5,1,75).

% program ends here
To execute the program do this:

For traversing to node 5 from node 1 by DFS write following command
?- depth_first([],5,P,N).
** Here N gives the number of node traversed and P returns path traversed