-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_eval.coffee
43 lines (37 loc) · 1.2 KB
/
pattern_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
fs = require 'fs'
{ patterns, n_indexes, N_PHASES, N_MOVES_PER_PHASE, SCORE_MULT } = require './pattern'
{ int } = require './util'
{ LOG_MULT } = require './logutil'
module.exports = (arg, invert=false) ->
if typeof arg == 'string'
weights = JSON.parse fs.readFileSync arg
else
weights = arg
single_tbl = []
offsets = []
for phase in [0...N_PHASES]
tbl = []
for p in patterns
tbl = tbl.concat(weights[p.name][phase].slice(1).reverse().map((x) -> -x))
tbl = tbl.concat(weights[p.name][phase])
if weights.clip == 16
single_tbl[phase] = new Int16Array tbl
else
single_tbl[phase] = tbl
offset = weights.meta[phase].offset or 0
offset *= if weights.meta[phase].logistic then LOG_MULT else SCORE_MULT
offsets[phase] = Math.round(offset)
pattern_eval = (board, me) ->
phase = int((board.n_discs - 5) / N_MOVES_PER_PHASE)
tbl = single_tbl[phase]
sum = offsets[phase]
for i in [0...n_indexes]
sum += tbl[board.indexes[i]]
sum * me
if invert
pattern_eval = do ->
orig = pattern_eval
(board, me) -> -orig(board, me)
pattern_eval.logistic = weights.meta[0].logistic
pattern_eval.weights = weights
pattern_eval