Week 3 Help desks – Mon 4-5 CO238 Tutor: Su – Wed 1-2 CO238 Tutor: Urvesh – Fri CO238 Tutor: Bing Lectures: Mon, Tue, 3-4, HU119 Tutorial: Fri, 3-4, HU119 Comp 307Lecture 19:1
Comp 307 Knowledge based systems Reasoning Backward chaining Forward chaining Reasoning implemented in Prolog More on Knowledge representation More on reasoning Challenges
Comp 307 Simple Rule-based System in Prolog mammal:-f(hair). mammal:-f(milk). bird:-f(feathers). bird:-f(eggs), f(flies). carnivore:-mammal, f(meat). classify(tiger):-carnivore, f(tawny), f(striped). classify(puma):-carnivore, f(black). f(hair). f(black). f(meat). |?-classify(X). X=puma.
An example classify.pl – Prolog – Knowledge in a special rule language – Expert system shell/tool – Backward chaining Available on lecture page It will ask questions, just answer “yes” or “no”, can ask “why”. Comp 307Lecture 19:4
Comp 307 Rule Language for the KnowledgeBase Defining the operators :- op(511, xfx, ::). :- op(505, fx, if). :- op(503, xfx, then). :- op(501, xfy, and). rule4:: if eggs and flies then bird. ::(rule4,if(then(and(eggs, flies),bird))).
Comp 307 An animal classifier: The knowledge base rule1:: if hair then mammal. rule2:: if milk then mammal. rule3:: if feathers then bird. rule4:: if eggs and flies then bird. rule5:: if mammal and meat then carnivore. rule6:: if mammal and claws and pointed_teeth and forward_eyes then carnivore. rule9:: if carnivore and tawny and striped then tiger. rule11::if carnivore and black then puma. askable(hair). askable(milk).... hypothesis(tiger). hypothesis(puma)....
Comp 307 Backward chaining classify:- hypothesis(Goal), confirm(Goal). confirm(Goal):- RuleNo:: if Preconds then Goal, confirm(Preconds). confirm(Condition1 and Condition2):- confirm(Condition1),!, confirm(Condition2). confirm(Condition):- askable(Condition), fact(Condition, yes).
Comp 307 Backward Chaining Interpreter Input: Goal G, Set S of rules and facts Output: yes if G is logically implied by S; no otherwise Initialize current goal to G; repeat Choose a goal A from the set of current goals; if A is a fact in S then remove A from current set of goals else if there is no rule in S whose head is A then exit and output no else begin Choose a rule A :-Body in S, where Body is a set of goals Remove A from current set of goals; Add Body to the current set of goals; end until the current set of goals is empty output yes ;
Comp 307 Backward Chaining: Example Goal: u u w, p. v n, r. w r. p a, b. q a, c, d. r e, p. a b e n
Comp 307 Operators in Prolog Examples of operators: :- ?-, ; + - * / is Operator declarations e.g. :- op(1200, fx, ?-). :- op(1200, xfx, :-). :- op(1000, xfy,,). :- op(700, xfx, is). :- op(500, yfx, [+,-]). :- op(400, yfx, [*,/]).
Comp 307 Operators in Prolog Operator precedence is represented as a number (A higher number means a lower precedence.) Operator types: xfx binary, infix, not associative xfy binary, infix, right-associative yfx binary, infix, left-associative fx,fy unary, prefix xf,yf unary, postfix
Comp 307 Operator Precedence and Parse Trees :- op(1200, xfx, :-). :- op(700, xfx, is). :- op(500, yfx, [+,-]). add(X,Y,Z) :- Z is X + Y Internal (term) representation: :- (add(X,Y,Z), is(Z,+(X,Y))). :- (1200) add(X,Y,Z)is (700) + (500)Z XY
Comp 307 Design your own rules in prolog :-op(800, fx, if). :-op(700, xfx, then). :-op(300, xfy, or). :-op(200, xfy, and). If hall_wet and kitchen_dry then leak_in_bathroom. if window_closed or no_rain then no_water_from_outside. fact(hall_wet). fact(bathroom_dry). fact(no_rain).
Comp 307 backward-chaining Pick one hypothesis Find the rule Verify each condition in the body of the rule – Each condition as a new goal
Comp 307 Backward chaining in Prolog is_true(p):- fact(p). is_true(P):- if Condition then P, is_true (Condition). is_true(P1 and P2):- is_true(P1), is_true(P2). is_true(P1 or P2):- is_true(P1) ; is_true(P2).
Comp 307 Forward Chaining Data driving, Data directed reasoning, bottom up – Search from facts to valid conclusions Given database of true facts: – Apply all rules that match facts in the database – Add conclusions to database – Repeat until a goal is reached OR – Repeat until no new facts added
Comp 307 Forward Chaining Interpreter Input: a set of goals G, Set S of rules and facts Output: An element of G logically implied by S Initialize working memory to facts in S; Repeat Choose a rule A:- Body in S; if Body is a subset of working memory then if A is not in working memory then add A to working memory until an element of G is in working memory; output the element of G in working memory
Comp 307 Forward Chaining: Example Goal: u u w, p. v n, r. w r. p a, b. q a, c, d. r e, p. a b e n
Comp 307 Design your own rules in prolog :-op(800,fx,if). :-op(700, xfx, then). :-op(300,xfy,or). :-op(200,xfy,and). If hall_wet and kitchen_dry then leak_in_bathroom. if window_closed or no_rain then no_water_from_outside. fact(hall_wet). fact(bathroom_dry).
Comp 307 Forward Chaining in Prolog :-dynamic fact/1. farward:- new_derived_fact(P),!, assert(fact(P)), farward. farward:-write(‘no more facts’). new_derived_fact(Conclusion):- if Cond then Conclusion, \+ fact(Conclusion), composed_fact(Cond). composed_fact(Cond):-fact(Cond). composed_fact(Cond1 and Cond2):- composed_fact(Cond1), composed_fact(Cond2). composed_fact(Cond1 or Cond2):- composed_fact(Cond1) ; composed_fact(Cond2).
Comp 307 Backward Chaining and Forward Chaining Backward chaining (Goal driving, Goal-directed reasoning, top-down) – Search from hypotheses to relevant facts Good when: – Limited number of hypotheses – Determining truth of facts costs – Very large number of possible facts, mostly irrelevant Forward chaining (Data driving, Data-directed reasoning, bottom-up) – Search from facts to valid conclusions Good when – Very large number of possible conclusions – True facts known at start Bi-directional reasoning
Knowledge Representation and reasoning More Knowledge Representation – Decision trees – Decision tables – Belief nets – Fuzzy logic – Neural networks – … Reasoning – Case based reasoning Knowledge: Cases and modification rules Reasoning: find similar case and adapt – … Comp 307
Decision Tables diethabitat coloranimal meatjungle stripedtiger meathouse stripedtabby grassplains stripedzebra meatjungle brownweasel Example Rules: tiger meat jungle striped jungle<-- meat /\ striped /\ tiger
Comp 307 Decision Tree Diet? grass meat zebra Color? weasel brown Habitat? striped tiger tabby house jungle
Knowledge based systems Challenges and limitations – Knowledge acquisition – Conflict resolution – Uncertainty – Common sense – …… Comp 307