1 Programming a Knowledge Based Application. 2 Overview.

52
1 Programming a Knowledge Based Application

Transcript of 1 Programming a Knowledge Based Application. 2 Overview.

Page 1: 1 Programming a Knowledge Based Application. 2 Overview.

1

Programming a Knowledge Based Application

Page 2: 1 Programming a Knowledge Based Application. 2 Overview.

2

Overview

Page 3: 1 Programming a Knowledge Based Application. 2 Overview.

3

Rule-based Intelligent UI

Inference EngineKnowledge Base (Rules)

WorkingMemory (Facts)

User Interface

Agenda

“Intelligence”(Knowledge-based system)

Page 4: 1 Programming a Knowledge Based Application. 2 Overview.

4

Rule-based Intelligent UI

Inference EngineKnowledge Base (Rules)

WorkingMemory (Facts)

User Interface

Agenda

Page 5: 1 Programming a Knowledge Based Application. 2 Overview.

5

Components of a Rule-Based System (1)

FACT BASE or fact list represents the initial state of the problem. This is the data from which inferences are derived.

RULE BASE or knowledge base (KB) contains a set of rules which can transform the problem state into a solution. It is the set of all rules.

Page 6: 1 Programming a Knowledge Based Application. 2 Overview.

6

Components of a Rule-Based Language (2)

INFERENCE ENGINE controls overall execution. It matches the facts against the rules to see what rules are applicable. It works in a recognize-act cycle: match the facts against the rules choose which rules instantiation to fire execute the actions associated with the

rule

Page 7: 1 Programming a Knowledge Based Application. 2 Overview.

7

CLIPS C Language Integrated Production System Public domain software Supports:

Forward Chaining Rules based on Rete algorithm Procedural Programming Object-oriented programming (COOL)

Can be integrated with other C/C++ programs/applications

Page 8: 1 Programming a Knowledge Based Application. 2 Overview.

8

JESS Java Expert System Shell Inspired by CLIPS => forward chaining rule

system + Rete algorithm Free demo version available (trial period of

30 days) Can be integrated with other Java code

Page 9: 1 Programming a Knowledge Based Application. 2 Overview.

9

Bakcground

Page 10: 1 Programming a Knowledge Based Application. 2 Overview.

10

Production Rules

Production rules were developed for use in automata theory, formal grammars, programming language design & used for psychological modeling before they were used for expert systems.

Also called condition-action, or situation-action rules.

Encode associations between patterns of data given to the system & the actions the system should perform as a consequence.

Page 11: 1 Programming a Knowledge Based Application. 2 Overview.

11

Canonical Systems

Production rules are grammar rules for manipulating strings of symbols.

Also called rewrite rules (they rewrite one string into another).

First developed by Post (1943), who studied the properties of rule systems based on productions & called his systems canonical systems.

He proved any system of mathematics or logic could be written as a type of production rule system. Minsky showed that any formal system can be realized as a canonical system.

Page 12: 1 Programming a Knowledge Based Application. 2 Overview.

12

Example of a Canonical System

Let A be the alphabet {a, b, c} With axioms a, b, c, aa, bb, cc Then these production rules will give all the

possible palindromes (and only palindromes) based on the alphabet, starting from the above axioms. (P1) $ -> a$a (P2) $ -> b$b (P3) $ -> c$c

Page 13: 1 Programming a Knowledge Based Application. 2 Overview.

13

Example continued

To generate bacab P1 is applied to the axiom c to get aca Then we apply P2 to get bacab Using a different order gives a

different result. If P2 is applied to c we get bcb If P1 is applied after we get abcba

Page 14: 1 Programming a Knowledge Based Application. 2 Overview.

14

Production Systems for Problem Solving

In KB systems production rules are used to manipulate symbol structures rather than strings of symbols.

The alphabet of canonical systems is replaced by a vocabulary of symbols and a grammar for forming symbol

structures.

Page 15: 1 Programming a Knowledge Based Application. 2 Overview.

15

Rule-Based Production Systems

A production system consists of a rule set / knowledge base / production memory a rule interpreter / inference engine

that decides when to apply which rules a working memory

that holds the data, goal statements, & intermediate results that make up the current state of the problem.

Rules have the general formIF <pattern> THEN <action>

P1, …, Pm Q1, …, Qn

Patterns are usually represented by OAV vectors.

Page 16: 1 Programming a Knowledge Based Application. 2 Overview.

16

An OAV Table

Object Attribute Value

Beluga Whale Dorsal Fin No

Beluga Whale Tail Fin No

Blue Whale Tail Fin Yes

Blue Whale Dorsal Fin Yes

Blue Whale Size Very Large

Page 17: 1 Programming a Knowledge Based Application. 2 Overview.

17

RULES General form: a b

IF … THEN …IF < antecedent, condition, LHS>THEN <consequent, action, RHS>

Antecedent match against symbol structure

Consequent contains special operator(s) to manipulate those symbol structures

Page 18: 1 Programming a Knowledge Based Application. 2 Overview.

18

Syntax of Rules

The vocabulary consists of a set N of names of objects in the domain a set P of property names that give

attributes to objects a set V of values that the attributes can

have. Grammar is usually represented by OAV

triplesOAV triple is (object, attribute, value) triplesExample: (whale, size, large)

Page 19: 1 Programming a Knowledge Based Application. 2 Overview.

19

Forward & Backward Chaining

Production rules can be driven forward or backward.

We can chain forward from conditions that we know to be true towards problem states those conditions allow us to establish the goal; or

We can chain backward from a goal state towards the conditions necessary for establishing it.

Forward chaining is associated with ‘bottom-up’ reasoning from facts to goals.

Backward chaining is associated with ‘top-down’ reasoning from facts to goals.

Page 20: 1 Programming a Knowledge Based Application. 2 Overview.

20

Forward Backward Chaining Chaining

facts

goal

Fact:Saw dorsal

Fin - NO

Fact:Saw tail fin

-NO

Inferred:Beluga

and

Concrete facts

Saw dorsal Fin - NO

Saw tail fin-NO

Goal:Beluga

Page 21: 1 Programming a Knowledge Based Application. 2 Overview.

21

Palindrome Example

If we have the following grammar rules(P1) $ -> a$a(P2) $ -> b$b(P3) $ -> c$c

They can be used to generate palindromes forward chainingapply P1, P1, P3, P2, to c > aca aacaa caacaac -

Or they can be used to recognize palindromes backward chainingbacab matches the RHS of P2 but acbcb will not be

accepted by any RHS

Page 22: 1 Programming a Knowledge Based Application. 2 Overview.

22

Forward Chaining

Fig. based on http://ai-depot.com/Tutorial/RuleBased-Methods.html

Determine possible

rules to fire

Select rule to

fire

Conflictresolutionstrategy

Exit

No rule found

Conflict set

Fire rule

Rule found

Exit if specified by the rule

Rulebase

Workingmemory

Page 23: 1 Programming a Knowledge Based Application. 2 Overview.

23

Chaining & CLIPS/JESS

CLIPS and JESS uses forward chaining. The LHS of rules are matched against

working memory. Then the action described in the RHS

of the rule, that fires after conflict resolution, is performed.

Page 24: 1 Programming a Knowledge Based Application. 2 Overview.

24

Palindrom Example

To generate bacab P1 is applied to the axiom c to get aca Then we apply P2 to get bacab Using a different order gives a

different result. If P2 is applied to c we get bcb If P1 is applied after we get abcba

Page 25: 1 Programming a Knowledge Based Application. 2 Overview.

25

Markov algorithm

A Markov algorithm (1954) is a string rewriting system that uses grammar-like rules to operate on strings of symbols. Markov algorithms have been shown to have sufficient power to be a general model of computation.

Important difference from canonical system: now the set of rules is ordered

Page 26: 1 Programming a Knowledge Based Application. 2 Overview.

26

Palindrom example revisited

To generate bacab P1 is applied to the axiom c to get aca Then we apply P2 to get bacab Using a different order gives a

different result. If P2 is applied to c we get bcb If P1 is applied after we get abcba

Page 27: 1 Programming a Knowledge Based Application. 2 Overview.

27

Making it more efficient The Rete algorithm is an efficient

pattern matching algorithm for implementing rule-based expert systems.

The Rete algorithm was designed by Dr. Charles L. Forgy of Carnegie Mellon University in 1979.

Rete has become the basis for many popular expert systems, including OPS5, CLIPS, and JESS.

Page 28: 1 Programming a Knowledge Based Application. 2 Overview.

28

RETE algorithm

Creates a decision tree where each node corresponds to a pattern occurring at the left-hand side of a rule

Each node has a memory of facts that satisfy the pattern

Complete LHS as defined by a path from root to a leaf.

Page 29: 1 Programming a Knowledge Based Application. 2 Overview.

29

Rete example

(http://aaaprod.gsfc.nasa.gov/teas/Jess/JessUMBC/sld010.htm)

x? y? x? y? z?Pattern Network

Rules: IF x & y THEN p IF x & y & z THEN q

p

Join Network

8 nodesq

Page 30: 1 Programming a Knowledge Based Application. 2 Overview.

30

Rete example

(http://aaaprod.gsfc.nasa.gov/teas/Jess/JessUMBC/sld010.htm)

x? y? z?Pattern Network

Rules: IF x & y THEN p IF x & y & z THEN q

p

Join Network

6 nodesq

Page 31: 1 Programming a Knowledge Based Application. 2 Overview.

31

Rete example

(http://aaaprod.gsfc.nasa.gov/teas/Jess/JessUMBC/sld010.htm)

x? y? z?Pattern Network

Rules: IF x & y THEN p IF x & y & z THEN q

p

Join Network

5 nodesq

Page 32: 1 Programming a Knowledge Based Application. 2 Overview.

32

Matching Patterns

At each cycle the interpreter looks to see which rules have conditions that can be satisfied.

If a condition has no variables it will only be satisfied by an identical expression in working memory.

If the condition contains variables then it will be satisfied if there is an expression in working memory with an attribute-value pair that matches it in a way that is consistent with the way other conditions in the same rule have already been matched.

Page 33: 1 Programming a Knowledge Based Application. 2 Overview.

33

Example of matching

(whale (species Beluga) (tail_fin NO)(dorsal_fin NO))

Matches the pattern (with variables)

(whale (species ?name) (tail_fin ?flukes) (dorsal_fin ?fin)

Page 34: 1 Programming a Knowledge Based Application. 2 Overview.

34

The Working Memory

Holds data in the form of OAV vectors. These data are then used by the interpreter

to activate the rules. The presence or absence of data elements

in the working memory will trigger rules by satisfying patterns on the LHS of rules.

Actions such as assert or modify the working memory.

Page 35: 1 Programming a Knowledge Based Application. 2 Overview.

35

Conflict Resolution

Production systems have a decision-making step between pattern matching & rule firing.

All rules that have their conditions satisfied are put on the agenda in CLIPS.

The set of rules on the agenda is sometimes called the conflict set.

Conflict resolution selects which rule to fire from the agenda.

Packages like CLIPS provide more than one option for conflict resolution

Sensibility (quick response to changes in WM) and Stability (continuous reasoning).

Page 36: 1 Programming a Knowledge Based Application. 2 Overview.

36

Conflict Resolution in CLIPS

First, CLIPS uses salience to sort the rules. Then it uses the other strategies to sort rules with equal salience.

CLIPS uses refraction, recency & specificity in the form of following 7 strategies: The depth strategy The breadth strategy The simplicity strategy The complexity strategy The LEX strategy The MEA strategy It is possible also to set strategy to random

Syntax: (set-strategy <strategy>)

Page 37: 1 Programming a Knowledge Based Application. 2 Overview.

37

Salience

Normally the agenda acts like a stack. The most recent activation placed on the

agenda is the first rule to fire. Salience allows more important rules to stay

at the top of the agenda regardless of when they were added.

If you do not explicitly say, CLIPS will assume the rule has a salience of 0. a positive salience gives more weight to a rule a negative salience gives less weight to a rule

Page 38: 1 Programming a Knowledge Based Application. 2 Overview.

38

Refractoriness

A rule should not be allowed to fire more than once for the same data.

Prevents loops Used in CLIPS and JESS (need to (refresh) to bypass it)

Page 39: 1 Programming a Knowledge Based Application. 2 Overview.

39

How to

Page 40: 1 Programming a Knowledge Based Application. 2 Overview.

40

Example

Simplified description of some varieties of cultivated apples:

Variety Size Color

Cortland large red

Golden delicious large yellow

Red Delicious large green

Granny Smith medium red

Page 41: 1 Programming a Knowledge Based Application. 2 Overview.

41

Rules The simple way – write standard

if..then rules:IF (color == red && size == large)

THEN variety = Cortland

We will need: 4 rules (+ rule(s) for asking questions) => minimum 5 rules

BUT: can do it in 2 rules in CLIPS/JESS

Page 42: 1 Programming a Knowledge Based Application. 2 Overview.

42

Define Template

(deftemplate apple(multislot variety (type SYMBOL) )(slot size (type SYMBOL))(slot color (type SYMBOL) (default red)))

Other useful slot type: NUMBERJESS note: in JESS multislots don’t have type

CLIPS allow both SYMBOL and STRING types, JESS – only STRING

Page 43: 1 Programming a Knowledge Based Application. 2 Overview.

43

Assert Facts

(deffacts apple_varieties

(apple (variety Cortland) (size large) (color red))

(apple (variety Golden delicious) (size large) (color yellow))

(apple (variety Red Delicious) (size medium) (color red))

(apple (variety Granny Smith) (size large) (color green))

)

Page 44: 1 Programming a Knowledge Based Application. 2 Overview.

44

Create Rules – Rule 1(defrule ask-size

(declare (salience 100)) ;NOTE: JESS don’t use salience(initial-fact)

=>(printout t “Please enter the apple characteristics :“  crlf)(printout t “- color (red, yellow, green) : “)(bind ?ans1 (read))(printout t crlf “-size (large or medium) : “)(bind ?ans2 (read))(assert (apple (variety users) (color ?ans1) (size ?ans2)))

)

Page 45: 1 Programming a Knowledge Based Application. 2 Overview.

45

Create Rules – Rule 2

(defrule variety(declare (salience 10)) ;JESS NOTE – take this out(apple (variety users) (size ?s) (color ?c))(apple (variety ?v&:(neq ?v users))(size ?s) (color ?c))

=>(printout t “You’ve got a “ ?v crlf)(halt))

Page 46: 1 Programming a Knowledge Based Application. 2 Overview.

46

Run CLIPS Type the code in a file, save it (e.g. apples.clp) start CLIPS (type clips or xclips in

UNIX/LINUX) do: File -> Load (in XCLIPS) or type

(load “apples.clp”) when the file is loaded CLIPS will display:

defining deftemplate appledefining deffacts apple_varietiesdefining defrule ask-size +jdefining defrule variety +j+jTRUE

Page 47: 1 Programming a Knowledge Based Application. 2 Overview.

47

Run CLIPS Type (reset) to put your initial facts in the fact

base CLIPS>(run)

Please enter the apple characteristics:

- color red, yellow, green: red

- size (large or medium) : large

You’ve got a Cortland

CLIPS> (exit)

Page 48: 1 Programming a Knowledge Based Application. 2 Overview.

48

Run JESS UNIX command line:

java –classpath jess.jar jess.Main Or start an applet console.html Jess> (batch apples.clp)

TRUEJess> (reset)TRUEJess> (run)Please enter the apple characteristics:

- color red, yellow, green: red- size (large or medium) : largeYou’ve got a Cortland

2Jess> (exit)

Page 49: 1 Programming a Knowledge Based Application. 2 Overview.

49

CLIPS resources Official CLIPS website (maintained by

Gary Riley):http://www.ghg.net/clips/CLIPS.html

CLIPS Documentation:http://www.ghg.net/clips/download/documentation

Examples:

http://www.ghg.net/clips/download/executables/examples/

Page 50: 1 Programming a Knowledge Based Application. 2 Overview.

50

Integrating CLIPS into C/C++

Go to the source code Replace CLIPS main with user-defined

main (follow the instructions within the main)

#include “clips.h” in classes that will use it

Compile all with ANSI C++ compiler

Page 51: 1 Programming a Knowledge Based Application. 2 Overview.

51

Resources for CLIPS C++ integration CLIPS advanced programming guide Anonymous ftp from hubble.jsc.nasa.gov

directory pub/clips/Documents DLL for CLIPS 5.1 for Windows at

ftp.cs.cmu.edu directory pub/clips/incoming Examples can be found also at

http://ourworld.compuserve.com/homepages/marktoml/cppstuff.htm and http://www.monmouth.com/%7Ekm2580/dlhowto.htm

Page 52: 1 Programming a Knowledge Based Application. 2 Overview.

52

JESS resources http://herzberg.ca.sandia.gov/jess/ Includes instructions and examples

for embedding JESS into a Java program (http://herzberg.ca.sandia.gov/jess/docs/61/embedding.html ) and or creating Java GUI from JESS (see http://herzberg.ca.sandia.gov/jess/docs/61/jessgui.html)