18csc305j Ai Unit 2 v3
18csc305j Ai Unit 2 v3
UNIT – 2
Unit 2 List of Topics
1 2
3 4
Blind strategies B F S
Search M e t h o d s
BFS characteristics
Completeness: if the branching factor is ‘b’ and the goal
node is at depth d , BFS will eventually fin d it.
Optimality: BFS is optimal if path cost is a non-decreasing
function of depth. a
1 + b + b2 + b3 + . . . + b d + b(b d − 1) =
Time complexity:
O(b d+1
).
Space complexity: O(b d + 1 ) . b
a
Otherwise, the shallowest node may not necessarily be optimal.
b
b branching factor; d depth of the goal node
spring 2011
Blind Search : Depth First Search (DFS)
Implementation:
fringe = LIFO queue, i.e., put successors at front.
1 2 3
4 5 N+1
…….
Search M e t h o d s
DFS characteristics
Small space requirements: only the path to the current node
and the siblings of each node in the path are stored.
Backtracking search generates only one successor for
each node.
Completeness: no, if the expanded subtree has an
infinite depth.
Optimality: no, if a solution located deeper, but located in a
spring 2011
Search M e t h o d s
spring 2011
Unit 2 List of Topics
DEPTH
LIMITED
SEARCH
Blind Search : Iterative Deepening DFS
(ID-DFS)
Blind Search : Iterative Deepening DFS Search M e t h o d s
(ID-DFS)
IDS characteristics
Completeness: yes.
Optimality: yes, if step cost = 1.
. . + b d = O(b d ).
Space complexity: O(bd) .
Numerical comparison for b = 10 , d = 5
• BFS SOLUTION?
– S-1-2-3-5-8-10-12-14-16-19-G
– SEARCH SOLUTION FOUND IN 12
STEPS
DFS SOLUTION?
S-1-2-3-6-5-8-9-10-11-13-16-18-G
SEARCH SOLUTION FOUND IN 14 STEPS
APPLICATION: MAZE GAME
• BFS SOLUTION?
– S-1-2-3-5-8-10-12-14-16-19-G
– SEARCH SOLUTION FOUND IN 12 STEPS
15
S 5 19
6 7 8 17 18
1 G 21
10
9 20 21
2
12
11 x
3 x 19
14
13
5 4
16
15
19
APPLICATION: MAZE GAME
• BFS SOLUTION?
– S-1-2-3-5-8-10-12-14-16-19-G
– SEARCH SOLUTION FOUND IN 12 STEPS
15
S 5 19
6 7 8 17 18
1 G 21
10
9 20 21
2
12
3
11 19 x
x
14
13
5 4
16
15
19
Blind Search : Uniform Cost Search
Minimum is S->D->C->G2
And also G2 is one of the destination nodes thus we found our
path.
In this way we can find the path with the minimum cumulative cost from
a start node to ending node – S->D->C->G2 with cost total cost as
13(marked with green color).
Uniform Cost Search
Implementation: fringe =
queue ordered by path cost
Equivalent to breadth-first if
all step costs all equal.
Breadth-first is only optimal
if step costs is increasing
with depth.
(e.g. constant). Can we
guarantee optimality for
any step cost?
Uniform-cost Search:
Expand node with
smallest
path cost g(n).
Blind strategies UCS
Search M e t h o d s
a
If all step costs are equal, UCS=BFS.
C ∗cost of optimal solution
b
Blind strategies UCS
Search M e t h o d s
32
Summary of Blind Search Algorithms
Space bd bm
Optimal? Yes No
Complete Yes No
?
b: branching d: solution m: maximum
factor depth depth
33
Summary of Blind Search Algorithms
34
Summary of Search Algorithms
35
Unit 2 List of Topics
• Best-first search
• A* search
• Heuristics
Generate-and-test
38
Generate-and-test
Example - Traveling Salesman Problem
(TSP)
• Traveler needs to visit n cities.
• Know the distance between each pair of cities.
• Want to know the shortest route that visits all
the cities once.
• n=80 will take millions of years to solve
exhaustively!
39
Generate-and-test
TSP Example
A 6 B
1 2
5 3
D 4 C
40
Generate-and-test Example
2. A - B - D-C
3. A - C - B-D C D B D C B
4. A - C - D-B
...
D C D B B C
41
Best First Search Algorithms
• Implementation:
Order the nodes in fringe increasing order of cost.
• Special cases:
– greedy best-first search
– A* search
Romania with straight-line dist.
Greedy best-first search
• f(n) = estimate of cost from n to goal
• e.g., f(n) = straight-line distance from n to
Bucharest
• Greedy best-first search expands the node
that appears to be closest to goal.
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
GBFS is not complete
c
b g
a goal state
start state
Demystifying AI algorithms
Heuristic Search : A* Algorithm
Path cost for S-D-G
S :10 f(S) = g(S) + h(S)
2 3 = 0 + 10 10
f(D) = (0+3) + 9 12
A :8 D :9 f(G) = (0+3+3) + 0 6
5
2 3 1 Total path cost = f(S)+f(D)+f(G) 28
Path cost for S-A-G’
E G’ G H
f(S) = 0 + 10 10
:4 :0 :0 :3
f(A) = (0+2) + 8 10
f(G’) = (0+2+2) + 0 4
* Path S-A-G’ is chosen =
Lowest cost Total path cost = f(S)+f(A)+f(G’) 24
Application of Heuristic Search : A* Algorithm –
Snake & Ladder
12 h(n)=0 11 10 9g(n)=8, h(n)=8,
g(n)=1, h(n)=2, f(n)=3 f(n)=16
8 7 6 5
1 2 3 4
g(n)=0, h(n)=11, f(n)=11 g(n)=1, h(n)=2, f(n)=3 g(n)=2, h(n)=9, f(n)=11 g(n)=3, h(n)=8, f(n)=11
1 2 (10) 3 4 5 6 7
2 (10) 3 4 5 6 7 8
3 4 5 6 7 8 9 (4)
4 5 6 7 8 9 (4) 10
5 6 7 8 9 (4) 10 11
6 7 8 9 (4) 10 11 12
7 8 9 (4) 10 11 12 X
8 9 (4) 10 11 12 X
9 (4) 10 11 12 X
10 11 12 X
11 12 X
12 X
Demystifying AI algorithms
A* search example
A* search example
A* search example
A* search example
A* search example
A* search example
try yourself !
6 1 straight-line distances
3 A D F 1
h(S-G)=10
2 h(A-G)=7
S 4 8 G
B E h(D-G)=1
h(F-G)=1
1 20 h(B-G)=10
C h(E-G)=8
h(C-G)=20
h(G-G)=0
The graph above shows the step-costs for different paths going from the start (S) to
the goal (G). On the right you find the straight-line distances.
1. Draw the search tree for this problem. Avoid repeated states.
2. Give the order in which the tree is searched (e.g. S-C-B...-G) for A* search.
Use the straight-line dist. as a heuristic function, i.e. h=SLD,
and indicate for each node visited what the value for the evaluation function, f, is.
Admissible heuristics
Try it out!
START
1 2 3
7 8 4
6 5
GOAL
1 2 3
8 4
7 6 5
64
Admissible heuristics – 8 Puzzle Problem
START
1 2 3
START
7 8 4
1 2 3
6 5
7 8 4
6 5
START START START
1 2 3 1 2 3 1 2 3
GOAL 7 4 7 8 4 7 8 4
1 2 3 6 8 5 6 5 6 5
8 4
7 6 5 H=5 H=4 H=6
65
Admissible heuristics
E.g., for the 8-puzzle:
• h1(n) = number of misplaced tiles
• h2(n) = total Manhattan distance
(i.e., no. of squares from desired location of each tile)
• h1(S) = ?
• h2(S) = ?
66
Evaluation of Search Algorithms
Completeness
Is the algorithm guaranteed to n d a solution if one exists?
Optimality
When the algorithm n d s a solution, is this the optimal one?
Time complexity
How long does it take to n d a solution? a
a
Often measured as number of nodes generated during search.
Space complexity
How much memory is needed to perform the search? a
a
Often measured as maximum number of nodes stored in memory.
Unit 2 List of Topics
E.g.,
1. forInitialize:
the 8-puzzle: Set G* = {s}, f(s) = h(s)
• h1(n) = number of misplaced
If s T,tiles
label s as SOLVED
• 2.h2(n)Terminate:
= total Manhattan distance If s is SOLVED, then terminate
(i.e.,
3. no.Select:
of squares from desired
Select alocation of each
non-terminal leaftile)
node n from the
marked sub-tree below s in G*
4. Expand: Make explicit the successors of n
For each successor, m, not already in G*:
Set f(m) = h(m)
If m is a terminal node, label m as
SOLVED
• 5.h1(S) = ?Revision: Call cost-revise(n)
Cost
• 6.h2(S)
Loop: Go To Step 2.
=?
69
Cost Revision in AO*: cost-revise(n)
E.g.,
1. for theZ 8-puzzle:
Create = {n}
•2. h1(n)
If Z =={ number
} return of misplaced tiles
Select a node m from Z such that m has no descendants in Z
•3. h2(n) = total Manhattan distance
4. If m is an AND node with successors r1, r2, … rk:
(i.e., no. of squares from desired location of each tile)
Set f(m) = [ f(ri) + c(m, ri) ]
Mark the edge to each successor of m
If each successor is labeled SOLVED, then label m as SOLVED
• If m is an OR node with successors r1, r2, … rk:
70
Searching OR Graphs
E.g.,
• for
Howthe 8-puzzle:
does AO* fare when the graph has only OR nodes?
• • h1(n)
What= number of misplaced
are the roles tiles and upper-bound estimates?
of lower-bound
• h2(n) – =Pruning criteria: LB distance
total Manhattan > UB
(i.e., no. of squares from desired location of each tile)
Searching Game Trees
• Consider an OR tree with two types of OR nodes, namely Min nodes and Max
nodes
• In Min nodes we select the minimum cost successor
• In Max nodes we select the maximum cost successor
71
Shallow and Deep Pruning
E.g., for the 8-puzzle:
• h1(n) = number of misplaced
ROOT tiles ROOT
• h2(n) = total Manhattan distance
(i.e., no. of squares
A from desired location of each tile)
10
B
10 F
Max node
14 C D
G Min node
• h1(S) = ? 5 E
• h2(S) = ? Cut-off
Shallow Deep Cut-off
72
Unit 2 List of Topics
74
Local search algorithms
• In many optimization problems, the path to the goal is
irrelevant; the goal state itself is the solution
75
Types of Local Search
• Hill-climbing Search
• Simulation Annealing Search
76
Terminology of Local
Search
77
Local search algorithms
Hill Climbing
78
Hill Climbing
Algorithm
1. Evaluate the initial state.
2. Loop until a solution is found or there are no
new operators left to be applied:
Select and apply a new operator
Evaluate the new state:
goal quit
better than current state new current state
79
Steepest Ascent Hill Climbing
80
Steepest Ascent/Descent Hill Climbing
current
6
4 10 3 2 8
81
Hill Climbing Example
A local heuristic function
Count +1 for every block that sits on the correct thing. The goal state has
the value +8.
Count -1 for every block that sits on an incorrect thing. In the initial state
blocks C, D, E, F, G, H count +1 each. Blocks A, B count -1 each , for the total
of +4.
Move 1 gives the value +6 (A is now on the correct support). Moves 2a and
2b both give +4 (B and H are wrongly situated). This means we have a local
maximum of +6.
82
Hill Climbing Example
83
Hill Climbing Example
84
Hill-climbing search
• Problem: depending on initial state, can get stuck in local maxima
85
Hill Climbing: Disadvantages
Local maximum
A state that is better than all of its
neighbours, but not better than
some other states far away.
Plateau
A flat area of the search space in
which all neighbouring states have
the same value.
Ridge
The orientation of the high region, compared to
the set of available moves, makes it impossible
to climb up. However, two moves executed
serially may increase the height. 86
Hill Climbing: Disadvantages
Ways Out
• Backtrack to some earlier node and try going
in a different direction.
• Make a big jump to try to get into a new
solution.
• Moving in several directions at once.
87
Unit 2 List of Topics
89
Simulated annealing search
• Idea: escape local maxima by allowing some "bad"
moves but gradually decrease their frequency.
90
Simulated annealing search
• A variation of hill climbing in which, at the
beginning of the process, some downhill moves
may be made.
91
Simulated annealing search
Physical Annealing
• Physical substances are melted and then
gradually cooled until some solid state is reached.
• The goal is to produce a minimal‐energy state.
• Annealing schedule: if the temperature is
lowered sufficiently slowly, then the goal will be
attained.
• Nevertheless, there is some probability for a
transition to a higher energy state: eΔE/kT.
92
Simulated annealing algorithm
93
80
Simulated annealing example
94
Simulated annealing example
95
Simulated annealing example
1. The objective function to minimize is a simple function of two variables:
min f(x) = (4 - 2.1*x1^2 + x1^4/3)*x1^2 + x1*x2 + (-4 + 4*x2^2)*x2^2;
x1, x2 >=0, x1,x2 <=20 5bits Tmin = 50
e(-
Delta DeltaE/T)
Decimal Decimal Binary Decimal Binary Decimal Delta E = f(x')- E<0? < r? =
k=1 Binary x1 x1 Binary x2 x2 f(x) x1' x1' x2' x2' f(x') f(x) Accept e(-DeltaE/T) r Accept
T=300 10 2 1001 925941.73 0 0 1000 8 16128 -9813.733333Yes Accept
T=T/2=150 0 0 1000 8 16128 1 1 1100 1282382.23 66254.23333No 1.494E-192 0.6Accept
T=75 1 1 1100 1282382.23 0 0 1110 14 152880 70497.76667No 0 0.7Accept
T=37.5
16128
6
2 5 1 1 3 2 1 2
96
Unit 2 List of Topics
98
Local Beam Search
99
Local Beam Search
100
Unit 2 List of Topics
• Search Space
• Cross Over
• Mutation
GA can be summarized as:
https://www.geeksforgeeks.org/genetic-algorithms/
Random Number Table for Solving
GA Problem
GA Calculation
https://www.tutorialandexample.com/adversarial-search-in-artificial-intelligence/#:~:text=AI%20Adversarial%20search%3A
%20Adversarial%20search,order%20to%20win%20the%20game.
Game playing and knowledge structure-
Elements of Game Playing search
• INITIAL STATE (S0): The top node in the
game-tree represents the initial state in
the tree and shows all the possible choice
to pick out one.
• PLAYER (s): There are two players, MAX
and MIN. MAX begins the game by
picking one best move and place X in the
empty square box.
• ACTIONS (s): Both the players can make
moves in the empty boxes chance by
chance.
• RESULT (s, a): The moves made
by MIN and MAX will decide the outcome
of the game.
• TERMINAL-TEST(s): When all the empty
boxes will be filled, it will be the
terminating state of the game.
• UTILITY: At the end, we will get to know
who wins: MAX or MIN, and accordingly,
the price will be given to them.
Unit 2 List of Topics
• "Unpredictable" opponent
specifying a move for every possible
opponent reply
Tree from
Max’s
perspective
Minimax Algorithm
• Minimax algorithm
– Perfect play for deterministic, 2-player game
– Max tries to maximize its score
– Min tries to minimize Max’s score (Min)
– Goal: move to position of highest minimax value
Identify best achievable payoff against best play
Minimax Algorithm
2 1 2 1
2 7 1 8 2 7 1 8 2 7 1 8
MIN
2 7 1 8
Minimax Algorithm (cont’d)
3 9 0 7 2 6
3 0 2
3 9 0 7 2 6
3 0 2
3 9 0 7 2 6
• Limitations
– Not always feasible to traverse entire tree
– Time limitations
• Key Improvement
– Use evaluation function instead of utility
• Evaluation function provides estimate of utility at given
position
Unit 2 List of Topics
Principle
– If a move is determined worse than another move already
examined, then there is no need for further examination of the
node.
α-β Pruning Example
α-β Pruning Example (cont’d)
α-β Pruning Example (cont’d)
α-β Pruning Example (cont’d)
α-β Pruning Example (cont’d)
Alpha-Beta Pruning (αβ prune)
• Rules of Thumb
β
Alpha-Beta Pruning Example
β
Alpha-Beta Pruning Example
β
Another Example
1.Search below a MIN
node may be alpha-
pruned if the beta
value is <= to the
alpha value of some
MAX ancestor.
S-159
Game Outcomes
S-160
Minimax Criterion
Look to the “cake cutting problem” to explain
Cutter – maximize the minimum the Chooser will
leave him
Chooser – minimize the maximum the Cutter will
get
Chooser Choose bigger piece Choose smaller piece
Cutter
Cut cake as evenly as Half the cake minus a Half the cake plus a
possible crumb crumb
If the upper and lower values are the same, the number is called the
value of the game and an equilibrium or saddle point condition exists
The value of a game is the average or expected game outcome if the game is
played an infinite number of times
A saddle point indicates that each player has a pure strategy i.e., the strategy
is followed no matter what the opponent does
Saddle Point
S-165
Pure Strategy - Minimax Criterion
Y1 Y2
Player X’s X1 10 6 6
strategies
X2 -12 2 -12
Maximum Column 10 6
Number
S-166
Mixed Strategy Game
When there is no saddle point, players will play each strategy for a
certain percentage of the time
The most common way to solve a mixed strategy is to use the expected
gain or loss approach
A player plays each strategy a particular percentage of the time so that the
expected value of the game does not depend upon what the opponent does
Y1 Y2 Expected Gain
P 1-P
X1 4 2 4P+2(1-P)
Q
X2 1 10 1P+10(1-p)
1-Q
4Q+1(1-Q) 2Q+10(1-q)
Mixed Strategy Game
: Solving for P & Q
4P+2(1-P) = 1P+10(1-P)
or: P = 8/11 and 1-p = 3/11
Expected payoff:
1P+10(1-P)
=1(8/11)+10(3/11)
EPX= 3.46
4Q+1(1-Q)=2Q+10(1-q)
or: Q=9/11 and 1-Q = 2/11
Expected payoff:
EPY=3.46
S-168
Mixed Strategy Game : Example
S-169
Mixed Strategy Game
Example
• This game can be solved by setting up the
mixed strategy table and developing the
appropriate equations:
S-170
Mixed Strategy Game: Example
S-171
Two-Person Zero-Sum and Constant-Sum
Games
Two-person zero-sum and constant-sum games are played according to
the following basic assumption:
Each player chooses a strategy that enables him/her to do the best he/she
can, given that his/her opponent knows the strategy he/she is following.
(1)
Two-Person Zero-Sum and Constant-Sum
Games (Cont)
Step 1 Check for a saddle point. If the game has none, go on to step 2.
Two-Person Zero-Sum and Constant-Sum
Games (Cont)
Let’s take the following example: Two TV channels (1 and 2) are competing
for an audience of 100 viewers. The rule of the game is to simultaneously
announce the type of show the channels will broadcast. Given the payoff
matrix below, what type of show should channel 1 air?
Two-person zero-sum game – Dominance
property
Step-2:
1. If all the elements of a Row-i are less than or equal to the corresponding elements of any
other Row-j, then the Row-i is dominated by the Row-j and it is removed from the matrix.
eg. If Row-3 ≤ Row-4, then remove Row-3
Step-3:
Again repeat Step-1 & Step-2, if any Row or Column is dominated, otherwise stop the
procedure.
Two-person zero-sum game – Dominance
property- To do problem!
Player A \Player B B1 B2 B3 B4
A1 3 5 4 2
A2 5 6 2 4
A3 2 1 4 0
A4 3 3 5 2
Solution Player B
B3 B4
A2 2 4 B3 B4
A4 5 2
A2 2 4
Player A
A4 5 2
The Prisoner’s Dilemma
– If only one prisoner turns state’s evidence and testifies against his partner he
will go free while the other will receive a 3 year sentence.
– Each prisoner knows the other has the same offer
– The catch is that if both turn state’s evidence, they each receive a 2 year
sentence
– If both refuse, each will be imprisoned for 1 year on the lesser charge
A game is described by
Player B
Left Right
Top 3, 0 0, -4
Player A
Bottom 2, 4 -1, 3
Game Playing
How to solve a situation like this?
• If Player A’s choice is optimal given Player B’s choice, and B’s
choice is optimal given A’s choice, a pair of strategies is a Nash
equilibrium.
• When the other players’ choice is revealed neither player like
to change her behavior.
• If a set of strategies are best responses to each other, the
strategy set is a Nash equilibrium.
Payoff matrix
Normal- or strategic form
Player B
Left Right
Top 1, 1 2, 3*
Player A
Bottom 2, 3* 1, 2
Solution
• Here you can find a Nash equilibrium; Top is the best response
to Right and Right is the best response to Top. Hence, (Top,
Right) is a Nash equilibrium.
• But there are two problems with this solution concept.
Problems
Player B
Left Right
Top 1, -1 -1, 1
Player A
-1, 1
Bottom 1, -1
Nash equilibrium in mixed strategies
Prisoner B
Confess Deny
Confess -2, -2 0, -4
Prisoner A
Solution
Confess is a dominant strategy for both. If both
Deny they would be better off. This is the
dilemma.
Nash Equilibrium – To do Problems!
COKE
L R B
PEPSI L R
U 6,8* 4,7
A
D 7,6 3,7 U 7,6* 5,5
D 4,5 6,4
GAME PLAYING & MECHANISM DESIGN
Mother
Social Planner
Mechanism Designer
Kid 1 Kid 2
Rational and Rational and
Intelligent Intelligent
Example 1: Mechanism Design
Fair Division of a Cake
GAME PLAYING & MECHANISM DESIGN
Tenali Rama
(Birbal)
Mechanism Designer
Baby
Mother 1 Mother 2
Rational and Rational and
Intelligent Player Intelligent Player
4 60 4 80
Buyers Buyers