Upload
myra-washington
View
236
Download
1
Embed Size (px)
Citation preview
L3. Knowledge Representation and Matching in Java
Matching.java
Unify.java
What to RepresentWhat 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
How to representHow to represent
Logical representation schemes
Procedural representation schemes
Network representation schemes
Structured representation schemes
different inference mechanisms
Representing Simple FactsRepresenting 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), …
Knowledge Base: FactsKnowledge 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)
The Need for RulesThe 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
Representing Simple RulesRepresenting 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)
Knowledge Bases: RulesKnowledge 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)
Note: compare with problem solving as search
KNOWLEDGEBASE (KB)
facts and rules
INFERENCE MECHANISM
Updates
Query Answer
Knowledge Base ArchitectureKnowledge Base Architecture
• 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 LanguageRepresenting Language
• 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 formula A is True
Reasoning with LogicReasoning with Logic
Sentence in propositional logicSentence 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)
Sentences in first-order logicSentences in first-order logic
• Atomic sentences = predicate(term1, term2, …termn)
or term1 = term2
Term = function(term1, term2, …termn)
or constant or variable
• Complex sentences: made from atomic sentences using connectives.
(not), (and), (or), (implies), and (equivalent)
Syntax of FOL: basic elementSyntax 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
Seven inference rules for propositional LogicSeven 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
1 2 … n
1 2 … n 1, 2, …, n
1 2 … n
i
,
,
The three new inference rulesThe 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}, )
Example of proofExample 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)
Unification and MatchingUnification 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)
UnifyUnify
Unification function, Unify, is to take two atomic sentences p and q and return a substitution that would make p and q look the same.
A substitute unifies atomic sentences p and q if p =q
For example,
p q -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Knows(John, x) | Knows(John, Jane) | {x/Jane} Knows(John, x) | Knows(y, OJ) | {y/John, x/OJ} Knows(John, x) | Knows(y, Mother(y)) | {y/John, x/Mother(John)}
e.g., Unify(Knows(John, x), Knows(John, Jane)) = {x/Jane} Idea: Unify rule premises with known facts, apply unifier to conclusion.
e.g., if we know q and Knows(John, x) Likes(John, x)
then we can conclude Likes(John, Jane)
Likes(John, OJ)
Likes(John, Mother(John))
Premise 前提
(unify ‘(?x ?y a) ‘(?y ?x ?x))
((?y . A) (?X . ?Y))
(unify ‘(?x) ‘(f ?x))
NIL
(unify ‘a ‘a)
((T . T))
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: }
UnifyUnify
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: }
Output: matching vs. unify
Output from Matching.java
?x is ?y and ?x Rose is rose and ?yfalse
Output from Unify.java
?x is ?y and ?xRose is rose and ?y{?y=rose, ?x=Rose}true