
4b701e25e12a7a04e4ed4da4747b9b4f.ppt
- Количество слайдов: 37
Chapter 8 Pattern Matching
8. 2 Variables in CLIPS are always written in the syntax of a question mark followed by a symbolic field name. n Examples: n ? speed ? sensor ? value ? noun ? color 2
Bound (Bind) Variables are used on the LHS of a rule to contain slot values that can later be compared to other values on the LHS of a rule or accessed on the RHS of a rule. n The term bound and bind are used to describe the assignment of a value to a variable. n 3
Use of variables One common use of variables is to bind a variable on the LHS of a rule and then use that value on the RHS of the rule. n Example: n CLIPS> (clear) CLIPS> (deftemplate person (slot name) (slot eyes) (slot hair)) 4
CLIPS> (defrule find-blue-eyes (person (name ? name) (eyes blue)) => (printout t ? name “has blue eyes. ” crlf)) CLIPS> (deffacts people (person (name Jane) (eyes blue) (hair red)) (person (name Joe) (eyes green) (hair brown)) (person (name Jack) (eyes blue) (hair black)) (person (name Jeff) (eyes green) (hair brown))) CLIPS> (reset) CLIPS> (run) Jack has blue eyes. Jane has blue eyes. CLIPS> Example 1 5
8. 3 Multiple Use of Variables n The first time a variable is bound to a value, it retains that value within the rule. Other variables with the same name that are bound to a value must be bound to the first variable’s value. n CLIPS> (deftemplate find (slot eyes)) CLIPS> (defrule find-eyes (find (eyes ? eyes)) (person (name ? name) (eyes ? eyes)) => (printout t ? name “ has” ? eyes “ eyes. ” crlf)) Example 2 6
n (reset) (assert (find (eyes blue))) <Fact-5> CLIPS> (run) Jack has blue eyes. Jane has blue eyes. CLIPS> (assert (find (eyes green))) <Fact-6> CLIPS> (run) Jeff has green eyes. Joe has green eyes. CLIPS> (assert (find (eyes purple))) <Fact-7> CLIPS> (run) CLIPS> 7
8. 4 Fact Addresses Retraction, modification, and duplication of facts are extremely common operations and are usually done on the RHS of a rule rather than at the top level. n Before a fact can be manipulated from the RHS of a rule, however, there must be some way to specify the fact that matched a particular pattern. n 8
Pattern Binding A variable can be bound to the fact address of the fact matching a pattern on the LHS of a rule by using the pattern binding operation, “<-”. n Once the variable is bound it can be used with the retract, modify, or duplication commands in place of a fact index. n 9
n CLIPS> (clear) CLIPS> (deftemplate person (slot name) (slot address)) CLIPS> (deftemplate moved (slot name) Without the retract command, (slot address)) the program will go into an CLIPS> (defrule process-moved-information infinite loop. ? f 1 <- (moved (name ? name) (address ? address)) ? f 2 <- (person (name ? name)) => (retract ? f 1) (modify ? f 2 (address ? address))) CLIPS> (deffacts example (person (name “John Hill”) (address “ 25 Mulberry Lane”)) (moved (name “John Hill”) (address “ 37 Cherry Lane”))) CLIPS> (reset) CLIPS> (run) Example 3 10
8. 5 Single-field Wildcard It is useful to test for the existence of a field within a slot without actually assigning a value to a variable. n Particularly useful for multifield slots. n Example: Suppose we want to print the social security number of every person with a specified last name…… n 11
n (deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people Not referred to by actions on the Instead of using a variable, a (person (name John Q. Public) pattern or slots be RHS or other wildcard can on single-field (social-security-number a of theis required, used when 483 -98 -9083)) the LHS field rule. (person (name Jack R. Public) USE! important. but the value is not NO (social-security-number 483 -98 -9084)) (defrule print-social-security-numbers (print-ss-numbers-for ? last-name) (person (name ? first-name ? middle-name ? last-name) (social-security-number ? ss-number)) => (printout t ? ss-number crlf)) 12
n (deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people Instead of using a variable, a (person (name John Q. Public) wildcard can be single-field (social-security-number a field is required, used when 483 -98 -9083)) (person (name Jack R. Public) but the value is not important. (social-security-number 483 -98 -9084)) (defrule print-social-security-numbers (print-ss-numbers-for ? last-name) (person (name ? ? ? last-name) (social-security-number ? ss-number)) => (printout t ? ss-number crlf)) Example 4 13
Blocks World n n n A good example of planning, might be applied to automated manufacturing, where a robot arm manipulates parts. To begin solving, it will be useful to set up a configuration of blocks that can be used for testing the program. What steps must be taken to D A move block C on top of E? B E C F 14
Simple Solution n Rule: directly move block C on top of block E. However, this rule could only be applied if both block C and block E had no blocks on top of them. Rule move-directly IF The goal is to move block ? upper on top of block ? lower and block ? upper is the top block in its stack and block ? lower is the top block in its stack THEN Move block ? upper on top of block ? lower C E C F 15
Simple Solution – cont. n The simple blocks world does not require that the blocks be restacked, just move them to the floor. above RULE Clear-upper-block IF The goal is to move block ? x and block ? x is not the top block in its stack and block ? above is on top of block ? x THEN The goal is to move block ? above to the floor X above X RULE Clear-lower-block IF The goal is to move another block on top of block ? x and block ? x is not the top block in its stack and block ? above is on top of block ? x THEN The goal is to move block ? above to the floor 16
Blocks World – cont. n Since the floor is really not a block, it may be necessary to treat it differently. RULE Move-to-floor IF The goal is to move block ? upper on top of the floor and block ? upper is the top block in its stack THEN Move block ? upper on top of the floor upper 17
Blocks World – cont. n Several types of facts will be needed: (deftemplate on-top-of (slot upper) (slot lower)) n Facts: (on-top-of (upper A) (lower B)) (on-top-of (upper B) (lower C)) (on-top-of (upper D) (lower E)) (on-top-of (upper E) (lower F)) (on-top-of (upper nothing) (lower A)) (on-top-of (upper C) (lower floor)) (on-top-of (upper nothing) (lower D)) (on-top-of (upper F) (lower floor)) A D B E C F 18
Floor and Nothing The words floor and nothing might be mistaken as the names of blocks. n Use implied deftemplate block to identify the blocks. n (block A) (block B) (block C) (block D) (block E) (block F) 19
Goals n Goal described: (deftemplate goal (slot move) (slot on-top-of) n Initial goal: (goal (move C) (on-top-of E)) 20
Initial Configuration n (deffacts initial-state (block A) (block B) (block C) (block D) (block E) (block F) (on-top-of (upper A) (lower B)) (on-top-of (upper B) (lower C)) (on-top-of (upper D) (lower E)) (on-top-of (upper E) (lower F)) (on-top-of (upper nothing) (lower A)) (on-top-of (upper C) (lower floor)) (on-top-of (upper nothing) (lower D)) (on-top-of (upper F) (lower floor)) (goal (move C) (on-top-of E))) 21
Determine Move-directly Rulethat there is a goal to move a block on top of another block. (defrule move-directly ? goal <- (goal (move ? block 1) (on-top-of ? block 2)) (block ? block 1) Ensure that a goal to move a (block ? block 2) block onto the (on-top-of (upper nothing) (lower ? block 1)) floor will not be processed by this rule. ? stack-1 <- (on-top-of (upper ? block 1) (lower ? block 3)) ? stack-2 <- (on-top-of (upper nothing) (lower ? block 2)) Update the stack information. => (retract ? goalagainst information Match ? stack-1 ? stack-2) (assert (on-top-of (upper ? block 1) (lower ? block 2)) necessary to update the stacks that (on-top-of (upper nothing) (lower ? block 3))) the moving block is being taken (printout t ? block 1 “ moved on top of “ ? block 2 “. ” crlf)) from and moved to. 1 1 3 2 2 3 22
Move-to-floor Rule (defrule move-to-floor ? goal <- (goal (move ? block 1) (on-top-of floor)) (block ? block 1) (on-top-of (upper nothing) (lower ? block 1)) ? stack <- (on-top-of (upper ? block 1) (lower ? block 2)) => (retract ? goal ? stack) (assert (on-top-of (upper ? block 1) (lower floor)) (on-top-of (upper nothing) (lower ? block 2))) (printout t ? block 1 “ moved on top of floor. ” crlf)) 1 23
Clear-upper-block Rule (defrule clear-upper-block (goal (move ? block 1)) (block ? block 1) (on-top-of (upper ? block 2) (lower ? block 1)) (block ? block 2) => (assert (goal (move ? block 2) (on-top-of floor))) 2 1 2 24
Clear-lower-block Rule 2 (defrule clear-lower-block (goal (on-top-of ? block 1)) 1 (block ? block 1) (on-top-of (upper ? block 2) (lower ? block 1)) (block ? block 2) => (assert (goal (move ? block 2) (on-top-of floor)))) n 2 Execution CLIPS> (unwatch all) CLIPS> (reset) CLIPS> (run) A moved on top of floor. B moved on top of floor. D moved on top of floor. C moved on top of E. CLIPS> A B B A C D E C F Example 5 D 25
Step-by-step Method n n n First: pseudorules were written using English-like text. Second: the pseudorules were used to determine the types of facts that would be required. Deftemplates describing the facts were designed, and the initial knowledge for the program was coded using these deftemplates. Finally, the pseudorules were translated to CLIPS rules using the deftemplates as a guide for translation. 26
Best Representation? It is not always possible to determine the best method for representing facts or the types of rules that will be needed to build an expert system. n Following a consistent methodology, however, can aid in the development of an expert system even when a great deal of prototyping and iteration need to be performed. n 27
8. 7 Multifield Wildcards Multifield wildcards ($? ) can be used to match against zero or more fields of a pattern. n Example: n (defrule print-social-security-numbers (print-ss-numbers-for ? last-name) (person (name ? ? ? last-name) (social-security-number ? ss-number)) => (printout t ? ss-number crlf)) will not match a person fact (person (name Joe Public) (social-security-number 345 -89 -3039)) 28
8. 7 Multifield Wildcards Multifield wildcards ($? ) can be used to Replace more fields of match against zero orthe two single-field a wildcards With a single multifield pattern. wildcard. n Example: n (defrule print-social-security-numbers (print-ss-numbers-for ? last-name) (person (name $? ? last-name) (social-security-number ? ss-number)) => (printout t ? ss-number crlf)) will not match a person fact (person (name Joe Public) (social-security-number 345 -89 -3039)) 29
Multifield Variables CLIPS> (reset) n CLIPS> (assert (print-children John Q. Public)) <Fact-3> n Example: construct showing how to print names of CLIPS> (run) all the children of Q. specified person. (Jane Paul Mary) (John a Public) has children (deftemplate person CLIPS> (multifield name) (multifield children)) The $ is only used on the LHS Multifield variables are preceeded by a “$? ”. to indicate that zero (deffacts some-people or more fields can be bound. Q. Public) (children Jane Paul Mary)) (person (name John to the (person (name Jack R. Public) (children Rick))) variable. (defrule print-children (print-children $? name) (person (name $? name) (children $? children)) => (printout t ? name “has children “ ? children crlf)) Example 6 30
More than one multifield variable can be used in a single slot CLIPS> (reset) n Example: find all the CLIPS> (assert (find-child a people who have Paul)) <Fact-3> child with a specific name. (run) CLIPS> (defrule find-child (John Q. Public) has child Paul (find-child ? child) Other children are (Jane) (Mary) (person (name $? name) CLIPS> (assert (find-child Rick)) (children $? before ? child $? after)) <Fact-4> => CLIPS> (run) (printout t ? name “has child” ? child crlf) (Jack R. Public) has child Rick (printout t “Other children are” ? before “ “ ? after crlf)) Other children are () () CLIPS> (assert (find-child Bill)) <Fact-5> CLIPS> (run) CLIPS> Example 6 31
Match pattern in more than one way n CLIPS> (reset) CLIPS> (assert (person (name Joe Fiveman) (children Joe Joe))) <Fact-3> CLIPS> (assert (find-child Joe)) <Fact-4> CLIPS> (agenda) 0 find-child: f-4, f-3 There are three different 0 find-child: f-4, f-3 ways in which the For a total of 3 activations. variables ? child ? before, CLIPS> (run) and ? after can be bound (Joe Fiveman) Has child Joe Other children are () (Joe Joe) with the fact f-3. (Joe Fiveman) Has child Joe Other children are (Joe) (Joe Fiveman) Has child Joe Example 6 Other children are (Joe Joe) () 32
Implementing a Stack A stack is an ordered data structure to which items can be added and removed. n Items are added and removed to one “end” of the stack. A new item can be pushed (added) to the stack or the last item added can be popped (removed) from the stack. n In a stack the first value added is the last item to be removed and the last item added is the first item to be removed. n 33
Push (defrule push-value ? push-value <- (push-value ? value) ? stack <- (stack $? rest) => (retract ? push-value ? stack) (assert (stack ? value $? rest)) (printout t “Pushing value “ ? value crlf)) (stack X Y Z) (push-value A) (stack A X Y Z) 34
Pop (stack A X Y Z) (pop-value) (defrule pop-value-valid ? pop-value <- (pop-value) (stack ? stack <- (stack ? value $? rest) => (retract ? pop-value ? stack) (assert (stack $? rest)) (printout t “Popping value “ ? value crlf)) X Y Z) (defrule pop-value-invalid ? pop-value <- (pop-value) (stack) => (retract ? pop-value) (printout t “Popping from empty stack” crlf)) Example 7 35
Blocks World Revisited (deffacts initial-state (stack A B C) (stack D E F) (goal (move C) (on-top-of E) (stack)) (defrule move-directly ? goal <- (goal (move ? block 1) (on-top-of ? block 2)) ? stack-1 <- (stack ? block 1 $? rest 1) ? stack-2 <- (stack ? block 2 $? rest 2) => (retract ? goal ? stack-1 ? stack-2) (assert (stack $? rest 1)) (assert (stack ? block 1 ? block 2 $? rest 2)) (printout t ? block 1 “ moved on top of “ ? block 2 “. ” crlf)) 36
Blocks World Revisited – cont. (defrule move-to-floor ? goal <- (goal (move ? block 1) (on-top-of floor)) ? stack-1 <- (stack ? block 1 $? rest) => (retract ? goal ? stack-1) (assert (stack ? block 1)) (assert (stack $? rest)) (printout t ? block 1 “ moved on top of floor. ” crlf )) (defrule clear-upper-block (goal (move ? block 1)) (stack ? top $? ? block 1 $? ) => (assert (goal (move ? top) (on-top-of floor)))) (defrule clear-lower-block (goal (on-top-of ? block 1)) (stack ? top $? ? block 1 $? ) => (assert (goal (move ? top) (on-top-of floor)))) Example 8 37
4b701e25e12a7a04e4ed4da4747b9b4f.ppt