-
Notifications
You must be signed in to change notification settings - Fork 0
/
minmax.coffee
254 lines (224 loc) · 6.66 KB
/
minmax.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
{ EMPTY, pos_from_str, pos_to_str, Board } = require './board'
{ PatternBoard, SCORE_MULT } = require './pattern'
solve = require './endgame'
{ INFINITY, format_eval } = require './util'
ext = require './ext'
corner_zone = do ->
b = new Board
b.load '''
- X - - - - X -
X X - - - - X X
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
X X - - - - X X
- X - - - - X -
'''
b
defaults =
evaluate: null
max_depth: 60
max_leafs: 3e6
verbose: true
invert: false
cq: false
board_class: PatternBoard
cache_size: 300000
zws: true
shuffle: true
use_mtdf: false
cache_depth: 5
ext: true
module.exports = (options={}) ->
{
evaluate
max_depth
max_leafs
verbose
invert
cq
board_class
cache_size
zws
shuffle
use_mtdf
cache_depth
} = opt = {defaults..., options...}
if invert
orig_evaluate = evaluate
evaluate = (board, me) -> -orig_evaluate(board, me)
n_leafs = 0
simple_minmax = (board, me, lower, upper, pass, depth) ->
if depth <= 0
n_leafs++
return evaluate(board, me)
any_moves = false
max = -INFINITY
board.each_empty (pos) ->
flips = board.move(me, pos)
if flips.length
any_moves = true
next_depth = depth - 1
if cq and next_depth == 0 and corner_zone.get(pos)
next_depth = 1
score = -simple_minmax(board, -me, -upper, -lower, 0, next_depth)
board.undo(me, pos, flips)
if score > max
max = score
if score > lower
lower = score
if score >= upper
return false # stop iteration
if any_moves
max
else
if pass
board.outcome(me) * SCORE_MULT
else
-simple_minmax(board, -me, -upper, -lower, 1, depth-1)
minmax = (board, me, lower, upper, pass, depth) ->
if depth <= 4
return simple_minmax(board, me, lower, upper, pass, depth)
moves = []
any_moves = false
board.each_empty (pos) ->
flips = board.move(me, pos)
if flips.length
any_moves = true
score = -simple_minmax(board, -me, -INFINITY, INFINITY, 0, 1)
board.undo(me, pos, flips)
moves.push [pos, score]
unless any_moves
if pass
return evaluate(board, me)
else
return -minmax(board, -me, -upper, -lower, 1, depth-1)
moves.sort (a, b) -> (b[1] - a[1])
max = -INFINITY
for [pos] in moves
flips = board.move(me, pos)
next_depth = depth - 1
if cq and next_depth == 0 and corner_zone.get(pos)
next_depth = 1
if zws and depth >= 4 and lower != -INFINITY and upper - lower > 1
score = -minmax(board, -me, -(lower+1), -lower, 0, next_depth)
if score > lower and score < upper
score = -minmax(board, -me, -upper, -score, 0, next_depth)
else
score = -minmax(board, -me, -upper, -lower, 0, next_depth)
board.undo(me, pos, flips)
if score > max
max = score
if score > lower
lower = score
break if score >= upper
break if n_leafs >= max_leafs
max
if cache_size
cache = require('./cache')(cache_size)
cached_minmax = (f) ->
(board, me, lower, upper, pass, depth) ->
if depth < cache_depth
return f board, me, lower, upper, pass, depth
value = cache.get(board, me, depth, lower, upper)
if value != null
return value
value = f board, me, lower, upper, pass, depth
cache.set board, me, depth, lower, upper, value
value
simple_minmax = cached_minmax simple_minmax
minmax = cached_minmax minmax
mtdf = (board, me, lower, upper, guess, depth) ->
l = lower
u = upper
guess = l if guess < l
guess = u if guess > u
while l < u
#process.stdout.write "[#{l},#{u}]" if verbose
#process.stdout.write "(#{guess})" if verbose
beta = if guess == l then l+1 else guess
value = minmax(board, me, beta-1, beta, 0, depth)
if value >= beta
if value >= upper
return value
l = value
else
if value <= lower
return value
u = value
guess = value
value
native_minmax =
opt.ext and
ext.is_enabled and
evaluate.weights and
require('./ext/minimax')
weights: evaluate.weights
verbose: verbose
max_depth: max_depth
max_nodes: max_leafs
inverted: invert
coffee_minmax = (board, me, moves=null) ->
board = new board_class board
moves or= board.list_moves(me)
move_scores = {}
if shuffle
moves.forEach (pos) -> move_scores[pos] = Math.random()
else
moves.forEach (pos) -> move_scores[pos] = 0
left = board.count(EMPTY)
first_depth = (max_depth & 1) or 2
guess = 0
n_leafs = 0
for depth in [first_depth..max_depth] by (if use_mtdf then 2 else 1)
if depth > left
break
process.stdout.write "depth=#{depth}: " if verbose
moves.sort (a, b) -> move_scores[b] - move_scores[a]
max = -INFINITY
best = 0
for pos in moves
flips = board.move(me, pos)
if flips.length
process.stdout.write "#{pos_to_str(pos)}" if verbose
if use_mtdf
score = -mtdf(board, -me, -INFINITY, -max, -guess, depth-1)
else
if zws and max != -INFINITY
score = -minmax(board, -me, -(max+1), -max, 0, depth-1)
if score > max
process.stdout.write(':\b') if verbose
score = -minmax(board, -me, -INFINITY, -score, 0, depth-1)
else
if zws
process.stdout.write(':\b') if verbose
score = -minmax(board, -me, -INFINITY, -max, 0, depth-1)
board.undo(me, pos, flips)
if score > max
if verbose
s = format_eval(score, evaluate.logistic)
process.stdout.write ":#{s} "
max = score
best = pos
else
if depth > 1
score = -99999
process.stdout.write " " if verbose
else
if verbose
s = format_eval(score, evaluate.logistic)
process.stdout.write ":#{s} "
move_scores[pos] *= .00001
move_scores[pos] += score
guess = max
break if n_leafs >= max_leafs
process.stdout.write '\n' if verbose
break if n_leafs >= max_leafs
cache.stats() if verbose and cache_size
{value: max, move: best, solved: null}
minmax_main = native_minmax or coffee_minmax
minmax_main.minmax = minmax
minmax_main.simple_minmax = simple_minmax
minmax_main
module.exports.defaults = defaults