© 2002-4 Franz J. Kurfess ES Implementation 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.

Slides:



Advertisements
Similar presentations
Heuristic Search techniques
Advertisements

Chapter 11 user support. Issues –different types of support at different times –implementation and presentation both important –all need careful design.
Semantics Static semantics Dynamic semantics attribute grammars
© 2002 Franz J. Kurfess Expert System Examples 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
© C. Kemke XPS Implementation 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Expert System Shells - Examples
CHAPTER 1: AN OVERVIEW OF COMPUTERS AND LOGIC. Objectives 2  Understand computer components and operations  Describe the steps involved in the programming.
1 01/12/2011Knowledge-Based Systems, Paula Matuszek Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011.
Chapter 7: Introduction to CLIPS
Intelligent systems Lecture 6 Rules, Semantic nets.
Rule Based Systems Michael J. Watts
Chapter 12: Expert Systems Design Examples
Expert System Human expert level performance Limited application area Large component of task specific knowledge Knowledge based system Task specific knowledge.
CPE/CSC 481: Knowledge-Based Systems
Xyleme A Dynamic Warehouse for XML Data of the Web.
© 2005 Franz J. Kurfess Expert System Examples 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
Introduction to CLIPS (Chapter 7) Fact List (contains data) Knowledge Base (contains rules) Inference Engine (controls execution)
Chapter 9 Describing Process Specifications and Structured Decisions
© Franz J. Kurfess Approximate Reasoning 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
© 2002 Franz J. Kurfess Introduction 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
© 2002 Franz J. Kurfess Approximate Reasoning 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
1 Chapter 9 Rules and Expert Systems. 2 Chapter 9 Contents (1) l Rules for Knowledge Representation l Rule Based Production Systems l Forward Chaining.
© C. Kemke1Reasoning - Introduction COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.
© 2002 Franz J. Kurfess Introduction 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
Chapter 8: Advanced Pattern Matching Expert Systems: Principles and Programming, Fourth Edition.
© Franz J. Kurfess Expert System Design CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
Chapter 9: Modular Design, Execution Control, and Rule Efficiency Expert Systems: Principles and Programming, Fourth Edition.
EXPERT SYSTEMS Part I.
© Franz J. Kurfess ES Implementation 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
CS 561, Session 25 1 Introduction to CLIPS Overview of CLIPS Facts Rules Rule firing Control techniques Example.
© 2001 Franz J. Kurfess Introduction 1 CPE/CSC 580: Knowledge Management Dr. Franz J. Kurfess Computer Science Department Cal Poly.
Katanosh Morovat.   This concept is a formal approach for identifying the rules that encapsulate the structure, constraint, and control of the operation.
Chapter 9: Modular Design, Execution Control, and Rule Efficiency Expert Systems: Principles and Programming, Fourth Edition.
Chapter 14: Artificial Intelligence Invitation to Computer Science, C++ Version, Third Edition.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Chapter 7: Introduction to CLIPS Expert Systems: Principles and Programming, Fourth Edition.
Chapter 10: Procedural Programming Expert Systems: Principles and Programming, Fourth Edition.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Jess: A Rule-Based Programming Environment Reporter: Yu Lun Kuo Date: April 10, 2006 Expert System.
Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall Knowledge Representation Semantic Web - Fall 2005 Computer.
Kansas State University Department of Computing and Information Sciences CIS 730: Introduction to Artificial Intelligence Lecture 17 Wednesday, 01 October.
CS62S: Expert Systems Based on: The Engineering of Knowledge-based Systems: Theory and Practice, A. J. Gonzalez and D. D. Dankel.
Expert Systems Chapter 7 Introduction to CLIPS Entering and Exiting CLIPS A> CLIPS  CLIPS (V6.5 09/01/97) CLIPS> exit exit CLIPS> (+ 3 4)  7 CLIPS>
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
© 2002 Franz J. Kurfess Introduction 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly.
Rule Based Expert System دكترمحسن كاهاني
1 Knowledge Based Systems (CM0377) Lecture 10 (Last modified 19th March 2001)
The Hashemite University Computer Engineering Department
DATA STRUCTURES (CS212D) Overview & Review Instructor Information 2  Instructor Information:  Dr. Radwa El Shawi  Room: 
Mostly adopted from Jason Morris notes (Morris Technical Solutions)
Forward and Backward Chaining
Expert System Seyed Hashem Davarpanah University of Science and Culture.
Some Thoughts to Consider 5 Take a look at some of the sophisticated toys being offered in stores, in catalogs, or in Sunday newspaper ads. Which ones.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Artificial Intelligence Knowledge Representation.
The CLIPS Expert System Shell Dr Nicholas Gibbins
Artificial Intelligence: Applications
Summary for final exam Agent System..
Introduction to CLIPS 2 Session 13 Course: T0273 – EXPERT SYSTEMS Year: 2014.
Knowledge Representation Techniques
Module 11: File Structure
Chapter 7: Introduction to CLIPS
Relational Algebra Chapter 4, Part A
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Chapter 11 user support.
Expert System Implementation
Chapter 11 Describing Process Specifications and Structured Decisions
Knowledge Representation
Presentation transcript:

© Franz J. Kurfess ES Implementation 1 CPE/CSC 481: Knowledge-Based Systems Dr. Franz J. Kurfess Computer Science Department Cal Poly

© Franz J. Kurfess ES Implementation 2 Course Overview u Introduction u Knowledge Representation u Semantic Nets, Frames, Logic u Reasoning and Inference u Predicate Logic, Inference Methods, Resolution u Reasoning with Uncertainty u Probability, Bayesian Decision Making u Expert System Design u ES Life Cycle u CLIPS Overview u Concepts, Notation, Usage u Pattern Matching u Variables, Functions, Expressions, Constraints u Expert System Implementation u Salience, Rete Algorithm u Expert System Examples u Conclusions and Outlook

© Franz J. Kurfess ES Implementation 3 Overview Implementation of Rule-Based Systems u Motivation u Objectives u Chapter Introduction u Important Concepts u Performance Aspects u Pattern Matching u Basic Idea u Unification u Pattern Matching in Rule- Based Systems u Rete Algorithm u Overview u Rete Network u Assert and Retract u Optimizations u Improvements u Rule Formulation u General vs. Specific Rules u Simple vs. Complex Rules u Loading and Saving Facts u Important Concepts and Terms u Chapter Summary

© Franz J. Kurfess ES Implementation 4 Logistics u Introductions u Course Materials u textbooks (see below) u lecture notes u PowerPoint Slides will be available on my Web page u handouts u Web page u u Term Project u Lab and Homework Assignments u Exams u Grading

© Franz J. Kurfess ES Implementation 5 Bridge-In

© Franz J. Kurfess ES Implementation 6 Pre-Test

© Franz J. Kurfess ES Implementation 7 Motivation  pattern matching and unification are powerful operations to determine the similarity and consistency of complex structures  they are at the core of many rule-based and predicate logic mechanisms  their application goes beyond rule-based systems  study concepts and methods that are critical for the functionality and performance of rule-based systems  pattern matching and the Rete algorithm  use and formulation of rules

© Franz J. Kurfess ES Implementation 8 Objectives  comprehend the mechanics of pattern matching in rule-based systems  basic concepts and techniques  Rete algorithm  understand the effects of matching and rule formulation on the performance of rule-based systems  learn to write rule-based programs and implement systems in an efficient way  analyze and evaluate the performance of rule-based programs and systems  identify bottlenecks  formulate and implement strategies for performance improvements

© Franz J. Kurfess ES Implementation 9 Evaluation Criteria

© Franz J. Kurfess ES Implementation 10 Overview Implementation of Rule-Based Systems u due to their more declarative nature, it can be difficult to evaluate and predict the performance of rule-based systems u time to complete a task u memory usage u disk space usage u pattern matching can be used to eliminate unsuitable rules and facts u but it can also introduce substantial overhead

© Franz J. Kurfess ES Implementation 11 Chapter Introduction u Important Concepts u entities with internal structure u data structures, objects, components u terms, sentences, graphs u diagrams, images u concepts, hierarchies u Performance Aspects u somewhat different from conventional programs u less control over the runtime behavior u pattern matching can do a lot of the work

© Franz J. Kurfess ES Implementation 12 Pattern Matching u determines if two or more compelx entities (patterns) are compatible with each other u patterns can be (almost) anything that has a structure u pictures: mugshot vs. person u drawings: diagrams of systems u expressions: words,sentences of a language, strings u graphs are often used as the underlying representation u the structure of the graphs must be compatible u usually either identical, or one is a sub-graph of the other u the individual parts must be compatible u nodes must have identical or compatible values uvariables are very valuable u links must indicate compatible relationships u compatibility may be dependent on the domain or task [Giarratano & Riley 1998, Friedmann-Hill 2003, Gonzalez & Dankel, 2004]

© Franz J. Kurfess ES Implementation 13 ????? Pattern Matching Example  images  Do both images refer to the same individual?  Do they have other commonalities?  test Bucky Bucky likes fish Bucky Bucky likes fish Bucky and Satchel Satchel likesBucky

© Franz J. Kurfess ES Implementation 14 Pattern Matching Example  shapes ?? ?????

© Franz J. Kurfess ES Implementation 15 Pattern Matching Examples  constants and variables “Hans” “Franz” ? “Josef” “Joseph” first_name “Joseph” ? last_name “Joseph”

© Franz J. Kurfess ES Implementation 16 Pattern Matching Examples  terms  composed of constants, variables, functions ? father(X) “Joseph” ? father(Y)father(X) mother(X) ?? father(father(X))grandfather(X)

© Franz J. Kurfess ES Implementation 17 Unification  formal specification for finding substitutions that make logical expressions identical  the unification algorithm takes two sentences and returns a unifier for them (if one exists) Unify(p,q) =  if Subst( ,p) = Subst( ,p)  if there is more than one such substitution, the most general unifier is returned  used in logic programming, automated theorem proving  possibly complex operation u quadratic in the size of the expressions u “occur check” sometimes omitted v determines if a variable is contained in the term against which it is unified

© Franz J. Kurfess ES Implementation 18 Pattern Matching in Rule-Based Systems  used to match rules with appropriate facts in working memory  rules for which facts can be found are satisfied  the combination of a rule with the facts that satisfy it is used to form activation records  one of the activation records is selected for execution

© Franz J. Kurfess ES Implementation 19 Simplistic Rule-Based Pattern Matching  go through the list of rules, and check the antecedent (LHS) of each rule against the facts in working memory  create an activation record for each rule with a matching set of facts  repeat after each rule firing  very inefficient  roughly (number of rules) * (number of facts) (number of patterns)  the actual performance depends on the formulation of the rules and the contents of the working memory

© Franz J. Kurfess ES Implementation 20 Rete Algorithm u in most cases, the set of rules in a rule-based system is relatively constant u the facts (contents of working memory) change frequently u most of the contents of working memory, however, don’t change every time u optimization of the matching algorithm u remember previous results u change only those matches that rely on facts that changed u the Rete algorithm performs an improved matching of rules and facts u invented by Charles Forgy in the early 80s u basis for many rule-based expert system shells [ Friedmann-Hill 2003, Giarratano & Riley 1998, Friedmann-Hill 2003, Gonzalez & Dankel, 2004]

© Franz J. Kurfess ES Implementation 21 Rete Network u the name comes from the latin word rete u stands for net u consists of a network of interconnected nodes u each node represents one or more tests on the LHS of a rule u input nodes are at the top, output nodes at the bottom u pattern nodes have one input, and check the names of facts u join nodes have two inputs, and combine facts u terminal node at the bottom of the network represent individual rules u a rule is satisfied if there is a combination of facts that passes all the test nodes from the top to the output node at the bottom that represents the rule u the Rete network effectively is the working memory for a rule- based system

© Franz J. Kurfess ES Implementation 22 Rete Network Example 1 (deftemplate x (slot a)) (deftemplate y (slot b)) (defrule example-1 (x (a ?v1)) (y (b ?v1)) ==> ) ?= x ?= y Left.0.a ?= Right.b example-1 ?v1 ?v1 = ?v1

© Franz J. Kurfess ES Implementation 23 Rete Left and Right Memories  left (alpha) memory  contains the left input of a join node  right (beta) memory  contains the right input of a join node  notation: Left.p.q ?= Right.r  compare the contents of slot q in fact p from the left memory with slot r in the fact from the right memory (deftemplate x (slot a)) (deftemplate y (slot b)) (defrule example-1 (x (a ?v1)) (y (b ?v1)) ==> ) ?= x ?= y Left.0.a ?= Right.b example-1 ?v1 ?v1 = ?v1

© Franz J. Kurfess ES Implementation 24 Running the Network  only facts x and y are considered  all facts where x.a == y.b pass the join network  all {x, y} tuples are fowarded to the next node  compare the contents of slot q in fact p from the left memory with slot r in the fact from the right memory (deftemplate x (slot a)) (deftemplate y (slot b)) (defrule example-1 (x (a ?v1)) (y (b ?v1)) ==> ) ?= x ?= y Left.0.a ?= Right.b example-1 ?v1 ?v1 = ?v1

© Franz J. Kurfess ES Implementation 25 Rete Network Example 2  shares some facts with Example 1 (deftemplate x (slot a)) (deftemplate y (slot b)) (deftemplate z (slot c)) (defrule example-2 (x (a ?v2)) (y (b ?v2)) (z) ==> ) ?= x ?= y ?= z Left.0.a ?= Right.b example-2 ?v2 ?v2 = ?v2 ?v2

© Franz J. Kurfess ES Implementation 26 Rete Network Example 2 with Assert  additional fact asserted (deftemplate x (slot a)) (deftemplate y (slot b)) (deftemplate z (slot c)) (defrule example-2 (x (a ?v2)) (y (b ?v2)) (z) ==> ) (assert (z (c 17)) ?= x ?= y ?= z Left.0.a ?= Right.b example-2 ?v2 ?v2 = ?v2 ?v2 ?v2 = 17 17

© Franz J. Kurfess ES Implementation 27 Assert and Retract with Rete  asserting additional facts imposes some more constraints on the network  retracting facts indicates that some previously computed activation records are not valid anymore, and should be discarded  in addition to the actual facts, tags are sent through the networks  ADD to add facts (i.e. for assert)  REMOVE to remove facts (i.e. for retract)  CLEAR to flush the network memories (i.e. for reset)  UPDATE to populate the join nodes of newly added rules  already existing join nodes can neglect these tokens

© Franz J. Kurfess ES Implementation 28 Rete Network Optimization  networks with shared facts can be combined (deftemplate x (slot a)) (deftemplate y (slot b)) (deftemplate z (slot c)) (defrule example-1 (x (a ?v1)) (y (b ?v1)) ==> ) (defrule example-2 (x (a ?v2)) (y (b ?v2)) (z) ==> ) ?= x ?= y ?= z Left.0.a ?= Right.b example-1example-2

© Franz J. Kurfess ES Implementation 29 Further Optimizations  sophisticated data structures to optimize the network  hash table to presort the tokens before running the join node tests  fine-tuning via parameters  frequently trade-off between memory usage and time

© Franz J. Kurfess ES Implementation 30 Special Cases for Pattern Matching  additional enhancements of the Rete network can be used to implement specific methods  backward chaining  requires a signal indicating to the network that a particular fact is needed  not conditional element  indicates the absence of a fact  requires special join nodes and special fields in the tokens passing through the network  test conditional element  uses a special join node that ignores its right input  the result of the function is passed on

© Franz J. Kurfess ES Implementation 31 Exploring the Rete Network in Jess  (watch compilations) function  diagnostic output when rules are compiled example-1: t  +1 one-input (pattern) node added to the Rete network  +2 two-input (pattern) node added  +t terminal node added  (view) function  graphical viewer of the Rete network in Jess  (matches ) function  displays the contents of the left and right memories of the join nodes for a rule  useful for examining unexpected rule behavior

© Franz J. Kurfess ES Implementation 32 Rule Formulation u Pattern Order u General vs. Specific Rules u Simple vs. Complex Rules u Loading and Saving Facts [Giarratano & Riley 1998]

© Franz J. Kurfess ES Implementation 33 Pattern Order  since Rete saves information about rules and facts, it can be critical to order patterns in the right way  otherwise a potentially huge number of partial matches can be generated

© Franz J. Kurfess ES Implementation 34 Example Pattern Order (deffacts information (find-match a c e g)f1 (item a)f2 (item b)f3 (item c)f4 (item d)f5 (item e)f6 (item f)f7 (item g))f8 (defrule match-1 (find-match ?x ?y ?z ?w)P1 (item ?x)P2 (item ?y)P3 (item ?z)P4 (item ?w)P5 ==> (assert (found-match ?x ?y ?z ?w)) (deffacts information (find-match a c e g) (item a) (item b) (item c) (item d) (item e) (item f) (item g)) (defrule match-1 (item ?x) (item ?y) (item ?z) (item ?w) (find-match ?x ?y ?z ?w) ==> (assert (found-match ?x ?y ?z ?w)) [Giarratano & Riley 1998]

© Franz J. Kurfess ES Implementation 35 Pattern Matches  full matches P1: f1 P2: f2,f3,f4,f5,f6,f7,f8 P3: f2,f3,f4,f5,f6,f7,f8 P4: f2,f3,f4,f5,f6,f7,f8 P5: f2,f3,f4,f5,f6,f7,f8  partial matches P1: [f1] P1-2: [f1,f2] P1-3: [f1,f2,f4] P1-4: [f1,f2,f4,f6] P1-5: [f1,f2,f4,f6,f8] Total: 29 full, 5 partial matches  full matches P1: f1 P2: f2,f3,f4,f5,f6,f7,f8 P3: f2,f3,f4,f5,f6,f7,f8 P4: f2,f3,f4,f5,f6,f7,f8 P5: f2,f3,f4,f5,f6,f7,f8  partial matches P1: [f2,f3,f4,f5,f6,f7,f8] P1-2:[f2,f2],[f2,f3],[f2,f4],[f2,f5], [f2,f6],[f2,f7],[f2,f8], [f3,f2],[f3,f3],[f3,f4],[f3,f5], [f3,f6],[f3,f7],[f3,f8],... P1-3, P1-4:... P1-5: [f2,f4,f6,f8, f1] Total: 29 full, 2801 partial matches

© Franz J. Kurfess ES Implementation 36 Guidelines for Pattern Ordering  most specific patterns first  smallest number of matching facts  largest number of variable bindings to constrain other facts  patterns matching volatile facts go last  facts that are changing frequently should be used by patterns late in the LHS  smallest number of changes in partial matches  may cause a dilemma with the above guideline  patterns matching the fewest facts first  reduces the number of partial matches

© Franz J. Kurfess ES Implementation 37 Multifield Variables  multifield wildcards and multifield variables are very powerful, but possible very inefficient  should only be used when needed  limit their number in a single slot of a pattern

© Franz J. Kurfess ES Implementation 38 Test Conditional Element  the test conditional element should be placed as close to the top of the rule as possible  reduces the number of partial matches  evaluation of expressions during pattern matching is usually more efficient

© Franz J. Kurfess ES Implementation 39 Built-In Pattern Matching Constraints  the built-in constraints are always more efficient than the equivalent expression  not so good: (defrule primary-color color ?x&: (or (eq ?x red) (eq ?x green) (eq ?x blue) ==> (assert (primary-color ?x)))  better: (defrule primary-color color ?x&red|green|blue) ==> (assert (primary-color ?x)))

© Franz J. Kurfess ES Implementation 40 General vs. Specific Rules  some knowledge can be expressed through many specific, or a few general rules  specific rules generate a top-heavy Rete network with many pattern nodes and fewer join nodes  general rules offer better opportunities for sharing pattern and join nodes  it usually is easier to write an inefficient general rule than an inefficient specific rule

© Franz J. Kurfess ES Implementation 41 Simple vs. Complex Rules  simple rules are sometimes elegant, but not necessarily efficient  storing temporary facts can be very helpful  especially in recursive or repetitive programs

© Franz J. Kurfess ES Implementation 42 Loading and Saving Facts  facts can be kept in a file, and loaded into memory when needed  (load-facts) and (save-facts) functions  may lead to visibility or scoping problems if the respective deftemplates are not contained in the current module

© Franz J. Kurfess ES Implementation 43 Figure Example

© Franz J. Kurfess ES Implementation 44 Post-Test

© Franz J. Kurfess ES Implementation 45 Evaluation u Criteria

© Franz J. Kurfess ES Implementation 46 Use of References  [Giarratano & Riley 1998] [Giarratano & Riley 1998]  [Russell & Norvig 1995] [Russell & Norvig 1995]  [Jackson 1999] [Jackson 1999]  [Durkin 1994] [Durkin 1994] [Giarratano & Riley 1998]

© Franz J. Kurfess ES Implementation 47 Important Concepts and Terms  agenda  assert  backward chaining  constant  fact  expert system (ES)  expert system shell  forward chaining  join node  knowledge base  knowledge-based system  left (alpha) memory  matches  matching  pattern  pattern matching  pattern node  RETE algorithm  retract  right (beta) memory  rule  substitution  term  test conditional element  unification  variable  view  working memory

© Franz J. Kurfess ES Implementation 48 Summary ES Implementation  for rule-based systems, an efficient method for pattern matching between the rule antecedents and suitable facts is very critical  matching every rule against all possible facts repeatedly is very inefficient  the Rete algorithm is used in many expert system shells  it constructs a network from the facts and rules in the knowledge base  since certain aspects of the knowledge base are quite static, repeated matching operations can be avoided  a few strategies can be used by programmers to achieve better performance  most specific patterns first, patterns with volatile facts last  careful use of multifield variables, general rules  use of the test conditional element, built-in pattern constraints  loading and saving of facts

© Franz J. Kurfess ES Implementation 49