-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_eval.coffee
96 lines (83 loc) · 2.49 KB
/
op_eval.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
Player = require './player'
defaults =
uct: false
max_search: 3000
depth: 13
wld: 22
full: 20
module.exports = (options={}) ->
opt = {defaults..., options...}
pattern_eval = require('./pattern_eval')('weights.json')
if opt.uct
do_evaluate = Player
book: null
strategy: require('./uct')
verbose: false
max_search: opt.max_search
evaluate: pattern_eval
by_value: true
solve_wld: opt.wld - 1
solve_full: opt.full - 1
verbose: false
endgame_eval: pattern_eval
(board, turn, moves) ->
max = -Infinity
for pos in moves or board.list_moves(turn)
flips = board.move(turn, pos)
throw new Error unless flips.length
if board.any_moves(-turn)
{ value, move, solved } = do_evaluate(board, -turn)
value = 0 unless value?
value = -value
else
{ value, move, solved } = do_evaluate(board, turn)
value = 0 unless value?
board.undo turn, pos, flips
if value > max
max = value
best = pos
best_move = move
if not solved and best_move > 0
flips = board.move(turn, best)
throw new Error unless flips.length
if board.any_moves(-turn)
{ value } = do_evaluate(board, -turn, [best_move])
value = 0 unless value?
value = -value
else
{ value } = do_evaluate(board, turn, [best_move])
value = 0 unless value?
board.undo turn, best, flips
max = Math.round((max + value) / 2)
{ value: max, move: best, solved }
else
minmax = require './minmax'
evaluator0 = Player
book: null
strategy: minmax
verbose: false
max_depth: opt.depth
evaluate: pattern_eval
solve_wld: opt.wld
solve_full: opt.full
verbose: false
endgame_eval: pattern_eval
evaluator1 = Player
book: null
strategy: minmax
verbose: false
max_depth: opt.depth - 1
evaluate: pattern_eval
solve_wld: opt.wld
solve_full: opt.full
verbose: false
endgame_eval: pattern_eval
(board, me, moves) ->
{ value, move, solved } = evaluator0(board, me, moves)
value = 0 unless isFinite(value)
unless solved
ev1 = evaluator1(board, me, [move])
ev1.value = 0 unless isFinite(ev1.value)
value = Math.round((value + ev1.value) / 2)
{ value, move, solved }
module.exports.defaults = defaults