Adversarial Search: game playing – part II
Stuart Russel and Peter Norvig, Artificial Intelligence: A modern
approach, 3rd edition, Pearson Education, 2010
http://aima.cs.berkeley.edu/ – chapter 6
Ernesto Costa e Anabela Simões, Inteligência Artificial:
Fundamentos e Aplicações (2ª edição) FCA, Set. 2008 – chapter 9
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 2
Minimax algorithm
function MINIMAX-DECISION(state) returns an action inputs: state, current state in game
v←MAX-VALUE(state)
return the action in SUCCESSORS(state) with value v
function MIN-VALUE(state) returns a utility value if TERMINAL-TEST(state) then return UTILITY(state) v ← ∞
for a,s in SUCCESSORS(state) do
v ← MIN(v,MAX-VALUE(s))
return v
function MAX-VALUE(state) returns a utility value if TERMINAL-TEST(state) then return UTILITY(state) v ← -∞
for a,s in SUCCESSORS(state) do
v ← MAX(v,MIN-VALUE(s))
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 3
Propriedades do Minimax
Completo
Discriminador
Complexidade temporal: O(b
m)
Complexidade espacial: O(bm)
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 4
Contas para o jogo das damas
Se a dimensão do espaço de procura for de 10^40
nós e conseguirmos gerar um nó em cada terço de
nanosegundo (1/3 *10^-9) quanto tempo será
necessário para gerar todos os nós do espaço?
…
Ora cada século tem 31 536*10^6 segundos
…
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 5
Interrupção da procura
Os recursos limitados fazem com que a terminação seja
impossível!
MinimaxCutoffigual a MinimaxValue excepto em 1. Terminal-Testé substituído por Cutoff-Test
2. Utilityé substituído por Eval (Função de avaliação heurísitica)
Factores a considerar na interrupção da procura
Algum agente ganhou?
Quantas jogadas já foram exploradas?
Quanto tempo está ainda disponível?
Quão promissor é o caminho?
Quão estável é a configuração actual?
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 6
Função de avaliação heurísitica, Eval
Eval: estimativa da função utilidade, Utility
O desempenho depende da qualidade de Eval
Requisitos:
Eval deve ordenar os nós terminais da mesma maneira
do que Utility.
Para os estados não terminais a função de avaliação
deve estar fortemente correlacionada com as possibilidades reais de ganhar.
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 7
Minimax – Jogo do Galo
Eval(p)=
+∞ Se MAX ganha
-∞ Se MIN ganha
(Nº linhas, colunas e diagonais abertas para MAX) – (Nº linhas, colunas e diagonais abertas para MIN)
Caso geral:
• Função de avaliação heurística para a posição p, Eval(p)
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 8
1 Start node X –2 O X 5 – 6 = –1 O X 5 – 5 = 0 O X 5 – 6 = –1 O X 6 – 6 = 0 O X 4 – 6 = –2 X –1 O X 6 – 5 = 1 X O5 – 5 = 0 X O 6 – 5 = 1 O X 5 – 5 = 0 O X 4 – 5 = –1 X 1 X O 5 – 4 = 1 O X 6 – 4 = 2 MAX’s move
© 1998 Morgan Kaufman Publishers
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 9
Minimax – Jogo do Galo
6-4=2 5-4=1 4-5=-1 5-5=0 6-5=1 5-5=0 6-5=1 6-6=0 5-6=-1 5-5=0 5-6=-1 4-6=-2 1 Start node X –2 O X 5 – 6 = –1 O X 5 – 5 = 0 O X 5 – 6 = –1 O X 6 – 6 = 0 O X 4 – 6 = –2 X –1 O X 6 – 5 = 1 X O 5 – 5 = 0 X O 6 – 5 = 1 O X 5 – 5 = 0 O X 4 – 5 = –1 X 1 X O 5 – 4 = 1 O X 6 – 4 = 2 MAX’s move
© 1998 Morgan Kaufman Publishers
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 10
Minimax – Jogo do Galo
6-4=2 5-4=1 4-6=-2 6-6=0 5-6=-1 5-5=0 5-6=-1 4-5=-1 5-5=0 6-5=1 5-5=0 6-5=1 1 -2 -1
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 11
Minimax – Jogo do Galo
O X X 1 O X X 0 O X X 1 O X X 0 O X X O3 – 3 = 0 O X X O 4 – 3 = 1 O X X O 4 – 3 = 1 O O X X4 – 2 = 2 O O X X4 – 2 = 2 O O X X3 – 2 = 1 O X O X5 – 2 = 3 O X O X4 – 2 = 2 O X O X4 – 2 = 2 O O X X 4 – 3 = 1 O O X X 4 – 3 = 1 O O X X 3 – 3 = 0 O X O X 5 – 3 = 2 O X X O3 – 3 = 0 O X O X 4 – 3 = 1 O O X X O X 1 3 – 2 = 1 O O X X 4 – 2 = 2 O O X X 3 – 2 = 1 O X O X 5 – 2 = 3 O X X O3 – 2 = 1 O X X O 4 – 2 = 2 MAX’s move Start node
© 1998 Morgan Kaufman Publishers
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 12
Minimax – Jogo do Galo
O O X X X –∞ 1 O O O X X X3 – 2 = 1 O O O X X X2 – 2 = 0 O O X X X2 – 2 = 0 O O X X O X– ∞ O X O X X –∞ O O X O X X3 – 1 = 2 O X O X O X3 – 1 = 2 O X O X O X2 – 1 = 1 O X O X O X– ∞ O O X X X –∞ O O O X X X3 – 2 = 1 D C B A O O O X X X2 – 2 = 0 O O X O X X3 – 2 = 1 O O X O X X–∞ O X O X X –∞ O X O O X X2 – 1 = 1 O X O X O X2 – 1 = 1 O X O X O X2 – 1 = 1 O X O X O X–∞ O O X X X 1 O O O X X X3 – 1 = 2 O O O X X X2 – 1 = 1 O O X O X X3 – 1 = 2 O O X X O X2 – 1 = 1 O O X X MAX’s move Start node
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 13
Xadrez - Funções de avaliação
Eval(s) = w
1f
1(s) + w
2f
2(s) + … + w
nf
n(s)
Ex: w1= 9 e f1(s)= (número
de rainhas brancas) -(número de rainhas pretos) Outras características possíveis para além da vantagem relativa das peças:
•Estrutura dos peões, •Segurança do rei; •Controlo do centro; •…
26-Feb-16 http://w3.ualg.pt/~jvo/ia 11 – 14
Xadrez - Interrupção da procura
Como se comporta na prática?
100 secs disponíveis e 104nós/sec
=>
10
6nós/jogada
b
m= 10
6, se b=35 então m=4
Jogador de xadrez com 4-ply é um nabo!
4-ply ≈ nabo
8-ply ≈ jogo comum em PC / nível de Mestre
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 15
α-β
α é um valor auxiliar provisório de um nó maximizante β é um valor auxiliar provisório de um nó minimizante
O α de um nó maximizante é igual ao máximo actual dos β dos
sucessores desse nó
O β de um nó minimizante é igual ao mínimo actual dos α dos
sucessores desse nó
Os αα nunca diminuem; só podem crescer Os ββ nunca aumentam; só podem descrescer
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 16
Cortes α-β – Regra 1
A procura pode ser interrompida abaixo de qualquer
nó
minimizante
cujo β seja
menor ou igual
ao α de
qualquer dos seus nós maximizantes antecessores –
independentemente do seu nível
À interrupção chama-se corte α e o valor de β passa
a definitivo (embora possa não ser o valor
verdadeiro é um valor apropriado para calculos
subsequentes)
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 17
α-cut, example of
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 18
Cortes α-β – Regra 2
A procura pode ser interrompida abaixo de
qualquer nó
maximizante
cujo α seja
maior ou
igual
ao β de qualquer dos seus nós
maximizantes
antecessores
–
independentemente do seu nível.
À interrupção chama-se corte β e o valor de α
passa a definitivo (embora possa não ser o valor
verdadeiro é um valor apropriado para calculos
subsequentes)
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 19
MAX
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 3 -3 0 -1 -2 0 1 4 5 1 -1 -1 3 -3 2 -2
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 21
How much do we gain?
Assume a game tree of constant branching factor b
In the worst-case, Minimax examines O(bm) nodes so does α-β
The gain for alpha-beta is maximum when:
The children of a MAX node are ordered in decreasing
backed up values
The children of a MIN node are ordered in increasing backed
up values
Then α-βexamines O(bm/2) nodes (Knuth and Moore, 1975)
(if we knew how to order nodes perfectly, we would not need to search the game tree)
If nodes are ordered at random, then the average number of nodes
examined by alpha-beta is O(b3m/4) (Knuth and Moore, 1975)
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 22
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 23
The α-β strategy
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 24
α-β search – some conclusions
Pruning does not affect final result
A good heuristic ordering of successors improves effectiveness
of pruning
In the best case, i.e., with perfect ordering, time complexity is
O(bm/2) i.e., doubles the solvable tractable depth of search
Example of meta-reasoning (A simple example of the value of
Deterministic games
in practice
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 26
Chinook is invincible!
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 27
Deep Blue II plays at human grandmaster level
n
http://www.research.ibm.com/deepblue/
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 28
Chess: Kasparov vs. Deep Blue
Kasparov 1,6 mts 80 kg 34 years 50 billion neurons 2 pos/sec Extensive Chemical Enormous Height Weight Age Hardware Max Speed Knowledge Power Source Ego Deep Blue 2 mts 1089 kg 4 years 30 IBM RS/6000 + 480 VLSI chess proc.
330,000,000 pos/sec Primitive Electrical None
26-Feb-16 10 – 29
Othello: Murakami vs. Logistello
Takeshi Murakami World Othello Champion
1997: The Logistello crushed Murakami by 6 games to 0
Michael Buro and is program Logistello
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 30
GO
State space: 10
170nodes
Branching factor: about 200
Sequential environment: A move now might make a
difference some dozen moves later
No good evaluation function
Local and global features
26-Feb-16 10 – 31
GO
In 2005: Computer GO is
useless
In 2008, MoGO, a
Monte Carlo Tree search
based
program, won one game against a 5
thdan pro Go
player on a 9x9 board
In 2013, Zen defeated a 9
thdan on a 19x19 board
26-Feb-16 http://w3.ualg.pt/~jvo/ml 10 – 32
26-Feb-16 http://w3.ualg.pt/~jvo/ia 10 – 33
Summary