Oradea: Bucharest Arad Craiova
Oradea: Bucharest Arad Craiova
Straight−line distance
71 to Bucharest
Neamt Arad 366
87 Bucharest 0
Zerind 151
75 Craiova 160
Iasi Dobreta 242
Arad 140 Eforie 161
92 Fagaras 178
Sibiu 99 Fagaras Giurgiu 77
118
Vaslui Hirsova 151
80
Iasi 226
Rimnicu Vilcea
Timisoara Lugoj 244
142 Mehadia 241
111 211 Neamt
Lugoj 97 Pitesti 234
Oradea 380
70 98 Pitesti 98
Hirsova
Mehadia 146 101 85 Urziceni Rimnicu Vilcea 193
75 138 86 Sibiu 253
Dobreta
120
Craiova
90Bucharest Timisoara
Urziceni
Vaslui
329
80
199
Eforie
Giurgiu Zerind 374
GREEDY SEARCH AND A*
• Greedy Search
• A*
GAMES & ADVERSARIAL
SEARCH
Chapter 5
Section 1 – 4
• In which we examine the problems that arise
when we try to plan ahead in a world where
other agents are planning against us.
• Games have engaged the intellectual faculties of humans—
sometimes to an alarming degree—for as long as civilization
has existed.
• For Al researchers, the abstract nature of games makes them an
appealing subject for study.
• The state of a game is easy to represent, and agents are usually
restricted to a small number of actions whose outcomes are
defined by precise rules.
GAMES VS. SEARCH PROBLEMS
X X X
MIN (O) X X X
X X X
XO X O X ...
MAX (X) O
XO X X O X O ...
MIN (O) X X
XO X X O X X O X ...
TERMINAL O X OO X X
O X X X
O O O
Utility −1 0 +1
Α-Β PRUNING EXAMPLE
MIN
..
..
..
MAX
MIN
V
PROPERTIES OF
• Pruning does not affect final result (it is exact).
• evaluation function
• When the search is cut off, we evaluate the current state
by estimating its utility. This estimate if captured by the
evaluation function.
EVALUATION FUNCTIONS
• e.g., w1 = 9 with
f1(s) = (number of white queens) – (number of black
queens), etc.
AI IN GAMES
Types of AI in games
Heuristics
• Common Heuristics
– Most Constrained
– Do the most difficult thing first
– Try the most promising thing first
Algorithms
Roles or functions played in AI-based games:
Path following Decision making
Collision avoidance Tactical analysis
Finding paths Terrain analysis
World Representations Coordinating action
Movement planning
Learning
Search Tree in Games?
deterministic chance
chess, checkers,
perfect connect four, backgammon
information othello
Max attempts to
maximize the 6-1 5-2 4-3 max
advantage and win.
2-1-1-1-1-1 max
PERFECT DECISION, TWO PERSON GAMES
Two players
MAX and MIN
MAX moves first; alternate turns thereafter.
Formal definition of game
Initial State
Successor Function
Terminal Test
Utility Function
No one player has full control, must develop a
strategy.
MINIMAX ALGORITHM: BASICS
3 MAX
3 0 2 MIN
3 9 0 7 2 6 MAX
MIN
2 3 5 9 0 7 4 2 1 5 6
Position p:
win for Max, u(p) =
loss for MAX, u(p) =–
otherwise, u(p) = (# of
complete rows, cols, &
diags still open for MAX) –
(# of complete rows, cols,
and diags still open for MIN)
TIC-TAC-TOE: 2ND MOVE
MAX PLAYER: 3RD MOVE
MAX PLAYER: 3RD MOVE
Search starts
depth first
Back up value
immediately
Do not
need to
generate
MIN these
nodes!
Example, chess:
decent amateur program 1000 moves /second
150 seconds /move (tournament play)
Look at approx. 150,000 moves
Chess branching factor of 35
generating trees that are 3-4 ply,
Resultant play – pure amateur
Definitions
value – lower bound on MAX node
value – upper bound on MIN node
Observations
value on MAX nodes never decrease
values on MIN nodes never increase
Application
Search is discontinued below any MIN node with min-
value v : cut off
Search is discontinued below any MAX node with
max-value v : cut off
ALPHA-BETA SEARCH EXAMPLE
a MAX
b c MIN
d e f g MAX
MIN
1 2 3 4 5 7 1 0 2 6 1 5
ALPHA-BETA SEARCH ALGORITHM
Computing and
value of MAX node = current largest final
backed-up value of its successors.
value of MIN node = current smallest final
backed-up value of its successors.
ALPHA-BETA SEARCH ALGORITHM
Alpha-Beta-
Search(state) returns
an action
v = MAX-VALUE(state,-
∞, +∞)
MAX-VALUE(state, , ) return the action
MIN-VALUE(state, , ) in
if TERMINAL-TEST(state) then SUCCESSORS(state) then
if TERMINAL-TEST(state)
return UTILITY(state) with value
return v
UTILITY(state)
v = -∞ v = +∞
for a, s in SUCCESSORS(state) do for a, s in SUCCESSORS(state) do
v = MAX(v, MIN-VALUE(s, v = MIN(v, MAX-VALUE(s,
)) ))
if v >= then return v if v <= then return v
= MAX(, v) = MIN(, v)
return v (a utility value) return v (a utility value)
ALPHA-BETA SEARCH PERFORMANCE
min Expectiminimax(s)
sSuccessors ( n )
node
if n is a Min node
sSuccessors(n)
Execution Management
AI receives
Group
information AI
Strategy Content Creation
Character
Scripting
AI
Decision Making AI has implications
for Related
technologies
Movement
Animation Physics
AI is turned into on-screen
action
AI GAMES – SCHEMATIC DIAGRAM
Content Main Game Engine AI Engine
Construction
Modeling
World
package
Interface
Level Loader
World interface
Extracts relevant
Game data
Level design AI
tool behavior
Game engine
Calls AI each manager
frame
MAX
MIN
MAX
MIN
1 2 3 4 0 5 4 2 1 6 3
EXERCISE FOR ALPHA-BETA SEARCH
a MAX
b c MIN
d e f g MAX
h i j k l m n MIN
MAX
5 7 2 0 1 2
5 4 2 4 3 0 2 8