-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfplay-rnd
executable file
·210 lines (193 loc) · 5.16 KB
/
selfplay-rnd
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
#!/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'
Player = require './player'
uct = require './uct'
pattern_eval = require('./pattern_eval')('weights.json')
Book = require './book'
{ int, watch_file } = require './util'
ext = require './ext'
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
R:
alias: 'random-play'
desc: 'Random play mode'
type: 'boolean'
default: false
r:
alias: 'randomness'
desc: 'Initial randomness'
type: 'number'
default: 0.1
requiresArg: true
min_col:
desc: 'Minimum collision rate'
type: 'number'
default: 0.1
requiresArg: true
max_col:
desc: 'Maximum collision rate'
type: 'number'
default: 0.2
requiresArg: true
D:
desc: 'Collision average duration'
type: 'number'
default: 20
requiresArg: true
a:
alias: 'auto_opening'
desc: 'Automaticaly select opening according to balance'
type: 'boolean'
default: false
b:
alias: 'balance'
desc: 'Tolerance of outcome/win-loss balance'
type: 'number'
default: 1000
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
requireArg: true
w:
alias: 'wld'
desc: 'Depth of win-loss-draw endgame search'
type: 'number'
default: Player.defaults.solve_wld
requireArg: true
f:
alias: 'full'
desc: 'Depth of full endgame search'
type: 'number'
default: Player.defaults.solve_full
requireArg: true
watch:
desc: 'Watch the file and exit when it changes'
default: 'weights.json'
type: 'string'
requireArg: true
wlb:
desc: 'Use win-loss balance instead of outcome'
type: 'boolean'
default: false
v:
alias: 'verbose'
default: false
type: 'boolean'
h:
alias: 'help'
.version false
.strict()
.argv
make_player = (randomness) ->
Player
book: null
strategy: uct
evaluate: if argv.R then (-> Math.random()) else pattern_eval
max_search: if argv.R then 1 else argv.s
verbose: false
random: randomness
verbose: false
solve_wld: argv.w
solve_full: argv.f
endgame_eval: pattern_eval
play = (player, opening) ->
board = new Board
turn = BLACK
moves = []
for move in pos_array_from_str(opening)
flips = board.move turn, move
throw new Error unless flips.length
moves.push {move, turn}
process.stdout.write pos_to_str(move, turn)
turn = -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
book.init()
randomness = argv.randomness
player = make_player(randomness)
n_col = 0
n_play = 0
n_skip = 0
n_save = 0
avg_col = (argv.min_col + argv.max_col) / 2
reload = if argv.watch then watch_file(argv.watch) else -> false
games = {}
until reload()
ext.reset_hash() if ext.is_enabled and ext.reset_hash?
balance =
if argv.wlb
book.win_loss_balance()
else
book.sum_nodes_outcome()
console.log "balance #{balance}" if argv.v
opening = argv.opening
if argv.auto_opening
if balance > 0
opening = 'F5d6'
else if balance > -argv.balance * .5
opening = 'F5f6'
else
opening = 'F5f4'
{moves, outcome} = play player, opening
moves_str = pos_array_to_str(moves)
collision = games[moves_str]
games[moves_str] = true
n_play++
if (balance >= argv.b and outcome > 0) or
(balance <= -argv.b and outcome < 0)
console.log "NOT ACCEPTED DUE TO BALANCE"
n_skip++
else
collision = not book.add_game(moves) unless collision
if collision
n_col++
console.log "COLLISION #{n_col}/#{n_play}"
avg_col = (avg_col * (argv.D - 1) + int(collision)) / argv.D
orig_randomness = randomness
if collision and avg_col > argv.max_col
randomness *= 1.07
else if avg_col < argv.min_col
randomness *= 0.99
if randomness != orig_randomness
player = make_player(randomness)
console.log 'collision rate', avg_col, 'randomness', randomness if argv.v
break if not collision and ++n_save >= argv.n
balance = book.sum_nodes_outcome()
console.log "played #{n_play + n_skip} collision #{n_col} skipped #{n_skip} balance #{balance}"