Logical Agents
CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring 2018
“Artificial Intelligence: A Modern Approach”, 3rd Edition, Chapter 7
Logical Agents CE417: Introduction to Artificial Intelligence - - PowerPoint PPT Presentation
Logical Agents CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring 2018 Soleymani Artificial Intelligence: A Modern Approach , 3 rd Edition, Chapter 7 Wumpus world Wumpus Pitts Gold Wumpus
CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring 2018
“Artificial Intelligence: A Modern Approach”, 3rd Edition, Chapter 7
2
Wumpus
+1000 for garbing gold -1000 for death -1 for each action -10 for using up the arrow Game ends when the agent dies or climbs out of the cave
4×4 grid Agent starts in [1,1] while facing to the right Gold and wumpus are located randomly in the squares except to [1,1] Each square other than [1,1] can be a pit with probability 0.2
3
4
Sensors: Stench, Breeze, Glitter, Bump, Scream
In the squares adjacent to wumpus, agent perceives a Stench It the squares adjacent to a pit, agent perceives a Breeze In the gold square, agent perceives a Glitter When walking into a wall, agent perceives a Bump When Wumpus is killed, agent perceives a Scream
Actuators: Forward, TurnLeft, TurnRight, Shoot, Grab, Climb
Forward,TurnLeft,TurnRight: moving and rotating face actions.
Moving to a square containing a pit or a live wumpus causes death. If an agent tries to move forward and bumps into a wall then it does not move.
Shoot: to fire an arrow in a straight line in the facing direction of the agent
Shooting kills wumpus if the agent is facing it (o.w. the arrow hits a wall)
The first shoot action has any effect (the agent has only one arrow)
Grab: to pick up the gold if it is in the same square as the agent. Climb: climb out of the cave but only from [1,1]
5
6
7
8
9
10
11
12
13
i.e., define truth of a sentence with respect to each possible world
It talks about facts Propositions can be true, false, or unknown
14
To illustrate basic ideas about logic & reasoning
15
16
𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 → 𝐵𝑢𝑝𝑛𝑗𝑑𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 | 𝐷𝑝𝑛𝑞𝑚𝑓𝑦𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 𝐵𝑢𝑝𝑛𝑗𝑑𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 → 𝑈𝑠𝑣𝑓 𝐺𝑏𝑚𝑡𝑓 𝑄 𝑅 𝑆 | … 𝐷𝑝𝑛𝑞𝑚𝑓𝑦𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 → (𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓) | ¬𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 | 𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 ∧ 𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 | 𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 ∨ 𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 | 𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 ⟹ 𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 | 𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 ⇔ 𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 𝑄𝑠𝑓𝑑𝑓𝑒𝑓𝑜𝑑𝑓: ¬, ∧, ∨, ⟹, ⇔
17
18
1,2, 𝑄2,2, 𝑄3,1
8 possible models
𝑛1 = {𝑄 1,2 = 𝑔𝑏𝑚𝑡𝑓, 𝑄2,2 = 𝑔𝑏𝑚𝑡𝑓, 𝑄3,1 = 𝑢𝑠𝑣𝑓}
𝑗,𝑘 is true if there is a wumpus in [𝑗, 𝑘], dead or alive.
𝑆1: 𝑄
1,1
𝑆2: 𝐶1,1 ⇔ 𝑄
1,2 ∨ 𝑄2,1
𝑆3: 𝐶2,1 ⇔ 𝑄
1,1 ∨ 𝑄2,2 ∨ 𝑄3,1
𝑆4: 𝐶1,1 𝑆5: 𝐶2,1
19
𝑛 ∈ 𝑁(𝛽) if 𝛽 is true in model 𝑛
20
21
22
𝛽 ⇒ 𝛾 is a tautology or valid
A sentence is valid or tautology if it is T𝑠𝑣𝑓 in all models (e.g.,
𝑄𝑄, (𝑄(𝑄𝑅))𝑅) 𝛾 logically follows from 𝛽 or 𝛽 is stronger than 𝛾
𝛽 entails 𝛾 iff 𝛾 is true in all worlds where 𝛽 is true
KB=“A is red” & “B is blue” α = “A is red”
23
24
25
26
𝑁(𝐿𝐶) ⊆ 𝑁(𝛽1) ⇒ 𝐿𝐶 ⊨ 𝛽1
27
28
29
sound if whenever 𝐿𝐶├𝑗 𝛽, it is also true that 𝐿𝐶 ⊨ 𝛽
all provable statements by this algorithm are true
complete if whenever 𝐿𝐶 ⊨ 𝛽, it is also true that 𝐿𝐶├𝑗 𝛽
all true statements are provable by this algorithm 30
Model checking (enumerating models)
truth table enumeration (always exponential in 𝑜) improved backtracking, e.g., Davis-Putnam-Logemann-Loveland (DPLL) heuristic search in model space (sound but incomplete)
e.g., min-conflicts-like hill-climbing algorithms
Theorem proving (searching proofs by applying inference rules)
Applying a sequence of inference rules on KB to find the desired sentence Legitimate (sound) generation of new sentences from old ones. 31
32
𝐿𝐶 ⊨ 𝛽?
Enumerate the models
“Is 𝛽 true in every model in which KB is true?”
𝑆1: 𝑄
1,1
𝑆2: 𝐶1,1 ⇔ 𝑄
1,2 ∨ 𝑄2,1
𝑆3: 𝐶2,1 ⇔ 𝑄
1,1 ∨ 𝑄2,2 ∨ 𝑄3,1
𝑆4: 𝐶1,1 𝑆5: 𝐶2,1 KB
33
function 𝑈𝑈_𝐹𝑂𝑈𝐵𝐽𝑀𝑇? (𝐿𝐶, 𝛽) returns 𝑢𝑠𝑣𝑓 or 𝑔𝑏𝑚𝑡𝑓 inputs: 𝐿𝐶, the knowledge base, a sentence in propositional logic 𝛽, the query, a sentence in propositional logic 𝑡𝑧𝑛𝑐𝑝𝑚𝑡 ← a list of the proposition symbols in 𝐿𝐶 and 𝛽 return 𝑈𝑈_𝐷𝐼𝐹𝐷𝐿_𝐵𝑀𝑀(𝐿𝐶, 𝛽 , 𝑡𝑧𝑛𝑐𝑝𝑚𝑡, {}) function 𝑈𝑈_𝐷𝐼𝐹𝐷𝐿_𝐵𝑀𝑀(𝐿𝐶, 𝛽, 𝑡𝑧𝑛𝑐𝑝𝑚𝑡, 𝑛𝑝𝑒𝑓𝑚) returns 𝑢𝑠𝑣𝑓 or 𝑔𝑏𝑚𝑡𝑓 if 𝐹𝑁𝑄𝑈𝑍? ( 𝑡𝑧𝑛𝑐𝑝𝑚𝑡) then if 𝑄𝑀_𝑈𝑆𝑉𝐹? (𝐿𝐶, 𝑛𝑝𝑒𝑓𝑚) then return 𝑄𝑀_𝑈𝑆𝑉𝐹? (𝛽, 𝑛𝑝𝑒𝑓𝑚) else return 𝑢𝑠𝑣𝑓 else 𝑄 ← 𝐺𝐽𝑆𝑇𝑈(𝑡𝑧𝑛𝑐𝑝𝑚𝑡) 𝑠𝑓𝑡𝑢 ← 𝑆𝐹𝑇𝑈(𝑡𝑧𝑛𝑐𝑝𝑚𝑡) return 𝑈𝑈_𝐷𝐼𝐹𝐷𝐿_𝐵𝑀𝑀(𝐿𝐶, 𝛽, 𝑠𝑓𝑡𝑢, 𝑛𝑝𝑒𝑓𝑚⋃{𝑄 = 𝑢𝑠𝑣𝑓}) and 𝑈𝑈_𝐷𝐼𝐹𝐷𝐿_𝐵𝑀𝑀(𝐿𝐶, 𝛽, 𝑠𝑓𝑡𝑢, 𝑛𝑝𝑒𝑓𝑚⋃{𝑄 = 𝑔𝑏𝑚𝑡𝑓})
34
35
e.g., 𝑄𝑅
𝛽 is valid or tautology iff ¬𝛽 is unsatisfiable. 𝛽 is satisfiable iff ¬𝛽 is not valid.
Proof by contradiction
36
Early termination
A clause is true if any literal is true. A sentence is false if any clause is false.
Pure symbol heuristic
Pure symbol: always appears with the same "sign" in all clauses.
e.g., In (𝐵𝐶) ∧ (¬𝐶 𝐷) ∧ (𝐷𝐵), 𝐵 and 𝐶 are pure, 𝐷 is impure.
Make a pure symbol literal true. In determining purity, we can ignore clauses already known to be true
Unit clause heuristic
The only literal in a unit clause will be assigned true.
37
Determining satisfiability of an input propositional logic sentence (in CNF)
38
Can ignore irrelevant propositions. When the number of models is large but the length of proof is
39
β
𝛾
40
Every valid implication describes a legitimate inference.
β
41
42
𝑆1: 𝑄
1,1
𝑆2: 𝐶1,1 ⇔ 𝑄
1,2 ∨ 𝑄2,1
𝑆3: 𝐶2,1 ⇔ 𝑄
1,1 ∨ 𝑄2,2 ∨ 𝑄3,1
𝑆4: 𝐶1,1 𝑆5: 𝐶2,1
1,2?
𝑆6: 𝐶1,1 ⇒ 𝑄
1,2 ∨ 𝑄2,1
1,2 ∨ 𝑄2,1 ⇒ 𝐶1,1
[biconditional elim. to 𝑆2]
𝑆7:
1,2 ∨ 𝑄2,1 ⇒ 𝐶1,1
[and elim. to 𝑆6]
𝑆8: ¬𝐶1,1 ⇒ ¬ 𝑄
1,2 ∨ 𝑄2,1
[(𝑄 ⇒ 𝑅) ≡ (¬𝑅 ⇒ ¬𝑄)]
𝑆9: ¬ 𝑄
1,2 ∨ 𝑄2,1
[Modus Ponens with 𝑆8 and 𝑆4]
𝑆10: ¬𝑄
1,2 ∧ ¬𝑄2,1
[De Morgan’ rule]
43
44
Inference rules can be applied where premises are found in KB
The assertion 𝛾 can not invalidate any conclusion 𝛽 already
45
Are the available inference rules adequate?
46
Full resolution rule: 𝑚𝑗 and 𝑛𝑘 are complementary literals
It resolution rule sound.Why? A resolution-based theorem prover can (for any sentences 𝛽 and 𝛾 in
Is 𝛽 ∧ ¬𝛾 unsatisfiable? CNF (Conjunctive Normal Form)
47
𝐷𝑂𝐺𝑇𝑓𝑜𝑢𝑓𝑜𝑑𝑓 → 𝐷𝑚𝑏𝑣𝑡𝑓1 ∧ ⋯ ∧ 𝐷𝑚𝑏𝑣𝑡𝑓𝑜 𝐷𝑚𝑏𝑣𝑡𝑓 → 𝑀𝑗𝑢𝑓𝑠𝑏𝑚1 ∨ ⋯ ∨ 𝑀𝑗𝑢𝑓𝑠𝑏𝑚𝑛 𝑀𝑗𝑢𝑓𝑠𝑏𝑚 → 𝑇𝑧𝑛𝑐𝑝𝑚 | ¬𝑇𝑧𝑛𝑐𝑝𝑚 𝑇𝑧𝑛𝑐𝑝𝑚 → 𝑄 𝑅 𝑆 | …
48
To show 𝐿𝐶 ⊨ 𝛽, we show 𝐿𝐶 ∧ ¬𝛽 is unsatisfiable
49
𝑘 in 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 do
𝑠𝑓𝑡𝑝𝑚𝑤𝑓𝑜𝑢𝑡 ← 𝑄𝑀_𝑆𝐹𝑇𝑃𝑀𝑊𝐹 (𝐷𝑗, 𝐷𝑘) if 𝑠𝑓𝑡𝑝𝑚𝑤𝑓𝑜𝑢𝑡 contains the empty clause then return 𝑢𝑠𝑣𝑓 𝑜𝑓𝑥 ← 𝑜𝑓𝑥 ∪ 𝑠𝑓𝑡𝑝𝑚𝑤𝑓𝑜𝑢𝑡 if 𝑜𝑓𝑥 ⊆ 𝐷𝑚𝑏𝑣𝑡𝑓𝑡 then return 𝑔𝑏𝑚𝑡𝑓 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 ← 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 ∪ 𝑜𝑓𝑥
To show 𝐿𝐶 ⊨ 𝛽, we show 𝐿𝐶 ∧ ¬𝛽 is unsatisfiable
50
𝑘 in 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 do
𝑠𝑓𝑡𝑝𝑚𝑤𝑓𝑜𝑢𝑡 ← 𝑄𝑀_𝑆𝐹𝑇𝑃𝑀𝑊𝐹 (𝐷𝑗, 𝐷𝑘) if 𝑠𝑓𝑡𝑝𝑚𝑤𝑓𝑜𝑢𝑡 contains the empty clause then return 𝑢𝑠𝑣𝑓 𝑜𝑓𝑥 ← 𝑜𝑓𝑥 ∪ 𝑠𝑓𝑡𝑝𝑚𝑤𝑓𝑜𝑢𝑡 if 𝑜𝑓𝑥 ⊆ 𝐷𝑚𝑏𝑣𝑡𝑓𝑡 then return 𝑔𝑏𝑚𝑡𝑓 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 ← 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 ∪ 𝑜𝑓𝑥 Each pair containing complementary literals is resolved and the resulted clause is added to the set if it is not already present. The process continues until one of these happens:
1,2 𝑄2,1
1,2
1,2 𝑄 2,1) (𝑄 1,2 𝐶1,1) (𝑄 2,1 𝐶1,1)(𝐶1,1)𝑄
1,2
51
Contradiction
OK?
52
1, 𝑄2, … , 𝑄𝑙 that appear in
Finite distinct clauses can be c
If 𝑆𝐷(𝑇) does not contain the empty clause, we can find an assignment of
53
We can use more restricted and efficient inference algorithms
1 ∨ ¬𝑄2 ∨…∨ ¬𝑄𝑙 ∨ 𝑄𝑙+1 ⇔ (𝑄 1 ∧ 𝑄2 ∧ … ∧ 𝑄𝑙) ⇒ 𝑄𝑙+1
Closed under resolution
54
𝑇𝑧𝑛𝑐𝑝𝑚 → 𝑄 𝑅 𝑆 | … 𝐼𝑝𝑠𝑜𝐷𝑚𝑏𝑣𝑡𝑓 → 𝐸𝑓𝑔𝑗𝑜𝑗𝑢𝑓𝐷𝑚𝑏𝑣𝑡𝑓 | 𝐻𝑝𝑏𝑚𝐷𝑚𝑏𝑣𝑡𝑓 𝐸𝑓𝑔𝑗𝑜𝑗𝑢𝑓𝐷𝑚𝑏𝑣𝑡𝑓 → 𝑇𝑧𝑛𝑐𝑝𝑚1 ∧ ⋯ ∧ 𝑇𝑧𝑛𝑐𝑝𝑚𝑚 ⇒ 𝑇𝑧𝑛𝑐𝑝𝑚 𝐻𝑝𝑏𝑚𝐷𝑚𝑏𝑣𝑡𝑓 → (𝑇𝑧𝑛𝑐𝑝𝑚1 ∧ ⋯ ∧ 𝑇𝑧𝑛𝑐𝑝𝑚𝑚) ⇒ 𝐺𝑏𝑚𝑡𝑓
Prolog: backward chaining
55
add its conclusion to the KB, until query is found
56
Arcs show conjunctions
57
function 𝑄𝑀_𝐺𝐷_𝐹𝑂𝑈𝐵𝐽𝑀𝑇? (𝐿𝐶, 𝑟) returns 𝑢𝑠𝑣𝑓 or 𝑔𝑏𝑚𝑡𝑓 inputs: 𝐿𝐶, the knowledge base (a set of propositional definite clauses) 𝑟, the query (a propositional symbol) 𝑑𝑝𝑣𝑜𝑢 ← a table where 𝑑𝑝𝑣𝑜𝑢[𝑑] is the number of symbols in 𝑑’s premise 𝑗𝑜𝑔𝑓𝑠𝑠𝑓𝑒 ← a table, where 𝑗𝑜𝑔𝑓𝑠𝑠𝑓𝑒[𝑡] is initially false for all symbols 𝑏𝑓𝑜𝑒𝑏 ← a queue of symbols, initially symbols known to be true in 𝐿𝐶 while 𝑏𝑓𝑜𝑒𝑏 ≠ {} do 𝑞 ← 𝑄𝑃𝑄(𝑏𝑓𝑜𝑒𝑏) if 𝑞 = 𝑟 then return 𝑢𝑠𝑣𝑓 if 𝑗𝑜𝑔𝑓𝑠𝑠𝑓𝑒[𝑞] = 𝑔𝑏𝑚𝑡𝑓 then 𝑗𝑜𝑔𝑓𝑠𝑠𝑓𝑒[𝑞] ← 𝑢𝑠𝑣𝑓 for each clause 𝑑 in 𝐿𝐶 where 𝑞 is in 𝑑. 𝑄𝑆𝐹𝑁𝐽𝑇𝐹 do 𝑑𝑝𝑣𝑜𝑢[𝑑]= 𝑑𝑝𝑣𝑜𝑢[𝑑]-1 if 𝑑𝑝𝑣𝑜𝑢[𝑑] = 0 then add 𝑑. 𝐷𝑃𝑂𝐷𝑀𝑉𝑇𝐽𝑃𝑂 to 𝑏𝑓𝑜𝑒𝑏 return false
Begin with known facts (positive literals) in KB The process continues until q is added or no further inference
58
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
59
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
60
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
61
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
62
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
63
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
64
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
65
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
66
Every inference is an application of Modus Ponens.
Every entailed atomic sentence will be derived?
67
68
69
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
70
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
71
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
72
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
73
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
74
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
75
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
76
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
77
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
78
𝑄 ⇒ 𝑅 𝑀 ∧ 𝑁 ⇒ 𝑄 𝐶 ∧ 𝑀 ⇒ 𝑁 𝐵 ∧ 𝑄 ⇒ 𝑀 𝐵 ∧ 𝐶 ⇒ 𝑀 𝐵 𝐶
May do lots of work that is irrelevant to the goal
Only relevant facts are considered.
79
We introduced DPLL algorithm as a systematic search method
Backtracking search algorithms
Now, we will introduceWalkSAT algorithm as a local search algorithm
80
state space: complete assignments evaluation function: number of unsatisfied clauses
Simple & effective Balance between greediness and randomness
To escape from local minima 81
82
If max _𝑔𝑚𝑗𝑞𝑡 = ∞ and 𝑞 > 0, WalkSAT will find a model (if any exists) If max _𝑔𝑚𝑗𝑞𝑡 = ∞, it never terminates for unsatisfiable sentences.
83
1)
2)
Cannot be reliably used for deciding entailment.
84
Reasoning
Logic as a general class of representation
Propositional logic First-order logic
85
Represent states, actions, percepts,…. Incorporate new percepts Update internal representations of the world Deduce hidden properties of the world Deduce appropriate actions
perceived the given percept at the given time.
should be done at the current time.
action was executed.
86
TELL: adds new sentences to the knowledge base ASK: asks a question of KB
the answer follows from previously TELLed sentences to the KB
Basis of TELL and ASK operations
+1000 for garbing gold -1000 for death -1 for each action -10 for using up the arrow Game ends when the agent dies or climbs out of the cave
4×4 grid Agent starts in [1,1] while facing to the right Gold and wumpus are located randomly in the squares except to [1,1] Each square other than [1,1] can be a pit with probability 0.2
87
88
Sensors: Stench, Breeze, Glitter, Bump, Scream
In the squares adjacent to wumpus, agent perceives a Stench It the squares adjacent to a pit, agent perceives a Breeze In the gold square, agent perceives a Glitter When walking into a wall, agent perceives a Bump When Wumpus is killed, agent perceives a Scream
Actuators: Forward, TurnLeft, TurnRight, Shoot, Grab, Climb
Forward,TurnLeft,TurnRight: moving and rotating face actions.
Moving to a square containing a pit or a live wumpus causes death. If an agent tries to move forward and bumps into a wall then it does not move.
Shoot: to fire an arrow in a straight line in the facing direction of the agent
Shooting kills wumpus if the agent is facing it (o.w. the arrow hits a wall)
The first shoot action has any effect (the agent has only one arrow)
Grab: to pick up the gold if it is in the same square as the agent. Climb: climb out of the cave but only from [1,1]
89
𝑄𝑗,𝑘 is true if there is a pit in [𝑗, 𝑘]. 𝑋
𝑗,𝑘 is true if there is a wumpus in [𝑗, 𝑘], dead or alive.
𝐶𝑗,𝑘 is true if the agent perceives a breeze in [𝑗, 𝑘]. 𝑇𝑗,𝑘 is true if the agent perceives a stench in [𝑗, 𝑘].
Variables for percepts and actions 𝑀𝑗,𝑘
𝑢 , 𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢𝑢, 𝐺𝑏𝑑𝑗𝑜𝑋𝑓𝑡𝑢𝑢, 𝐺𝑏𝑑𝑗𝑜𝑂𝑝𝑠𝑢ℎ𝑢, 𝐺𝑏𝑑𝑗𝑜𝑇𝑝𝑣𝑢ℎ𝑢
𝐼𝑏𝑤𝑓𝐵𝑠𝑠𝑝𝑥𝑢 𝑋𝑣𝑛𝑞𝑣𝑡𝐵𝑚𝑗𝑤𝑓𝑢
90
1,1
1,1
𝑦,𝑧+1 𝑄 𝑦,𝑧−1 𝑄 𝑦+1,𝑧 𝑄 𝑦−1,𝑧)
𝑦,𝑧+1 𝑋 𝑦,𝑧−1 𝑋 𝑦+1,𝑧 𝑋 𝑦−1,𝑧)
1,1 𝑋 1,2 … 𝑋 4,4
1,1 𝑋 1,2
1,1 𝑋 1,3
91
𝑁𝐵𝐿𝐹_𝑄𝐹𝑆𝐷𝐹𝑄𝑈_𝑇𝐹𝑂𝑈𝐹𝑂𝐷𝐹 𝐶𝑠𝑓𝑓𝑨𝑓, 𝑇𝑢𝑓𝑜𝑑ℎ, 𝑂𝑝𝑜𝑓, 𝑂𝑝𝑜𝑓, 𝑂𝑝𝑜𝑓 , 𝑢
𝑁𝐵𝐿𝐹_𝐵𝐷𝑈𝐽𝑃𝑂_𝑇𝐹𝑂𝑈𝐹𝑂𝐷𝐹 𝐺𝑝𝑠𝑥𝑏𝑠𝑒, 𝑢 𝐺𝑝𝑠𝑥𝑏𝑠𝑒𝑢 is added to KB
92
Changing aspect of world (Temporal variables)
They are also called as fluents or state variables
Initial KB includes the initial status of some of the temporal variables:
𝑀1,1
0 , 𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢0, 𝐼𝑏𝑤𝑓𝐵𝑠𝑠𝑝𝑥0, 𝑋𝑣𝑛𝑞𝑣𝑡𝐵𝑚𝑗𝑤𝑓0
Percepts are connected to fluents describing the properties of
𝑀𝑦,𝑧
𝑢
𝑀𝑦,𝑧
𝑢
…
Transition model: fluents can change as the results of agent’s actions
93
1
∧ ¬𝑀1,1
1 )
𝑀1,1 ∧ 𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢0 ∧ 𝑈𝑣𝑠𝑜𝑀𝑓𝑔𝑢0 ⇒ (𝑀1,1
1
∧ 𝐺𝑏𝑑𝑗𝑜𝑂𝑝𝑠𝑢ℎ1 ∧ ¬𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢1)
𝐵𝑇𝐿 𝐿𝐶, 𝐼𝑏𝑤𝑓𝐵𝑠𝑠𝑝𝑥1 = 𝑔𝑏𝑚𝑡𝑓
Why?
94
𝐺𝑢+1 ⇔ 𝐵𝑑𝑢𝑗𝑝𝑜𝐷𝑏𝑣𝑡𝑓𝑡𝐺𝑢 ∨ (𝐺𝑢 ∧ ¬𝐵𝑑𝑢𝑗𝑝𝑜𝐷𝑏𝑣𝑡𝑓𝑡𝑂𝑝𝑢𝐺𝑢)
𝑢+1 ⇔ (𝑀1,1 𝑢
𝑢
𝑢
95
𝑃𝐿𝑦,𝑧
𝑢
𝑦,𝑧 ∧ ¬(𝑋 𝑦,𝑧 ∧ 𝑋𝑣𝑛𝑞𝑣𝑡𝐵𝑚𝑗𝑤𝑓𝑢)
𝑡𝑏𝑔𝑓 ←
𝑢
𝑣𝑜𝑤𝑗𝑡𝑗𝑢𝑓𝑒 ←
𝑢′
96 function 𝐼𝑍𝐶𝑆𝐽𝐸_𝑋𝑉𝑁𝑄𝑉𝑇_𝐵𝐻𝐹𝑂𝑈(𝑞𝑓𝑠𝑑𝑓𝑞𝑢) returns an action sequence inputs: 𝑞𝑓𝑠𝑑𝑓𝑞𝑢, a list, 𝑐𝑠𝑓𝑓𝑨𝑓, 𝑡𝑢𝑓𝑜𝑑ℎ, 𝑚𝑗𝑢𝑢𝑓𝑠, 𝑐𝑣𝑛𝑞, 𝑡𝑑𝑠𝑓𝑏𝑛 persistent: 𝐿𝐶, initially the atemporal “wumpus physics” 𝑢, a time counter, initially 0 𝑞𝑚𝑏𝑜, an action sequence, initially empty TELL(𝐿𝐶, 𝑁𝐵𝐿𝐹_𝑄𝐹𝑆𝐷𝐹𝑄𝑈_𝑇𝐹𝑂𝑈𝐹𝑂𝐷𝐹 𝑞𝑓𝑠𝑑𝑓𝑞𝑢, 𝑢 ) TELL the 𝐿𝐶 the temporal “physics” sentences for time 𝑢 𝑡𝑏𝑔𝑓 ← 𝑦, 𝑧 : 𝐵𝑇𝐿 𝐿𝐶, 𝑃𝐿𝑦,𝑧
𝑢
= 𝑢𝑠𝑣𝑓 if 𝐵𝑇𝐿(𝐿𝐶, 𝐻𝑚𝑗𝑢𝑢𝑓𝑠𝑢) = 𝑢𝑠𝑣𝑓 then 𝑞𝑚𝑏𝑜 ← 𝐻𝑠𝑏𝑐 + 𝑄𝑀𝐵𝑂_𝑆𝑃𝑉𝑈𝐹 𝑑𝑣𝑠𝑠𝑓𝑜𝑢, 1,1 , 𝑡𝑏𝑔𝑓 + [𝐷𝑚𝑗𝑛𝑐] if 𝑞𝑚𝑏𝑜 = {} then 𝑣𝑜𝑤𝑗𝑡𝑗𝑢𝑓𝑒 ← 𝑦, 𝑧 : 𝐵𝑇𝐿 𝐿𝐶, 𝑀𝑦,𝑧
𝑢′
= 𝑔𝑏𝑚𝑡𝑓 𝑔𝑝𝑠 𝑏𝑚𝑚 𝑢′ ≤ 𝑢 𝑞𝑚𝑏𝑜 ← 𝑄𝑀𝐵𝑂_𝑆𝑃𝑉𝑈𝐹(𝑑𝑣𝑠𝑠𝑓𝑜𝑢, 𝑣𝑜𝑤𝑗𝑡𝑗𝑢𝑓𝑒 ∩ 𝑡𝑏𝑔𝑓, 𝑡𝑏𝑔𝑓) if 𝑞𝑚𝑏𝑜 = {} and 𝐵𝑇𝐿 𝐿𝐶, 𝐼𝑏𝑤𝑓𝐵𝑠𝑠𝑝𝑥𝑢 = 𝑢𝑠𝑣𝑓 then 𝑞𝑝𝑡𝑡𝑗𝑐𝑚𝑓_𝑥𝑣𝑛𝑞𝑣𝑡 ← 𝑦, 𝑧 : 𝐵𝑇𝐿 𝐿𝐶, ¬𝑋
𝑦,𝑧 = 𝑔𝑏𝑚𝑡𝑓
𝑞𝑚𝑏𝑜 ← 𝑄𝑀𝐵𝑂_𝑇𝐼𝑃𝑈(𝑑𝑣𝑠𝑠𝑓𝑜𝑢, 𝑞𝑝𝑡𝑡𝑗𝑐𝑚𝑓_𝑥𝑣𝑛𝑞𝑣𝑡, 𝑡𝑏𝑔𝑓) if 𝑞𝑚𝑏𝑜 = {} then 𝑜𝑝𝑢_𝑣𝑜𝑡𝑏𝑔𝑓 ← 𝑦, 𝑧 : 𝐵𝑇𝐿 𝐿𝐶, ¬𝑃𝐿𝑦,𝑧
𝑢
= 𝑔𝑏𝑚𝑡𝑓 𝑞𝑚𝑏𝑜 ← 𝑄𝑀𝐵𝑂_𝑆𝑃𝑉𝑈𝐹(𝑑𝑣𝑠𝑠𝑓𝑜𝑢, 𝑣𝑜𝑤𝑗𝑡𝑗𝑢𝑓𝑒 ∩ 𝑜𝑝𝑢_𝑣𝑜𝑡𝑏𝑔𝑓, 𝑡𝑏𝑔𝑓) if 𝑞𝑚𝑏𝑜 = {} then 𝑞𝑚𝑏𝑜 ← 𝑄𝑀𝐵𝑂_𝑆𝑃𝑉𝑈𝐹 𝑑𝑣𝑠𝑠𝑓𝑜𝑢, 1,1 , 𝑡𝑏𝑔𝑓 + [𝐷𝑚𝑗𝑛𝑐] action ← 𝑄𝑃𝑄(𝑞𝑚𝑏𝑜) 𝑈𝐹𝑀𝑀(𝐿𝐶, 𝑁𝐵𝐿𝐹_𝐵𝐷𝑈𝐽𝑃𝑂_𝑇𝐹𝑂𝑈𝐹𝑂𝐷𝐹(𝑏𝑑𝑢𝑗𝑝𝑜, 𝑢)) 𝑢 ← 𝑢 + 1 return 𝑏𝑑𝑢𝑗𝑝𝑜
97
𝑢′
𝑦,𝑧 = 𝑔𝑏𝑚𝑡𝑓
98
𝑢
99
function 𝑄𝑀𝐵𝑂_𝑆𝑃𝑉𝑈𝐹(𝑑𝑣𝑠𝑠𝑓𝑜𝑢, 𝑝𝑏𝑚, 𝑏𝑚𝑚𝑝𝑥𝑓𝑒) returns an action sequence inputs: 𝑑𝑣𝑠𝑠𝑓𝑜𝑢, the agent’s current position 𝑝𝑏𝑚𝑡, a set of squares; try to plan a route to one of them 𝑏𝑚𝑚𝑝𝑥𝑓𝑒, a set of squares that can form part of the route 𝑞𝑠𝑝𝑐𝑚𝑓𝑛 ← 𝑆𝑃𝑉𝑈𝐹_𝑄𝑆𝑃𝐶𝑀𝐹𝑁(𝑑𝑣𝑠𝑠𝑓𝑜𝑢, 𝑝𝑏𝑚𝑡, 𝑏𝑚𝑚𝑝𝑥𝑓𝑒) return 𝐵∗_𝐻𝑆𝐵𝑄𝐼_𝑇𝐹𝐵𝑆𝐷𝐼(𝑞𝑠𝑝𝑐𝑚𝑓𝑛)
100
101
Initial state fluents (t=0) state fluents at t=k action fluents at t=k-1
102
Instantiate literals containing variable (replace with ∨ over constants).
successor-state axioms at each time up to 𝑢
𝐺𝑢+1 ⟺ 𝐵𝑑𝑢𝑗𝑝𝑜𝐷𝑏𝑣𝑡𝑓𝑡𝐺𝑢 ∨ (𝐺𝑢 ∧ ¬𝐵𝑑𝑢𝑗𝑝𝑜𝐷𝑏𝑣𝑡𝑓𝑡𝑂𝑝𝑢𝐺𝑢)
precondition axioms:
𝐵𝑢 ⇒ PRECOND 𝐵 𝑢
action exclusion axioms:
¬𝐵𝑗
𝑢 ∨ ¬𝐵𝑘 𝑢
T
One operator “move” the robot
𝑁𝑝𝑤𝑓 𝑚, 𝑚’
𝑄𝑆𝐹𝐷𝑃𝑂𝐸: 𝐵𝑢(𝑚) 𝐹𝐺𝐺𝐹𝐷𝑈: 𝐵𝑢(𝑚’) ∧ ¬𝐵𝑢(𝑚)
103
𝑀1 𝑀2
104
Initial state:
𝐵𝑢(𝑀1, 0) ∧ ¬ 𝐵𝑢(𝑀2, 0)
Goal:
𝐵𝑢(𝑀2, 1)
Actions preconditions:
𝑁𝑝𝑤𝑓(𝑀1, 𝑀2, 0) ⇒ 𝐵𝑢(𝑀1, 0) 𝑁𝑝𝑤𝑓(𝑀2, 𝑀1, 0) ⇒ 𝐵𝑢(𝑀2, 0)
Action exclusion axiom:
¬𝑁𝑝𝑤𝑓(𝑀2, 𝑀1, 0) ∨ ¬𝑁𝑝𝑤𝑓(𝑀1, 𝑀2, 0)
𝐵𝑢 𝑀2, 1 ⟺ (𝐵𝑢 𝑀2, 0 𝑁𝑝𝑤𝑓 𝑀2, 𝑀1, 0 ) ∨ 𝑁𝑝𝑤𝑓 𝑀1, 𝑀2, 0
105
106
: conjunction of encoding initial state, goals, successor-state axioms,
This means 𝑄 has a solution of length 𝑙
This is the 𝑗’th action of the plan.
𝐵𝑢(𝑀1, 0) ∧ ¬𝐵𝑢(𝑀2, 0) ∧ 𝐵𝑢(𝑀2, 1) ∧ [𝑁𝑝𝑤𝑓 𝑀1, 𝑀2, 0 𝐵𝑢 𝑀1, 0 ] ∧ [𝑁𝑝𝑤𝑓 𝑀1, 𝑀2, 0 𝐵𝑢 𝑀2, 1 ] ∧ [¬𝑁𝑝𝑤𝑓(𝑀2, 𝑀1, 0) ∨ ¬𝑁𝑝𝑤𝑓(𝑀1, 𝑀2, 0)] ∧ [𝐵𝑢 𝑀1, 0 𝐵𝑢 𝑀1, 1 𝑁𝑝𝑤𝑓 𝑀2, 𝑀1, 0 ] ∧ [𝐵𝑢 𝑀2, 0 𝐵𝑢 𝑀2, 1 𝑁𝑝𝑤𝑓 𝑀1, 𝑀2, 0 ] ∧ [𝐵𝑢 𝑀1, 0 𝐵𝑢 𝑀1, 1 𝑁𝑝𝑤𝑓 𝑀1, 𝑀2, 0 ] ∧ 𝐵𝑢 𝑀2, 0 𝐵𝑢 𝑀2, 1 𝑁𝑝𝑤𝑓 𝑀2, 𝑀1, 0
107
SAT formula for (P,1)
108
109
function SATPLAN(𝑗𝑜𝑗𝑢, 𝑢𝑠𝑏𝑜𝑡𝑗𝑢𝑗𝑝𝑜, 𝑝𝑏𝑚, 𝑈_𝑛𝑏𝑦) returns solution or failure inputs: 𝑗𝑜𝑗𝑢, 𝑢𝑠𝑏𝑜𝑡𝑗𝑢𝑗𝑝𝑜, 𝑝𝑏𝑚, constitute a description of the problem 𝑈_𝑛𝑏𝑦, an upper limit for plan length for 𝑢 = 0 to 𝑈_max do 𝑑𝑜𝑔 ← TRANSLATE_TO_SAT(𝑗𝑜𝑗𝑢, 𝑢𝑠𝑏𝑜𝑡𝑗𝑢𝑗𝑝𝑜, 𝑝𝑏𝑚, 𝑢 ) 𝑛𝑝𝑒𝑓𝑚 ← SAT_SOLVER(𝑑𝑜𝑔) if 𝑛𝑝𝑒𝑓𝑚 ≠ {} then return EXTRACT_SOLUTION(𝑛𝑝𝑒𝑓𝑚) return 𝑔𝑏𝑗𝑚𝑣𝑠𝑓 It is guaranteed to find the shortest plan if one exist.
110
DPLL (Davis Putnam Logemann Loveland)
WalkSAT
Wumpus world
Distinct rules “for each square [x,y]” and “for each time t”
𝑀𝑦,𝑧
𝑢
∧ 𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢𝑢 ∧ 𝑈𝑣𝑠𝑜𝑀𝑓𝑔𝑢𝑢 ⇒ (𝑀𝑦,𝑧
𝑢+1 ∧ 𝐺𝑏𝑑𝑗𝑜𝑉𝑞𝑢+1 ∧ ¬𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢𝑢+1)
Many rules are needed to describe the world axioms
Rather impractical for bigger world
We can not express general knowledge about “physics” of the world
More expressive language is needed.
Qualification problem: specifying all the exceptions
Probability theory allows us to summarize all the exceptions (without
111