• 沒有找到結果。

Introduction to CLIPS

N/A
N/A
Protected

Academic year: 2021

Share "Introduction to CLIPS"

Copied!
42
0
0

加載中.... (立即查看全文)

全文

(1)

Chapter 7

(2)

Course Outline

• Introduction to Artificial Intelligence

• Introduction to Expert Systems

• Representation of Knowledge

• Methods of Inference

• CLIPS

• Reasoning under Uncertainty

• Design of Expert Systems

(3)

CLIPS

(C Language Integrated Production Systems)

KB fact

list Inference Engine

Expert KE Users

(4)

The CLIPS Programming Tool

• History of CLIPS

– Influenced by OPS5 and ART

– Implemented in C for efficiency and portability – Developed by NASA, distributed & supported by

COSMIC

– Runs on PC, Mac, also under UNIX and VAX VMS

• CLIPS provides mechanisms for expert systems

– A top-level interpreter

– Production rule interpreter

– Object oriented programming language – LISP-like procedural language

(5)

Components of CLIPS

• Rule-Based Language with 3 components

– Can create a fact list

– Can create a rule set (knowledge base)

– An inference engine matches facts against rules

• Object-Oriented Language

– Can define classes

– Can create different sets of instances

(6)

Notation

• Symbols & characters are entered exactly as shown except [ ], < >, { }

– e.g. (example) => (example)

• []: optional

– e.g. (example [1]) => (example) or (example 1)

• < >:variable

– e.g. (example <integer> ) => (example 1) or (example 5)…

• < >*: zero or more occurrences (or using << >>)

– e.g. (example <<integer>>) => (example 1) or (example 5 1) …

• < >+ :one or more occurrences (or using<<<>>>)

– = <> + <<>>

• {}: alternative (or using a vertical bar | )

(7)

Fields

1

• Seven types of fields (primitive data types)

– float, integer, symbol, string,

– external address, instance name, instance address

• Number (integer and float):

1, +3, -1, 65

(8)

Fields

2

• Symbol type:

– A printable ASCII character followed by zero or more characters.

A word may not begin with < |& $ ? + - ( ) ; and may not contain

< | & ( ) ; delimiters e.g. valid words:

emergency, fire, emergency-fire, A-B-C, A-387, !?#$^*

(9)

Fields

3

• String Type:

“This is a book” “!?#$^” “<-;() +-”

““fire”” “ + fire + ” ( 3 tokens)

“\“fire\”” “fire” (to include “ inside string, use \) Special character: the following character is used as the contents of the string

(10)

Facts and templates

1

• A fact: a “chunk” of information • 2 ways to create facts

– (assert (factname factvalue))

– Define fact template first, then use assert or deffacts functions

• Fact consists of a relation name followed by zero or more slots and their associated values

– (Day Tuesday) ;fact Day with value Tuesday – (emergency fire) ;fact emergency with value fire – (number-list 5 1 6 8) ;ordered fact with values 5 1 6 8 – (components computer CPU disk-drive terminal)

– (person (name “John Doe”) ;person template must be defined first

– (age 23) ;person is relation name – (eye-color blue) ;(slot value)

(11)

Facts and templates

2

• Deftemplate construct

– (deftemplate <relation-name> [<optional comment>] – <slot-definition>*)

– <slot-definition>* = (slot <slot-name>) | (multislot <slot-name>)

(deftemplate person ;define fact template for person – (multislot name) ;multi-field slot

– (slot age) ;single value slot

– (slot eye-color)

(12)

Basic operations of CLIPS

1

1. Start CLIPS: C:\CLIPS>CLIPS CLIPS> 2. Leave CLIPS: CLIPS> (exit) C:\> 3. Define facts:

• CLIPS>(assert (emergency fire)) ;(assert <fact>+)

CLIPS>(assert (person (name “John Doe”)(age 23)(eye-color blue)(hair-color black))) ;if person template has been defined • Use READ or READLINE in rules

• (READ) or (READLINE)

(13)

Basic operations of CLIPS

2

Define facts:

• Assert a set of facts after deftemplate is defined • (deffacts <deffacts name> [<optional comment>] • <facts>*)

• Instead of typing many times of assertion like

– (assert (person (name “John Doe”)(age 23)(eye-color blue)(hair-color black))) …

• Use deffacts function

– (deffacts people “Some people we know”

• (person (name “John Doe”)(age 23)(eye-color

• blue)(hair-color black)))

• (person (name “Jane Doe”)(age 36)(eye-color

(14)

Basic operations of CLIPS

3

4. Operations on facts:

Show all facts

CLIPS> (facts)

f-0 (Tom 7015) ;f-0 is fact identifier, 0 is fact index f-1 (John 8023) f-2 (Mary 1235) f-3 (Peter 1256) • Delete facts CLIPS> (retract f-1) CLIPS> (facts) f-0 (Tom 7015) f-2 (Mary 1235) f-3 (Peter 1256)

Remove the definitions of facts

(15)

Basic operations of CLIPS

4

5. Read facts or rules from files:

CLIPS> (LOAD filename) facts + rules ; will return TRUE if no errors occur

CLIPS> (LOAD-facts filename) facts only CLIPS> (SAVE filename)

(16)

6. Reset command

CLIPS> (Reset)

1>Remove all facts from the fact list. 2>Insert facts defined by deffacts.

The matched rules will be put into AGENDA during the insertions of facts. 3> Clips automatically defines 2 constructs (deftemplates initial-fact) and

Inference engine

AGENDA

rules deffacts

new facts Select matched rules

Select rule to fire and

remove the rule from agenda

Basic operations of CLIPS

5

(reset) (run) activated rules

(17)

Basic operations of CLIPS

6

7. Execution

CLIPS> (Run) ;(run [<limit>]) max # of rules to be fired

8. Define rules

; This is a rule

(defrule class-A-fire-emergency ;rule name (emergency fire) ;IF part

(fire-class A) ;patterns must be defined =>

(printout t “Activate sprinkler system” crlf)) (assert (sprinkler system work))

(18)

Basic operations of CLIPS

7

8. Define rules

(defrule <rulename> [<comment>]

<patterns>* ;LHS of the rule =>

<actions>* ;RHS of the rule

-If a rule has no patterns, initial-fact is asserted, hence will be placed on agenda.

-If agenda has no rules, program ceases execution. -When multiple rules in agenda, the rule with highest priority (salience) is fired.

(19)

Basic operations of CLIPS

8

9. Operations for rules:

CLIPS > (AGENDA)

list the rules in AGENDA

CLIPS > (rules)

list the rules being defined

CLIPS > (pprule rule-name)

display contents of a rule

CLIPS > (excise rule-name)

remove a rule

10. System call

CLIPS > (system “DIR”)

CLIPS > (system “command”)

CLIPS > (system “PE2”)

(20)

Basic operations of CLIPS

9

11. The use of special symbol

CLIPS > (system “C:\\DOS50\\ mem”)

1

st

‘\’: The following is some special symbol.

2

nd

‘\’: The special symbol used as the content

(21)

Basic operations of CLIPS

10

• (modify

<fact-index>

<slot-modifier>+)

– <slot-modifier>+ is (<slot-name> <slot-value>) – E.g. (modify 0 (age 24)) ;modify John Doe’s age

• (duplicate

2

(name “Jack S. Doe”))

– a new fact is created with new name slot

• (watch <facts, rules, activations, all>)

– Turn on debugging command watch

• (unwatch <facts, rules, activations, all>)

(22)

Basic operations of CLIPS

11

• (refresh <rule-name>)

– to make the rule fire again

• (list-defrules)

– display current list of rules

• (list-deftemplates)

• (list-defacts)

• (ppdefrule <defrule-name>)

– Pretty print defrule

(23)

Basic operations of CLIPS

12

• (undefrule (defrule-name>)

– To delete a defrule

– Similar for templates and facts

• (clear)

– Remove all information contained in the CLIPS

environment

• (printout <logical-name> <print-item>*)

– Logical-name: output destination

– e.g., t is terminal

(24)

Basic operations of CLIPS

13

• (matches <defrule-name>)

– Display the list of facts and partial matches that

matches the specified rule

• (set-break <rule-name>)

– Set a breakpoint for the specified rule, causing

rule execution to halt before the rule is fired.

• (show-breaks)

– List the rules that have breakpoints set

• (remove-break [<rule-name>])

(25)

CLIPS Example

1

• Convert the following general net representing

airline routes using deftemplate and deffacts.

Chicago

San Francisco New York

Indianapolis

Los Angeles

(26)

CLIPS Example

2

• (deftemplate flight

– (multislot arrival)

– (multislot departure))

• (deffacts airline-routes

– (flight (departure Houston) (arrival San Francisco)) – (flight (departure Houston) (arrival Indianapolis)) – (flight (departure Houston) (arrival New York))

– (flight (departure San Francisco) (arrival Los Angeles)) – (flight (departure San Francisco) (arrival Chicago))

– (flight (departure Chicago) (arrival Indianapolis)) – (flight (departure Indianapolis) (arrival Houston)) – (flight (departure Los Angeles) (arrival Houston)) )

(27)

CLIPS Example

3

• Fires are classified according to the principal

burning material

. Translate the following

information to rules for determining

fire classes

– Type A fires involve ordinary combustibles such as paper, wood, and cloth

– Type B fires involve flammable and combustible liquids (oil and gas), greases, and similar materials

• The

type of extinguisher

that should be used on a

fire depends on the

fire class

– Class A fires should be water or water-based liquids and dry chemicals

(28)

CLIPS Example

4

• (defrule paper-fire

• (material-burning paper) • =>

• (assert (fire-class A))) • (defrule wood-fire

• (material-burning wood) • =>

• (assert (fire-class A))) • (defrule oil-fire

• (material-burning oil) • =>

(29)

CLIPS Example

5

• (defrule class-A-fire

(fire-class A)

=>

(printout t “Extinguish the fire with

heat-absorbing or” crlf)

(printout t “combustion-retarding extinguishers

such as “ crlf)

(printout t “water or water based liquids and dry

chemicals.” crlf))

(30)

CLIPS Example

6

(31)

CLIPS Example

7

• Guess Animal (facts)

– (node (name root) (type decision)

– (question "Is the animal warm-blooded?")

– (yes-node node1) (no-node node2) (answer nil)) – (node (name node1) (type decision)

– (question "Does the animal purr?")

– (yes-node node3) (no-node node4) (answer nil)) – (node (name node2) (type answer) (question nil) – (yes-node nil) (no-node nil) (answer snake)) – (node (name node3) (type answer) (question nil) – (yes-node nil) (no-node nil) (answer cat)) – (node (name node4) (type answer) (question nil) – (yes-node nil) (no-node nil) (answer dog))

(32)

CLIPS Example

8

• Guess Animal (rules)

– ;;;=================================================== – ;;; Self Learning Decision Tree Program

– ;;; This program was introduced in Section 12.3. – ;;; It tries to determine the animal you are

– ;;; thinking of by asking questions. – ;;; CLIPS Version 6.0 Example

– ;;; To execute, merely load, reset and run.

– ====================================================== – (deftemplate node – (slot name) – (slot type) – (slot question) – (slot yes-node) – (slot no-node) – (slot answer))

(33)

– (defrule initialize

– (not (node (name root))) – =>

– (load-facts "animal.dat") – (assert (current-node root)))

– (defrule ask-decision-node-question – ?node <- (current-node ?name) – (node (name ?name)

– (type decision)

– (question ?question)) – (not (answer ?))

– =>

– (printout t ?question " (yes or no) ") – (assert (answer (read))))

– (defrule bad-answer

– ?answer <- (answer ~yes&~no)

(34)

– (defrule proceed-to-yes-branch – ?node <- (current-node ?name) – (node (name ?name)

– (type decision)

– (yes-node ?yes-branch)) – ?answer <- (answer yes) – =>

– (retract ?node ?answer)

– (assert (current-node ?yes-branch)))

– (defrule proceed-to-no-branch – ?node <- (current-node ?name) – (node (name ?name)

– (type decision)

– (no-node ?no-branch)) – ?answer <- (answer no) – =>

– (retract ?node ?answer)

– (assert (current-node ?no-branch)))

(35)

– (defrule ask-if-answer-node-is-correct – ?node <- (current-node ?name)

– (node (name ?name) (type answer) (answer ?value)) – (not (answer ?))

– =>

– (printout t "I guess it is a " ?value crlf) – (printout t "Am I correct? (yes or no) ") – (assert (answer (read))))

– (defrule answer-node-guess-is-correct – ?node <- (current-node ?name)

– (node (name ?name) (type answer)) – ?answer <- (answer yes)

– =>

– (assert (ask-try-again)) – (retract ?node ?answer))

(36)

– (defrule answer-node-guess-is-incorrect – ?node <- (current-node ?name)

– (node (name ?name) (type answer)) – ?answer <- (answer no)

– =>

– (assert (replace-answer-node ?name)) – (retract ?answer ?node))

– (defrule ask-try-again – (ask-try-again)

– (not (answer ?)) – =>

– (printout t "Try again? (yes or no) ") – (assert (answer (read))))

(37)

– (defrule one-more-time – ?phase <- (ask-try-again) – ?answer <- (answer yes) – =>

– (retract ?phase ?answer) – (assert (current-node root)))

– (defrule no-more

– ?phase <- (ask-try-again) – ?answer <- (answer no) – =>

– (retract ?phase ?answer)

– (save-facts "animal.dat" local node))

(38)

– (defrule replace-answer-node

– ?phase <- (replace-answer-node ?name) – ?data <- (node (name ?name)

– (type answer) – (answer ?value)) – =>

– (retract ?phase)

– ; Determine what the guess should have been – (printout t "What is the animal? ")

– (bind ?new-animal (read))

– ; Get the question for the guess

– (printout t "What question when answered yes ") – (printout t "will distinguish " crlf " a ")

– (printout t ?new-animal " from a " ?value "? ") – (bind ?question (readline))

– (printout t "Now I can guess " ?new-animal crlf) –

(39)

– ; Create the new learned nodes – (bind ?newnode1 (gensym*)) – (bind ?newnode2 (gensym*)) – (modify ?data (type decision) – (question ?question) – (yes-node ?newnode1) – (no-node ?newnode2)) – (assert (node (name ?newnode1) – (type answer)

– (answer ?new-animal))) – (assert (node (name ?newnode2) – (type answer)

– (answer ?value)))

– ; Determine if the player wants to try again – (assert (ask-try-again)))

(40)

Understanding the concepts of Expert Systems

+

Knowing how to elicit Expertise

Knowing how to build and Expert System

Lack of practical concepts and programming skills.

(41)

Limitations of CLIPS

• Single level rule sets

– in LOOPS, you could arrange rule sets in a hierarchy, embedding one rule set inside another, etc

• Loose coupling of rules and objects

– rules can communicate with objects via message passing

– rules cannot easily be embedded in objects, as in Centaur

• CLIPS has no explicit agenda mechanism

– the basic control flow is forward chaining

(42)

Alternatives to CLIPS

• Eclipse

– has same syntax as CLIPS (both are based on ART) – supports goal-driven (i.e., backwards) reasoning

– has a truth maintenance facility for checking consistency – can be integrated with C++ and dBase

– new extension RETE++ can generate C++ header files

• NEXPERT OBJECT

– another rule- and object-based system

– has facilities for designing graphical interfaces

– has a ‘script language’ for designing user front-end – written in C, runs on many platforms, highly portable

參考文獻

相關文件

All participants should be made aware of the potential hazards of the field and the necessary safety precautions during briefings on the fieldwork or upon arrival at the site.

Recycling Techniques are Available to Address Specific Pavement Distress and/or Pavement Structural Requirement.. Introduction to Asphalt Introduction

 The nanostructure with anisotropic transmission characteristics on ITO films induced by fs laser can be used for the alignment layer , polarizer and conducting layer in LCD cell.

Though there are many different versions of historical accounts regarding the exact time of his arrival, Bodhidharma was no doubt a historical figure, who, arriving in

 一般我們如過是透過分享器或集線器來連接電腦 的話,只需要壓制平行線即可(平行線:兩端接 頭皆為EIA/TIA 568B),

 一般我們如過是透過分享器或集線器來連接電腦 的話,只需要壓制平行線即可(平行線:兩端接 頭皆為 EIA/TIA 568B ), 如果是接機器對機器 的話,需要製作跳線( Crossover :一端為

3) 請先充分地搓 揉預計切除的部 分。這樣使外皮 會與裡面芯線產 生間隙。.. 4) 將網路線夾在剝 皮工具的最外側溝

3) 請先充分地搓 揉預計切除的部 分。這樣使外皮 會與裡面芯線產 生間隙。?. 4) 將網路線夾在剝 皮工具的最外側溝