AI Lab Manual
AI Lab Manual
Lab Manual
for
Program: Regular
Semester: Second
Prepared by Melaku M
January 24, 2023
Contents
1. AI Programming with Prolog...................................................................................................... 3
2. Building blocks of the logic programming (Facts, Rules and Queries) ...................................... 3
3. Prolog in English......................................................................................................................... 4
5. Terminologies ............................................................................................................................. 5
12. Developing menu driven program to implement member, concatenation, add, and delete
functions ........................................................................................................................................ 20
A) FACTS
Facts are true statements that form the basis for the knowledge base. We can define fact as an
explicit relationship between objects, and the properties these objects might have. So, facts are
unconditionally true in nature.
Example: parent(jane,alan). Can be read as “Jane is the parent of Alan.”
B) RULES
► Rules are constraints which allow us to infer that a property or relationship holds based on
preconditions.
► Rules are defined as implicit relationships between objects. So, rules are conditionally true.
► A rule consists of a head (a predicate) and a body.
Example: parent(X,Y) :- mother(X,Y). Read as “Person X is the parent of person Y if X is Y’s
mother”
C) QUERIES
► To run a program, we ask questions about the database of facts and rules.
► Responses to user queries are determined through a form of inference control known as
resolution.
Example: parent(Who,alan). Read as “Who is the parent of alan?”
Who=jane
3. Prolog in English
4. Prolog in Prolog
5. Terminologies
Predicate
❖ Both facts and rules are predicate definitions.
❖ The name of the relationship, which comes just
before the round brackets, is called the predicate.
Clauses
❖ The prolog program contains a sequence of one or more clauses.
❖ The clauses are of two types: facts and rules.
❖ The following is a simple Prolog program with 2 clauses:
man(socrates).
mortal(X) :- man(X).
Arguments
❖ Arguments are parameters of the predicate.
❖ Arguments consist of terms, which can be:
✓ Constants e.g. jane,
✓ Variables e.g. Person1, X and etc.
Terms: Constants
❖ Constants are names that begin with lowercase letters.
Step 4: Asking queries: - After consulting the file, we can ask different queries to prolog systems
as described below in figure 4.
The query likes(ram, What) (i.e., the question “Ram likes what?”) and the prolog system answers
the question with mango, because the fact “likes(ram, mango)” has been previously stored in the
Prolog database.
?-likes(ram,What).
What= mango
❖ Next, suppose we want to know “Who likes Cindy?”. The corresponding query would be:
?-likes(Who, cindy).
❖ Who is a variable. We can also choose any other name for it, as long as it starts with a
capital letter. The Prolog interpreter replies as follows:
?-likes(Who, cindy).
Who= bill
➢ Queries: “Who owns what?”
?-owns(Who,What). /* Note: Who and What is used as variable. */
Who= john
What= gold
Step 5: To exit from Prolog: -type |?-halt. Or press Control + D
OUTCOME: Students will understand how to write simple facts and ask queries in prolog
programming.
8. Defining Rules
/* Facts */
dances(lili).
searchingFor(tom, food).
lovesToPlay(jack, cricket).
lovesToPlay(bili, cricket).
isClosed(school).
free(ryan).
/* Rules */
happy(X) :- dances(X).
hungry(X) :- searchingFor(X, food).
friends(X, Y):- lovesToPlay(X, cricket), lovesToPlay(Y, cricket).
goToPlay(W):- isClosed(X), free(W).
Create a prolog file with name “rule.pl”, and write up the above program, then consult it into the
prolog console window. After consulting the file into prolog console, we can ask the question as
follows:
Note: Before running these programs, please try to understand efficiently how the above English
statement is converted into facts and rule sets. If you understand the relationship between facts
and rules clearly, it is easy to interact with the programs.
Family relationship
% facts
male(jack).
male(peter).
male(oliver).
male(james).
male(simon).
male(fitsum).
female(helen).
female(sopia).
female(hana).
female(lily).
parent_of(jack,hana).
parent_of(jack,lily).
parent_of(helen,hana).
parent_of(helen,lily).
parent_of(peter,simon).
parent_of(hana,simon).
parent_of(lily,fitsum).
parent_of(james,fitsum).
parent_of(oliver,james).
parent_of(sopia,james).
% rules
mother(X,Y):-female(X),parent_of(X,Y).
father(X,Y):-male(X),parent_of(X,Y).
sister(X,Y):-parent_of(Z,X), parent_of(Z,Y),female(X),X\==Y.
brother_of(X,Y):-male(X), parent_of(Z, X), parent_of(Z, Y).
grandmother(X,Y):-female(X),parent_of(X, Z), parent_of(Z,Y).
grandfather(X,Y):-male(X),parent_of(X, Z), parent_of(Z,Y).
aunt(X,Z):-sister(X,Y), parent_of(Y,Z),female(X),!.
9. Lab Activity
Hint:
Activity 2: Write a Prolog code for the following facts and rules about family relationship and
show the valid/ correct response of the Prolog during the interaction.
Facts:
So far, we have seen that we can write a program and the query on the console to execute. In some
cases, we print something on the console, that are written in our prolog code. So here we will see
that writing and reading tasks in more detail using prolog.
The read() predicate is used to read from the console. The read() is generally used to read from the
console, but this can also be used to read from files. Now let us see one example to see how read()
works.
Note: To run these programs write “start” command in the prolog console after consulting it.
start:-write("Enter two number"), nl, read(Num1), nl, read(Num2),
Mult is Num1 * Num2, nl, write("The Multiplication of ")write(Num1), write("*"),
write(Num2), write("="), write(Mult).
Output
❖ Knowledge base(write_read_predicate.pl)
Note: To run these programs write “cube” in the prolog console after consulting it.
Output as follows
Prog. Write the prolog program to convert Celsius temperatures to Fahrenheit, the other checks
if a temperature is below freezing.
fact(0,1).
fact(N,F) :-
N>0,
N1 is N-1,
fact(N1,F1),
F is N * F1.
Output
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 apple, mango, banana etc. Such a list can be written in Prolog as:
1. Membership
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]) is. true. The program for the membership relation can be based on the following
observation:
X is a member of L if either
► X is the head of L, or
► 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 | Tail]).
member (X, [Head | Tail]) :- member( X, Tail).
2. Concatenation
For concatenating lists we will define the relation conc(Ll, L2, L3) Here L1 and L2 are two lists,
and L3 is their concatenation.
In the definition of conc we will have again two cases, depending on the first argument, L1:
► If the first argument is the empty list then the second and the third arguments must be the
same list (call it L); this is expressed by the following Prolog fact: conc([], L, L).
► If the first argument of conc is a non-empty list then it has a head and a tail and must look
like this: [X|L1]
Figure below explains about the concatenation of [X|L1] and some list L2. The result of the
concatenation is the list [X | L3] where L3 is the concatenation of L1 and L2.
3. Adding an item
To add an item to a list, it is easiest to put the new item in front of the list so that it becomes the
new head. If X is the new item and the list to which X is added is L then the resulting list is simply
[X |L ] .
add( X, L, [X I|L] ).
4. Deleting an item
Deleting an item, X, from a list, L, can be programmed as a relation del (X, L, L1) where L1 is
equal to the list L with the item X removed. The del relation can be defined similarly to the
membership relation.
we have, again, two cases:
1. If X is the head of the list, then the result after the deletion is the tail of the list.
2. If X is in the tail, then it is deleted from there
del(X,[X|L],L).
del(X,[Y|L],[Y|L1]):-
del(X,L,L1).
Output
Output
Lab activity 3: To solve the following problems you can use either prolog or any other AI
programming languages.
► Implement breadth first search algorithm.
► Solve any problems by using depth first search algorithm.
► Solve any problems by using breadth first search algorithm.
Water Jug problem definition: You are provided with two jugs: one having the capacity
to hold 3 lit of water and the other has the capacity to hold 4 lit of water. There is no other
water measuring equipment available and Neither jug has any measuring markings on it.
There is a pump that can be used to fill the jugs with water. How can you get exactly 2 lit
of water in the 4-lit of jug?
Solution:
Representation of water Jug Problem in terms of state-space search,
❖ We will represent a state of the problem as a tuple (X, Y).
Where,
✓ X represents the quantity of water in the 4-lit jug
✓ Y represents the quantity of water in 3-lit jug
✓ Note 0 ≤ X ≤ 4, and 0 ≤ Y ≤ 3.
❖ Start State: (0,0)
❖ Goal State: (2,Y), where 0 ≤ y ≤ 3.
contains(_,Y,1):-start(4,Y).
contains(X,_,2):-start(X,3).
contains(_,Y,3):-start(0,Y).
contains(X,_,4):-start(X,0).
contains(X,Y,5):-N is Y-4+X, start(4,N).
contains(X,Y,6):-N is X-3+Y, start(N,3).
contains(X,Y,7):-N is X+Y, start(N,0).
Step1: Identification:
► Selecting the appropriate problems. Determining the characteristics of the problem.
Includes clear identification of the problem, expert, user etc.
Step2- Conceptualization:
► Finding the concept to produce the solution, analyzing the problems.
Step3- Formalization:
► Designing structures to organize the knowledge.
Step4- Implementation:
► Formulating facts, rules which embody knowledge. select the techniques which are
appropriate for developing ES.
Step5- Testing:
► Validating the rules.
16.3 Mini Project: Medical Diagnosis Expert System
Getting the right diagnosis is a key aspect of healthcare. Diseases should be treated well and at
the right time. Diagnostic errors may lead to negative health outcomes, psychological distress, and
financial costs. If a diagnostic error occurs, inappropriate or unnecessary treatment may be given
to a patient, these problems may become the cause of losing the precious life. In an effort to address
such a problem, the project/study made an attempt to design and develop expert systems which
can provide advice and support for physicians and patients to facilitate the diagnosis process and
recommend treatment of patients.
cold:-verify(runny_nose),
verify(sneezing),
verify(headache),
verify(sore_throat),
verify(chills).
measles:-verify(runny_nose),
verify(sneezing),
verify(conjunctivitis),
verify(fever),
verify(rash),
verify(cough).
covid:- verify(sore_throat),
verify(tiredness),
verify(shortness_of_breath),
verify(dry_cough).
assert(no(Question)), fail).
/* A dynamic predicate is introduced using dynamic/1, after which clauses may be added using assertz/1 */
:-dynamic yes/1,no/1.
Project (20%)
Make a group of 1 to 5 teams (proceed with your final year project group) and select one project
area to develop a simple expert system using prolog facts and rules. With the progress of the
project, identify the basic underlying facts and rules of the domain. Then try to encode each of the
statements with the appropriate prolog codes to run it.