forked from pnprog/goreviewpartner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaq_analysis.py
executable file
·537 lines (430 loc) · 15.8 KB
/
aq_analysis.py
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# -*- coding: utf-8 -*-
from gtp import gtp, GtpException
import sys
from gomill import sgf, sgf_moves
from sys import exit,argv
from tkinter import *
import sys
import os
import configparser
from time import sleep
import os
import threading
import tkinter.ttk as ttk
from toolbox import *
from toolbox import _
import tkinter.messagebox
class AQAnalysis():
def win_rate(self,current_move,value,roll):
#see discussion at https://github.com/ymgaq/AQ/issues/20
if current_move<=160:
lmbd=0.8
else:
lmbd=0.8-min(0.3,max(0.0,(current_move-160)/600))
rate=lmbd*value+(1-lmbd)*roll
#print "winrate(m=",current_move,",v=",value,",r=",roll,")=",rate
return rate
def run_analysis(self,current_move):
one_move=go_to_move(self.move_zero,current_move)
player_color=guess_color_to_play(self.move_zero,current_move)
aq=self.aq
log()
log("==============")
log("move",str(current_move))
if player_color in ('w',"W"):
log("AQ plays white")
answer=aq.play_white()
else:
log("AQ plays black")
answer=aq.play_black()
if current_move>1:
es=aq.final_score()
#one_move.set("ES",es)
save_position_data(one_move,self.data_in_comments,"ES",es,bot="AQ")
log("AQ preferred move:",answer)
save_position_data(one_move,self.data_in_comments,"CBM",answer,bot="AQ") #Computer Best Move
all_moves=aq.get_all_aq_moves()
if (answer.lower() not in ["pass","resign"]):
#one_move.set("CBM",answer.lower()) #Computer Best Move
best_move=True
log("Number of alternative sequences:",len(all_moves))
#log(all_moves)
#for sequence_first_move,one_sequence,one_score,one_monte_carlo,one_value_network,one_policy_network,one_evaluation,one_rave,one_nodes in all_moves:
for sequence_first_move,count,value,roll,prob,one_sequence in all_moves[:self.maxvariations]:
log("Adding sequence starting from",sequence_first_move)
previous_move=one_move.parent
current_color=player_color
one_score=self.win_rate(current_move,value,roll)
first_variation_move=True
for one_deep_move in one_sequence.split(' '):
if one_deep_move.lower() in ["pass","resign"]:
log("Leaving the variation when encountering",one_deep_move.lower())
break
i,j=gtp2ij(one_deep_move)
new_child=previous_move.new_child()
new_child.set_move(current_color,(i,j))
if player_color=='b':
bwwr=str(one_score)+'%/'+str(100-one_score)+'%'
mcwr=str(roll)+'%/'+str(100-roll)+'%'
vnwr=str(value)+'%/'+str(100-value)+'%'
else:
bwwr=str(100-one_score)+'%/'+str(one_score)+'%'
mcwr=str(100-roll)+'%/'+str(roll)+'%'
vnwr=str(100-value)+'%/'+str(value)+'%'
if first_variation_move:
first_variation_move=False
#variation_comment=_("black/white win probability for this variation: ")+black_win_rate+'/'+white_win_rate
save_variation_data(new_child,self.data_in_comments,"BWWR",bwwr)
#variation_comment+="\nCount: "+str(count)
#new_child.set("PLYO",str(count))
save_variation_data(new_child,self.data_in_comments,"PLYO",str(count))
#variation_comment+="\nValue: "+str(value)
save_variation_data(new_child,self.data_in_comments,"VNWR",vnwr)
#variation_comment+="\nRoll: "+str(roll)
save_variation_data(new_child,self.data_in_comments,"MCWR",mcwr)
#variation_comment+="\nProb: "+str(prob)+"%"
save_variation_data(new_child,self.data_in_comments,"PNV",str(prob)+"%")
#new_child.add_comment_text(variation_comment)
if best_move:
best_move=False
save_position_data(one_move,self.data_in_comments,"BWWR",bwwr,bot="AQ")
save_position_data(one_move,self.data_in_comments,"MCWR",mcwr,bot="AQ")
save_position_data(one_move,self.data_in_comments,"VNWR",vnwr,bot="AQ")
previous_move=new_child
if current_color in ('w','W'):
current_color='b'
else:
current_color='w'
log("==== no more sequences =====")
aq.undo()
else:
log('adding "'+answer.lower()+'" to the sgf file')
# additional_comments+="\n"+_("For this position, %s would %s"%("AQ",answer.lower()))
if answer.lower()=="pass":
aq.undo()
elif answer.lower()=="resign":
if self.stop_at_first_resign:
log("")
log("The analysis will stop now")
log("")
self.move_range=[]
return answer
def initialize_bot(self):
aq=aq_starting_procedure(self.g,self.profile)
self.aq=aq
self.time_per_move=0
return aq
def aq_starting_procedure(sgf_g,profile="slow",silentfail=False):
return bot_starting_procedure("AQ","AQ",AQ_gtp,sgf_g,profile,silentfail)
class RunAnalysis(AQAnalysis,RunAnalysisBase):
def __init__(self,parent,filename,move_range,intervals,variation,komi,profile="slow"):
RunAnalysisBase.__init__(self,parent,filename,move_range,intervals,variation,komi,profile)
class LiveAnalysis(AQAnalysis,LiveAnalysisBase):
def __init__(self,g,filename,profile="slow"):
LiveAnalysisBase.__init__(self,g,filename,profile)
import ntpath
import subprocess
import threading, queue
class AQ_gtp(gtp):
def quit(self):
self.write('\x03')
def __init__(self,command):
self.c=1
aq_working_directory=command[0][:-len(ntpath.basename(command[0]))]
log("AQ working directory:",aq_working_directory)
self.process=subprocess.Popen(command,cwd=aq_working_directory, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.size=0
self.stderr_queue=queue.Queue()
threading.Thread(target=self.consume_stderr).start()
self.history=[]
def place_black(self,move):
self.write("play black "+move)
answer=self.readline()
if answer[0]=="=":
self.history.append(["b",move])
return True
else:return False
def place_white(self,move):
self.write("play white "+move)
answer=self.readline()
if answer[0]=="=":
self.history.append(["w",move])
return True
else:return False
def play_black(self):
self.write("genmove black")
answer=self.readline().strip()
try:
move=answer.split(" ")[1]
if move.lower()!="resign":
self.history.append(["b",move])
return move
except Exception as e:
raise GtpException("GtpException in genmove_black()\nanswer='"+answer+"'\n"+str(e))
def play_white(self):
self.write("genmove white")
answer=self.readline().strip()
try:
move=answer.split(" ")[1]
if move.lower()!="resign":
self.history.append(["w",move])
return move
except Exception as e:
raise GtpException("GtpException in genmove_white()\nanswer='"+answer+"'\n"+str(e))
def undo(self):
self.write("clear_board")
answer=self.readline()
try:
if answer[0]!="=":
return False
self.history.pop()
history=self.history[:]
self.history=[]
for color,move in history:
if color=="b":
if not self.place_black(move):
return False
else:
if not self.place_white(move):
return False
return True
except Exception as e:
raise GtpException("GtpException in undo()\n"+str(e))
def get_all_aq_moves_old(self):
buff=[]
sleep(.1)
while not self.stderr_queue.empty():
while not self.stderr_queue.empty():
buff.append(self.stderr_queue.get())
sleep(.1)
buff.reverse()
answers=[]
for err_line in buff:
if "total games=" in err_line:
v1,v2=[int(v) for v in err_line.split("=")[-1].replace(")","").split("(")]
if v2!=0:
log("Computing requirement margin: "+str(int(100.*v1/v2)-100)+"%")
if v1<v2:
log("=======================================================")
log("=== WARNING: AQ thinking time seems to be too low ! ===")
log("=======================================================")
elif v1==0 and v2==0:
log("===============================================================================")
log("=== WARNING: AQ thinking time IS too low for the analysis to be performed ! ===")
log("===============================================================================")
log("\a") #let's make this annoying enough :)
if ("->" in err_line) and ('|' in err_line):
log(err_line)
sequence=err_line.split("|")[-1].strip()
sequence=sequence.replace(" ","")
sequence=sequence.replace("\t","")
sequence=sequence.replace("->"," ")
err_line=err_line.replace("|"," ")
err_line=err_line.strip().split()
#print err_line.strip().split()
one_answer=err_line[0]
count=err_line[1]
try:
value=float(err_line[2])
except:
value=0.0
roll=err_line[3]
prob=err_line[4]
depth=err_line[5]
if sequence:
answers=[[one_answer,int(count),value,float(roll),float(prob),sequence]]+answers
return answers
def get_all_aq_moves(self):
buff=[]
sleep(.1)
while not self.stderr_queue.empty():
while not self.stderr_queue.empty():
buff.append(self.stderr_queue.get())
sleep(.1)
buff.reverse()
answers=[]
for err_line in buff:
#log(err_line)
if "total games=" in err_line:
v1,v2=[int(v) for v in err_line.split("=")[-1].replace(")","").split("(")]
if v2!=0:
log("Computing requirement margin: "+str(int(100.*v1/v2)-100)+"%")
if v1<v2:
log("=======================================================")
log("=== WARNING: AQ thinking time seems to be too low ! ===")
log("=======================================================")
elif v1==0 and v2==0:
log("===============================================================================")
log("=== WARNING: AQ thinking time IS too low for the analysis to be performed ! ===")
log("===============================================================================")
log("\a") #let's make this annoying enough :)
if ("->" in err_line) and ('|' in err_line):
log(err_line)
sequence=err_line.split("|")[-1].strip()
sequence=sequence.replace(" ","")
sequence=sequence.replace("\t","")
sequence=sequence.replace("->"," ")
err_line=err_line.replace("|"," ")
err_line=err_line.strip().split()
#print err_line.strip().split()
one_answer=err_line[0]
count=err_line[1]
try:
value=float(err_line[2])
except:
value=0.0
roll=err_line[3]
prob=err_line[4]
depth=err_line[5]
if sequence:
answers=[[one_answer,int(count),value,float(roll),float(prob),sequence]]+answers
return answers
class AQSettings(Frame):
def __init__(self,parent):
Frame.__init__(self,parent)
log("Initializing AQ setting interface")
Config = configparser.ConfigParser()
Config.read(config_file)
bot="AQ"
row=0
Label(self,text=_("%s settings")%bot, font="-weight bold").grid(row=row,column=1,sticky=W)
row+=1
Label(self,text="").grid(row=row,column=1)
row+=1
Label(self,text=_("Slow profile parameters")).grid(row=row,column=1,sticky=W)
row+=1
Label(self,text=_("Command")).grid(row=row,column=1,sticky=W)
SlowCommand = StringVar()
SlowCommand.set(Config.get(bot,"SlowCommand"))
Entry(self, textvariable=SlowCommand, width=30).grid(row=row,column=2)
row+=1
Label(self,text=_("Parameters")).grid(row=row,column=1,sticky=W)
SlowParameters = StringVar()
SlowParameters.set(Config.get(bot,"SlowParameters"))
Entry(self, textvariable=SlowParameters, width=30).grid(row=row,column=2)
row+=1
Label(self,text="").grid(row=row,column=1)
row+=1
Label(self,text=_("Fast profile parameters")).grid(row=row,column=1,sticky=W)
row+=1
row+=1
Label(self,text=_("Command")).grid(row=row,column=1,sticky=W)
FastCommand = StringVar()
FastCommand.set(Config.get(bot,"FastCommand"))
Entry(self, textvariable=FastCommand, width=30).grid(row=row,column=2)
row+=1
Label(self,text=_("Parameters")).grid(row=row,column=1,sticky=W)
FastParameters = StringVar()
FastParameters.set(Config.get(bot,"FastParameters"))
Entry(self, textvariable=FastParameters, width=30).grid(row=row,column=2)
row+=1
Label(self,text="").grid(row=row,column=1)
row+=1
Label(self,text=_("%s availability")%bot).grid(row=row,column=1,sticky=W)
row+=1
value={"slow":_("Slow profile"),"fast":_("Fast profile"),"both":_("Both profiles"),"none":_("None")}
Label(self,text=_("Static analysis")).grid(row=row,column=1,sticky=W)
analysis_bot = StringVar()
analysis_bot.set(value[Config.get(bot,"AnalysisBot")])
OptionMenu(self,analysis_bot,_("Slow profile"),_("Fast profile"),_("Both profiles"),_("None")).grid(row=row,column=2,sticky=W)
row+=1
Label(self,text=_("Live analysis")).grid(row=row,column=1,sticky=W)
liveanalysis_bot = StringVar()
liveanalysis_bot.set(value[Config.get(bot,"LiveAnalysisBot")])
OptionMenu(self,liveanalysis_bot,_("Slow profile"),_("Fast profile"),_("Both profiles"),_("None")).grid(row=row,column=2,sticky=W)
row+=1
Label(self,text=_("Live analysis as black or white")).grid(row=row,column=1,sticky=W)
liveplayer_bot = StringVar()
liveplayer_bot.set(value[Config.get(bot,"LivePlayerBot")])
OptionMenu(self,liveplayer_bot,_("Slow profile"),_("Fast profile"),_("Both profiles"),_("None")).grid(row=row,column=2,sticky=W)
row+=1
Label(self,text=_("When opening a position for manual play")).grid(row=row,column=1,sticky=W)
review_bot = StringVar()
review_bot.set(value[Config.get(bot,"ReviewBot")])
OptionMenu(self,review_bot,_("Slow profile"),_("Fast profile"),_("Both profiles"),_("None")).grid(row=row,column=2,sticky=W)
row+=1
Label(self,text="").grid(row=row,column=1)
row+=1
Label(self,text=_("See AQ parameters in aq_config.txt")).grid(row=row,column=1,columnspan=2,sticky=W)
self.SlowCommand=SlowCommand
self.SlowParameters=SlowParameters
self.FastCommand=FastCommand
self.FastParameters=FastParameters
self.analysis_bot=analysis_bot
self.liveanalysis_bot=liveanalysis_bot
self.liveplayer_bot=liveplayer_bot
self.review_bot=review_bot
def save(self):
log("Saving AQ settings")
Config = configparser.ConfigParser()
Config.read(config_file)
bot="AQ"
Config.set(bot,"SlowCommand",self.SlowCommand.get())
Config.set(bot,"SlowParameters",self.SlowParameters.get())
Config.set(bot,"FastCommand",self.FastCommand.get())
Config.set(bot,"FastParameters",self.FastParameters.get())
value={_("Slow profile"):"slow",_("Fast profile"):"fast",_("Both profiles"):"both",_("None"):"none"}
Config.set(bot,"AnalysisBot",value[self.analysis_bot.get()])
Config.set(bot,"LiveanalysisBot",value[self.liveanalysis_bot.get()])
Config.set(bot,"LivePlayerBot",value[self.liveplayer_bot.get()])
Config.set(bot,"ReviewBot",value[self.review_bot.get()])
Config.write(open(config_file,"w"))
class AQOpenMove(BotOpenMove):
def __init__(self,sgf_g,profile="slow"):
BotOpenMove.__init__(self,sgf_g,profile)
self.name='AQ'
self.my_starting_procedure=leela_zero_starting_procedure
AQ={}
AQ['name']="AQ"
AQ['gtp_name']="AQ"
AQ['analysis']=AQAnalysis
AQ['openmove']=AQOpenMove
AQ['settings']=AQSettings
AQ['gtp']=AQ_gtp
AQ['liveanalysis']=LiveAnalysis
AQ['runanalysis']=RunAnalysis
AQ['starting']=aq_starting_procedure
if __name__ == "__main__":
if len(argv)==1:
temp_root = Tk()
filename = open_sgf_file(parent=temp_root)
temp_root.destroy()
log(filename)
log("gamename:",filename[:-4])
if not filename:
sys.exit()
log("filename:",filename)
top = Tk()
bot=AQ
slowbot=bot
slowbot['profile']="slow"
fastbot=dict(bot)
fastbot['profile']="fast"
RangeSelector(top,filename,bots=[slowbot, fastbot]).pack()
top.mainloop()
else:
try:
parameters=getopt.getopt(argv[1:], '', ['no-gui','range=', 'color=', 'komi=',"variation="])
except Exception as e:
show_error(str(e)+"\n"+usage)
sys.exit()
if not parameters[1]:
show_error("SGF file missing\n"+usage)
sys.exit()
top=None
batch=[]
for filename in parameters[1]:
move_selection,intervals,variation,komi,nogui=parse_command_line(filename,parameters[0])
if nogui:
log("File to analyse:",filename)
app=RunAnalysis("no-gui",filename,move_selection,intervals,variation-1,komi)
app.terminate_bot()
else:
if not top:
top = Tk()
top.withdraw()
one_analysis=[RunAnalysis,filename,move_selection,intervals,variation-1,komi]
batch.append(one_analysis)
if not nogui:
top.after(1,lambda: batch_analysis(top,batch))
top.mainloop()