Presentation is loading. Please wait.

Presentation is loading. Please wait.

L3. Knowledge Representation and Matching in Java Matching.java Unify.java.

Similar presentations


Presentation on theme: "L3. Knowledge Representation and Matching in Java Matching.java Unify.java."— Presentation transcript:

1 L3. Knowledge Representation and Matching in Java Matching.java Unify.java

2 What to Represent Facts about the world Definitions and general rules An agent’s beliefs and those of other agents Plans of action Degrees of certainty and uncertainty

3 How to represent Logical representation schemes Procedural representation schemes Network representation schemes Structured representation schemes different inference mechanisms

4 Representing Simple Facts Objects: potato, mike. Cooker, … Predicates : male, father, at, … Properties: male(mike), female(rose), rich(john), … Relations: father(mike, rose), in(john, restaurant, now), …

5 Knowledge Base: Facts parent(jock, morag) parent(jock, alasdair) parent(jock, hamish) parent(mairi, morag) parent(mairi, alasdair) parent(mairi, hamish) parent(fergus, jock) parent(rhoda, jock) parent(fergus, flora) parent(rhoda, flora) male(fergus) male(jock) male(alasdair) male(hamish) female(rhoda) female(mairi) female(morag) female(flora)

6 The Need for Rules Some facts can be inferred from others, e.g. fergus is morag’s grandfather Need generality variables stand for arbitrary objects, e.g. X Define some relations in terms of others e.g. mother is female parent

7 Representing Simple Rules Variables: x, P, C,... Generalised Facts: female(P), parent(P, C),... Conjunctions: parent(P, C)  female(P) Rules: parent(P, C)  female(P)  mother(P, C)

8 Knowledge Bases: Rules Family Relations parent(P, C)  female(P)  mother(P, C) parent(P, C)  male(P)  father(P, C) parent(GP, P)  parent(P, C)  grandparent(GP, C) parent(GP, P)  female(GP)  parent(P, C)  grandmother(GP, C) grandparent(GP, C)  male(GP)  grandfather(GP, C)

9 Note: compare with problem solving as search KNOWLEDGE BASE (KB) facts and rules INFERENCE MECHANISM Updates QueryAnswer Knowledge Base Architecture

10 Formal language to represent facts and rules about a microworld as sentences. Interpreted sentences represent a model of the microworld Syntax: how sentences formed mother_of(sarah,tal)  mother_of(sarah,mor) Semantics: how to interpret sentences True/False Set of all sentences (axioms, rules) is the abstract representation of the KB Base sentences are called axioms, derived sentences theorems, derivations proofs Representing Language

11 Mathematical logics have well-defined syntax, semantics, and models: –Propositional: facts are True/False –First Order: facts, objects, relations are True/False –Temporal logic: First Order + time –Probability theory: facts, degree of belief [0…1] Interpretation: truth assignment to each element on the formulaA is True Reasoning with Logic

12 Sentence in propositional logic Sentence  AtomicSentence | ComplexSentence AtomicSentence  proposition symbols like P, Q, R True | False ComplexSentence  (Sentence) | Setence Connective Sentence like P  Q  (P  Q)  (  P   Q) True | False Connectives:  (not),  (and),  (or),  (implies), and  (equivalent)

13 Sentences in first-order logic Atomic sentences = predicate(term 1, term 2, …term n ) or term 1 = term 2 Term = function(term 1, term 2, …term n ) or constant or variable Complex sentences: made from atomic sentences using connectives.  (not),  (and),  (or),  (implies), and  (equivalent)

14 Syntax of FOL: basic element Constant symbols: refer to the same object in the same interpretation e.g. Mike Jason, 4, A, B, … Predicate symbols: refer to a particular relation in the model. e.g., Brother, >, Function symbols: refer to particular objects without using their names. Some relations are functional, that is, any given object is related to exactly one other object by the relation. (one-one relation) e.g., Cosine, FatherOf, Variables: substitute the name of an objec. e.g., x, y, a, b,…  x, Cat(x)  Mammal(x) if x is a cat then x is a mammal. Logic connectives:  (not),  (and),  (or),  (implies), and  (equivalent) Quantifiers:  (universal quantification symbol),  (existential quantification symbol)  x, for any x, …  x, there is a x, … Equality: = e.g. Father(John) = Henry

15 Seven inference rules for propositional Logic (1) Modus Ponens (2) And-Elimination (3) And-Introduction (4) Or-Introduction (5) Double-Negation Elimination (6) Unit Resolution (7) Logic connectives:    ,  ii  1   2  …   n  1,  2, …,  n  1   2  …   n  i          ,          ,    

16 The three new inference rules (8) Universal Elimination: For any sentence , variable v, and ground term g: e. g.,  x Likes(x, IceCream), we can use the substitute {x/Rose} and infer Like(Rose, IceCream). (9) Existential Elimination: For any sentence , variable v, and constant symbol k that does not appear elsewhere in the knowledge base: e. g.,  x Kill(x, Victim), we can infer Kill(Murderer, Victim), as long as Murderer does not appear elsewhere in the knowledge base. (10) Existential Introduction: For any sentence , variable v that does not occur in , and ground term g that does occur in  : e. g., from Likes(Rose, IceCream) we can infer  x Likes(x, IceCream). SUBST({v/g},  )  v  Ground term is a term that contains no variables. SUBST({v/k},  )  v   v SUBST({g/v},  ) 

17 Example of proof Bob is a buffalo | 1. Buffalo(Bob) Pat is a pig | 2. Pig(Pat) Buffaloes outrun pigs | 3.  x, y Buffalo(x)  Pig(y)  Faster(x,y) ---------------------------------------------------------------------------------------------------- Bob outruns Pat --------------------------------------------------------------------------------------------------- Apply (3) to 1 And 2 | 4. Buffalo(Bob)  Pig(Pat) Apply (8) to 3 {x/Bob, y/Pat} | 5. Buffalo(Bob)  Pig(Pat)  Faster(Bob,Pat) Apply (1) to 4 And 5 | 6. Faster(Bob,Pat)

18 Unification and Matching The process of matching is called unification: for example, - p(x) matches p(Jack) with x = Jack - q(fatherof(x),y) matches q(y,z) with y=fatherof(x) and z = y note the result of the match is q(fatherof(x),fatherof(x)) - p(x) matches p(y) with x = Jack and y = Jack x = John and y = John or x = y - The match that makes the least commitment is called the most general unifier (MGU)

19 String matching: string1 = string2 e.g. “rose” = “rose” if string1.equals(string2) “I am Rose” = “I am Rose” “I am ?x” = “I am Rose” “I am ?x” = “?y am Rose” I = ?y am = am ?x = Rose ? Check? String  Tokens 40: // 同じなら成功 41: if(string1.equals(string2)) return true; 44: st1 = new StringTokenizer(string1); 45: st2 = new StringTokenizer(string2); 46: 47: // 数が異なったら失敗 48: if (st1.countTokens() != st2.countTokens()) 49: return false; 51: int length = st1.countTokens(); // 定数同士 52: for (int i = 0 ; i < length; i++){ 53: if (!tokenMatching(st1.nextToken(),st2.nextToken())){ 54: // トークンが一つでもマッチングに失敗したら失敗 55: return false; 56: } 57: } Unify

20 Token matching: token1 = token2 e.g. two strings’ matching, “I am ?x” = “?y am Rose” Three pairs of tokens’ matching: I = ?y am = am ?x = Rose 64: boolean tokenMatching(String token1,String token2){ 65: if(token1.equals(token2)) return true; 66: if( var(token1) && !var(token2)) 67: return varMatching(token1,token2); 68: if(!var(token1) && var(token2)) 69: return varMatching(token2,token1); 70: return false; 71: } 73: boolean varMatching(String vartoken,String token){ 74: if(vars.containsKey(vartoken)){ 75: if(token.equals(vars.get(vartoken))){ 76: return true; 77: } else { 78: return false; 79: } 80: } else { 81: vars.put(vartoken,token); 82: } 83: return true; 84: } 86: boolean var(String str1){ 87: // 先頭が ? なら変数 88: return str1.startsWith("?"); 89: } 27: Matcher(){ 28: vars = new Hashtable(); 29: }

21 Output: Matching.java vs. Unify.java Output from Matching.java ?x is ?y and ?x Rose is rose and ?y false Output from Unify.java ?x is ?y and ?x Rose is rose and ?y {?y=rose, ?x=Rose} true


Download ppt "L3. Knowledge Representation and Matching in Java Matching.java Unify.java."

Similar presentations


Ads by Google