Expert & Knowledge-Based Systems
Expert & Knowledge-Based Systems
Originally called expert systems, they would mimic the problem solving processes of domain experts
Such as doctors performing diagnosis, or engineers performing design, or wall street analysts selecting stock transactions
It was discovered that many problems were being solved by chaining through rules (if-then statements) that would operate on a collection of facts and partial conclusions
Called working memory
Introduction: Dendral
The Dendral system (DENDRitic ALgorithm) was the first expert system, developed in the 1960s
The idea was, given mass spectrogram data, determine what the chemical composition was
DENDRALs Processes
Operation Component (program)
MOLION PLANNER
CONGEN GENOA STEREO PREDICTOR MSPRUNE
Input
Mass spectrum Planning rules
Constraints
Output
Ion constraints Other constraints Superatoms
Candidate molecular structures
Planning
Generating Testing
Most plausible structures Structures consistent with spectrum Structures consistent with known reactions
DENDRAL in Action
Pattern Matching
An inference engine
How to apply the knowledge
For Dendral, this was done by generate-and-test, a very weak form of search Mycin would perform backward chaining through rules
More generically though, the inference engine would embody some form of pattern matching One of the first systems that clearly demonstrated pattern matching was Eliza
Eliza was a spoof of an expert system but the code will show us how pattern matching works
Eliza
Created by Joseph Weizenbaum This system would act like a Rogerian psychoanalyst by holding a dialog with a person
the dialog would be consist of the doctor (Eliza) asking questions, the human responding, and the doctor using the response to ask another question
Sample dialog:
H: Men are all alike E: In what way? H: Theyre always bugging us about something or another. E: Can you be more specific? H: Well, my boyfriend made me come here. E: Your boyfriend made you come here? H: He says Im depressed much of the time. E: I am sorry to hear you are depressed. H: Its true. I am unhappy. E: Do you think coming here will help you not to be unhappy? H: I need some help, that much seems certain. E: What would it mean to you if you got some help? H: Perhaps I could learn to get along with my mother. E: Tell me more about your family
Weizenbaum wrote the program so that it would generate an English response/question based on a group of patterns
If the user sentence matched a pattern, this pattern would be used to generate the next sentence/question
Repeat
Input a sentence Find a rule in the Eliza knowledge-base that matches the pattern
Attempt to perform pattern match (see next slide) Attempt to perform segment match (see two slides)
If rule found, select one of the responses randomly (each pattern will have at least one response) Fill in any variables Substitute values (you for I, I for you, me for you, am for are, etc) Respond
Until user quits Most of the work goes on in the pattern match and segment match functions
Pattern Match
(defun pat-match (pattern input &optional (bindings no-bindings)) "Match pattern against input in the context of the bindings" (cond ((eq bindings fail) fail) ((variable-p pattern) (match-variable pattern input bindings)) ((eql pattern input) bindings) ((segment-pattern-p pattern) Note: fail = nil (segment-match pattern input bindings)) ((and (consp pattern) (consp input)) (pat-match (rest pattern) (rest input) (pat-match (first pattern) (first input) bindings))) (t fail)))
Segment Match
(defun segment-match (pattern input bindings &optional (start 0)) (let ((var (second (first pattern))) (pat (rest pattern))) (if (null pat) (match-variable var input bindings) (let ((pos (position (first pat) input :start start :test #'equal))) (if (null pos) fail (let ((b2 (pat-match pat (subseq input pos) (match-variable var (subseq input 0 pos) bindings)))) (if (eq b2 fail) (segment-match pattern input bindings (+ pos 1)) b2)))))))
Start controls where to start looking over this segment in case part has already matched
Eliza Rules
(defparameter *eliza-rules* '((((?* ?x) hello (?* ?y)) (How do you do. Please state your problem.)) Here we see an excerpt (((?* ?x) I want (?* ?y)) from the rules of Eliza (What would it mean if you got ?y) (Why do you want ?y) (Suppose you got ?y soon)) For instance, if the (((?* ?x) if (?* ?y)) input were I want to (Do you really think its likely that ?y) have a cheeseburger, (Do you wish that ?y) the second pattern (What do you think about ?y) (Really-- if ?y)) (((?* ?x) no (?* ?y)) would match (Why not?) (You are being a bit negative) Eliza would respond (Are you saying "NO" just to be negative?)) with one of three outputs using to have (((?* ?x) I was (?* ?y)) (Were you really?) a cheeseburger in (Perhaps I already knew you were ?y) place of ?y (Why do you tell me you were ?y now?)) Such as Why do you (((?* ?x) I feel (?* ?y)) (Do you often feel ?y ?)) want to have a (((?* ?x) I felt (?* ?y)) cheeseburger (What other feelings do you have?))))
(defun match-variable (var input bindings) ;; does the given var match input the input? Updates the bindings (let ((binding (get-binding var bindings))) (cond ((not binding) (extend-bindings var input bindings)) ((equal input (binding-val binding)) bindings) (t fail))))
(defun segment-pattern-p (pattern) ;; segment-matching pattern like ((?* var) . pat)? (and (consp pattern) (consp (first pattern)) (symbolp (first (first pattern))) (segment-match-fn (first (first pattern)))))
A Grammar of Patterns
Here, we break down the components of a pattern matcher into specific grammatical components
pat var constant segment-pat single-pat (pat . pat) single-pat (?is var predicate) (?or pat1 pat2 ) (?and pat1 pat2 ) (?not pat) segment-pat ((?* var) ) ((?+ var) ) ((?? var) ) ((?if expr) ) var ?chars constant atom match any one expression to a variable or to a constant (see below) match against a sequence match against one expression match the first and the rest of a list
test predicate on one expression match on any of the patterns match on every of the expressions match if expression does not match
match on zero or more expressions match on one or more expressions match zero or one expression test if expression is true variables of the form ?name constants are any atoms (symbols, numbers, chars)
Consider enhancing pat-match to include new arguments ?is, ?or and ?and to apply setf, or and and while doing the pattern matching
(pat-match ((x = (?is ?n numberp)) (x = 34)) ((?n . 34)) (pat-match ((x = (?is ?n numberp)) (x = x)) NIL (pat-match ((?x (?or < = > ?y) (3 < 4)) ((?Y . 4) (?X . 3)) (pat-match (x = (?and (?is ?n numberp) (?is ?n oddp))) (x = 3)) ((?N . 3))
MYCIN
Implemented in the early 1970s, Mycin is perhaps the most recognized and cited expert system
Developed at Stanford, it performs bacteriological diagnosis both disease identification and treatment
Tested against doctors, interns, medical teachers, and medical students, Mycin actually outperformed them all in an experiment of some 80 different cases!
Combining CFs
CF(P1 & P2) = minimum(CF(P1), CF(P2)) CF(P1 ^ P2) = maximum(CF(P1), CF(P2)) CF(R1 R2) = CF(R1) * CF(R2) Assume R1 concludes C1 and R2 concludes C1, what is our conclusion of C1 given R1 and R2? CF(C1) = CF(R1) + CF(R2) CF(R1) * CF (R2)
if CF(R1) > 0 and CF(R2) > 0
(defun cf-or (a b) (cond ((and (> a 0) (> b 0)) (+ a b (* -1 a b))) ((and (< a 0) (< b 0)) (+ a b (* a b))) (t (/ (+ a b) (- 1 (min (abs a) (abs b)))))))
MYCIN Code
(defun use-rules (parm) ;; Try every rule associated with this parameter (some #'true-p (mapcar #'use-rule (get-rules parm))))
(defun use-rule (rule) ;; apply a rule (put-db 'current-rule rule) ;; If any premise is known false, give up. ;; If every premise can be proved true, then ;; draw conclusions (weighted with the certainty factor). (unless (some #'reject-premise (rule-premises rule)) (let ((cf (satisfy-premises (rule-premises rule) true))) (when (true-p cf) (dolist (conclusion (rule-conclusions rule)) (conclude conclusion (* cf (rule-cf rule)))) cf))))
Code Continued
(defun satisfy-premises (premises cf-so-far) ;; see if all premises of a rule are satisfied (if not, try to satisfy them) ;; cf-so-far is an accumulator of certainty factors (cond ((null premises) cf-so-far) ((not (true-p cf-so-far)) false) (t (satisfy-premises (rest premises) (cf-and cf-so-far (eval-condition (first premises))))))) (defun eval-condition (condition &optional (find-out-p t)) ;; See if this condition is true, optionally using FIND-OUT (multiple-value-bind (parm inst op val) (parse-condition condition) (when find-out-p (find-out parm inst)) ;; Add up all the (val cf) pairs that satisfy the test (loop for pair in (get-vals parm inst) when (funcall op (first pair) val) sum (second pair))))
(defun cf->english (cf) (cond ((= cf 1.0) "there is certain evidence") ((> cf .8) "there is strongly suggestive evidence") ((> cf .5) "there is suggestive evidence") ((> cf 0.0) "there is weakly suggestive evidence") ((= cf 0.0) "there is NO evidence either way") ((< cf 0.0) (concatenate 'string (cf->english (- cf)) "AGAINST the conclusion")))) (defun print-condition (condition stream number) (format stream "~& ~d)~{ ~a~}" number (let ((parm (first condition)) (inst (second condition)) (op (third condition)) (val (fourth condition))) (case val (YES `(the ,inst ,op ,parm)) (NO `(the ,inst ,op not ,parm)) (T `(the ,parm of the ,inst ,op ,val))))))
(defun print-rule (rule &optional (stream t) depth) (declare (ignore depth)) (format stream "~&Rule ~a:~& If" (rule-number rule)) (print-conditions (rule-premises rule) stream) (format stream "~& Then ~a (~a) that" (cf->english (rule-cf rule)) (rule-cf rule)) (print-conditions (rule-conclusions rule) stream))
EMYCIN
it was later determined that MYCIN was performing a task called Heuristic Classification
IF: THE MOST CURRENT ACTIVE CONTEXT IS ASSIGNING A POWER SUPPLY AND AN SBI MODULE OF ANY TYPE HAS BEEN PUT IN A CABINET R1 sample AND THE POSITION IT OCCUPIES IN THE CABINET IS KNOWN rule: AND THERE IS SPACE IN THE CABINET FOR A POWER SUPPLY AND THERE IS NO AVAILABLE POWER SUPPLY AND THE VOLTAGE AND FREQUENCY OF THE COMPONENTS IS KNOWN THEN: FIND A POWER SUPPLY OF THAT VOLTAGE AND FREQUENCY AND ADD IT TO THE ORDER
Beyond EMYCIN
Once rule-based systems had been introduced, a number of programming languages were released that allowed quick and easy construction of rule-based systems
Often called Production System Languages because a rule-base is a type of production system Most of these languages supported either backward chaining or forward chaining OPS5: forward chaining, used to develop many expert systems, included the ability to encode certainty factors or other forms of uncertainty (such as probabilities) Prolog: backward chaining, logic statements only (no mechanisms for uncertainty, no ability to represent NOT) SOAR: OPS5 + chunking (a rudimentary learning algorithm) CLIPS: Written in C++ but looks like Lisp, forward and backward chaining + salience (how useful a rule might be) Jess: CLIPS re-written in Java with GUI capabilities
CLIPS Code
(defrule advice18 (high mortgage-rate) => (assert (not (buy now)))) (defrule advice19 (and (rising house-prices) (not (high inflation))) => (assert (buy now))) (defrule advice20 (high inflation) => (assert (high mortgage-rate)) (assert (rising house-prices))) (defrule diagnose63 (and (parent ?p ?c) (allergy-risk ?p ?d)) => (assert (allergy-risk ?c ?d))) (defrule diagnose64 (and (parent ?p ?c) (allergy-risk ?c ?d)) => (assert (allergy-risk ?p ?d)))
(defrule prescribe221 (and (infection gram-positive) (tolerable penicillin))) => (assert (indicated penicillin)))
(defrule check95 (not (allergic-to penicillin)) => (assert (tolerable penicillin)))
Disadvantages:
Some knowledge is not necessarily in rule form Many experts give inconsistent (or hesitate to provide) certainty factors
Meta-knowledge: knowledge that will help you select what knowledge to apply or examine
Puff/Centaur
The Puff expert system performed pulmonary disorder diagnosis
Implemented using rules and EMCYIN
Another system, Centaur, used the same knowledge, but in a different way
Rules were grouped together into specialized agents One agent per diagnostic conclusion Conclusions were grouped into a hierarchy so that a generic disease would be higher in the disease taxonomy and its children would be more specific instances Rather than thousands of rules, Centaur had dozens of agents (each implemented as an object) Each agent contained the knowledge necessary to diagnose that one conclusion
More on Centaur
A portion of Centaurs taxonomy is shown to the left Diseases were divided into more specific categories all the way down to the most specific such as
Severe Asthma Mild Bronchitis
MYCIN demonstrated a task called Heuristic Classification but confused matters by forcing all knowledge into rule form Centaur separated out the classification task from the rules Taking this further, we can clearly identify different tasks to perform during a problem solving process
We will call these tasks
Taking It Further
Thus, we might separate out the knowledge that allows us to categorize diseases from the knowledge that allows us to recognize a specific disease from the knowledge that allows us to explain why we believe a specific disease is responsible for the data
These low-level tasks have been dubbed Generic Tasks
Classes:
Classifier (an entire hierarchy) Classification Specialist (a single node in the hierarchy) Recognition-Agent (a single set of rules to determine how likely a given concept/hypothesis matches the data available) Match-1-Recognition-Agent (an RA with multiple sets of rules) Discrete-Pattern-Recognition-Agent (an RA with only a single set of rules to match against) Abducer (an object that knows how to explain data given hypotheses that can explain the data)
GT Tools
Generic Task Class Definition Slot Slot Slot Compile-class Macro Fills slots in with proper information and/or code Expert Knowledge Defintions
Sample code
(define-classification-specialist FuelSystem (display-name= "Fuel System Specialist") (establish-reject= (judge FuelSystemSummary)) (classifier= AutoMechSystem) (super-specialists= AutoMech) (sub-specialists= Delivery Mixture Vacuum AirIntake BadFuel) (creation-date= "5 July 1988") (last-modification-date= "5 July 1988") (author= "John D. McElroy"))