0% found this document useful (0 votes)
67 views59 pages

AI Lab Manual Dessalew

The document describes a lab manual on artificial intelligence and Prolog programming. It covers topics like understanding Prolog, installing development tools, writing basic Prolog programs, working with variables and lists. Examples of Prolog code are provided to demonstrate facts, rules, and queries.

Uploaded by

dagne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views59 pages

AI Lab Manual Dessalew

The document describes a lab manual on artificial intelligence and Prolog programming. It covers topics like understanding Prolog, installing development tools, writing basic Prolog programs, working with variables and lists. Examples of Prolog code are provided to demonstrate facts, rules, and queries.

Uploaded by

dagne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

ARTIFICIAL INTELLIGENCE

Lab manual
By: Dessalew G.
AI Lab Manual By Dessalew 04/15/2024
Lab class one
1. Introduction
2. Understanding prolog programming
3. Installing developing tools
4. Starting basic prolog programing
5. Executing the first code
6. Exercise for lab one

AI Lab Manual By Dessalew 04/15/2024 2


Introduction
 Artificial intelligence has the prospects of replicating human intelligence in the future

 AI is a potential business developer and hence, many might want to understand AI

 For efficiently building AI systems one should know at least one programming

language

 In our lab session we are going to learn prolog programming, which is one of the

programming languages for AI.

AI Lab Manual By Dessalew 04/15/2024 3


Cont…
 Advantages :

 Easy to build database.

 Doesn’t need a lot of programming effort.

 Pattern matching is easy.

 Search is recursion based.

 It has built in list handling.

 Makes it easier to play with any algorithm involving lists

AI Lab Manual By Dessalew 04/15/2024 4


Understanding prolog
 Prolog is a logic - programming language

 It has important role in artificial intelligence

 Unlike many other programming languages, Prolog is intended primarily as a

declarative programming language

 In prolog, logic is expressed as relations (Facts and Rules)

 Core heart of prolog lies at the logic being applied

 Formulation or Computation is carried out by running a query over these relations

AI Lab Manual By Dessalew 04/15/2024 5


Cont…
 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.
 More complicated facts consist of a relation and the items that they refers to.

 These items are called arguments

 Facts can have arbitrary number of arguments from zero upwards

AI Lab Manual By Dessalew 04/15/2024 6


Cont…
 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 Socrates is a human. Our program will be :

mortal(X) :- human(X).

human(socrates).

AI Lab Manual By Dessalew 04/15/2024 7


Installing developing tools
 To code with prolog you need to install two basic software components

1. Text editor (sublime, notepad++, Vs code)

2. Compiler (SWI-PROLOG)

 Both of these software are open source, they are available freely.

 Now, start downloading both of these software.

 Once you download, install them one by one.

 When you open notepad++ and SWI-PROLOG the following interfaces are displayed

respectively.
AI Lab Manual By Dessalew 04/15/2024 8
Cont…

AI Lab Manual By Dessalew 04/15/2024 9


Cont…

AI Lab Manual By Dessalew 04/15/2024 10


Start basic prolog
programing
 Syntax and basic tasks

 Prolog program has two parts:

1. The program:
 In prolog, We declare some facts and rules

 These facts constitute the Knowledge Base of the system

2. We can query against the Knowledge Base

 We get output as affirmative if our query is already in the knowledge Base or

it is implied by Knowledge Base, otherwise we get output as negative


AI Lab Manual By Dessalew 04/15/2024 11
Cont…
 Knowledge Base can be considered as similar to database, against which we can

query

 Prolog facts are expressed in definite pattern

AI Lab Manual By Dessalew 04/15/2024 12


Cont…
 Entities are written within the parenthesis separated by comma (, )

 Their relation is expressed at the start and outside the parenthesis

 Variables are declared with Capital letters (start with capital letter)

 Every fact/rule ends with a dot (.)

Format : relation(entity1, entity2, ....N'th_entity)


Example :
friends(daniel, joseph).
teacher(yared).
odd_number(5).
AI Lab Manual By Dessalew 04/15/2024 13
Cont…
 Some basic operands

Operands Denoted by:


And ,

If :-

Or ;

Not not

AI Lab Manual By Dessalew 04/15/2024 14


Cont…
Facts
?- married(petros, marta). :false
likes(james, marta).
?- married(james, marta). :true
likes (marta, james). ?- likes(petros, marta). :true
?- likes(james, marta). :true
likes(petros, marta).
?- likes (marta, petros). :false
Rule
married(X,Y):-
likes(X,Y),
likes(Y,X).

AI Lab Manual By Dessalew 04/15/2024 15


Executing the first code
 Open your text editor (notepad++ or sublime or any other)
 Write the following facts and rules

AI Lab Manual By Dessalew 04/15/2024 16


Cont…
 Save the file as lab1.pl on the desktop (prolog files must be saved with .pl extension)
 Open SWI-PROLOG
 Write the path of the file you saved on the displayed interface as shown below

AI Lab Manual By Dessalew 04/15/2024 17


Cont…
 Query
 Write the following queries one by one (press Enter after each query)
 ?- married(petros, marta). :false
 ?- married(james, marta). :true
 ?- likes(petros, marta). :true
 ?- likes(james, marta). :true
 ?- likes(marta, petros). :false

 If the answer for the query exists on the facts and the rules the out put will be true,
otherwise the answer is false.
AI Lab Manual By Dessalew 04/15/2024 18
Cont…

AI Lab Manual By Dessalew 04/15/2024 19


Exercise for lab one
1. Write the facts and rules for grading system for specific course (students have the
mark, and write the rule for grading)
2. Write some queries for the above program.

AI Lab Manual By Dessalew 04/15/2024 20


The end of Lab one

AI Lab Manual By Dessalew 04/15/2024 21


Lab class two
1. Working with variables
2. Lists

AI Lab Manual By Dessalew 04/15/2024 22


1. Working with variables
eats(sam, dal). compatible(Person1,Person2):-
eats(josie, samosas). eats(Person1,Food1), rule
eats(sam, curry). eats(Person2,Food1).
eats(josie, curry). facts
eats(rajiv, burgers).
eats(rajiv, dal).

Queries with
eats(sam,What). one variable
eats(Who,curry).
compatible(sam, Who). queries
Compatible(Who,rajiv)
Queries wit
h more
than one va
eats(Who,What). riable
AI Lab Manual By Dessalew 04/15/2024 23
Cont…
vertical(line(point(X,Y), point(X,Y2))).
horizontal(line(point(X,Y), point(X2,Y))).

AI Lab Manual By Dessalew 04/15/2024 24


2. Lists
 The list is a simple data structure widely used in non-numeric programming.
 A list is a sequence of any number of items, such as ann, tennis, tom, skiing.
 Such a list can be written in Prolog as:

[ ann, tennis, tom, skiing]


 All structured objects in Prolog are trees
 The list is either empty or non-empty
 In the first case the list is simply written as a prolog atom, [].
 In the second case the list can be viewed as consisting of two things.
1. the first item, called the head of the list
2. the remaining part of the list, called the tail

AI Lab Manual By Dessalew 04/15/2024 25


cont.…
 For our example list

[ ann, tennis, tom, skiing]


the head is ann and the tail is the list
[ tennis, tom, skiing]
 In general, the head can be anything (any Prolog object, for example, a tree or a
variable);
 The tail has to be a list

[a,b,c] = [a | [b,c] ] = [a,b | [c] ] =[a,b,c | [] ]

AI Lab Manual By Dessalew 04/15/2024 26


Cont…
[a, b, c ]
≡[a | [b, c ] ]
≡[a | [b| [c ] ] ]
≡[a | [b| [c| [] ] ] ]
≡[a , b| [c ] ]

AI Lab Manual By Dessalew 04/15/2024 27


Some operators on lists
 Let us implement the membership relation as
member( X, L)
where X is an object and L is a list.
The goal member (X, L) is true if X occurs in L.
For example,

member( b, [a,b,c]) is
true
member( b, [a,[b,c]] )
is not true,
but member([b,c], [a,[b,c]] )
true
AI Lab Manual By Dessalew 04/15/2024 28
Cont…
 The program for the membership relation can be based on the following observation: X is a member of L

if either if
1. X is the head of L, or

2. X is a member of the tail of L.

 This can be written in two clauses, the first is a simple fact and the second is a rule:

member( X, [X I Tail] ).

member( X, [Head I Tail] ) :-

member( X, Tail).
AI Lab Manual By Dessalew 04/15/2024 29
List unification
The following table gives examples of list unification:

Variable binding list2 list1


X=book, Y=ball, Z=pen [ book, ball, pen ] [X, Y, Z]

X=7,Y=[ ] [X|Y] [7]


X=1,Y=2,Z=[3,4] [X, Y|Z ] [1, 2, 3, 4]
fail [3|X] [1,2]

AI Lab Manual By Dessalew 04/15/2024 30


Concatenation of lists
 For concatenating lists we will define the relation append( Ll, L2,L3)

Here Ll andL2 are two lists, and L3 is their concatenation.


For exampre
append( [a,b], [c,d], [a,b,c,d] ) is true,
but
append( [a,b], [c,d], [a,b,a,c,d] ) is false

append([1,2,3],[5,6],L).
Then we gate the concatenated list:
L= [1,2,3,5,6].

AI Lab Manual By Dessalew 04/15/2024 31


Exercise
1. Write a family tree knowledge base and work with variables
2. Write some clear list and perform list operations(membership, unification,
concatenation)

AI Lab Manual By Dessalew 04/15/2024 32


End of lab class two

AI Lab Manual By Dessalew 04/15/2024 33


Lab class 3
1. Arithmetic operations
2. Prolog recursion

AI Lab Manual By Dessalew 04/15/2024 34


1. Arithmetic operations
 Some of the arithmetic operations in prolog are explained by example as follows

X is 3+4 = 7 addition
X is 5-3 =2 subtraction
X is 3*4= 12 multiplication
X is 5/2 =2.5 division
X is 5//2 = 2 integer division
X is 75 mod 12 =3 modulus
X is 2**3 = 8 2 the power of three

AI Lab Manual By Dessalew 04/15/2024 35


Cont
 Fo to Co conversion

avg_temp(dire, 100).
avg_temp(harar, 68).

avg_temp_cel(Town, C_Temp):-
avg_temp(Town, F_Temp),
C_Temp is (F_Temp - 32) * 5//9.

AI Lab Manual By Dessalew 04/15/2024 36


2. Recursion
 Recursion is a technique in which one predicate uses itself (maybe with some other
predicates) to find the truth value.
let us look un example:
is_digesting(X,Y):- just_ate(X,Y).
is_digesting(X,Y):- just_ate(X,Z), is_digesting(Z,Y).
 So this predicate is recursive in nature.
 Suppose we say that:

just_ate(deer, grass),
 it means

is_digesting(deer, grass) is true.


AI Lab Manual By Dessalew 04/15/2024 37
Cont…
 Now if we say:

is_digesting(tiger, grass),
 this will be true if

is_digesting(tiger, grass) :- just_ate(tiger,


deer), is_digesting(deer, grass),
 then the statement

is_digesting(tiger, grass) is also true.

AI Lab Manual By Dessalew 04/15/2024 38


Cont…
 There may be some other examples also, so let us see one family example.

 So if we want to express the predecessor logic, that can be expressed using the

following diagrams

AI Lab Manual By Dessalew 04/15/2024 39


parent parent

Z
Predecessor

parent parent parent

Z
X

AI Lab Manual By Dessalew


Y1

Y2
Predecessor

parent parent parent


parent
X

Y1

Z
Y2

Y2

Predecessor

Predecessor

parent
Y
X

Z

Predecessor
04/15/2024
40
Cont…
 So we can understand the predecessor relationship is recursive.

 We can express this relationship using the following syntax

predecessor(X, Z) :- parent(X, Z).


predecessor(X, Z) :- parent(X, Y),predecessor(Y, Z).

AI Lab Manual By Dessalew 04/15/2024 41


Recursive function
 Consider the following pattern

%f(0) =0
%f(1)=2
%f(2)=3

The mathematical formula for this pattern is:


%f(n) = 2 * f(n-2) + f(n-1)
The prolog implementation of this recursive function is as follows

AI Lab Manual By Dessalew 04/15/2024 42


function(0,0).
function(1,2).
function(2,3).

function(X,Y) :- X1 is X-1,
X2 is X-2,
function(X1,Y1),
function(X2,Y2),

AI Lab Manual By Dessalew


Y is (Y2 * 2) + Y1. 04/15/2024 43
Factorial
factorial(0,1).
factorial(N,F) :-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N * F1.

AI Lab Manual By Dessalew 04/15/2024 44


Lab exercise for lab 3
1. Write a prolog program to add all the numbers in the range between two
numbers.
 example:
if the numbers are (1, 5),
then your program is going to add (1+2+3+4+5)

AI Lab Manual By Dessalew 04/15/2024 45


End of lab class three

AI Lab Manual By Dessalew 04/15/2024 46


Lab class 4
1. Strings in prolog
2. Type checking
3. The structure of terms

AI Lab Manual By Dessalew 04/15/2024 47


1. Strings in prolog
 Strings are represented in Prolog by a list of character codes

 Prolog offers double quotes for an easy notation for strings

 E.g.

?- S = “Vicky“. Query

S = [86,105,99,107,121] yes response

 There are several standard predicates for working with strings

 A particular useful one is atom_codes


AI Lab Manual By Dessalew 04/15/2024 48
Cont…
 E.g.

?- atom_codes(vicky,S).  query
S = [118,105,99,107,121] yes  response

AI Lab Manual By Dessalew 04/15/2024 49


1.1 Input output
 How can we receive input from a user and write an out put to the screen?

say_hi :-
write(‘ what is your name’),
read(X), String wise

write(‘Hi’),
write(X) .

AI Lab Manual By Dessalew 04/15/2024 50


Cont…
fav_char :-
write(‘what is your favorite character’),
get(X),
format(‘the ascii value ~w is ’, [X]), String wise

put(X), nl.

AI Lab Manual By Dessalew 04/15/2024 51


2. Type checking
 atom ---- Is the argument an atom?
 integer ---- is the argument an integer?
 float ---- is the argument a floating point number?
 number --- is the argument an integer or float?
 var --------- is the argument an un-instantiated variable?
 nonvar ---- is the argument instantiated variable or another term that is not an un-
instantiated variable

AI Lab Manual By Dessalew 04/15/2024 52


Cont… ?- atomic(mia). query
yes response
?- atom(a). query
?- atomic(5). query
yes response
yes response
?- atom(7). query
?- atomic(loves(vincent,mia)). query
no response
No response
?- atom(X). query
no response
?- X=a, atom(X). query
X = a. yes response
?- atom(X), X=a. query
no  response

AI Lab Manual By Dessalew 04/15/2024 53


Cont…
?- var(mia). query ?- nonvar(X). query
no response
no response
?- nonvar(mia). query
?- var(X). query yes response
?- nonvar(23). query
yes response
Yes response
?- X=5, var(X). query

no response

AI Lab Manual By Dessalew 04/15/2024 54


3. The structure of terms
 Given a complex term of unknown structure, what kind of information might we want

to extract from it?

 Obviously:

 The functor

 The arity

 The argument

 Prolog provides built-in predicates to produce this information

AI Lab Manual By Dessalew 04/15/2024 55


Cont…
 The functor predicate gives the functor and arity of a complex predicate

e.g.
?- functor(friends(lou,andy),F,A). query
F = friends
responses
A=2
?- functor(friends(lou,andy,eld),F,A). query
F = friends
responses
A=3

AI Lab Manual By Dessalew 04/15/2024 56


Cont…
 What happens when we use functor with constants?

?- functor(mia,F,A). query
F = mia
response
A=0
?- functor(14,F,A). query
F = 14
response
A=0

AI Lab Manual By Dessalew 04/15/2024 57


Cont…
 You can also use functor to construct terms:

e.g.
?- functor(Term,friends,2). query
Term = friends(_,_) response

AI Lab Manual By Dessalew 04/15/2024 58


Cont…
 Prolog also provides us with the predicate arg

 This predicate tells us about the arguments of complex terms

 It takes three arguments:

1. A number N

2. A complex term T

3. The Nth argument of T

?- arg(2,likes(lou,andy),A). query

A = andy yes response


AI Lab Manual By Dessalew 04/15/2024 59

You might also like