cs206 class discussion
DESCRIPTION
Various Question and Answer about basic Artificial Intelligence problem. Including Types of Agent, A* Search, BFS, DFS, Alpha - Beta Prunning, Propositional Logic, and Many more.TRANSCRIPT
CS206 - AIClass Discussions
2
Simple Reflex Agent
Use table lookup of percept-action pairs defining all possible condition-action rules (reflex actions)
Is this an optimal architecture for: Vacuum cleaner world? (Goal: clean floor) Vacuum cleaner world? (Goal: clean floor & minimize energy) Taxi agent world?
3
Model-Based Agent
Uses memory to maintain internal state of the world to store percept history and a model of how the world works.
Is this an optimal architecture for: Vacuum cleaner world? (Goal: clean floor) Vacuum cleaner world? (Goal: clean floor & minimize energy) Taxi agent world?
4
Goal-Based Agent
Chooses actions so as to achieve a goal Involves planning if long sequences of
path to the goal state (Here, both action 1 and 2 will lead to the goal)
Is this an optimal architecture for: Taxi agent world? (Goal: shortest path)
action 1
Start
Goal
action 2
action 3
5
Utility-Based Agent
Uses utility function, to choose multiple possible alternatives to the goal state utility(goal) > utility(A)
Is this an optimal architecture for: Taxi agent world? (Goal: shortest path)
action 1
Start
Goal
action 2
A
6
Romania Map
Questions:1. Give the states, goal test, successor function, and step cost
for the above problem (start from Arad).
2. Draw the state space of the search problem up to depth 2.
7
Romania Map Solution
States: Cities Goal test: At Bucharest? Successor function: Arad Sibiu, Arad Zerind, etc.
(following the path in the map) Step cost: Distance between cities.
8
Vacuum-Cleaner World
Percepts: location and content, e.g. [A,Dirty]
Actions: Left, Right, Suck. The figure on the right is the
initial state.
Questions:1. Give the states, goal test, successor function, and step cost
for the above problem so that you can solve the problem by a search algorithm.
2. Draw the state space of the problem.
9
Vacuum-Cleaner World Solution
States: Boolean dirt and robot location Goal test: No dirt on all locations? Successor function: Left, Right, Suck Step cost: 1 per action.
10
Vacuum-Cleaner World Solution
The state space:
11
Blind Search Algorithms
Solve using:- Depth-First Search
- Breadth-First Search
- Uniform Cost Search
S: Start
G: Goal
12
Blind Search Algorithms
Solve using:- Depth-First Search
- Breadth-First Search
- Uniform Cost Search
- Iterative Deepening Search
Node 0: Start
Node 8: Goal
13
Blind Search Algorithms
0
1 4 3
5 8 6 4 62 4 7
5 4 5 8 67
8 5 8 67
Partial search tree:
DFS
BFS, UCS, IDS
14
Problem:
Three missionaries and three cannibals are on one side of a river, along with a boat that can hold one or two people.
Find a way to get everyone to the other side of the river, without ever leaving a group of missionaries in one place outnumbered by the cannibals in that place.
Questions:1. Formulate the problem precisely (give the states, goal test,
successor function, and step cost for the above problem).
2. Draw the state space of the problem. Note: assume you have a cycle detection so that cycles are not drawn.
Missionaries and Cannibals
15
States:
Six tuples of integers listing the number of missionaries, cannibals, and boat on the first side, and then the second side of the river.
Goal test:
Is the three missionaries and three cannibals are on the second side of the river?
Successor function:
All states that move 1 or 2 people and 1 boat from one side to another.
Step cost:
One per action.
Missionaries and Cannibals
16
Missionaries and Cannibals
3,3,1 ; 0,0,0
2,2,0 ; 1,1,1 3,1,0 ; 0,2,1 3,2,0 ; 0,1,1
1,1,0 ; 2,2,1
3,2,1 ; 0,1,0
3,0,0 ; 0,3,1
3,1,1 ; 0,2,0
2,2,1 ; 1,1,0
0,2,0 ; 3,1,1
0,3,1 ; 3,0,0
0,1,0 ; 3,2,1
0,2,1 ; 3,1,0
0,0,0 ; 3,3,1
Why do you think people
have a hard time solving
this puzzle, given that
the search space is small?
17
Examples of Search Problems
States: Locations of 8 tiles and blank
Initial State: Any state
Succ Func: Generates legal states that result from trying 4 actions (blank up, down, left, right)
Goal test: Does state match desired configuration
Path cost: Number of steps
18
Importance of Problem Formulation
States: Arrangement of 0 to 8 queens on the board
Initial State: No queens on the board
Succ Func: Add a queen to an empty space
Goal test: 8 queens on board, none attacked
Path cost: ?
How many states in the search tree? 64 x 63 x … x 57 ≈ 3 x 1014
19
Importance of Problem Formulation
States: Arrangement of n queens (n is between 0 and 8), one per column in the leftmost column, with no queens attacking another.
Initial State: No queens on the board
Succ Func: Add a queen to any space square in the leftmost empty column such that it is not attacked by another queen.
Goal test: 8 queens on board, none attacked
Number of state space? 2057 solutions are easy to find
However, for 100 queens this improved formulation has ≈ 1052 states!
20
More Examples
21
Informed Search Algorithms
Solve using:- Best-First Search
- A* Search
S: Start
G: Goal
22
Informed Search Algorithms
Best-First Search
Expnd. node Open list
S:26
S not goal N2:16.4, N1:25.1
N2 not goal N6:8.5, N5:8.8, N1:25.1
N6 not goal N10:6.5, N5:8.8, N1:25.1
N10 not goal N5:8.8, N1:25.1
N5 not goal G:0, N9:10, N1:25.1
G is goal N9:10, N1:25.1
23
Informed Search Algorithms
A* Search
Expnd. node Open list
S:26
S not goal N2:27, N1:35
N2 not goal N5:28.5, N6:31.9, N1:35
N5 not goal G:28.5, N6:31.9, N1:35, N9:38.3
G is goal N6:31.9, N1:35, N9:38.3
24
Question:
Use the Best-first and A* algorithm to find the route from Arad to Bucharest and draw the state space.
A* in Romania
25
Best-First in Romania
26
A* in Romania
Expnd. node Open list
Arad:366
Arad not goal Sibiu:393, Timisoara:447, Zerind:449
Sibiu not goal Rimnicu:413, Faragas:415, Timisoara:447, Zerind:449, Arad:646, Oradea:671
Rimnicu not goal Faragas:415, Pitesti:417, Timisoara:447, Zerind:449, Craiova:526, Sibiu:533, Arad:646, Oradea:671
Faragas not goal Pitesti:417, Timisoara:447, Zerind:449, Bucharest:450, Craiova:526, Sibiu:533, Sibiu:591, Arad:646, Oradea:671
Pitesti not goal Bucharest:418, Timisoara:447, Zerind:449, Bucharest:450, Craiova:526, Sibiu:533, Rimnicu:607, Craiova:615, Arad:646, Oradea:671
Bucharest is goal Timisoara:447, Zerind:449, Craiova:526, Sibiu:533, Rimnicu:607, Arad:646, Oradea:671
27
A* in Romania
28
Alpha-Beta Pruning #1
max A
B C E
I2
J6
K9
L12
M5
N1
min
F3
G14
H9
29
Alpha-Beta Pruning #1 Solution
max A
B C E
I2
J6
K9
L12
M5
N1
min
F3
G14
H9
=3
=3
=2 =1
Result: choose move B
30
Alpha-Beta Pruning #2
max A
B C E
I2
J6
K9
L1
M15
N9
min
F3
G14
H9
31
Alpha-Beta Pruning #2 Solution
max A
B C E
I2
J6
K9
L1
M15
N9
min
F3
G14
H9
=3
=3
=2 =1
Result: choose move BThe number of pruned nodes depends on the order of nodes.
32
Alpha-Beta Pruning #3
Amax
min
5
B C
H I J K L M N O
D E F G
6 4 7 7 6 1 -9 1 0 0 -4 4 -1 2 -3
33
Alpha-Beta Pruning #3 Solution
Amax
min B
5
C
H I J K L M N O
D E F G
6 4 7 7 6 1 -9 1 0 0 -4 4 -1 2 -3
=5
=5
=5 =6 =0
=5 =4 =6
Result: choose move B
=0
=0
=0
34
Alpha-Beta Pruning #4
1. Draw the game tree of Tic-tac-toe up to depth 2 (one X and one O on the board), taking symmetry into account.
2. Evaluate the evaluation values of all nodes at depth 2, using the following SBE function:Eval(n) = 3.X2(n) + X1(n) – ( 3.O2(n) + O1(n) )where, Xn is the number of rows, columns, or diagonals with exactly n X’s and no O’s. Similarly, On is the number of rows, columns, or diagonals with just n O’s.
3. Calculate the minimax values of the above game tree.4. Re-order the game tree so that the alpha-beta algorithm
can prune away as much nodes as possible.
35
Alpha-Beta Pruning #4
max
min
The game tree up to depth 2, taking symmetry into account.
XX
X
X O
O
X
X
O
X
O
X
O
X
O
X
O
X O
O
X
X
O
X OX
O
36
Alpha-Beta Pruning #4
max
min
Minimax values.
X
h=-2
X
h=-1
X
h=1
h=1
X O
h=0
O
X
h=2
X
O
h=0
X
O
h=-2
X
O
h=0
X
O
h=-1
X
O
h=-1
X O
h=1
O
X
h=1
X
O
h=1
X O
h=-1
X
h=0
O
37
Alpha-Beta Pruning #4
max
min
Optimal ordering of nodes.
X
h=-2
X
h=-1
X
h=1
h=1
X O
h=0
O
X
h=2
X
O
h=0
X
O
h=-2
X
O
h=0
X
O
h=-1
X
O
h=-1
X O
h=1
O
X
h=1
X
O
h=1
X O
h=-1
X
h=0
O
38
Alpha-Beta Pruning #5
• Consider the 2-player game below. The figure shows the starting position.
• Player A moves first. The two players move in turns, and each player must move his token to an open adjacent space in either direction. If the opponent occupies an adjacent space, then a player may jump over the opponent to the next open space if any. E.g. if A is on 3 and B is on 2, then A may move back to 1.
• The game ends when one player reaches the opposite end of the board. If player A reaches space 4 first, the value of the game for A is +1; if player B reaches space 1 first, then the value of the game to A is -1.
A B1 2 3 4
39
Alpha-Beta Pruning #5
1. Draw the complete game tree, using the following convention:– Write each state as (sA, sB), where sA, sB denote the token locations.– Put each terminal state in a square boxes and write its value in a circle.– Put loop states in double square boxes. Since it is not clear how to
assign values to loop states, annotate each with a “?” in a circle.
2. Now mark each node with its minimax value. Explain how you handled the “?” values and why.
3. Explain why the standard minimax algorithm will fail on this game tree and briefly explain how you might fix it. Does your modified algorithm give optimal decisions for all games with loops?
A B1 2 3 4
40
Alpha-Beta Pruning #5
1. The complete game tree.
A B1 2 3 4
(1,4)
(2,4)
(2,3)
(4,3) (1,3)
(1,4)(1,2)
(3,2)
(3,1) (3,4)
(2,4)
+1
-1
?
?
Max
41
Alpha-Beta Pruning #5
2. The complete game tree with minimax values.
A B1 2 3 4
(1,4)
(2,4)
(2,3)
(4,3) (1,3)
(1,4)(1,2)
(3,2)
(3,1) (3,4)
(2,4)
+1
+1
+1
+1
-1
-1
-1
-1
?
?
?
• Assumption: an agent with the choice of either winning the game or entering a “?” state, will always choose the win. So, min(-1,?) is -1 and max(1,?) is 1. If all successors are “?”, the backed-up is “?”.
42
Alpha-Beta Pruning #5
3. Standard minimax is based on depth-first search, thus would go into infinite loop. It can be fixed by remembering states that have been expanded, so if repeated will return a “?” value.However, it does not work all cases because it is not clear how to compare “?” with a drawn position. In games with chance nodes, it is unclear to compute the average of a number and a “?”.Note that it is not correct to treat repeated states as drawn positions. E.g. both (1,4) and (2,4) repeat in the tree but they are won positions.
A B1 2 3 4
43
Propositional inference: Enumeration
A B C (A C) (B C) KB KB False False False
False False True
False True False
False True True
True False False
True False True
True True False
True True True
• Let = A B and KB = (A C) (B C).• Is it the case that KB ╞ ?• Check all possible models: must be true whenever KB is true.
44
Propositional inference: Solution
A B C (A C) (B C) KB KB
False False False False True False False True
False False True True False False False True
False True False False True False True True
False True True True True True True True
True False False True True True True True
True False True True False False True True
True True False True True True True True
True True True True True True True True
• Let = A B and KB = (A C) (B C).• Is it the case that KB ╞ ?• Check all possible models: must be true whenever KB is true.
45
Converting a Sentence to CNF
Convert this sentence into CNF:– B (P Q)
46
Converting a Sentence to CNF
B (P Q)• Eliminate
(B (P Q)) ((P Q) B)• Eliminate
(B (P Q)) ((P Q) B)• Move inwards
(B (P Q)) (P Q) B)• Apply distributivity law
(B P Q) (P B) (Q B)• The original sentence is now in CNF, as a conjunction of
three clauses.
47
Deducing New Sentences from KB in PL
Given the following, can you prove that the unicorn is mythical? How about magical? Horned?
“If the unicorn is mythical, then it is immortal, but if it is not mythical, then it is a mortal mammal. If the unicorn is either immortal or a mammal, then it is horned. The unicorn is magical if it is horned.”
48
Deducing New Sentences from KB in PL
“If the unicorn is mythical, then it is immortal, but if it is not mythical, then it is a mortal mammal. If the unicorn is either immortal or a mammal, then it is horned. The unicorn is magical if it is horned.”
Equivalent PL sentences:1. mythical immortal2. mythical immortal mammal3. immortal mammal horned4. horned magical
49
Deducing New Sentences from KB in PL
Translating into CNF:1. mythical immortal mythical immortal2. mythical immortal mammal
2a. mythical immortal 2b. mythical mammal
3. immortal mammal horned3a. immortal horned3b. mammal horned
4. horned magicalhorned magical
Resulting clauses Parent clauses
5. immortal mammal 1 & 2b
6. mammal horned 5 and 3a
7. horned 6 and 3b
8 magical 7 and 4
Conclusion:• The unicorn is horned and magical.• Mythical cannot be proved.
50
One-Bit Full Adder
X
Y
Z
S
Sumxor1
xor2
C1
C2
Carry
and1
and2
or1
A digital circuit C1
51
Einstein’s Challenge
• There are five houses, each of a different color and inhibited by a man of a different nationality, with a different pet, drink, and brand of cigarettes.
a) The Englishman lives in the red house.b) The Spaniard owns the dog.c) Coffee is drunk in the green house.d) The Ukrainian drinks tea.e) The green house is immediately to the right of the ivory house.f) The Winston smoker owns snails.g) Kools are smooked in the yellow house.h) Milk is drunk in the middle house.i) The Norwegian lives in the first house on the left.j) The man who smokes Chesterfields lives in the house next to the man with the fox.k) Kools are smoked in the house next to the house were the horse is kept.l) The Lucky Strike smoker drinks orange juice.m) The Japanese smokes Parliaments.n) The Norwegian lives next to the blue house.
Who owns the Zebra? Who drinks water?
52
First-Order Logic #1
• Translate into FOL:
1. Some students took French in Spring 20012. Every students who takes French passes it3. Only one student took Greek in Spring 20014. The best score in Greek is always higher than the best
score in French
Use the following vocabulary:- takes(x,c,s) : student x takes course c in semester s- passes(x,c,s) : student x passes course c in semester s- score(x,c,s) : score of student x in course c in semester s
53
First-Order Logic #1 Solution
1. Some students took French in Spring 2001x students(x) takes(x, F, Spring2001)
2. Every students who takes French passes itx,s students(x) takes(x, F, s) passes(x, F, s)
3. Only one student took Greek in Spring 2001x students(x) takes(x, G, Spring2001) y y ≠ x takes(y, G, Spring2001)
4. The best score in Greek is always higher than the best score in Frenchs x y score(x, G, s) > score(y, F, s)
54
Unification Algorithm
• Find the MGU of the following pair of atomic sentences if it exists.a. P(A, B, B) and P(x, y, z)
b. Q(y, G(A, B)) and Q(G(x, x), y)
c. Older(Father(y), y) and Older(Father(x), John)
d. Knows(Father(y), y), Knows(x, x)
55
Unification Algorithm (2)
• Find the MGU of the following pair of atomic sentences if it exists.a. P(A, B, B) and P(x, y, z){ x/A, y/B, z/B }
b. Q(y, G(A, B)) and Q(G(x, x), y)No unifier: x cannot bind to both A and Bc. Older(Father(y), y) and Older(Father(x), John){ y/x, x/John }d. Knows(Father(y), y), Knows(x, x)
No unifier: occur check prevents unification of y with Father(y)