Game Playing

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 53

Game Playing

Why study games?


 Interesting, hard problems which require minimal
“initial structure”
 Clear criteria for success
 Offer an opportunity to study problems involving
{hostile, adversarial, competing} agents and
the uncertainty of interacting with the natural
world
 Historical reasons
 Fun, good, easy to understand
 Games often define very large search spaces
– chess 35100 nodes in search tree, 1040 legal states
Types of Games
• Perfect information: A game with the perfect information is that in which agents can look
into the complete board. Agents have all the information about the game, and they can see each
other moves also.
• Imperfect information: If in a game agents do not have all information about the game and
not aware with what's going on, such type of games are called the game with imperfect
information.
• Deterministic games: Deterministic games are those games which follow a strict pattern and
set of rules for the games, and there is no randomness associated with them.
• Non-deterministic games: Non-deterministic are those games which have various
unpredictable events and has a factor of chance or luck. This factor of chance or luck is
introduced by either dice or cards. These are random, and each action response is not fixed.
Such games are also called as stochastic games.
Games vs. Search Problems
Search Problem Game
Search problems are only associated Games are associated with more than
with a single agent that aims to find the one agent is termed as multi-agent
solution which often expressed in the environment, in which each agent is an
form of a sequence of actions. opponent of other agent and playing
against each other. Each agent needs to
consider the action of other agent and
effect of that action on their performance

Solution is (heuristic) method for finding Solution is strategy


goal  strategy specifies move for
every
possible opponent reply.
Heuristics and CSP techniques can find Time limits force an approximate
optimal solution solution

Evaluation function: estimate of Evaluation function:


cost from start to goal through given evaluate “goodness” of game position
node
Typical assumptions
• Two agents whose actions alternate
• Utility values for each agent are the
opposite of the other
• Fully observabl e environments
  

• In game theory terms:


– “Deterministic, turn-taking, zero-sum games of
perfect information”
Zero-sum: one player’s loss is the other’s gain
Perfect information: both players have access to complete information about
the state of the game. No information is hidden from either player.
Deterministic: No chance (e.g., dice) involved
How to Play (How to Search)
• Obvious approach:
– From current game state:
– Consider all the legal moves you can make
–   Compute new position resulting from each
move
– Evaluate each resulting position
– Decide which is best
– Make that move
– Wait for your opponent to move and repeat
• Key problems are:
– Representing the “board”
– Generating all legal next boards
– Evaluating a position
Games
A game can be formally defined as a kind of search problem
with 04 components:
1. The initial state, which includes the board position &
identifies the player to move
2. A successor function, which returns a list of (move, state)
pairs, each indicating a legal move & resulting state
3. A terminal test, which determines when the game is over.
States where the game has ended are called terminal states
4. A utility / evaluation function, which gives a numeric
values for the terminal states.
- In chess, the outcome is a win, loss, or draw, with values
+1, -1, or
0.
Game Trees
 Game trees are used to represent two-player games.
 Alternate moves in the game are represented
by alternate levels in the tree.
 Root node represents the current board configuration
 Nodes in the tree represent positions.
 Edges between nodes represent legal moves.
 Leaf nodes represent won, lost or drawn positions.
Game Tree (2-player,
Deterministic,
computer’
s turn
Turns)
opponent’
s turn

computer’ The computer is Max.


s turn
The opponent is
opponent’ Min.
s turn
At the leaf nodes, the utility
function is employed.
leaf nodes •f(n) = +1 if the position is a win for X.
are evaluated
•f(n) = -1 if the position is a win for O.
•f(n) = 0 if the position is a draw.
Mini-Max Terminology
 Utility function: the function applied to leaf nodes
 Backed-up value
– of a max-position: the value of its largest successor
– of a min-position: the value of its smallest successor
 Create start node: MAX node, current board state
 Minimax procedure: search down several levels; at
the bottom level apply the utility function, back-up
values all the way up to the root node, and that node
selects the move.
Minimax
• Perfect play for deterministic games
• Idea: choose move to position with highest minimax value
= best achievable payoff against best play
• E.g., 2-ply game:
Minimax Strategy
• Why do we take the min value every other
level of the tree?

– These nodes represent the opponent’s choice of


move.
– The computer assumes that the human will choose
that move that is of least value to the computer.
Minimax Function
• MINIMAX-VALUE(n) =
– UTILITY(n) if n is a terminal
state
– maxs  Successors(n) MINIMAX-VALUE(s) if n
is a
MAX node
– mins  Successors(n) MINIMAX-VALUE(s) if n
is a
MIN node
Minimax – Example
Max 3 6 The computer can
obtain 6 by
choosing the right
Min 6 hand edge from the
5 3 first node.

Max 1 3 6 0 7
5

5 2 1 3 6 2 0 7
Minimax algorithm
Properties of Minimax
• Complete? Yes (if tree is finite)
• Optimal? Yes (against an optimal opponent)
• Time complexity? O(bm)
• Space complexity? O(bm) (depth-first exploration)

• For chess, b ≈ 35, m ≈100 for "reasonable" games


 exact solution completely infeasible

Need to speed it up.


Alpha-Beta pruning
 We can improve on the performance of the
minimax algorithm through alpha-beta pruning
 Basic idea: “If you have an idea that is surely bad,
don't take the time to see how truly awful it is.” --
Pat Winston

MAX >=2 • We don’t need to compute


the value at this node.
MIN =2 <=1
• No matter what it is, it can’t
affect the value of the root
MAX node.
2 7 1 ?
Alpha-Beta pruning
• Traverse the search tree in depth-first order
• At each MAX node n, alpha(n) = maximum
value found so far
• At each MIN node n, beta(n) = minimum value
found so far
– The alpha values start at -∞ and only increase, while
beta
values start at +∞ and only decrease
• Beta cutoff: Given MAX node n, cut off search below n
(i.e., don’t generate/examine any more of n’s children)
if alpha(n) >= beta(i) for some MIN node ancestor i of
n.
• Alpha cutoff: stop searching below MIN node n if
Alpha-Beta General example

MAX 3

MIN 3 2 - prune 14 1 -
prune

3 12 8 2 14 1
Alpha-Beta Pruning Example-2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 -3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 -3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 -3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 3

0 -3 3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 3

0 -3 3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 3

0 -3 3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 3

0 -3 3 5

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 3 2

0 -3 3 2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 3 2

0 -3 3 2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2

0 2

0 3 2

0 -3 3 2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2

0 2

0 3 2

0 -3 3 2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2

0 2

0 3 2

0 -3 3 2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2

0 2

0 3 2

0 -3 3 2 5

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2

0 2

0 3 2 1

0 -3 3 2 1

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2

0 2

0 3 2 1

0 -3 3 2 1 -3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2

0 2

0 3 2 1

0 -3 3 2 1 -3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2 1

0 2 1

0 3 2 1

0 -3 3 2 1 -3

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2 1

0 2 1

0 3 2 1

0 -3 3 2 1 -3 -5

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2 1

0 2 1

0 3 2 1

0 -3 3 2 1 -3 -5

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2 1

0 2 1 -5

0 3 2 1 -5

0 -3 3 2 1 -3 -5

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 2 1

0 2 1 -5

0 3 2 1 -5

0 -3 3 2 1 -3 -5

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 1

0 2 1

0 2 1 -5

0 3 2 1 -5

0 -3 3 2 1 -3 -5

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 1

0 2 1 2

0 2 1 -5 2

0 3 2 1 -5 2

0 -3 3 2 1 -3 -5 2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Alpha-Beta Pruning Example-2

0 1

0 2 1 2

0 2 1 -5 2

0 3 2 1 -5 2

0 -3 3 2 1 -3 -5 2

0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
Exercise
The α-β algorithm

cutoff
The α-β algorithm

cutoff
Why is it called α-β?
 is the value of the best
(i.e., highest-value) choice
found so far for MAX at any
choice point along the path
to the root.
– If v is worse than , MAX
will avoid it
 prune that branch

 is the value of the best


(i.e., lowest-value) choice
found so far for MIN at any
choice point along the path
for to the root.
Properties of α-β
• Pruning does not affect final result. This means that it gets
the exact same result as does full minimax.

• Good move ordering improves effectiveness of pruning

• With "perfect ordering," time complexity = O(bm/2)


 doubles depth of search

• A simple example of the value of reasoning about


which computations are relevant (a form of
metareasoning)
State of the art
• Chess:
– Deep Blue beat Gary Kasparov in 1997
– Garry Kasparav vs. Deep Junior (Feb 2003): tie!
– Kasparov vs. X3D Fritz (November 2003): tie!
• Checkers: Chinook is the world champion
• Go: Computer players are decent, at best
• Bridge: “Expert” computer players exist, but no world
champions yet
• Poker: CPRG regularly beats human experts
• Check out: http://www.cs.ualberta.ca/~games/
Summary
• Games are fun to work on!

• They illustrate several important points about AI.

• Perfection is unattainable  must approximate.

• Game playing programs have shown the world what


AI can do.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy