general reest and game retes - computer science...tic-tac-toe for example, a possible heuristic in...
TRANSCRIPT
General Trees Game Trees
General Trees and Game Trees
ENGI 4892: Data Structures
Andrew Vardy
Faculty of Engineering & Applied ScienceMemorial University of Newfoundland
June 30, 2011
General Trees Game Trees
General Trees
A general tree has no restriction on the number of children per
node. We can represent a node in a general tree by using two
pointers in addition to the item �eld:
childPtr: A pointer to the �rst of the node's children
Set to NULL if the node has no children
siblingPtr: A pointer to the next sibling (the next child of this
node's parent)
Set to NULL if the node is the last (rightmost or only) of itsparent's children
General Trees Game Trees
General Trees
A general tree has no restriction on the number of children per
node. We can represent a node in a general tree by using two
pointers in addition to the item �eld:
childPtr: A pointer to the �rst of the node's children
Set to NULL if the node has no children
siblingPtr: A pointer to the next sibling (the next child of this
node's parent)
Set to NULL if the node is the last (rightmost or only) of itsparent's children
General Trees Game Trees
General Trees
A general tree has no restriction on the number of children per
node. We can represent a node in a general tree by using two
pointers in addition to the item �eld:
childPtr: A pointer to the �rst of the node's children
Set to NULL if the node has no children
siblingPtr: A pointer to the next sibling (the next child of this
node's parent)
Set to NULL if the node is the last (rightmost or only) of itsparent's children
General Trees Game Trees
For example, the tree on the left is represented by the structure on
the right:
General Trees Game Trees
Traversals
Pre- and postorder traversals are implemented in a slightly di�erent
manner than for binary trees. A preorder traversal appears quite
similar.
void preOrder ( Node ∗node ) {if ( node != NULL ) {
// V i s i t t h i s node ( p r i n t i t )cout << node−>item << endl ;preOrder ( node−>childPtr ) ;preOrder ( node−>siblingPtr ) ;
}}
General Trees Game Trees
Traversals
Pre- and postorder traversals are implemented in a slightly di�erent
manner than for binary trees. A preorder traversal appears quite
similar.
void preOrder ( Node ∗node ) {if ( node != NULL ) {
// V i s i t t h i s node ( p r i n t i t )cout << node−>item << endl ;preOrder ( node−>childPtr ) ;preOrder ( node−>siblingPtr ) ;
}}
General Trees Game Trees
For a postorder traversal of a binary tree, two recursive calls were
made before visiting the node itself (one for each subtree).
However, in the code below the �rst recursive call actually traverses
all of the node's subtrees. The last recursive call traverses the
subtree rooted at this node's sibling�this occurs last for both
preorder and postorder traversals.
void postOrder ( Node ∗node ) {if ( node != NULL ) {
postOrder ( node−>childPtr ) ;// V i s i t t h i s node ( p r i n t i t )cout << node−>item << endl ;postOrder ( node−>siblingPtr ) ;
}}
General Trees Game Trees
For a postorder traversal of a binary tree, two recursive calls were
made before visiting the node itself (one for each subtree).
However, in the code below the �rst recursive call actually traverses
all of the node's subtrees. The last recursive call traverses the
subtree rooted at this node's sibling�this occurs last for both
preorder and postorder traversals.
void postOrder ( Node ∗node ) {if ( node != NULL ) {
postOrder ( node−>childPtr ) ;// V i s i t t h i s node ( p r i n t i t )cout << node−>item << endl ;postOrder ( node−>siblingPtr ) ;
}}
General Trees Game Trees
Game Trees
Game playing presents excellent examples of the application of
trees. Board games such as chess, checkers, go, and connect-4 can
be played by computers at or above the level of the best human
players. How does this work?
The computer "looks ahead" some number of moves. But how
can it look beyond one move? It would need to know what the
human player was thinking!
The answer is to assume that the human player is using the
same method of reasoning as the computer. This allows the
computer to guess the human's move and look further ahead
based on this.
General Trees Game Trees
Game Trees
Game playing presents excellent examples of the application of
trees. Board games such as chess, checkers, go, and connect-4 can
be played by computers at or above the level of the best human
players. How does this work?
The computer "looks ahead" some number of moves. But how
can it look beyond one move? It would need to know what the
human player was thinking!
The answer is to assume that the human player is using the
same method of reasoning as the computer. This allows the
computer to guess the human's move and look further ahead
based on this.
General Trees Game Trees
Game Trees
Game playing presents excellent examples of the application of
trees. Board games such as chess, checkers, go, and connect-4 can
be played by computers at or above the level of the best human
players. How does this work?
The computer "looks ahead" some number of moves. But how
can it look beyond one move? It would need to know what the
human player was thinking!
The answer is to assume that the human player is using the
same method of reasoning as the computer. This allows the
computer to guess the human's move and look further ahead
based on this.
General Trees Game Trees
Example: Grundy's Game
The game of nim begins with a number of tokens placed in a pile.
The two players take turns dividing the pile into smaller piles under
the following condition:
A pile must be split into two nonempty piles of di�erent sizes.
The �rst player who is left without a legal move loses the
game.
General Trees Game Trees
Example: Grundy's Game
The game of nim begins with a number of tokens placed in a pile.
The two players take turns dividing the pile into smaller piles under
the following condition:
A pile must be split into two nonempty piles of di�erent sizes.
The �rst player who is left without a legal move loses the
game.
General Trees Game Trees
Example: Grundy's Game
The game of nim begins with a number of tokens placed in a pile.
The two players take turns dividing the pile into smaller piles under
the following condition:
A pile must be split into two nonempty piles of di�erent sizes.
The �rst player who is left without a legal move loses the
game.
The following �gure shows the state space for Grundy's Game
beginning with one pile of 6 items. All possible moves for both
players are shown.
Next, we label each level of this tree with the names of our two
players. It will be convenient to refer to the computer player as
MAX and the human player as MIN:
If we have a leaf node on a MAX level then we know MAX is going
to lose if it gets in that state. Therefore, we set the value �eld of
such nodes to 0. Similarly, if we have a leaf node on a MIN level
then we know MAX is going to win! Set the value to 1.
Next, we label each level of this tree with the names of our two
players. It will be convenient to refer to the computer player as
MAX and the human player as MIN:
If we have a leaf node on a MAX level then we know MAX is going
to lose if it gets in that state. Therefore, we set the value �eld of
such nodes to 0. Similarly, if we have a leaf node on a MIN level
then we know MAX is going to win! Set the value to 1.
General Trees Game Trees
The Minimax Algorithm
Minimax Algorithm:
Do a postorder traversal of the tree:
At leaf nodes, evaluate game state for player MAX and store innode's value �eldAt non-leaf MAX nodes, set node's value to the maximum ofall child value'sAt non-leaf MIN nodes, set node's value to the minimum of allchild value's
How do we choose the best top-level move? It will be the one with
the highest value.
General Trees Game Trees
The Minimax Algorithm
Minimax Algorithm:
Do a postorder traversal of the tree:
At leaf nodes, evaluate game state for player MAX and store innode's value �eldAt non-leaf MAX nodes, set node's value to the maximum ofall child value'sAt non-leaf MIN nodes, set node's value to the minimum of allchild value's
How do we choose the best top-level move? It will be the one with
the highest value.
General Trees Game Trees
Application of Minimax to Grundy's Game
To choose its move the computer should take the move with the
maximum value. For the game above, MAX can force a win by
splitting the pile of 6 into two piles of 4 and 2. If MAX were to
split the pile into 5 and 1 then MIN could force a win.
General Trees Game Trees
Heuristic Evaluation Function
Can we apply this strategy to games such as chess and checkers?
Not directly. The game trees for these games would be too large to
search exhaustively. Instead we limit the tree size to some �xed
depth. We then use a heuristic evaluation function to evaluate
partially complete game states.
General Trees Game Trees
Heuristic Evaluation Function
Can we apply this strategy to games such as chess and checkers?
Not directly. The game trees for these games would be too large to
search exhaustively. Instead we limit the tree size to some �xed
depth. We then use a heuristic evaluation function to evaluate
partially complete game states.
Tic-Tac-Toe
For example, a possible heuristic in tic-tac-toe is the number of
possibly winning lines for MAX, minus the number for MIN.
A similar heuristic can be used for connect-4. See the code in class
Evaluator of assignment 5 for details.
Given such a heuristic we can create a game tree of some �xed
depth and then evaluate its leaves based on this heuristic. Below is
a game tree of height 3 for tic-tac toe.
What about winning or losing game states? Such states are
represented by leaves and take on extremely high values (e.g.
in�nity, 1000) for winning states, or extremely low values for losing
states (e.g. -in�nity, -1000).