Rule Based Expert System - SKKUmodsim.skku.ac.kr/bbs/Course/Data/Simulation/LectureNote... · 2016....

33
1 Artificial Intelligence Copyright (c) 2015 Intelligence Modeling Lab. All rights reserved. Rule Based Expert System

Transcript of Rule Based Expert System - SKKUmodsim.skku.ac.kr/bbs/Course/Data/Simulation/LectureNote... · 2016....

  • 1Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Rule Based Expert System

  • 2Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Rule-based Expert System

    Rule

    E.S

    Interface EngineKnowledge Base

    Facts, Assertions

    (Work Memory)

  • 3Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Rule-based Expert System

    Rule 1 : If Condition

    then action

    Rule 2 : If Condition

    then action

    .

    .

    .

    Rule n : If Condition

    then action

  • 4Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Rule-based Expert System

    Example : Medical diagnosis expert system

    Rules : medical knowledge

    Facts : Status of patients

    (blood pressure, running nose, weight, …)

    Diagnosis is done by applying the rules to the facts.

  • 5Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Deduction System Identifies Animals

    Z1 : If ?X has hair

    Then ?X is a mammal

    Z2 : If ?X gives milk

    Then ?X is a mammal

    Z3 : If ?X has feathers

    Then ?X is a bird

    Z4 : If ?X flies AND ?X lays eggs

    Then ?X is a bird

    Z5 : If ?X is a mammal

    AND ?X eats meat

    Then ?X is a carnivore

    Z6 : If ?X is a mammal AND ?X eats meat AND ?X has claws

    AND ?X’s eyes point forward

    Then ?X is a carnivore

    Z7 : If ?X is a mammal AND ?X has hoofs

    Then ?X is an ungulate

    Z8 : If ?X is a mammal AND ?X chews cud

    Then ?X is an ungulate

  • 6Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Deduction System Identifies Animals

    Z9 : If ?X is a carnivore AND ?X has a tawny color

    AND ?X has a dark spots

    Then ?X is a cheetah

    Z10 : If ?X is a carnivore AND ?X has a tawny color

    AND ?X has black strips

    Then ?X is a tiger

    Z11 : If ?X is an ungulate AND ?X has long legs

    AND ?X has a long neck

    AND ?X has a tawny color AND ?X has dark spots

    Then ?X is a giraffe

    Z12 : If ?X is an ungulate AND ?X has a white color

    AND ?X has black stripes

    Then ?X is a zebra

    Z13 : If ?X is a bird AND ?X does not fly AND ?X has long legs

    AND ?X has a long neck AND ?X is black and white

    Then ?X is an ostrich

    Z14 : If ?X is a bird AND ?X does not fly AND swims AND ?X is black and white

    Then ?X is a pengiun

    Z15 : If ?X is a bird AND ?X is a good flyer

    Then ?X is an albatross

  • 7Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Forward Chaining

    Interface Engine

    Forward Chaining Backward Chaining

  • 8Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Forward Chaining

  • 9Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Forward Chaining

    1. Start with initial state

    2. Determine the conflict set = rules where condition

    is true (triggered rules)

    3. Apply conflict Resolution to select a rule from

    conflict set

    4. Fire this rule – carry out its action to change state

    5. Test for reaching goal

    Repeat 2 to 5 until goal is reached or conflict set is

    empty

  • 10Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Deduction System Identifies Animals

    Gives milk

    chews cud

    long legs

    long neck

    tawny color

    dark spot

    Z2 : If ?X gives milk

    Then ?X is a mammal

    * : inferred fact

    ** : goal fact

    *mammal

    Working Memory

  • 11Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Deduction System Identifies Animals

    Working Memory

    Gives milk

    chews cud

    long legs

    long neck

    tawny color

    dark spot

    *mammal

    Z8 : If ?X is a mammal AND ?X chews cud

    Then ?X is an ungulate

    * : inferred fact

    ** : goal fact

    *ungulate

    Z2

  • 12Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Deduction System Identifies Animals

    Working Memory

    Gives milk

    chews cud

    long legs

    long neck

    tawny color

    dark spot

    *mammal

    *ungulate* : inferred fact

    ** : goal fact** giraffe

    Z11 : If ?X is an ungulate

    AND ?X has long legs

    AND ?X has a long neck

    AND ?X has a tawny color

    AND ?X has dark spots

    Then ?X is a giraffe

    Z2, Z8

  • 13Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Reaction System Bags Groceries

    Item Container type Size Frozen?

    Bread plastic bag medium No

    Glop jar small No

    Granola cardboard box large No

    Ice cream cardboard carton medium Yes

    Potato chips plastic bag medium No

    Pepsi bottle large No

    * List of Items to be bagged.

    * Initial Data of Working MemoryStep is check-order

    Bag1 is a bag

    Bread is to be bagged

    Glop is to be bagged

    Granola is to be bagged

    Ice cream is to be bagged

    Potato chips are to be bagged

  • 14Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Reaction System Bags Groceries

    B1 : If step is check-order

    potato chips are to be bagged

    there is no Pepsi to be bagged

    Then ask the customer whether he would like a bottle of Pepsi

    B2 : If step is check-order B2: If step is check-order

    Then step is no longer check-order delete step is check-order

    step is bag-large-items add step is bag-large-items

    B3 : If step is bag-large-items

    a large item is to be bagged

    the large item is a bottle

    the current bag contains < 6 large items

    delete the large item is to be bagged

    add the large item is in the current bag

    B4 : If step is bag-large-items

    a large item is to be bagged

    the current bag contains < 6 large items

    delete the large item is to be bagged

    add the large item is in the current bag

  • 15Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Reaction System Bags Groceries

    B5 : If step is bag-large-items

    a large item is to be bagged

    an empty bag is available

    delete the current bag is the current bag

    add the empty bag is the current bag

    B6 : If step is bag-large-items

    delete step is bag-large-items

    add step is bag-medium-items

    * A Inter mediate state of working memoryStep is bag-medium-items

    Bag1 contains Pepsi

    Bag1 contains granola

    Bread is to be bagged

    Glop is to be bagged

    Ice cream is to be bagged

    Potato chips are to be bagged

  • 16Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Reaction System Bags Groceries

    B7 : If step is bag-medium-items

    a medium item is frozen, but not in a freezer bag

    delete the medium item is not in a freezer bag

    add the medium item is in a freezer bag

    B8 : If step is bag-medium-items

    a medium item is to be bagged

    the current bag is empty or contains only medium items

    the current bag contains no large items

    the current bag contains < 12 medium items

    delete the medium item is to be bagged

    add the medium item is in the current bag

  • 17Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Reaction System Bags Groceries

    B9 : If step is bag-medium-items

    a medium item is to be bagged

    an empty bag is available

    delete the current bag is the current bag

    add the empty bag is the current bag

    B10 : If step is bag-medium-items

    delete step is bag-medium-items

    add step is bag-small-items

    * A Inter mediate state of working memoryStep is bag-small-items

    Bag1 contains Pepsi

    Bag1 contains granola

    Bag2 contains Bread

    Bag2 contains Ice cream(in freezer bag)

    Bag2 contains Potato chips

    Glop is to be bagged

  • 18Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Reaction System Bags Groceries

    B11 : If step is bag-small-items

    a small item is to be bagged

    the current bag contains no large items

    the current bag contains no medium items

    the current bag contains < 18 medium items

    delete the small item is to be bagged

    add the small item is in the current bag

    B12 :If step is bag-small-items

    a small item is to be bagged

    an empty bag is available

    delete the current bag is the current bag

    add the empty bag is the current bag

  • 19Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    A Toy Reaction System Bags Groceries

    B13 : If step is bag-small-items

    delete step is bag-small-items

    add step is done

    * A Inter mediate state of working memoryStep is done

    Bag1 contains pepsi

    Bag1 contains granola

    Bag2 contains bread

    Bag2 contains ice cream(in freezer bag)

    Bag2 contains potato chips

    Bag3 contains glop

  • 20Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Conflict Resolution Strategies

    1. Rule Ordering : Arrange all rules in one long prioritized

    list. Use the triggered rule that has the highest priority.

    Ignore the others.

    2. Context Limiting : Reduce the likelihood of conflict by

    separating the rules into groups, only some of which

    are active at any time.

    3. Specificity Ordering : Whenever the conditions of one

    triggered rule are a superset of the conditions of

    another triggered rule, use the superset rule on the

    ground that it deals with more specific situations.

  • 21Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Conflict Resolution Strategies

    4. Data Ordering : Arrange all possible assertions in one

    long prioritized list. Use the triggered rule that has the

    condition pattern that matches the highest priority

    assertion in the list.

    5. Size Ordering : Use the triggered rule with the

    toughest requirements, where toughest means the

    longest list of conditions.

    6. Recency Ordering : Use the least recently used rule.

  • 22Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Design of Expert System

    Entities

    Knowledge-base Inference-Engine

    Synthesis

    Knowledge

    Base

    Classification

    Knowledge

    Base

    Synthesis

    Inference

    Engine

    Classification

    Inference

    Engine

    ~inference-engine

    ~object-classes

    ~rule-class

    ~kb-component

    ~it-ie

    ~local-memory-name

    ~lst

    ~name

    ~ : class or instance variable

    * : method

  • 23Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    Goal : If action is known,

    Then print action

    And conclude consultation.

    1

    Rule 4:

    If means = drive,

    and location = downtown,

    Then action = take cab.

    2

    Rule 5:

    If means = drive,

    and location = not downtown,

    Then action = drive.

    9Rule 6:

    If means = walk,

    and weather = bad,

    Then action =

    take coat and walk.

    10

    Rule 7:

    If means = walk,

    and weather = good,

    Then action = walk.

    11

    Rule 1:

    If distance > 5 miles,

    Then means = drive.

    3

    Question:

    What is distance ?

    4

    (User answer: 2)

    Rule 2:

    If distance > 1 miles,

    and time < 15 minutes,

    Then means = drive.

    5

    Question:

    How many minutes until showtime?

    6

    (User answer: 10 minutes) (User answer: downtown)

    Question:

    Is location or in suburbs?

    8

    Rule 3:

    If distance > 1 miles,

    and time > 15 minutes,

    Then means = walk.

    7

  • 24Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    Rules about Getting to the Theater

    Rule IF: THEN:

    (Premise) (Conclusion)

    1 Distance > 5 miles Means is “drive”

    2 Distance > 1 miles

    and Time < 15 minutes Means is “drive”

    3 Distance > 1 miles

    and Time > 15 minutes Means is “walk”

    4 Means is “drive”

    and Location is

    “downtown” Action is “take a cab”

  • 25Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    Rule IF: THEN:

    (Premise) (Conclusion)

    5 Means is “drive”

    and Location is

    not “downtown” Action is “drive your car”

    6 Means is “walk”

    and Weather is

    “bad” Action is “take a coat and walk”

    7 Means is “walk”

    and Weather is

    “good” Action “walk”

  • 26Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Forward Chaining ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    (define-structure rule

    condition

    action

    text

    )

    (define rules ’())

    (define (test-condition rule)

    (when (eval (rule-condition rule)) rule)

    )

    (define (execute-action rule)

    (eval (rule-action rule))

    )

    (define (conflict-resolve rules)

    (car rules)

    )

  • 27Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    (define (forward-chain)

    (let loop ( (temp rules) )

    (if (test-condition goal)

    (execute-action goal)

    (let*

    ( (conflict-set

    (clean (map (lambda (r) (test-condition r)) temp))

    )

    (fire (conflict-resolve conflict-set))

    )

    (if fire

    (begin

    (writeln (rule-text fire))

    (newline)

    (execute-action fire)

    (loop (delete! fire temp))

    ) ; begin

    (writeln “No rules left: -> terminates”)

    ); if fire

    ) ; let*

    ) ; if (test-cond…)

    ) ; let

    ) ; define

  • 28Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; getting to the theatre example ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    (define distance ’u)

    (define time ’u)

    (define weather ’u)

    (define location ’u)

    (define means ’u)

    (define action ’u)

    (define (initialize)

    (set! time ’u)

    (set! weather ’u)

    (set! location ’u)

    (set! means ’u)

    (set! action ’u)

    )

    (define r1 (make-rule

    ’condition ’(and (equal? means ’u)

    (> distance 5)

    )

    ’action ’(set! means “drive”)

    ’text “R1: if distance > 5 then means is drive”

    ) )

  • 29Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    (define r2 (make-rule

    ’condition ’(and (equal? means ’u)

    (> distance 1) (< time 15)

    )

    ’action ’(set! means “drive”)

    ’text “R2: if distance > 1 and time < 15 then means is drive”

    )

    )

    (define r3 (make-rule

    ’condition ’(and (equal? means ’u)

    (> distance 1)

    (> time 15)

    )

    ’action ’(set! means “walk”)

    ’text “R3: if distance > 1 and time > 15 then means is walk”

    )

    )

  • 30Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    (define r4 (make-rule

    ’condition ’(and (equal? means “drive”)

    (equal? location “downtown”)

    )

    ’action ’(set! means “take a cab”)

    ’text “R4: if means is drive and location is downtown then take a cab”

    )

    )

    (define r5 (make-rule

    ’condition ’(and (equal? means “drive”)

    (not (equal? location “downtown”))

    )

    ’action ’(set! action “drive your car”)

    ’text “R5: if means is drive and location is not downtown then drive your car”

    )

    )

  • 31Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    (define r6 (make-rule

    ’condition ’(and (equal? means “walk”)

    (equal? weather “bad”)

    )

    ’action ’(set! action “take a coat and walk”)

    ’text “R6: if means is walk and weather is bad then take a coat and walk”

    )

    )

    (define r7 (make-rule

    ’condition ’(and (equal? means “walk”)

    (equal? weather “good”))

    )

    ’action ’(set! action “walk”)

    ’text “R7: if means is walk and weather is good then walk”

    )

    )

  • 32Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    (define rules (list r1 r2 r3 r4 r5 r6 r7))

    (define goal (make-rule

    ’condition ’(not (equal? Action ‘u))

    ’action ’(writeln “The action to take is: ”

    action)

    )

    )

  • 33Artificial IntelligenceCopyright (c) 2015 Intelligence Modeling Lab. All rights reserved.

    Expert System

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Initial condition ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    (define distance 2)

    (define time 20)

    (define weather “bad”)

    (define location “downtown”)

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    (define (clean lst)

    (delete! ’() lst)

    )