You are on page 1of 42

Fundamentals of Artificial

Intelligence
Module Code & Version #

Knowledge Representation
Topic & Structure of The Lesson

• Rule-based Representation (Production Rules)


• Network Based Representation (Semantic Network)
• Frames Based Representation

Module Code and Module Title Title of Slides Slide 2 of 21


Learning Outcomes

• At the end of this topic, You should be able to:


– Explain about rule-based, network-based, and frames.
– Implement rule-based, network-based, and frames to represent
knowledge.

Module Code and Module Title Title of Slides Slide ‹3› of 24


Key Terms You Must Be Able To
Use
• If you have mastered this topic, you should be able to use
the following terms correctly in your assignments and
exams:

• Procedural Representation
• Semantic Networks
• Frames

Module Code and Module Title Title of Slides Slide ‹4› of 24


Rule-based Representation

• It also known as Production Rules.


• It represent knowledge in terms of a set of rules that tell you
what you should do or what you can conclude from different
situations.
• If a given condition is met then an associated action or series
of actions is performed.
• It has THREE (3) components:
- Database of facts (working memory)
- Set of Production Rules that alter the facts in the
database of facts. It is the form of IF <condition> THEN
<action>
- Interpreter that decides which rules to apply and handle
conflicts.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
Database of Facts (Working Memory)
• It represent all the knowledge of the system at any given
moment. A simple database of facts that are true of the
domain at that time. It resemble human working memory.
• The content of the database change as facts are added or
remove according to the application of the rules.

Production Rules
• It is in form of IF <condition> THEN <action>.
• If the condition of the rules is True (according to the facts
currently in the working memory), the action associated with
the rule is performed.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
Interpreter
• It is responsible in finding the rules whose condition are
matched. It must then decides which rule to apply.
• If there is more than one rule whose condition matches, then
apply conflict resolution strategy – will be discuss later
• Strategy 1:
- Choose the first rule that matches.
- If this is the case the sequence of writing the production
rules is important.
• Strategy 2:
- Favor the most specific rule.
- Choosing a rule that matches all the conditions of its
contenders but also contain further conditions that
match or
- Choosing the rule that instantiate variables or qualifies a
fact.
Module Code and Module Title Title of Slides Slide ‹ 5 of 24
Rule-based Representation
Example

IF <salary is high> and <age>40>


is more specific than
IF <salary is high>

Similarly,

IF <salary > 40,000>


is more specific than
IF <salary > 20,000>
Since fewer instances will match it.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
R1: IF <client working? is unknown> R6: IF <client working? is NO> and
THEN ask “Ask are you working?” <client student? is YES>
read WORKING THEN discuss student loan
remove <client working? is unknown> clear database
add <client working? is working> finish

R2: IF <client working? is YES> and <salary is unknown> R7: IF <client working? is NO> and
THEN ask “What is your salary?” <client student? is NO>
read SALARY THEN refuse loan
remove <salary? is unknown> clear database
add <salary is SALARY> finish

R3: IF <client working? is YES> and <salary is SALARY>


and SALARY > (5*AMOUNT REQUESTED)
THEN grant loan AMOUNT REQUESTED
clear database
finish

R4: IF <client working? is YES> and <salary is SALARY>


and SALARY < (5*AMOUNT REQUESTED)
THEN grant loan (SALARY/5)
clear database
finish

R5: IF <client working? is NO>


and <client student? is unknown>
THEN ask “Are you a student?”
read STUDENT
remove <client student? is unknown>
add <client student? is STUDENT>

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
Initial State

Interface Working Memory

<client working? is unknown>


<client student? is unknown>
<salary is unknown>
<AMOUNT REQUESTED is RM2000>

Fire Rule 1

Interface Working Memory

Q: Are you working? <client working? YES>


A: Yes <client student? is unknown>
<salary is unknown>
<AMOUNT REQUESTED is RM2000>

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
Fire Rule 2

Interface Working Memory

Q: Are you working? <client working? YES>


A: Yes <client student? is unknown>
Q: What is your salary? <salary is RM7500>
A: RM7500 <AMOUNT REQUESTED is RM2000>

Fire Rule 4

Interface Working Memory

Q: Are you working? <client working? YES>


A: Yes <client student? is unknown>
Q: What is your salary? <salary is RM7500>
A: RM7500 <AMOUNT REQUESTED is RM2000>
R: Grant loan: RM1500 <grant loan RM1500>
Final State

Interface Working Memory

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Forward Chaining

• It is a method of reasoning which the rules are applied to the


existing facts to extract additional facts until a conclusion is
achieved.
• Facts are held in working memory which is continually
updated as the rules are invoked.
• Rules represent possible actions to take when specific facts
occur in the working memory – production rules. They are
held in knowledge base. These actions are adding or
removing facts from the working memory, others are such as
printing message.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Forward Chaining

How it works?
1. Find all the rules that have condition (IF part) satisfied.
2. Select one, using conflict resolution strategies. (discuss
later)
3. Perform action in conclusion, possibly modifying the
working memory until no rules can be fire or halt in working
memory.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Forward Chaining
R1: IF hot AND smoky THEN ADD fire
R2: IF alarm_beeps THEN ADD smoky
R3: IF fire THEN ADD switch_on_sprinklers

Initial State Fire R2 Fire R1

Working Memory Working Memory Working Memory


F1: alarm_beeps F1: alarm_beeps F1: alarm_beeps
F2: hot F2: hot F2: hot
F3: smoky F3: smoky
F4: fire

Fire R3/Final State

Working Memory Note:


F1: alarm_beeps Rules are fire depends on what is in working
F2: hot memory and not the order in which the rules
F3: smoky are given. However what happens if, in some
F4: fire cycle, more than one rule has its condition
F5: switch_on_sprinklers satisfied?

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Forward Chaining
R1: IF hot AND smoky THEN ADD fire
R2: IF alarm_beeps THEN ADD smoky
R3: IF fire THEN ADD switch_on_sprinklers
R4: IF dry THEN ADD switch_on_humidifier
R5: IF sprinklers_on THEN DELETE dry

Working Memory This can be done either:


F1: alarm_beeps Fire R4, then humidifier is switch on OR;
F2: hot Fire R2, followed by R1, R3, R4, and R5 – dry is deleted
F3: dry
from working memory and the humidifier never switch
on.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Forward Chaining

Conflict Resolution Strategy for Forward Chaining:


Strategy 1: Choose first rule that matches.
• Based on the example, alarm_beeps in at the top in the
working memory, thus R2 will be chosen.

Strategy 2: Prefer to fire rules that involve the facts that have
been recently added to working memory.
• If initially R2 fires, smoky will be added to the working
memory, thus R1 will fires rather than R4 as smoky has been
recently added to the working memory.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Forward Chaining

Strategy 3: Prefer to fire rules with more specific condition.


• For example, a new rule R6: IF hot THEN ADD summer,
added to the knowledge base. R1 would be fired in
preference to R6 as it has more specific condition.
R1: IF hot AND smoky THEN ADD fire
R2: IF alarm_beeps THEN ADD smoky
R3: IF fire THEN ADD switch_on_sprinklers
R4: IF dry THEN ADD switch_on_humidifier
R5: IF sprinklers_on THEN DELETE dry
R6: ID hot THEN ADD summer

Strategy 4: Allow user to prioritize the rules.


• R4 in the example could have a very low priority, only fire of
nothing else can be fire.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Forward Chaining

Pattern Matching – adding variable into the production rules.


This will increase the flexibility of the rules.

R7: IF tempreture (R, hot) AND environment (R, smoky)


THEN ADD fire_in(R)

Working Memory
F6: tempreture (kitchen, hot)
F7: environment (kitchen, smoky)
F8: fire_in (R)

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Backward Chaining

• It is a method of reasoning which involved backtracking the


conclusion (hypothesis) to rules and facts that led to the
conclusion.
• It avoid drawing irrelevant inferences and focus on
hypothesis in the question.
• Let’s get the fire example previously, the conclusion or
hypothesis here is “should the sprinklers be switched on?”.

How it works?
1. If G is in the initial facts in the working memory then it is
proven.
2. Otherwise, find a rule which can be used to conclude G, and
try to prove each of the rule’s preconditions. G is then
proved True if all the preconditions are proved True.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Backward Chaining
R1: IF hot AND smoky THEN ADD fire Initial State
R2: IF alarm_beeps THEN ADD smoky
Working Memory
R3: IF fire THEN ADD switch_on_sprinklers
F1: alarm_beeps
F2: hot

The goal to try to prove is:

G1: switch_on_sprinklers

Check whether the goal is match the facts in initial state of the working memory. As it isn’t
there, try to match it against the conclusion of the rules. It matches R3. The precondition of
this rule is set as a new goal to prove:

G2: Fire

Now, this isn’t in the initial facts in the working memory but it matches the conclusion of
R1, so R1 preconditions are set as new goals to prove:

G3: smoky
G4: hot
Module Code and Module Title Title of Slides Slide ‹ 5 of 24
Backward Chaining
R1: IF hot AND smoky THEN ADD fire Initial State
R2: IF alarm_beeps THEN ADD smoky
Working Memory
R3: IF fire THEN ADD switch_on_sprinklers
F1: alarm_beeps
F2: hot

G3 is considered first. It matches the conclusion of R2, switch_on_sprinklers


so alarm_beeps is set as new goal. The goals are now R4
prove:

G5: alarm_beeps
fire
G4: hot
R1

Now, both of these goals are in the initial facts in the


smoky hot
working memory, so are trivially True. This can be
R3 F2
conclude that sprinkler should be switch on.

Alarm_beeps
F1

Proof three fo Sprinkler Example


Module Code and Module Title Title of Slides Slide ‹ 5 of 24
Rule-based Representation

• Production rules as discuss before are written in pseudo-


code.
• To be entered into an expert system shell, they must be expressed in
an appropriate knowledge representation language (KRL).
• A production rule is usually expressed using the keywords IF
and THEN, and has two parts: conditions and actions.
• The IF part states a condition (or premise) and the THEN part
expresses a corresponding action or conclusion.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation

Example:
Forward Chaining
IF the animal produce live offerings (condition)
THEN the animal is mammal (conclusion)

Backward Chaining
The animal is a mammal (conclusion)
IF animal produces live offspring. (condition)

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
Multiple conditions in production rules
• Production rules can have one or more conditions.
• Conditions can be combined using the three keywords AND,
OR and NOT (these are known as Boolean operators).

Examples:

Rule 1 IF the bathroom is dry


AND the hall is wet
THEN there is a leak in the kitchen

Rule 2 IF the kitchen is wet


OR the hall is wet
THEN there is a leak in the kitchen

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation

Rule 3 IF the bathroom is dry


AND the hall is not wet
THEN there is a leak in the bathroom

Rule 4 IF the hall is wet


AND (the bathroom is dry OR the kitchen is wet)
THEN there is a leak in the kitchen

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
Example

Consider the following passage of text about different methods of transport:

There are three main modes of transport: land, sea and air.

Land-based locomotion is either by road or rail. Road-based locomotion includes


cars, which are designed for carrying people, and trucks, which are designed for
carrying freight. Trams and trains travel on rails, with trams designed for carrying
people, while trains are designed for carrying people and freight.

Sea-based locomotion is by ship. Tankers are designed for carrying freight, while
ferries are designed for carrying people.

Air travel is by aeroplane or helicopter. Aeroplanes are designed for carrying people
and freight, while helicopters are designed for carrying people

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
An expert system is required to classify methods of transport. How should
the knowledge contained within the text be extracted and converted into
production rules?

Answer

Step 1: Convert it into factor table.


• A useful technique to help structure the knowledge is to use a factor
table.
• A factor table is simply a way of matching up conditions to actions or
conclusions. The factor table is similar to a table in a database.
• The column headings correspond to fields in a database table. The rows of
the table correspond to records in a database table.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
Transport Mode Cargo Locomotion
Car Land People Road
Truck Land Freight Road
Tram Land People Rail
Train Land People & freight Rail
Tanker Sea Freight
Ferry Sea People
Aeroplane Air People & freight
Helicopter Air People

As in a database, the column headings are called attributes, with the values
listed in the rows below. The factor table produces a set of attribute–value
pairs, which classify each type of transport.

For example, here is the set of attribute–value pairs for a car: mode = land,
cargo = people, locomotion = road

As most expert systems work by using attribute–value pairs to represent


facts in the knowledge base, a factor table is a useful way of listing these.
Module Code and Module Title Title of Slides Slide ‹ 5 of 24
Rule-based Representation
Transport Mode Cargo Locomotion
Car Land People Road
Truck Land Freight Road
Tram Land People Rail
Train Land People & freight Rail
Tanker Sea Freight
Ferry Sea People
Aeroplane Air People & freight
Helicopter Air People

The rows of the factor table correspond to rules, and the columns
correspond to conditions or questions. So, for the factor table above, there
should be eight rules, with up to three conditions each.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Rule-based Representation
Step 2: From factor table to production rules.

IF the mode is land


AND the cargo is people
AND the locomotion is road
THEN method of transport is car

IF the mode is sea


AND the cargo is freight
THEN method of transport is tanker

Figure the rest of the production rules…

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Semantic Network

• Semantic Network is an alternative for predicate logic for


knowledge representation.
• In semantic network knowledge is represented as a graph,
where the nodes in the graph represent concept and the links
represent relations between the concepts.
• There are TWO (2) notations in semantic network:
• Node (oval shape) – Class, subclass and object.
• Link (arrow) – relation between nodes.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Semantic Network

• There TWO(2) relationship in semantic network:


• is_a – relationship that exists between the class and
subclasses. OR relationship that exists between class and
object.
• kind_of – relationship that represent the property or
behavior of class, subclass and object.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Semantic Network

• An object is an identifiable entity


with some property and behavior.
• A class is a group of objects that
share common properties and
behavior.
• A subclass is a class that derives
from another class. A subclass
inherits property and behavior
from all of its superclass.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Semantic Network
Example 1
Mammal and reptile are animals, that mammals
have heads, an elephants is a larger grey
mammal. Clyde and Nellie are both elephants
Animal
and Nellie likes apple.
Is_an Is_an

has-part

Reptile Mammal Head

Is_a

size colour
Large Elephant Grey

Is_an
is_an

likes
Clyde Nellie Apple

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Semantic Network
Example 2
Computers are a class of objects that all have a processor, memory and input and
output devices. Also computers can be used to write letters, to store names and to
play games. The Dell Optiplex is a computer and so it inherits all the properties of
computers.

Write
Processor
Letters
has used to

used to
has Store
Memory Computer
names

has used to
I/O devices Play games
is a

Dell
Otiplex

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Semantic Network
Example 3
cat(tom)
caught(tom, bird) likes Cat
owned(tom, john) Fish
color(tom, ginger)
likes(cat, fish) sat

sat(cat, mat) Is_a

Mat

Tom owned
caught

color
John
Bird
Ginger

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Frames

• Frames are variant of semantic networks.


• It is a straight forward translation between semantic
network and frame.
• Nodes in semantic network become object in frame, links
become slots and the node on the other links becomes the
slot values.

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Frames
Semantic Network Elephant Frame

Animal Mammal:
subclass: animal
subclass subclass has-part: head
has-part
Elephant:
Reptile Mammal Head
subclass: Mammal
colour: grey
subclass
size: large
size colour
Large Elephant Grey Nellie:
instance: Elephant
instance
instance likes: apple

likes
Clyde Nellie Apple

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Frames
Semantic Network Computer Frame

Write Computer:
Processor
has used to
Letters has: processor
has: memory
has: I/O devices
used to
has Store use to: write letters
Memory Computer
names use to: Store names
use to: Play games
has used to
Play games Dell Optiplex:
I/O devices
is a subclass: Computer

Dell
Otiplex

Module Code and Module Title Title of Slides Slide ‹ 5 of 24


Summary of Main Teaching Points

• It is important to understand the order in which the facts


are read, that the ‘verb’ or link term is placed outside the
brackets and the objects to which it applies are inside.
• Also the first object is applied by the link to the second (if
there is one).
• In other words we would read the following fact.

Module Code and Module Title Title of Slides Slide ‹40› of 24


Question and Answer Session

Q&A

Module Code and Module Title Title of Slides Slide ‹41› of 24


What we will cover next

• Uniform Search

Module Code and Module Title Title of Slides Slide ‹42› of 24

You might also like