220 likes | 525 Views
L7. Logic for knowledge representation and inference. 知識表現 推理. Propositional logic (continue …) Propositional logic versus first-order logic ( 命題論理対 一階述語論理 ) Apply first-order logic e.g. A family relations A proof. Sentence in propositional logic.
E N D
L7. Logic for knowledge representation and inference 知識表現 推理 • Propositional logic (continue …) • Propositional logic versus first-order logic • (命題論理対一階述語論理 ) • Apply first-order logic • e.g. A family relations • A proof
Sentence in propositional logic • Sentence AtomicSentence | ComplexSentence • AtomicSentence proposition symbols like P, Q, R • True | False • ComplexSentence (Sentence) • | Sentence Connectives • like P Q (P Q) (P Q) • True | False • Connectives: (not), (and), (or), (implies), and (equivalent) 原子文 複文 連結語 (必然的に)伴う
FOL: basic elementsand sentences • Constant (定数) symbols: refer to the same object in the same interpretation • Predicate (述語) symbols: refer to a particular relation in the model. • Function (関数) symbols: refer to particular objects without using their names. • Logic connectives: (not), (and), (or), (implies), and (equivalent) • Equality (対等): = e.g. Father(John) = Henry • Variables (変数): substitute (取り替える) for the name of an object. • e.g., x, y, a, b,… x, Cat(x) Mammal(x) //Mammal:哺乳動物. • Quantifiers (数量詞): (universal (一般) quantification symbol) • (existential (存在に関する) quantification symbol) • x, for any x, … x, there is a x, … • Atomic sentences = predicate(term1, term2, …termn) • Term = function(term1, term2, …termn) • or constant or variable • Complex sentences: made from atomic sentences using connectives.
, 1 2… n i 1, 2, …, n i 1 2… n 1 2… n , Seven inference rules for propositional Logic • R(1) Implication-Elimination • R(2) And-Elimination • R(3) And-Introduction • R(4) Or-Introduction • R(5) Double-Negation Elimination • R(6) Unit Resolution • R(7) Logic connectives: ,
v v SUBST({v/g}, ) SUBST({v/k}, ) v SUBST({g/v}, ) The three new inference rules added to FOL • R(8) Universal-Elimination: For any sentence , variable v, and ground term g: • e. g., xLikes(x, IceCream), we can use the substitute {x/Rose} and • infer Like(Rose, IceCream). • R(9) Existential-Elimination: For anysentence , 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. • R(10) Existential-Introduction: For anysentence , 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). Ground term is a term that contains no variables. v/g k
Representing Simple Rules Variables: x, P, C, ... Generalised Facts: female(P), parent(P, C), ... Conjunctions: parent(P, C) female(P) Rules: P, Cparent(P, C) female(P) mother(P, C)
Knowledge Bases: Rules Family Relations GP, P, C 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)
Knowledge Base Architecture Updates KNOWLEDGE BASE (KB) facts and rules Query Answer INFERENCE MECHANISM
Example of proof in first-order logic Bob is a buffalo | 1. Buffalo(Bob) --f1 Pat is a pig | 2. Pig(Pat) --f2 Buffaloes run faster than pigs| 3. x, y Buffalo(x) Pig(y) Faster(x,y) --r1 ---------------------------------------------------------------------------------------------------- To proof: Bob runs faster than Pat --------------------------------------------------------------------------------------------------- Apply R(3) to f1 And f2 | 4. Buffalo(Bob) Pig(Pat) --f3 (And-Introduction) Apply R(8) to r1 {x/Bob, y/Pat} | 5. Buffalo(Bob) Pig(Pat) Faster(Bob,Pat) --f4 (Universal-Elimination) Apply R(1) to f3 And f4 | 6. Faster(Bob,Pat) --f5 (Inplication-Elimination )
Search with primitive inference rules Operators are inference rules States are sets of sentences Goal test checks state to see if it contains query sentence R(1) to R(10) are common inference pattern 1 2 3 Apply R(3) to 1 & 2 1 2 3 4 Apply R(8) to 3 1 2 3 4 5 Apply R(1) to 4 & 5 1 2 3 4 56
A Reasoning System Rule Base Working Memory Interaction with Inference Engine Matching Fire a Rule Select a Rule Acting
Finding gold The agent has been in [1,1]->[2,1]->[1,1]->[1,2] and knows there is a wumpus in [1,3] and a pit in [3,1] then goes to [2,2] continues to [3,2] (forward since it perceives nothing special) Now, percept sentences (add a new one): there is a breeze in the square [3,2] B3,2 Since knowledge is not enough to judge whether [4,2] and [3,3] are safe or not, the agent goes back to [2,2], turn left and goes to [2,3]. Now, percept sentences(add new ones): there is a glitter in the square [2,3] G2,3 The agent has been in [1,1]->[2,1]->[1,1]->[1,2] -> [2,2] -> [3,2] -> [2,2]-> [2,3] Here is the answer: the gold is in [2,3].
Finding a way back to the start position • Go back along the path (5 steps) • [1,1]->[2,1]->[1,1]->[1,2] -> [2,2] -> [3,2] -> [2,2]-> [2,3] • (a doubly linked list would be a good data structure for recording • agent’s history path) • Using safe squares to build a tree, search a shortest path • from [2,3] to [1,1] (3 steps). p w g p A p
[2,3] [2,2] [1,2] [3,2] [2,1] [1,1] Finding a way back to the start position • Go back along the path (5 steps) • [1,1]->[2,1]->[1,1]->[1,2] -> [2,2] -> [3,2] -> [2,2]-> [2,3] • (a doubly linked list would be a good data structure for recording • agent’s history path) • Using safe squares to build a tree, search a shortest path • from [2,3] to [1,1] (3 steps). Quiz: Draw a tree from the square where there is a gold) to the square [1,1] p w g p A p
Appendix (追加) Apply first-order logic to making inference in the Wumpus world. (If you have time, please take a look this appendix.) A gold finding agent in the Wumpus world
Predicates and rules • At(p, l, S) : Agent p is at the location l in the situation S • Percept([Smell, b, g, u, v], l, S, t): Agent perceives smell at time t at the location l in the situation S • Perception rules • R1: b, g, u, v, l, s, t Percept([Smell, b, g, u, v], s, t) Smell(t) • R2: sm, g, u, v, l, s, t Percept([sm, Breeze, g, u, v], s, t) Breeze(t) • R3: sm, b, u, v, l, s, t Percept([sm, b, Glitter, u, v], s, t) AtGoldRoom(t) • R4: sm, b, u, v, l, s, t Percept([sm, b, g, Wall, v], s, t) Wall(t) • R6: sm, b, g, u, v, l, s, t Percept([sm, b, g, u, v], s, t) • Smell(t) Breeze(t) AtGoldRoom(t) Wall(t) • Action rules R7: i, j Result(Forward, li) = lj i ji, j Result(Turn, Sm) = Sn m n • Orientation rules R8:s (Orientation(p, s ) =0 Face(p, east) ) (Orientation(p, s) = 90 Face(p, north ) (Orientation(p, s) = 180 Face(p, west) (Orientation(p, s) = 270 Face(p, south) ) R9: a, d, p, s Orientation(p, Result(a, s)) =d [(a=Turn(Right) d=Mod(Orientation(p,s) – 90, 360)) (a=Turn(Left) d=Mod(Orientation(p,s) + 90, 360) ) (Orientation(p,s)=d (a=Turn(Right) a=Turn(Left)))]
Location rules • R10:l, l x, y [x, y] • R11: (a) x, yLocationToward([x, y], 0) = [x+1, y] (b) x, yLocationToward([x, y], 90) = [x, y+1] • R12: (a) x, yLocationToward([x, y], 180) = [x-1, y] (b) x, yLocationToward([x, y], 270) = [x+1, y-1] • R13: p, l, s At(p, l, s) LocationAhead(p,S) = LocationToward(l, Orientation(p,s)) • R14: l1, l2 Adjacent(l1, l2) d l1=LocationToward(l2, d) • R15: x, y Wall([x, y]) (x=0 x=5 y=0 y=5) • R16: a, d, p, s At(p, l, Result(a, s)) [(a=Forward l = LocationAhead(p,s) Wall(l)) • (At(p,l,s) a Forward)] • Hidden properties (rules) • R17:l, s At(Agent, l, s) Smell(s) Smell(l) • R18:l, s At(Agent, l, s) Breeze(s) Breeze(l) • R19:l1, s Breeze(l1) l2 At(Pit, l2,s) (l2, = l1 Adjacent(,l1, l2)) • R20:l1, l2, s At(Pit, l1) Adjacent(,l1, l2) Breeze(l2) • R21:l1, s Breeze(l1) l2At(Pit, l2, s ) (l2, = l1 Adjacent(,l1, l2))
Hidden properties (rules) • R22: l1, s Smell(l1) l2 At(Wumpus, l2,s) (l2, = l1 Adjacent(,l1, l2)) • R23:l1, l2, s At(Wumpus, l1) Adjacent(,l1, l2) Smell(l2) • R24:l1, s Smell(l1) l2At(Wumpus, l2, s ) (l2, = l1 Adjacent(,l1, l2)) • R25:x, y, g, u, v, s, t Percept([None, None, g, u, v], t) At(Agent, x, s) Adjacent(,x, y) OK(y) • R26:x, t ( At(Wumpus, x, t) At(Pit, x, t)) OK(x) • Obtaining Gold • R27: x, s AtGoldRoom( x, s) Present(Gold, x, s) • R28: x, s Present( x, s) Portable(x) Holding(x, Result(Grab, s)) • R29: x, sHolding(x, Result(Release, s)) • R30: a, x, s Holding(x, s) (aRelease) Holding(x, Result(a, s)) • R31: a, x, s Holding(x, s) (aGrab (Present(x, s) Portable(x)) Holding(x, Result(a, s)) • P true afterwards [an action made P true P true already and no action made P false] • R32: a, x, s Holding(x, Result(a, s)) • [(a=Grab (Present(x, s) Portable(x)) (Holding(x, s) aRelease) ]
Since the world is not changing, our inferences below drop time term. Initial state At(Agent, [1,1]) Apply And-Elimination rule to R6, we obtain C1Smell([1,1]) , Breeze([1,1]) , AtGoldRoom([1,1]) , Wall([1,1]) Apply And-Elimination rule to R21 and apply R15, we obtain C2 At(Pit, [1,1]), At(Pit, [2,1]), At(Pit, [2,1]), Wall([1,0]), Wall([0,1]) Apply And-Elimination rule to R24, we obtain C3 At(Wumpus, [1,1]), At(Wumpus, [2,1]), At(Wumpus, [1,2]) Apply R26 to C2 and C3, we obtain C4 OK([1,1]), OK([2,1]), OK([1,2]) Apply R11(a) and R16, we obtain At(Agent, [2,1]) Apply And-Elimination rule to R2, we obtain C5Smell([2,1]) , Breeze([2,1]) , AtGoldRoom([2,1]) , Wall([2,1]) Apply R19 to Breeze([2,1]), we obtain C6 At(Pit, [1,1]) At(Pit, [2,1]) At(Pit, [2,2]) At(Pit, [3,1])
Apply unit resolution twice to C6, we obtain C7 At(Pit, [2,2]) At(Pit, [3,1]) Use C4 and C7, (for a conservative agent, it would not go where is not sure), and apply R9, R12(a) and R16, we obtain At(Agent, [1,1]), Orientation(Agent, [1,1])=180 Use C4 and apply R9, R11(b) and R16, we obtain At(Agent, [1,2]), Orientation(Agent, [1,2])=90 Apply R22 to Smell([1,2]), we obtain C8 At(Wumpus, [1,1]) At(Wumpus, [1,3]) At(Wumpus, [1,2]) At(Wumpus, [2,2]) Apply unit resolution twice to C8, we obtain C9 At(Wumpus, [2,2]) At(Wumpus, [1,3]) Apply R24 andAnd-Elimination rule to Smell([2,1]), we obtain C10At(Wumpus, [1,1]), At(Wumpus, [1,2]), At(Wumpus, [2,2]), At(Wumpus, [2,1]) Apply unit resolution to C9, we obtain C11At(Wumpus, [1,3]) Apply R21 and And-Eleimination rule to Breeze([1,2]), we obtain C12At(Pit, [1,1]), At(Pit, [2,1]), At(Pit, [2,2]), At(Pit, [3,1])
Apply R26 to C10 and C12, we obtain C13 OK([2,2]), Use C13 and apply R9, R11(a) and R16, we obtain At(Agent, [2,2]), Orientation(Agent, [2,2])=0 Use C4 and apply R9, R11(b) and R16, we obtain At(Agent, [1,2]), Orientation(Agent, [1,2])=90 Apply R21 and And-Elimination rule to Breeze([2,2]), and apply R24 and And-Elimination rule to Smell([2,2]), we obtain C14At(Pit, [2,1]), At(Pit, [1,2]), At(Pit, [2,2]), At(Pit, [2, 3], At(Pit, [2, 3]) C15At(Wumpus, [2,1]), At(Wumpus, [1,2]), At(Wumpus, [2,2]), At(Wumpus, [2,3], At(Wumpus, [3,2]) Apply R26 to C14 and C15, we obtain C16 OK([2,3]), OK([3,2]) Apply R11(a) and R16, we obtain At(Agent, [3,2]) Apply R19 to Breeze([3,2]), we obtain C17 At(Pit, [2,2]) At(Pit, [2,3]) At(Pit, [3,2]) At(Pit, [3,1]) At(Pit, [4,2]) Apply twice unit resolution to C9, we obtain C18 At(Pit, [3,2]) At(Pit, [3,1]) At(Pit, [4,2])
For safe, agent goes back to [2, 2] Use C13 and apply R9, R12(a) and R16, we obtain At(Agent, [2,2]), Orientation(Agent, [2,2])=180 Use C16 and apply R9, R11(b) and R16, we obtain At(Agent, [2,3]), Orientation(Agent, [2,3])=90 Apply R3, we obtain C19 AtGoldRoom( [2,3]) Apply R32, we obtain C20 Holding([2,3], Result(Grab)) The rest of question is how to go back safely?