(FO) Inference Methods
E N D
Presentation Transcript
(FO) Inference Methods CPSC 386 Artificial Intelligence Ellen Walker Hiram College
Inference Methods • Unification (prerequisite) • Forward Chaining • Production Systems • RETE Method (OPS) • Backward Chaining • Logic Programming (Prolog) • Resolution • Transform to CNF • Generalization of Prop. Logic resolution
Forward Chaining • Given a new fact, generate all consequences • Assumes all rules are of the form • C1 and c2 and c3 and…. --> result • Each rule & binding generates a new fact • This new fact will “trigger” other rules • Keep going until the desired fact is generated • (Semi-decidable as is FOL in general)
Efficient Forward Chaining • Order conjuncts appropriately • E.g. most constrained variable • Don’t generate redundant facts; each new fact should depend on at least one newly generated fact. • Production systems • RETE matching • CLIPS (on CS)
OPS • Facts • Type, attributes & values • (goal put-on yellow-block red-block) • Rules • If conditions, then action. • Variables (<x>, <y>, etc) can be bound • If (goal put-on <x> <y>) AND • (clear <x>) THEN add (goal clear <y>)
RETE Network • Based only on Left Sides (conditions) of rules • Each condition (test) appears once in the network • Tests with “AND” are connected with “JOIN” • Join means all tests work with same bindings
Example Rules • If (goal put-on <x> <y>) AND (clear <x>) AND (clear <y>) THEN add (on <x> <y>) delete (clear <y>) 2. If (goal clear <x>) AND (on <y> <x>) AND (clear <y>) THEN add (clear <x>) add (on <y> table) delete (on <y> <x>) 3. If (goal clear <x>) AND (on <y> <x>) THEN add (goal clear <y>) 4. If (goal put-on <x> <y>) AND (clear <x>) THEN add (goal clear <y>) 5. If (goal put-on <x> <y>) AND (clear <y>) THEN add (goal clear <x>)
Using the RETE Network • Each time a fact comes in… • Update bindings for the relevant node (s) • Update join(s) below those bindings • Note new rules satisfied • Each processing cycle • Choose a satisfied rule
Example (Facts) • (goal put-on yellow-block red-block) • (on blue-block yellow-block) • (on yellow-block table) • (on red-block table) • (clear red-block) • (clear blue-block)
Why RETE is Efficient • Rules are “pre-compiled” • Facts are dealt with as they come in • Only rules connected to a matching node are considered • Once a test fails, no nodes below are considered • Similar rules share structure • In a typical system, when rules “fire”, new facts are created / deleted incrementally • This incrementally adds / deletes rules (with bindings) to the conflict set
CLIPS • CLIPS is a forward-chaining production system that uses the RETE method. • Important commands • (assert fact) (deffacts fact1 fact2 … ) • (defrule rule-name rule) • (reset) - eliminates all facts except “initial-fact” • (load file) (load-facts file) • (run) • (watch all) • (exit)
CLIPS Rule Example (defrule putting-on ?g <- (goal put-on ?x ?y) (clear ?x) ?bottomclear <- (clear ?y) ==> (assert (on ?x ?y)) (retract ?g) (retract ?bottomclear) )
Blocks example • (load “blocks.clp”) • (assert (on blue table)) • (assert (clear blue)) • (assert (on red table)) • (assert (on green red)) • (assert (clear green) • (assert (goal put-on red blue)) • (watch all) • (run)
Backward Chaining • Consider the item to be proven a goal • Find a rule whose head is the goal (and bindings) • Apply bindings to the body, and prove these (subgoals) in turn • If you prove all the subgoals, increasing the binding set as you go, you will prove the item. • Logic Programming (gprolog, on CS)
Prolog Rules • Rule Example • puton(X,Y) :- cleartop(X), cleartop(Y),takeoff(X,Y). • Capital letters are variables • Three parts to the rule • Head (thing to prove) • Neck :- • Body (subgoals, separated by ,) • Rules end with .
Prolog Environment • SWI-Prolog on windows – to start, double-click plwin • To read a file, consult(‘file’). • To enter data directly, consult(user). Type control-D when done. • Every statement must end in a period. If you forget, put it on the next line. • To prove a fact, enter the fact directly at the command line. gprolog will respond Yes, No, or give you a binding set. If you want another answer, type ; otherwise return. • Trace(predicate) or trace(all) will allow you to watch the backward chaining process.
Monkey and Banana in Prolog movedTo(X,Y) :- at(X,Y,Z). movedTo(monkey,Y) :- at(monkey,X,floor), write('Walk to '), write(Y), nl. movedTo(ladder,Y) :- at(ladder,X,floor), movedTo(monkey,X), write('Drag ladder to '), write(Y), nl. reachable(X) :- at(X,Y,ceiling), movedTo(ladder,Y), movedTo(monkey,Y). reach(X) :- reachable(X), write('Reach for '), write(X), nl.
Running the Monkey & Banana consult(user). compiling user for byte code... at(monkey,corner,floor). at(monkey,corner,floor). at(ladder,left,floor). at(ladder,left,floor). at(banana,center,ceiling). at(banana,center,ceiling). Control-D reach(banana).
In-class assignment • Try the following in both Prolog and Clips: • Define the concept of a prerequisite, and at least the following facts: • Prerequisite of 172 is 171 Prerequisite of 201 is 172 • Prerequisite of 400 is 201 Prerequisite of 386 is 172 • Create a rule that a course cannot be taken unless its prerequisite has been taken • Create facts to indicate that Mary has taken 172 and 171. • Prove the following in your system: • Mary can take 386, Mary cannot take 400