Tutorial 8 Introduction to CLIPS and Assignment Discussion

advertisement
Tutorial 8
Introduction to CLIPS and
Assignment Discussion
COMPSCI 367
By:Aashmeet Kalra
AGENDA
• Machine Learning Assignment DiscussionReport Structure etc.
• INTRODUCTION TO CLIPS
• CLIPS DEMO (INSTALLATION and RUN)
• CLIPS Syntax with Demo
• Assignment 4 Preview
• Home Task.
MACHINE LEARNING ASSIGNMENT
• Run a number of experiments –compare different
algorithms for each dataset
e.g: training versus testing splits ,no. of runs,statistics
taken,…
REPORT:
a) The task: Describe the algorithms ,the datasets and
parameters
b) Explain your experimental methodology
c) Present your algorithms and results
d) Analyze results :Explain why you think certain algorithms
performed better than others.
INTRODUCTION TO CLIPS
• Download CLIPS from:
http://clipsrules.sourceforge.net/
CLIPS Documentation:
• Two CLIPS documents: (1) User’s Guide (2) Reference
Manual
• User’s Guide
Introduction to CLIPS
http://www.cs.auckland.ac.nz/compsci367s2c/resourc
es/clips/documentation/usrguide.pdf
CLIPS
• Reference Manual
• Volume I: “Basic programming guide”
http://www.cs.auckland.ac.nz/compsci367s2c/re
sources/clips/documentation/bpg.pdf
Syntax definitions and examples
• Volume II: “Advanced Programming Guide”
advanced stuff, experienced users
Volume III: “Interfaces Guide”
details about machine-specific interfaces
CLIPS Language
• C Language Implementation Production
System
• CLIPS is a tool for building expert systems
– Originally developed by the Software Technology
Branch (STB) at NASA Johnson Space Center
– First release in 1986
• CLIPS was designed to facilitate the
development of software to model human
knowledge
– Facts
– Rules
– Deffunctions and generic functions
– Object oriented programming
Facts
• Fact Assertion
– (assert (play Ivan tennis))
– (assert (duck))
– (assert (chair red))
• As facts are entered into the KB, they are assigned a
Fact Index
– (retract 1)
• Removes fact 1 from the KB
– (clear)
• Removes all facts from the fact base and KB
Facts
• Fact Assertion
– (facts)
• Dump the “fact base”
• Fact identifier – “time tag”
– f-0
– f-1
– Special fact
• (initial-fact)
• Is always F0 and is used to match the first/start rules
Facts
• When (reset) is entered, the result is…
f-0(initial-fact)
f-1(athelete Ivan very-good)
f-2(play Ivan tennis)
f-3(athelete Martina very-good)
f-4(play Martina tennis)
Templates
• To enter a fact with multiple slots you must declare its template
using the deftemplate construct.
• > (deftemplate frog “info about a frog”
(slot name)
(slot age)
)
> (list-deftemplates)
> (assert (frog (name jane)))
• When using templates to make facts, don’t forget to start with
“assert”
e.g. “(assert (frog …”
deffacts
• Useful if the same set of assertions will be used
every time a program is run
> (deffacts nice “stuff that is tasty”
(nice watermelon)
(nice fudgecake)
)
> (list-deffacts)
• Activate deffacts by resetting the facts:
> (reset) …only clears facts (keeps rules and
deffacts)
Mathematical Operators
• Uses prefix notation as in Lisp
(+ 3 4)
(+ (* 3 4) (* 5 6))
• Use = as assignment for fact assertion on left
hand side
• (assert (answer = ( * 3 4 ) ) )
– put
• (answer 12)
– in the fact list
In built Functions
• (+ ) (- ) (* ) (\ ) (** ) (mod
)
• (= ) (< ) (> ) (<= ) (>= ) (<>
)
• (and ) (or ) (not )
• (random min max)
• (sin ) (cos ) (tan ) (sqrt )
• (printout t “hello” crlf)
deffunction
•
•
•
•
•
Functions compute simple values
Many built-in:
> (+ 5 2)
> (sin 0.2)
We can define functions using deffunction
Use variables – a variable starts with “?”, e.g. ?name
When referring to variables inside a function, don’t forget to always include
“?”
> (deffunction increment (?i)
(+ ?i 1)
)
> (increment 6) …returns “7”
> (list-deffunctions)
> (undeffunction increment) …remove the function
Rules
• Syntax
(defrule r-name
pattern-1
…
pattern-n
=>
action-1
…
action-m)
“comment”
Rules
•
•
•
•
r-name is the rule name
comment must be surrounded by quotes
pattern-i is the antecedent pattern
action-j is the consequent pattern
• Rules are: IF conditions THEN (=>) results
• Don’t forget to “assert” facts on right hand side
(after “=>”)
> (defrule weather
(or (wearing raincoat) (holding umbrella))
=>
(assert (raining))
)
> (rules)
> (agenda) …shows which rules are ready to fire
• Start using rules with run, e.g.
> (assert (holding umbrella))
> (run)
> (facts)
• (raining)
> (undefrule weather) …remove the rule
Rules
• The agenda is the list of rules that have been
matched and are waiting execution
• (agenda) will print out the rules
• The agenda is prioritized by salience value
– Salience is specified by the programmer and is from 10000 to 10000
– Default is 0 if (declare (salience 25)) is not in rule e.g.
– Rules are selected for firing by salience
– Two rules of same salience use LIFO to fire
Note
• Rules have a property called refraction which
means that they will fire only once for the
same set of facts. If run is issued again the rule
won’t fire again. We can however force this to
happen by retracting and asserting the fact
again or by using refresh to refresh the rule:
(refresh fire-emergency)
Variables
• Variables start with a ?
– E.g. ?age
– Bindings are valid within a rule only
bind
Associate symbols (e.g. “bill”, “<Fact-1>”, “4”
etc.) to variables (e.g.“?name”)
• > (bind ?percent (random 1 100))
Wildcard matching
• ?
– matches one
• $?
– matches any number
• $?name
– match and bind
Wildcard matching
•
•
Example
(name ? ?Kennedy)
– will match
• (name John Fitzgerald Kennedy)
•
(name ? $? SMITH)
– will match
• (name John SMITH)
• (name Suzie Jane SMITH)
• (name John James Jones SMITH)
– but would not match
• (name SMITH)
• (name John Jones SMITH Rogers)
•
$?name is the same as the previous but the matches are bound to
$?name
Other Bits n Pieces
• To load an example file (e.g. “stove.clp”) use load command
> (load “C:/stove.clp”)
> (reset)
> (run)
• The open command is used for file i/o (reading / writing)
> (open “mfile.clp” file-handle “r”)
> (readline file-handle)
…
• If you’re using the command prompt and nothing happens when
you press
enter (just a blank new line) then you might need to add (a) a
closing bracket ‘)’ or (b) closing quotes ‘ ” ‘
Debugging
•
•
•
•
•
•
•
> (watch facts)
> (watch activation)
> (unwatch facts)
> (ppdefrule weather) …shows rule
> (ppdeffunction increment) …shows function
> (ppdeffacts nice)
> (printout t "quack" crlf))
…prints quoted text (t means terminal, crlf means
new line)
Assignment Briefing
• Next Tutorial Agenda
a) Modeling Knowledge
b) Decision Tree Representation
c) Implementation in CLIPS (guide you to write
logic for assignment).
Home Task
Below are heuristics for choosing which wine to have with a meal:
• “If the main course is red meat then serve red wine.”
• “If the main course is poultry and it is turkey then serve red wine.”
• “If the main course is poultry and it is not turkey then serve white
wine.”
• “If the main course is fish then serve white wine.”
Tasks
a)
b)
Construct a binary decision tree (yes/no branching) to represent
these heuristics.
Using the wine-choosing heuristics above, construct a decision
tree with multiplebranches.
Hint: make the root decision “What is the main course?”.
Download