69
Juegos Nelson Becerra Correa Fundación FABBECOR-ONG

Juegos minimax AlfaBeta

Embed Size (px)

DESCRIPTION

Descripcion minimax alfabeta .

Citation preview

Page 1: Juegos minimax AlfaBeta

Juegos

Nelson Becerra Correa

Fundación FABBECOR-ONG

Page 2: Juegos minimax AlfaBeta
Page 3: Juegos minimax AlfaBeta

Bibliografía• Conferencia : Miguel Illescas, Gran Maestro Internacional.

Kasparov, Deep Blue y la mente del ajedrecista.

• http://www.ocf.berkeley.edu/~yosenl/extras/alphabeta/alphabeta

• Jorge Egger M. Desarrollo de la maestría ajedrecística computacional. Utilizando recursos restringidos. Universidad de chile. Facultad de ciencias físicas y matemáticas. Departamento de ciencias de la computación, Santiago de chile, Julio 2003.

• Técnicas de inteligencia artificial en Juego. Nelson Becerra Correa, Fondo de publicaciones Universidad Distrital (FJC), Año 2003.

• w.acm.uiuc.edu/sigart/docs/MinimaxPresentation.ppt

Page 4: Juegos minimax AlfaBeta
Page 5: Juegos minimax AlfaBeta

TEORIA DE JUEGOS Fué creada para solucionar problemas

económicos. Los pioneros de esta teoría son John Von Newman y Oskar Morgestern.

En la actualidad se aplica a la Psicología, la administarción , las ciencias políticas etc.

Page 6: Juegos minimax AlfaBeta

Clasificación de los juegos

NUMERO PARTICIPANTES

•Unipersonales•Bipersonales•N-personas N>2

NUMEROESTRATEGIAS

•Finito (estrategias finitas)•Infinitas

DE ACUERDO A LA

INFORMACION

•Información perfecta (Secuenciales)

•Información Imperfecta (Yan ken Po) (Dilema pri)

DE ACUERDO A LA SUMA

•Juegos de suma cero (ajedrez, oso, go,mankala)

•Juegos de suma distinta de cero (F)

Page 7: Juegos minimax AlfaBeta
Page 8: Juegos minimax AlfaBeta
Page 9: Juegos minimax AlfaBeta

Como juegan las maquinas

Page 10: Juegos minimax AlfaBeta

Como juegan las maquinas

Page 11: Juegos minimax AlfaBeta

Como juegan las maquinas

Page 12: Juegos minimax AlfaBeta

Como juegan las maquinas

Page 13: Juegos minimax AlfaBeta

Como juegan las maquinas

Page 14: Juegos minimax AlfaBeta

DEEP-BLUE potencia de calculo

Page 15: Juegos minimax AlfaBeta

Función de evaluación

Page 16: Juegos minimax AlfaBeta

Función de evaluación

Page 17: Juegos minimax AlfaBeta

Posición que un programa sin Tablas de Transposición no puede solucionar.Las Blancas ganan mediante 1.Rb1!! Rb7 2.Rc1!!

1. 30 movimientos sería necesaria, y eso tomaría miles de horas de tiempo de CPU.

2. Con tablas Hash 6 movimientos, puesto que existen 130 diferentes tipos de posiciones obtenibles desde la inicial.

Page 18: Juegos minimax AlfaBeta
Page 19: Juegos minimax AlfaBeta
Page 20: Juegos minimax AlfaBeta

Basic Idea

• Search problem– Searching a tree of the possible moves in order

to find the move that produces the best result– Depth First Search algorithm

• Assume the opponent is also playing optimally– Try to guarantee a win anyway!

Page 21: Juegos minimax AlfaBeta

Árbol de variantes

1. En los inicios de 1970 la búsqueda en árboles de variantes alcanzaba la cantidad aproximada de 200 posiciones por segundo.

2. En año 2003, DEEP BLUE busca en 2.000.000 de posiciones por segundo.

3. Los mejores programas logran examinar todas las secuencias de movimientos con una profundidad de 8 a 10 movidas en el árbol (4 a 5 jugadas por bando).

Page 22: Juegos minimax AlfaBeta
Page 23: Juegos minimax AlfaBeta

Required Pieces for Minimax

• An initial state– The positions of all the pieces– Whose turn it is

• Operators– Legal moves the player can make

• Terminal Test– Determines if a state is a final state

• Utility Function

Page 24: Juegos minimax AlfaBeta

Utility Function• Gives the utility of a game state

– utility(State)

• Examples– -1, 0, and +1, for Player 1 loses, draw, Player 1 wins,

respectively– Difference between the point totals for the two players– Weighted sum of factors (e.g. Chess)

• utility(S) = w1f

1(S) + w

2f

2(S) + ... + w

nf

n(S)

– f1(S) = (Number of white queens) – (Number of black queens), w

1 = 9

– f2(S) = (Number of white rooks) – (Number of black rooks), w

2 =

5– ...

Page 25: Juegos minimax AlfaBeta

Two Agents• MAX

– Wants to maximize the result of the utility function

• MIN– Wants to minimize the result of the evaluation

function

Page 26: Juegos minimax AlfaBeta
Page 27: Juegos minimax AlfaBeta

… MINIMAXEjemplo de definición de función evaluadora:• Si p no es posición ganadora para ningún ganado, entonces:

– e(p)= (el nº de filas, columnas y diagonales completas que todavía estan libres para MAX) – (nº de filas, columnas, diagonales completas que todavía estan libres para MIN)

• Si p es una posición ganadora para MAX, entonces– e(p)= ∞ (donde ∞ indica un número positivo muy grande)

• Si p es una posición ganadora para MIN, entonces– e(p)= -∞

Page 28: Juegos minimax AlfaBeta

… MINIMAXOX

Se tiene que e(p)=6 – 4 = 2

http://www.ocf.berkeley.edu/~yosenl/extras/alphabeta/alphabeta.html

Page 29: Juegos minimax AlfaBeta

Basic Algorithm

Page 30: Juegos minimax AlfaBeta

ALGORITMOS DE JUEGOS

• Algoritmos de primera generación

– se caracterizan por confiar en la capacidad bruta de las maquinas

• Algoritmos de segunda generación:

– Surge la inquietud de que mientras la maquina examina miles de posiciones su oponente humano solo examina algunas de ellas

Page 31: Juegos minimax AlfaBeta

ALGORITMOS DE PRIMERA GENERACION

• MINIMAX

• NEGAMAX

• ALFA/BETA

• FALFA/BETA

• LALFA/BETA

• PALFA/BETA

• SCOUT

Page 32: Juegos minimax AlfaBeta

MINIMAX NEGAMAX

ALFA/BETA FALFA/BETA

Page 33: Juegos minimax AlfaBeta

LALFA/BETA PALFA/BETA

SCOUT

Page 34: Juegos minimax AlfaBeta

Max

Min

Max

Min

Starting node and labelsMiniMax Search

Page 35: Juegos minimax AlfaBeta

Max

Min

Max

Min

Continue expand the search space

Page 36: Juegos minimax AlfaBeta

Max

Min

Max

Min

Continue expand the search space

Page 37: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3 5 09 7 4

Expand the search space down3-ply

Page 38: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3 5 09 7 4

Propagate the leaf values backwardPick Max values at Max Level

9 03 77

Page 39: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3 5 09 7 4

Continue propagate the values backwardPick Min values at Min Level

9 03 77

3 0

Page 40: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3 5 09 7 4

Continue propagate the values backwardPick Max values at Max Level

9 03 77

3 0

3

Page 41: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3 5 09 7 4

The move picked by Max

9 03 77

3 0

3

Page 42: Juegos minimax AlfaBeta

Example MINIMAX

• Coins game– There is a stack of N coins– In turn, players take 1, 2, or 3 coins from the

stack– The player who takes the last coin loses

Page 43: Juegos minimax AlfaBeta

Coins Game: Formal Definition

• Initial State:

The number of coins in the stack• Operators:

1. Remove one coin

2. Remove two coins

3. Remove three coins

• Terminal Test:

There are no coins left on the stack• Utility Function: F(S)

– F(S) = 1 if MAX wins, 0 if MIN wins

Page 44: Juegos minimax AlfaBeta

N = 4K =

N = 3K =

N = 2K =

N = 1K =

N = 0K =

N = 1K =

N = 2K =

N = 0K =

N = 0K =

N = 1K =

N = 0K =

N = 1K =

N = 0K =

N = 0K =

12

3

3 21 2 1 1

1211

N = 0K =

1 F(S)=0

F(S)=0

F(S)=0

F(S)=1

F(S)=1

F(S)=1

F(S)=1

MAXMIN

Page 45: Juegos minimax AlfaBeta

N = 4K = 1

N = 3K = 0

N = 2K = 0

N = 1K = 1

N = 0K = 1

N = 1K = 0

N = 2K = 1

N = 0K = 1

N = 0K = 1

N = 1K = 0

N = 0K = 0

N = 1K = 1

N = 0K = 0

N = 0K = 0

12

3

3 21

2 1 1

1211

N = 0K = 1

1 F(S)=0F(S)=0 F(S)=0

F(S)=1

F(S)=1

FFS(51791527S)=1

F(S)=1

MAXMIN

Solution

F(S)=1

Page 46: Juegos minimax AlfaBeta

Analysis• Max Depth: 5• Branch factor: 3• Number of nodes: 15• Even with this trivial example, you can see that

these trees can get very big– Generally, there are O(bd) nodes to search for

• Branch factor b: maximum number of moves from each node• Depth d: maximum depth of the tree

– Exponential time to run the algorithm!– How can we make it faster?

Page 47: Juegos minimax AlfaBeta
Page 48: Juegos minimax AlfaBeta

Alpha-Beta Pruning

• Main idea: Avoid processing subtrees that have no effect on the result

• Two new parameters– α: The best value for MAX seen so far– β: The best value for MIN seen so far

• α is used in MIN nodes, and is assigned in MAX nodes

• β is used in MAX nodes, and is assigned in MIN nodes

Page 49: Juegos minimax AlfaBeta

Alpha-Beta Pruning

• MAX (Not at level 0)– If a subtree is found with a value k greater than

the value of β, then we do not need to continue searching subtrees• MAX can do at least as good as k in this node, so MIN

would never choose to go here!

• MIN– If a subtree is found with a value k less than the

value of α, then we do not need to continue searching subtrees• MIN can do at least as good as k in this node, so MAX

would never choose to go here!

Page 50: Juegos minimax AlfaBeta

Max

Min

Max

Min

Starting node and labelsAlpha-Beta Prune

Page 51: Juegos minimax AlfaBeta

Max

Min

Max

Min

Perform a DFS

Page 52: Juegos minimax AlfaBeta

Max

Min

Max

Min

Continue the DFS

Page 53: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

Until reach the depth we want, i.e., 3-ply in this case

Page 54: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

Propagate leaf values backward

Page 55: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

Page 56: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3

Page 57: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3 Alpha Node

Beta Node

Page 58: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3 Alpha

Beta

5

New DFS path Ended with 5

Page 59: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3 Alpha

Beta

5

5

Propagate backward

Page 60: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3 Alpha

Beta

5

5

Because 3 < 5

The branch is pruned.

Beta-Prune

Page 61: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3 Alpha

Beta

5

5

New 3-ply DFSEnded with 0

0

Page 62: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3 Alpha

Beta

5

5

0

0

Propagate backward

Page 63: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3 Alpha

Beta

5

5

0

0

Propagate backward

0

Page 64: Juegos minimax AlfaBeta

Max

Min

Max

Min2 3

3

3

3 Alpha

Beta

5

5

0

0

The branch is cut offBecause 3 > 0

Alpha Prune

0

Page 65: Juegos minimax AlfaBeta

Algorithm

Page 66: Juegos minimax AlfaBeta

N = 4K =

N = 3K =

N = 2K =

N = 1K =

N = 0K =

N = 1K =

N = 2K =

N = 0K =

N = 0K =

N = 1K =

N = 3K =

N = 1K =

N = 0K =

N = 0K =

12

3

3 21

2 1 1

1211

N = 0K =

1 F(S)=0F(S)=0 F(S)=0

F(S)=1

F(S)=1

F(S)=1 F(S)=1

MAXMIN

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

α =β =

Page 67: Juegos minimax AlfaBeta

N = 4K = 0 1

N = 3K = 1 0

N = 2K = 1 0

N = 1K = 1

N = 0K = 1

N = 1K = 0

N = 2K = 1

N = 0K = 1

N = 0K = 1

N = 1K =

N = 3K = 0

N = 1K = 1

N = 0K =

N = 0K = 0

12

3

3 21

2 1 1

1211

N = 0K = 1

1 F(S)=0F(S)=0 F(S)=0

F(S)=1

F(S)=1

F(S)=1 F(S)=1

MAXMIN

α =β = 1 0

α = 0β =

α = 0β = 1 0

α =β = 0

α = 1β =

α = 0β = 0

α = 0β = 0

α = 0β = 1

α = 0β =

α = 0β = 0

α = 0β =

α = 1β = 0

α =β =

α = 1β = 0

α = 0β = 1

Page 68: Juegos minimax AlfaBeta
Page 69: Juegos minimax AlfaBeta

Conclusion

• Minimax finds optimal play for deterministic, fully observable, two-player games

• Alpha-Beta reduction makes it faster