Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Riga Technical University
Faculty of Computer Science and Information Technology
Department of Systems Theory and Design
FUNDAMENTALS OF ARTIFICIAL
INTELLIGENCE
Lecture 6
USING HEURISTICS IN
GAMES
Dr.habil.sc.ing., professor Janis Grundspenkis, Dr.sc.ing., lecturer Alla Anohina-Naumeca
Departm ent of System s Theory and Design
Faculty of Com puter Science and I nform ation Technology
R iga Technical University
E-m ail:
{ janis.grundspenkis, alla.anohina-naum eca} @rtu.lv
Address:
M eza street 1/ 4- { 550, 545} , R iga, Latvia, LV-1048
P hone:
(+371) 67089{ 581, 595}
Heuristics and games
Games are one of the oldest field of the application of Artificial
Intelligence, because any game demands intelligence.
Implementing games two methods of artificial intelligence are used:
search and heuristics.
In this course the simplest kind of games, so called two-person games
with perfect information, will be considered.
2/25
Two-person games
Two-person games are characterized by the following features:
• Two players participate in a game, for example, one is a person,
other is a computer
• Players make moves one by one up to the end of the game
• There is a conflict because each player wishes to win the game, so
“stupid” moves are avoided
• Players use heuristics to guide the game along a path to a winning
state
3/25
Two-person games with perfect
information (1)
In two-person games with perfect information:
• Both players have access to complete information about the state
of the game. No information is hidden from either player
• No chance (e.g., using dice) involved
4/25
Two-person games with perfect
information (2)
Examples of two-person games with perfect information are:
• Checkers
• Tic-tac-toe
• Chess
Non-examples:
• Bridge
• Solitaire
• Backgammon
5/25
Game tree
Two-person games with perfect information are represented using a
game tree (a special kind of a state space).
A game tree consists of nodes and arcs connecting nodes.
This is a visual representation of a game tree:
6/25
Nodes of a game tree
Nodes of a game tree display possible discrete states of the game. For
example, in Tic-tac-toe a state is a number of X and O.
A game tree includes the following nodes:
• A root
• Dead-ends
• Internal nodes
7/25
Levels of a game tree
Typically a game tree consists of several levels. One or several nodes
are located at each level and they display states of the game from
which only one of two players makes moves.
Player 1
Player 2
Player 1
Player 2
8/25
A root
A root is located at the highest level of a game tree and displays an
initial state of the game.
For example, this is an initial state of Tic-tac-toe:
9/25
Dead-ends
Dead-ends of a game tree do not have outgoing arcs. They correspond
to final states of the game.
Some final states of Tic-tac-toe are the following:
X
0
X
0
X
X
0
0
X
0
X
X
X
0
0
X
0
0
X
10/25
Internal nodes
Internal nodes represent intermediate states of the game.
Some intermediate states of Tic-tac-toe are the following:
X
0
0
X
0
0
0
X
X
0
X
11/25
Arcs of a game tree
Arcs of a game tree display moves allowed by the rules of the game. A
move is s transition from one state to another:
X
0
A move
0
X
0
0
X
A state before a
move
A state after the
move
Two restrictions are imposed on arcs displaying two-person game with
perfect information with a game tree.
12/25
First restriction of arcs
Arcs between nodes on the same level are not allowed
Forbidden
13/25
Second restriction of arcs
Arcs over some levels are not allowed.
Forbidden
14/25
Outgoing arcs
It is possible to perform several moves from the same state. This
situation is displayed by several outgoing arcs.
Example:
X
…
0
0
X
X
X
0
15/25
Ingoing arcs
It is possible to reach a state form several different states. This situation
is displayed by several ingoing arcs.
0
Example:
0
X
0
0
X
X
0
X
0
X
0
0
X
X
0
X
16/25
Minimax algorithm
Minimax is one of the algorithms for the implementation of two-person
games.
In the mentioned algorithm players are referred as a maximizer and a
minimizer.
Each of players has a particular strategy in the game:
• The maximizer tries to win or to maximize his/her result
• The minimizer attempts to minimize maximizer’s score
17/25
Maximizer and minimizer
Levels of a game tree are labeled as MIN or MAX alternately. It is not
important which level is the first:
MIN
MAX
MAX
MIN
MIN
MAX
MAX
MIN
18/25
Steps of the Minimax algorithm (1)
1. Create a game tree
2. Label levels of the game tree as MIN and MAX
3. Acquire heuristic values of dead-ends. Heuristic values are assigned
in the following way:
• a state acquires 1 if it is a win for the maximizer, 0 – if it is a win
for the minimizer, or
• +1 if it is a win for the maximizer, 0 – it is a drawn game, -1 – if it
is a win for the minimizer
19/25
Steps of the Minimax algorithm (2)
4. Propagate heuristic values of dead-ends up the tree according to the
following rules:
• If a state is a node on MAX level it acquires the maximum value
among its direct descendants
• If a state is a node on MIN level it acquires the minimal value
among its direct descendants
5. Determine a result of the game and winning paths
20/25
Result of a game and winning paths
The result of the game is a value of the root node. It points out who wins the
game.
Winning paths are determined on the basis of the result of a game:
A winning path starts from a root node and reaches a dead-end:
1. All nodes in a winning path are closely related
2. All nodes in a winning path have the same heuristic value equal to the
result of the game:
• If the result of the game is 1, then the maximizer wins the game
and all nodes in winning paths have the heuristic value 1
• If the result of the game is 0 (or -1 if we use three heuristic
values: +1, 0, -1), then the minimizer wins the game and all
nodes in winning paths have the heuristic value 0 (or -1)
21/25
Example (1)
As an example a game called “Nim” will be considered. There are 7 safety-matches. In each move
they must be divided in two piles with different number of safety-matches. A player who cannot
perform a move loses a game.
Step 1 and 2. Creation of a game tree and labeling of its levels by MIN and MAX
7
6-1
5-1-1
MIN
5-2
4-3
4-2-1
3-2-2
4-1-1-1
3-2-1-1
2-2-2-1
3-1-1-1-1
2-2-1-1-1
2-1-1-1-1-1
MAX
3-3-1
MIN
MAX
MIN
MAX
22/25
Example (2)
Step 3. Acquiring of heuristic values of dead-ends
7
6-1
5-1-1
4-1-1-1
3-1-1-1-1
2-1-1-1-1-1
MIN
5-2
4-3
4-2-1
3-2-2
3-2-1-1
2-2-2-1
2-2-1-1-1 1,
3-3-1
0,
because from this state the
maximizer must perform its move
but it is impossible, so the
maximizer loses the game
because from this state the
minimizer must perform its move but
it is impossible, so the minimizer
loses the game but the maximizer
wins
because from this state the
maximizer must perform its move but it
is impossible, so the maximizer loses the
game
0,
MAX
MIN
MAX
MIN
MAX
23/25
Example (3)
Step 4. Propagation of heuristic values from the lowest level up to the root of the game tree
7
6-1 1
5-1-1 0
4-1-1-1 0
3-1-1-1-1 0
2-1-1-1-1-1 0
1
MIN
5-2 1
4-3 1
4-2-1 1
3-2-2 0
3-2-1-1 1
2-2-2-1 0
2-2-1-1-1
1
MAX
3-3-1 1
MIN
MAX
MIN
MAX
24/25
Example (4)
Step 5. Determination of winning paths
7
6-1 1
5-1-1 0
4-1-1-1 0
3-1-1-1-1 0
2-1-1-1-1-1 0
1,
this is the result of the game. So the
maximizer wins. Winning paths are in red
5-2 1
4-3 1
4-2-1 1
3-2-2 0
3-2-1-1 1
2-2-2-1 0
2-2-1-1-1
1
MIN
MAX
3-3-1 1
MIN
MAX
MIN
MAX
25/25