Logical Agents
CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring 2017
“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 2017 Soleymani Artificial Intelligence: A Modern Approach , 3 rd Edition, Chapter 7 Knowledge-based agents Knowledge-based agents
CE417: Introduction to Artificial Intelligence Sharif University of Technology Spring 2017
“Artificial Intelligence: A Modern Approach”, 3rd Edition, Chapter 7
2
Logic as a general class of representation
Propositional logic First-order logic
3
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.
4
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
5
+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
6
7
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
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]
8
9
10
11
12
13
14
15
16
i.e., define truth of a sentence with respect to each possible world
17
To illustrate basic ideas about logic & reasoning
18
19
20
21
1,2, 𝑄2,2, 𝑄3,1
8 possible models
𝑛1 = {𝑄 1,2 = 𝑔𝑏𝑚𝑡𝑓, 𝑄2,2 = 𝑔𝑏𝑚𝑡𝑓, 𝑄3,1 = 𝑢𝑠𝑣𝑓}
𝑆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
22
𝑛 ∈ 𝑁(𝛽) if 𝛽 is true in model 𝑛
E.g., For 𝛽: 𝑦 + 𝑧 = 4, all possible assignments of values to 𝑦, 𝑧 are
23
24
25
𝛽 ⇒ 𝛾 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
26
27
28
29
𝑁(𝐿𝐶) ⊆ 𝑁(𝛽1) ⇒ 𝐿𝐶 ⊨ 𝛽1
30
31
32
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 33
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)
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. 34
35
𝐿𝐶 ⊨ 𝛽?
1,1
1,2 ∨ 𝑄2,1
1,1 ∨ 𝑄2,2 ∨ 𝑄3,1
36
37
38
e.g., 𝑄𝑅
𝛽 is valid or tautology iff ¬𝛽 is unsatisfiable. 𝛽 is satisfiable iff ¬𝛽 is not valid.
39
A clause is true if any literal is true. A sentence is false if any clause is false.
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
The only literal in a unit clause will be assigned true.
40
41
42
43
Every valid implication describes a legitimate inference.
44
45
𝑆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
𝑆6: 𝐶1,1 ⇒ 𝑄
1,2 ∨ 𝑄2,1
1,2 ∨ 𝑄2,1 ⇒ 𝐶1,1
𝑆7:
1,2 ∨ 𝑄2,1 ⇒ 𝐶1,1
𝑆8: ¬𝐶1,1 ⇒ ¬ 𝑄
1,2 ∨ 𝑄2,1
𝑆9: ¬ 𝑄
1,2 ∨ 𝑄2,1
𝑆10: ¬𝑄
1,2 ∧ ¬𝑄2,1
46
47
48
49
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)
50
51
To show 𝐿𝐶 ⊨ 𝛽, we show 𝐿𝐶 ∧ ¬𝛽 is unsatisfiable
52
𝑘 in 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 do
To show 𝐿𝐶 ⊨ 𝛽, we show 𝐿𝐶 ∧ ¬𝛽 is unsatisfiable
53
𝑘 in 𝑑𝑚𝑏𝑣𝑡𝑓𝑡 do
1,2
54
OK?
55
1, 𝑄2, … , 𝑄𝑙 that appear in
Finite distinct clauses can be c
If 𝑆𝐷(𝑇) does not contain the empty clause, we can find an assignment of
56
We can use more restricted and efficient inference algorithms
Closed under resolution
57
Prolog: backward chaining
58
add its conclusion to the KB, until query is found
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
May do lots of work that is irrelevant to the goal
Only relevant facts are considered.
82
We introduced DPLL algorithm as a systematic search method
Backtracking search algorithms
Now, we will introduceWalkSAT algorithm as a local search algorithm
83
state space: complete assignments evaluation function: number of unsatisfied clauses
Simple & effective Balance between greediness and randomness
To escape from local minima 84
85
86
87
𝑄𝑗,𝑘 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 𝑀𝑗,𝑘
𝑢 , 𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢𝑢, 𝐺𝑏𝑑𝑗𝑜𝑋𝑓𝑡𝑢𝑢, 𝐺𝑏𝑑𝑗𝑜𝑂𝑝𝑠𝑢ℎ𝑢, 𝐺𝑏𝑑𝑗𝑜𝑇𝑝𝑣𝑢ℎ𝑢
𝐼𝑏𝑤𝑓𝐵𝑠𝑠𝑝𝑥𝑢 𝑋𝑣𝑛𝑞𝑣𝑡𝐵𝑚𝑗𝑤𝑓𝑢
88
89
𝑁𝐵𝐿𝐹_𝑄𝐹𝑆𝐷𝐹𝑄𝑈_𝑇𝐹𝑂𝑈𝐹𝑂𝐷𝐹 𝐶𝑠𝑓𝑓𝑨𝑓, 𝑇𝑢𝑓𝑜𝑑ℎ, 𝑂𝑝𝑜𝑓, 𝑂𝑝𝑜𝑓, 𝑂𝑝𝑜𝑓 , 𝑢
𝑁𝐵𝐿𝐹_𝐵𝐷𝑈𝐽𝑃𝑂_𝑇𝐹𝑂𝑈𝐹𝑂𝐷𝐹 𝐺𝑝𝑠𝑥𝑏𝑠𝑒, 𝑢 𝐺𝑝𝑠𝑥𝑏𝑠𝑒𝑢 is added to KB
90
They are also called as fluents or state variables
𝑀1,1
0 , 𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢0, 𝐼𝑏𝑤𝑓𝐵𝑠𝑠𝑝𝑥0, 𝑋𝑣𝑛𝑞𝑣𝑡𝐵𝑚𝑗𝑤𝑓0
𝑀𝑦,𝑧
𝑢
𝑀𝑦,𝑧
𝑢
…
91
1
1 )
1
𝐵𝑇𝐿 𝐿𝐶, 𝐼𝑏𝑤𝑓𝐵𝑠𝑠𝑝𝑥1 = 𝑔𝑏𝑚𝑡𝑓
Why?
92
𝐺𝑢+1 ⇔ 𝐵𝑑𝑢𝑗𝑝𝑜𝐷𝑏𝑣𝑡𝑓𝑡𝐺𝑢 ∨ (𝐺𝑢 ∧ ¬𝐵𝑑𝑢𝑗𝑝𝑜𝐷𝑏𝑣𝑡𝑓𝑡𝑂𝑝𝑢𝐺𝑢)
𝑢+1 ⇔ (𝑀1,1 𝑢
𝑢
𝑢
93
𝑃𝐿𝑦,𝑧
𝑢
𝑦,𝑧 ∧ ¬(𝑋 𝑦,𝑧 ∧ 𝑋𝑣𝑛𝑞𝑣𝑡𝐵𝑚𝑗𝑤𝑓𝑢)
𝑡𝑏𝑔𝑓 ←
𝑢
𝑣𝑜𝑤𝑗𝑡𝑗𝑢𝑓𝑒 ←
𝑢′
94 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 𝑏𝑑𝑢𝑗𝑝𝑜
95
𝑢′
𝑦,𝑧 = 𝑔𝑏𝑚𝑡𝑓
96
𝑢
97
98
We must save the results of inference to use them in the next time steps
Belief state: the agent’s knowledge about the state of the world
1,3 ∨ 𝑄2,2)
1
State estimation: process of updating the belief state when doing
99
𝑜 fluent symbols, 2𝑜 possible physical states, 22𝑜 possible belief states
e.g., 1-CNF belief state: given the belief state at 𝑢 − 1, agent tries to
Distinct rules “for each square [x,y]” and “for each time t”
𝑀𝑦,𝑧
𝑢
∧ 𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢𝑢 ∧ 𝑈𝑣𝑠𝑜𝑀𝑓𝑔𝑢𝑢 ⇒ (𝑀𝑦,𝑧
𝑢+1 ∧ 𝐺𝑏𝑑𝑗𝑜𝑉𝑞𝑢+1 ∧ ¬𝐺𝑏𝑑𝑗𝑜𝐹𝑏𝑡𝑢𝑢+1)
Rather impractical for bigger world
More expressive language is needed.
Probability theory allows us to summarize all the exceptions (without
100