-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfplay-op
executable file
·169 lines (161 loc) · 4.17 KB
/
selfplay-op
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
#!/usr/bin/env coffee
require './rejection_handler'
fs = require 'fs'
{ Board, BLACK, pos_to_str, pos_array_to_str, pos_array_from_str } =
require './board'
make_player = require './player'
uct = require './uct'
pattern_eval = require('./pattern_eval')('weights.json')
Book = require './book'
{ watch_file } = require './util'
minmax = require './minmax'
Player = require './player'
op_eval = require './op_eval'
argv = require 'yargs'
.usage "Usage: #{process.argv[1]} [options...]"
.options
o:
alias: 'opening'
desc: 'Opening'
type: 'string'
default: 'F5'
requiresArg: true
n:
desc: 'Number of games to play'
type: 'number'
requiresArg: true
B:
alias: 'book'
desc: 'Database file to save games'
type: 'string'
default: 'book.db'
requiresArg: true
s:
alias: 'search'
desc: 'Number of UCT searches per move'
type: 'number'
default: uct.defaults.max_search
requiresArg: true
S:
alias: 'scope'
desc: 'UCT constant'
type: 'number'
default: .5
requiresArg: true
w:
alias: 'wld'
desc: 'Depth of win-loss-draw endgame search'
default: make_player.defaults.solve_wld
requiresArg: true
f:
alias: 'full'
desc: 'Depth of full endgame search'
default: make_player.defaults.solve_full
requiresArg: true
u:
alias: 'uct'
desc: 'Use UCT to evaluate leafs'
default: false
type: 'boolean'
U:
desc: 'Number of evaluation UCT search'
type: 'number'
default: op_eval.defaults.max_search
requiresArg: true
D:
alias: 'depth'
desc: 'Depth of minimax evaluation of leafs'
default: op_eval.defaults.depth
requiresArg: true
W:
desc: 'Depth of evaluation win-loss-draw solve'
type: 'number'
default: op_eval.defaults.wld
requiresArg: true
F:
desc: 'Depth of evaluation full solve'
type: 'number'
default: op_eval.defaults.full
requiresArg: true
a:
alias: 'auto_opening'
desc: 'Automaticaly select opening according to balance'
type: 'boolean'
default: false
watch:
desc: 'Watch the file and exit when it changes'
default: 'weights.json'
requiresArg: true
v:
alias: 'verbose'
type: 'boolean'
default: false
h:
alias: 'help'
.version false
.strict()
.argv
player = make_player
book: null
strategy: uct
evaluate: pattern_eval
max_search: argv.s
verbose: false
verbose: false
solve_wld: argv.w
solve_full: argv.f
endgame_eval: pattern_eval
evaluate = op_eval
uct: argv.uct
max_search: argv.U
depth: argv.depth
wld: argv.W
full: argv.F
play = (player, opening) ->
board = new Board
moves = []
for {move, turn} in opening.moves
flips = board.move turn, move
throw new Error unless flips.length
moves.push {move, turn}
process.stdout.write pos_to_str(move, turn)
console.log ':', Math.round(opening.value)
console.log board.dump true
turn = opening.turn
loop
unless board.any_moves turn
turn = -turn
unless board.any_moves turn
break
{move, solved} = player board, turn
flips = board.move turn, move
console.assert flips.length
moves.push {move, turn, solved}
process.stdout.write pos_to_str(move, turn)
turn = -turn
outcome = board.outcome()
process.stdout.write " #{outcome}\n"
{moves, outcome}
do ->
book = new Book argv.book, evaluate: evaluate
book.init()
n_games = 0
reload = if argv.watch then watch_file(argv.watch) else -> false
op = pos_array_from_str(argv.opening)
until reload()
if argv.auto_opening
balance = book.sum_nodes_outcome()
if balance >= 0
op = pos_array_from_str('f5d6')
else if balance >= -5000
op = pos_array_from_str('f5f6')
else
op = pos_array_from_str('f5f4')
opening = book.find_opening(argv.scope, op)
unless book.has_game(opening.moves)
{moves, outcome} = play(player, opening)
moves_str = pos_array_to_str(moves)
book.add_game moves
n_games++
book.extend argv.scope, op
break if n_games >= argv.n