0% found this document useful (0 votes)
35 views

AI Lab Manual

artificial intelligence document

Uploaded by

eliaschane19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

AI Lab Manual

artificial intelligence document

Uploaded by

eliaschane19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Institute of Technology

Faculty of Computing and Software Engineering

Lab Manual

for

Target Group: G4 Software Engineering

Program: Regular

Semester: Second

Class Year: 2023

Prepared by Melaku M
January 24, 2023

Arba Minch, Ethiopia


Introduction to Artificial Intelligence Lab Manual

Contents
1. AI Programming with Prolog...................................................................................................... 3

2. Building blocks of the logic programming (Facts, Rules and Queries) ...................................... 3

3. Prolog in English......................................................................................................................... 4

4. Prolog in Prolog .......................................................................................................................... 5

5. Terminologies ............................................................................................................................. 5

6. Simple Prolog programs to represent facts ................................................................................. 7

7. Steps to execute simple prolog programs ................................................................................... 7

8. Defining Rules ......................................................................................................................... 10

9. Lab Activity .............................................................................................................................. 12

10. Handling input and output in prolog ....................................................................................... 14

10.1 The write() Predicate ........................................................................................................ 14

10.2 The read() Predicate ......................................................................................................... 14

10.3 Read and write predicate with practical demonstrations ................................................. 15

11. List Manipulation .................................................................................................................... 17

12. Developing menu driven program to implement member, concatenation, add, and delete
functions ........................................................................................................................................ 20

13. Write a program to implement depth first search ................................................................... 22

14. Write a program to implement Breadth first search................................................................ 24

15. Water Jug problem in AI......................................................................................................... 24

16. Designing Expert System ........................................................................................................ 26

16.1 Components of an expert system ....................................................................................... 27

16.2 Steps to Develop an Expert System: .................................................................................. 27

16.3 Mini Project: Medical Diagnosis Expert System .............................................................. 28

Target Group: G4 Software Engineering Prepared by Melaku M. Page 2


Introduction to Artificial Intelligence Lab Manual

1. AI Programming with Prolog

Prolog (Logic programming) is a programming paradigm in which problems are expressed as


facts and rules by the program statements but with the system of formal logic. In it, logic is used
to represent knowledge, and inference is used to manipulate it, based on all available data.

❖ Prolog is a programming language for symbolic, non-numeric computation.


❖ Prolog is used for solving problems that involve objects and the relationships b/n objects.
❖ A program consists of a database containing one or more facts and zero or more rules.

Logic programming has applications in AI and databases: -


► Natural language processing (NLP)
► Intelligent Database Retrieval
► Machine Learning
► Robot Planning
► Automated reasoning and theorem proving
► Expert systems (e.g., MYCIN)

2. Building blocks of the logic programming (Facts, Rules and Queries)

Programming in PROLOG 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.

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.”

Target Group: G4 Software Engineering Prepared by Melaku M. Page 3


Introduction to Artificial Intelligence Lab Manual

Following are some guidelines to write facts:


• Names of properties/relationships begin with lowercase letters.
• The properties /relationship name appears as the first term.
• A period "." must come at the end of fact.
Syntax: relation(object1,object2...).

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

Target Group: G4 Software Engineering Prepared by Melaku M. Page 4


Introduction to Artificial Intelligence Lab Manual

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.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 5


Introduction to Artificial Intelligence Lab Manual

❖ Constants can either be:


o Numbers:
▪ integers are the usual form (e.g. 1, 0, -1, etc), but
▪ floating-point numbers can also be used (e.g. 3.0E7)
o Symbolic (non-numeric) constants:
▪ always start with a lower-case alphabetic character and contain any mixture of
letters, digits, and underscores (but no spaces, punctuation, or an initial capital).
▪ e.g. abc, big_long_constant, x4_3t).
o String constants:
▪ are anything between single quotes e.g. ‘Like this’.
Terms: Variables
❖ Variables take the place of constants in facts or rules. Variables begin with upper case
letters.
❖ e.g. X, ABC, _89two5, _very_long_variable
Naming Tips
► Use real English when naming predicates, constants, and variables.
e.g. “John wants to help Somebody.”
Could be: wants(john,to_help,Somebody).
Not: x87g(j,_789).
► Use a Verb Subject Object structure to represent facts and rules.
► BUT do not assume prolog understands the meaning of your chosen names!
Unification
❖ Unification is the process of finding an instantiation of a variable for which “match” is
found in a database of facts and rules.
❖ When two terms match, we say that they unify, their structures and arguments are
compatible. The matching process works from left to right.
Instantiation
► Binding of a variable to value (and thus, a type).

Target Group: G4 Software Engineering Prepared by Melaku M. Page 6


Introduction to Artificial Intelligence Lab Manual

Figure 1: Unification and instantiation

6. Simple Prolog programs to represent facts

OBJECTIVE: To Familiarize with writing simple fact

a. Ram likes mango.


b. Seema is a girl.
c. Bill likes Cindy.
d. Rose is red.
e. Priya can cook and dance.
f. John owns gold.

7. Steps to execute simple prolog programs

Step 1: Install SWI-prolog in your computer and launch it.


Step 2: Creating a prolog file: - Create a file by clicking on “file menu” then choose “new”
option, next to this save the file with .pl extension e.g., “fact.pl” and write the above simple prolog
programs as presented in figure 2.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 7


Introduction to Artificial Intelligence Lab Manual

Figure 2: Writing simple prolog program


Step 3: Running a program: - To run the program, load/insert the file into the prolog
system/console by clicking on “file” menu option then choose “Consult” option. After that, open
the intended or required file from the location where you have saved it prior, as shown in figure 3.

Figure 3: Consulting the file into prolog


Alternatively, you can consult the file by using the “consult” command in the prolog console.
Example: |?- consult(‘fact’). Or [fact].
Note: this method works only, if you are working in the same directory of the prolog interpreter.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 8


Introduction to Artificial Intelligence Lab Manual

Step 4: Asking queries: - After consulting the file, we can ask different queries to prolog systems
as described below in figure 4.

Figure 4: Asking queries to prolog system


?-likes(ram, What).
Note: What is used as a variable. In place of What you can use another variable like X.

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

Target Group: G4 Software Engineering Prepared by Melaku M. Page 9


Introduction to Artificial Intelligence Lab Manual

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

Objective: To understand how to define rules

❖ Lili is happy if she dances.


❖ Tom is hungry if he is searching for food.
❖ Jack and Bili are friends if both of them love to play cricket.
❖ Ryan will go to play if school is closed, and he is free.

Writing a syntactically correct facts & rules


Goal: to motivate logical representations of facts and rules in prolog.

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

Target Group: G4 Software Engineering Prepared by Melaku M. Page 10


Introduction to Artificial Intelligence Lab Manual

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.

Alternative consult commands: - consult(‘filename.pl’).

Queries: Is lili happy?


?- happy(lili).
true. //True: means lili is happy.
Queries: Are jack and bili friends?
?- friends(jack, bili).
true. //True: means jack and bili are friends.
Queries: Are Kebede and bili friends?
?- friends(kebede, bili).
False. // False: means kebede and bili are not friends.

Family relationship

Mother relationship Sister relationship

Target Group: G4 Software Engineering Prepared by Melaku M. Page 11


Introduction to Artificial Intelligence Lab Manual

% 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

Activity 1: Write a prolog code for the following family tree.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 12


Introduction to Artificial Intelligence Lab Manual

Hint:

Facts Generate rules for the following


• Male - Bob • Child
- Chad • Siblings
- Frank • Brother
- Gary • Sister
- Howard, jane, kelly • Father
- Isaiah • Mother
• Aunt
• Female • Uncle
• Parent • Grand mother
• Grand father

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:

Nigus and Nigist are parents.


Nigus and Leul are male
Nigist and Leilit are female.
Leul and Leilit are children of Nigus and Nigist
Rules:

1. If someone is male and parent then he is father.


2. If someone is female and parent then she is mother.
3. If X is male and is the child of Y then X is son of Y.
4. If X is female and is the child of Y then X is daughter of Y.
5. If X is female and is child of Z and Y is also child of Z then X is sister of Y.
6. If X is male and is the child of Z and Y is also child of Z then X is brother of Y.
7. If X is mother and Y is the child of X then X is mother of Y.
8. If X is the father and Y is the child of X then X is father of Y.
9. If X is the mother of Z and Y is the father of Z then X is the wife of Y.
10. If X is the father of Z and Y is the mother of Z then X is the husband of Y.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 13


Introduction to Artificial Intelligence Lab Manual

10. Handling input and output in prolog

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.

Objective: Understanding how to handle input and output with prolog

10.1 The write() Predicate


The write() predicate is used to write the contents into the prolog console.

10.2 The read() Predicate

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.

Examples of read() predicate.


❖ Knowledge base (read_predicate.pl).

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

Target Group: G4 Software Engineering Prepared by Melaku M. Page 14


Introduction to Artificial Intelligence Lab Manual

Output

10.3 Read and write predicate with practical demonstrations

❖ Knowledge base(write_read_predicate.pl)

Note: To run these programs write “cube” in the prolog console after consulting it.

cube:-write("Enter the number:"), read(Number), nl, process(Number).


process(stop):- !.
process(Number):-Cu is Number * Number * Number, write("The cube of "),write(Number), write(": "),
write(Cu),nl, cube.

Output as follows

Understanding how to write a program using rules in prolog.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 15


Introduction to Artificial Intelligence Lab Manual

Prog. Write the prolog program to convert Celsius temperatures to Fahrenheit, the other checks
if a temperature is below freezing.

Prog. Write a program to find the factorial of given number*/

fact(0,1).
fact(N,F) :-
N>0,
N1 is N-1,
fact(N1,F1),
F is N * F1.
Output

How the factorial program works(presented below)

Target Group: G4 Software Engineering Prepared by Melaku M. Page 16


Introduction to Artificial Intelligence Lab Manual

11. List Manipulation

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:

[ apple, mango, banana]

Lists can be viewed as consisting of two things: -

► the first item, called the head of the list;


► the remaining part of the list, called the tail.
For our example lists
[ apple, mango, banana]
The head is apple and the tail is the list [ mango, banana]

Some operations on lists

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.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 17


Introduction to Artificial Intelligence Lab Manual

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.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 18


Introduction to Artificial Intelligence Lab Manual

Figure: Concatenation of lists.


ln prolog this is written as:
concatenation( [], L, L).
concatenation( [X| L1], L2, [X |L3] ) :- conc( Ll, L2,L3).

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

Target Group: G4 Software Engineering Prepared by Melaku M. Page 19


Introduction to Artificial Intelligence Lab Manual

12. Developing menu driven program to implement member, concatenation,


add, and delete functions
Programs to be saved as “menu.pl”

start:-write( "AI Menu Driven Programs"),


write("\n 1. Concatenation \n 2. Member\n \n 3. Delete \n 4. Add\n "),
write(" Enter the choice :: "),
read(X),
choice(X).

%Concatenating two lists


choice(1):-write("\nEnter the 1st set "),
read(Y1),
write("\nEnter the 2nd set"),
read(Y2),
concatenation(Y1,Y2,Y3),
write(Y3),
write(" is the concatenated list"),
write("\nEnter the choice :: "),
read(X),
choice(X).
concatenation([],L,L). /*to concatenate two lists*/
concatenation([H|T],L1,[H|L2]):-
concatenation(T,L1,L2).
% membership: checking availability of elements in the list.
choice(2):-write("Enter the value (element): "),
read(X),
write("Enter the list "),
read(Y),
member(X,Y),
write(X),
write(" is available in the list"),

Target Group: G4 Software Engineering Prepared by Melaku M. Page 20


Introduction to Artificial Intelligence Lab Manual

write("\n Enter the choice :: "),


read(Q),
choice(Q).
member(X,[X|L]). /*to find the member*/
member(X,[F|L]):-member(X,L).

%deleting an element from the list


choice(3):-write("Enter an element to deleted: "), read(X),
write("Enter the list "),
read(Y1),
del(X,Y1,Y2),
write(Y2),
write(" is the list after deleting"),
write("\nEnter the choice :: "),
read(X),
choice(X).
del(X,[X|L],L).
del(X,[Y|L],[Y|L1]):-
del(X,L,L1).

%inserting an element to list


choice(4):-write("Enter an element to be added: "),
read(X),
write("Enter the list"),
read(Y1),
add(X,Y1,Y2),
write(Y2),
write(" is the list after insertion"),
write("\nEnter the choice :: "),
read(X),
choice(X).
add(X,L,[X|L]).

Target Group: G4 Software Engineering Prepared by Melaku M. Page 21


Introduction to Artificial Intelligence Lab Manual

Output

13. Write a program to implement depth first search

child(a,b). /*b is child of a*/


child(a,c). /*c is child of a*/
child(a,d). /*d is child of a*/
child(b,e). /*b is child of b*/
child(b,f). /*f is child of b*/
child(c,g). /*g is child of c*/
path(A,G,[A|Z]):- /*to find the path from root to leaf*/
childnode(A,G,Z).
childnode(A,G,[G]):- /*to determine whether a node is child of other*/
child(A,G).
childnode(A,G,[X|L]):-
child(A,X),
childnode(X,G,L).

Output

Target Group: G4 Software Engineering Prepared by Melaku M. Page 22


Introduction to Artificial Intelligence Lab Manual

Target Group: G4 Software Engineering Prepared by Melaku M. Page 23


Introduction to Artificial Intelligence Lab Manual

14. Write a program to implement Breadth first search

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.

15. Water Jug problem in AI

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.

► We basically perform three operations to achieve the goal.


1. Fill a water jug.
2. Empty water jug
3. Transfer water jug: We pour the water from one jug into another one.
Generating production rules for the solving water jug problem
Operators – we must define a set of operators that will take us from one state to another

Target Group: G4 Software Engineering Prepared by Melaku M. Page 24


Introduction to Artificial Intelligence Lab Manual

Rule Description of action taken Conditions Final States


1 Fill 4-lit jug (X,Y | X<4) → (4,Y)
2 Fill 3-lit jug (X,Y |Y<3) → (X,3)
3 Empty 4- lit jug (X,Y |X>0) → (0,Y)
4 Empty 3-lit jug (X,Y | Y>0) → (X,0)
5 Pour water from 3-lit jug to fill 4- (X,Y | 0 < x+y ≥ 4 and y > →(4,Y-(4-
lit jug 0) X))
6 Pour water from 4-lit jug to (X,Y | 0 < x+y ≥ 3 and x > →(X-(3-
fill 3-lit jug. 0) Y),3)
7 Pour all of water from 3-lit jug (X,Y | 0 < x+y ≤ 4 and y ≥ → (X+Y,0)
into 4-lit jug 0)
8 Pour all of water from 4-lit jug (X,Y | 0 < x+y ≤ 3 and x ≥ → (0,X+Y)
into 3-lit jug 0)
9 Pour 2 lit of water from 3 lit jug (0,2) → (2,0)
into 4 lit jug

Implementing water jug problems


start(2,0):-write(' 4lit Jug: 2 | 3lit Jug: 0|\n'),
write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'),
write('Goal Reached! Congrats!!\n'),
write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n').
start(X,Y):-write(' 4lit Jug: '),write(X),write('| 3lit Jug: '),
write(Y),write('|\n'),
write(' Enter the move::'),
read(N),
contains(X,Y,N).

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

Target Group: G4 Software Engineering Prepared by Melaku M. Page 25


Introduction to Artificial Intelligence Lab Manual

contains(X,Y,8):-N is X+Y, start(0,N).

main():-write(' Water Jug Game \n'),


write('Intial State: 4lit Jug- 0lit\n'),
write(' 3lit Jug- 0lit\n'),
write('Final State: 4lit Jug- 2lit\n'),
write(' 3lit Jug- 0lit\n'),
write('Follow the Rules: \n'),
write('Rule 1: Fill 4lit Jug\n'),
write('Rule 2: Fill 3lit Jug\n'),
write('Rule 3: Empty 4lit Jug\n'),
write('Rule 4: Empty 3lit Jug\n'),
write('Rule 5: Pour water from 3lit Jug to fill 4lit Jug\n'),
write('Rule 6: Pour water from 4lit Jug to fill 3lit Jug\n'),
write('Rule 7: Pour all of water from 3lit Jug to 4lit Jug\n'),
write('Rule 8: Pour all of water from 4lit Jug to 3lit Jug\n'),
write(' 4lit Jug: 0 | 3lit Jug: 0'),nl,
write(' Enter the move::'),
read(N),nl,
contains(0,0,N).

16. Designing Expert System


The expert system is an intelligent computer program that solves the problems in a specialized
domain that normally requires human expertise.
❖ Expert system manipulates the encoded kgs to solve the problems, make decisions or
draw the conclusion, and answer the queries.
❖ An expert system’s knowledge is obtained from expert sources such as: -
o Textbooks, journal articles, databases, domain experts etc.
❖ Once a sufficient body of expert knowledge has been acquired,
o it must be encoded in a form suitable for the system to use in its inference or
reasoning processes.
o It must be loaded into the knowledge base, then tested, and refined.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 26


Introduction to Artificial Intelligence Lab Manual

16.1 Components of an expert system

The components of the expert system consist of four major parts.


1. User Interface
❖ It enables the users to enter instruction and information into the expert system and to
receive information from it.
2. Knowledge Base
❖ Knowledge base contains a really high-quality and extraordinary knowledge in a
particular domain. In short, kg base is the database of facts and rules.
❖ An exceptionally remarkable knowledge is required to model intelligent systems.
❖ Kg base must be accurate and also the bug-free b/c the whole success of an expert system
depends upon the knowledge.
3. Inference engine
❖ The inference engine of the expert system is the rule that defines how the expert process
interprets the knowledge in an appropriate manner.
❖ In simple terms, the inference engine takes the knowledge base and then it applies
forward chaining or backward chaining and it comes out with a conclusion.
4. Development Engine
❖ Development engine is used to create the expert system. This process usually
involved building the rule set.
❖ There are two basic approaches-
o Programming Language: An ES can be created using programming
language, e.g., Lisp, Prolog.
o Expert system shell: readymade processor that can be tailored to specific
problem domain through the addition of the appropriate Kg base.\
16.2 Steps to Develop an Expert System:

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.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 27


Introduction to Artificial Intelligence Lab Manual

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

Rationale of designing 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.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 28


Introduction to Artificial Intelligence Lab Manual

Sample codes of the Medical Diagnosis Expert System

/* Medical Diagnosis Expert System */


go:-hyphothesis(Disease),
write("The patient have desease:"),
write(Disease), nl,
undo.

/* hyphothesis that have been tested */


hyphothesis(malaria):-malaria, !.
hyphothesis(cold):-cold, !.
hyphothesis(measles):-measles, !.
hyphothesis(covid):-covid,!.
hypothesize(unknown). /* no diagnosis */

/* hyphothesis disease identification rule */


malaria:-verify(fever),
verify(headache),
verify(vomiting),
verify(nausen),
verify(sweating).

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

ask(Question):-write("Does the patient have: "),


write(Question), nl,
read(Response),nl,
((Response=yes; Response==y)
->assert(yes(Question));

Target Group: G4 Software Engineering Prepared by Melaku M. Page 29


Introduction to Artificial Intelligence Lab Manual

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.

/* verify the Symptom of Disease from the patient */


verify(Symptom):-(yes(Symptom)-> true; (no(Symptom)->fail; ask(Symptom))).

/* undo all yes and no assertions */


undo:-retract(yes(_)), fail.
undo:- retract(no(_)), fail.
undo.

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.

Target Group: G4 Software Engineering Prepared by Melaku M. Page 30

You might also like