[Londerings-commits] CVS: go/simple_go map_opening.py, NONE, 1.1 9x9_black_opening.lst, 1.8, 1.9 9x
Brought to you by:
aloril
Update of /cvsroot/londerings/go/simple_go In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv11989 Modified Files: 9x9_black_opening.lst 9x9_white_opening.lst config.py game.py game_experimental.py map_opening_vs_gnugo.py Added Files: map_opening.py Log Message: map_opening against arbitary program; some openings against CrazyStone --- NEW FILE: map_opening.py --- # -*- coding: cp1252 -*- #! /usr/bin/env python # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # This program is distributed with GNU Go, a Go program. # # # # Write gn...@gn... or see http://www.gnu.org/software/gnugo/ # # for more information. # # # # Copyright 1999, 2000, 2001, 2002, 2003 and 2004 # # by the Free Software Foundation. # # # # This program is free software; you can redistribute it and/or # # modify it under the terms of the GNU General Public License # # as published by the Free Software Foundation - version 2. # # # # This program is distributed in the hope that it will be # # useful, but WITHOUT ANY WARRANTY; without even the implied # # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # # PURPOSE. See the GNU General Public License in file COPYING # # for more details. # # # # You should have received a copy of the GNU General Public # # License along with this program; if not, write to the Free # # Software Foundation, Inc., 59 Temple Place - Suite 330, # # Boston, MA 02111, USA. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # some comments (like above) and # lots of code copied from twogtp.py from gnugo-3.6-pre4 # additions/changes by Aloril 2004,2005,2006 # minor changes by Blubb Fallo 2004 # Aloril modified to work with simple_go.py # Aloril later modified to map random games against gnugo usage = """ This program plays gnugo against opening book until it finds position not covered in opening book. Usage: start with command: python map_opening_vs_gnugo.py """ import popen2 import sys import string import time import os import traceback import random import utils import simple_go import config from play_gtp import get_next_filename, Logger, log import opening_tree SUPER_KO = "superko violation" UNKNOWN_POSITION = "unknown position" OPPONENT_RESIGNED = "opponent resigned" WE_RESIGNED = "we resigned" GAME_FINISHED = "game finished" def coords_to_sgf(size, board_coords): global debug board_coords = string.lower(board_coords) if board_coords == "pass": return "" letter = board_coords[0] digits = board_coords[1:] if letter > "i": sgffirst = chr(ord(letter) - 1) else: sgffirst = letter sgfsecond = chr(ord("a") + int(size) - int(digits)) return sgffirst + sgfsecond class GTP_connection: # # Class members: # outfile File to write to # infile File to read from def __init__(self, command): try: infile, outfile = popen2.popen2(command) except: print "popen2 failed" sys.exit(1) self.infile = infile self.outfile = outfile log_name = get_next_filename("gtpa%04i.log") self.log_fp = open(log_name, "w") self.log_fp.write(command+"\n") self.log_fp.flush() def exec_cmd(self, cmd): if cmd[-1]!="\n": cmd = cmd + "\n\n" self.outfile.write(cmd) self.outfile.flush() if self.log_fp: self.log_fp.write("Time: %f\n" % time.time()) self.log_fp.write(cmd + "\n\n") self.log_fp.flush() result = "" while 1: line = self.infile.readline() if not line: break line = string.replace(line, "\r", "") if not result and line=="\n": continue if self.log_fp: self.log_fp.write(line) self.log_fp.flush() result = result + line if line=="\n": break return result class Match: def __init__(self, size, command, color): self.engine_command = command self.engine = GTP_connection(command) self.color = color self.boardsize(size) def boardsize(self, size): if size<5 or size>19: return "? unacceptable size\n\n" self.size = size self.engine.exec_cmd("quit") self.engine = GTP_connection(self.engine_command) result = self.engine.exec_cmd("boardsize " + str(size)) if result[0]=="?": return result self.engine.exec_cmd("clear_board") self.opening_engine = simple_go.Game(size) self.opening_engine.save_c_state() self.opening_engine.opening_tree = opening_tree.OpeningTree(self.size, self.color) self.opening_engine.restore_c_state() if not self.opening_engine.opening_tree.opening_tree_dict: msg = "Opening tree not found for " + self.color print msg raise ValueError, msg def play(self, print_flag = False): self.boardsize(self.size) color = simple_go.BLACK self.last_found_opening = "None" while not self.opening_engine.has_2_passes(): if color==simple_go.BLACK: color_str = "black" else: color_str = "white" if color==self.color: move = self.opening_engine.generate_move() if move==simple_go.RESIGN_MOVE: return WE_RESIGNED self.opening_engine.make_move(move) move_str = simple_go.move_as_string(move) result = self.engine.exec_cmd("play %s %s" % (color_str, move_str)) if result[0]=="?": raise ValueError, "engine didn't accept move " + move_str stats = string.replace(self.opening_engine.statistics_str, "%s", "") else: move_str = self.engine.exec_cmd("genmove " + color_str) move_str = string.upper(move_str.split()[1]) move = simple_go.string_as_move(move_str) if not self.opening_engine.make_move(move): return SUPER_KO if move_str=="RESIGN": return OPPONENT_RESIGNED stats = self.opening_engine.score_estimate_as_string() if print_flag: print color_str, move_str color = self.opening_engine.current_board.side log("move made: %s %s: %s\n" % (color_str, move_str, stats)) log(str(self.opening_engine.current_board)) log("-"*60 + "\n") return GAME_FINISHED def map_opening(count, size=9): config.time_per_move_limit = 300 config.games_per_move_limit = 30000 config.resign_flag = True engine_cmd = "./map_opponent" color = simple_go.WHITE m = Match(size, engine_cmd, color) fp = open("map_opening_%ix%i.log" % (size, size), "a") fp.write("-"*60 + "\n") fp.write("engine command: %s\n" % engine_cmd) fp.flush() for i in range(count): t0 = time.time() result = m.play() t1 = time.time() score = m.opening_engine.chinese_score_position() score2 = m.opening_engine.score_estimate_as_string() if m.opening_engine.current_board.side!=color: score = -score msg = "%i %.3fs %s %.1f %s %s\n" % (i, t1-t0, result, score, score2, simple_go.move_list_as_string(m.opening_engine.move_history)) print msg fp.write(msg) fp.flush() log(result + "\n") log("="*60 + "\n") if result in (UNKNOWN_POSITION, WE_RESIGNED): break if result==GAME_FINISHED and score < 0: break fp.close() if __name__=="__main__": #print usage map_opening(10000, 9) Index: 9x9_black_opening.lst =================================================================== RCS file: /cvsroot/londerings/go/simple_go/9x9_black_opening.lst,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -r1.8 -r1.9 *** 9x9_black_opening.lst 31 Oct 2006 08:11:51 -0000 1.8 --- 9x9_black_opening.lst 1 Nov 2006 15:24:05 -0000 1.9 *************** *** 118,119 **** --- 118,137 ---- E5 C3 E3 G6 C5 G3 C7 F7 E8 F8 F2 E7 H2 D6 C6 D5 D4 D8 H4 G4 H5 G5 J8 C8 B8 E9 H7 B9 B7 C4 B4 B3 B5 A6 A4 H3 J3 H6 J6 G2 G1 B2 D2 A8 A7 A3 A9 C9 C2 G8 H1 H9 J2 H8 J4 A8 D3 F4 E4 A9 E6 D7 B1 G7 J7 F3 J5 J9 F5 F6 A2 PASS B6 PASS A5 PASS PASS #Aloril, map_opening53 vs GNU Go 3.7.10 E5 D4 E4 C6 E3 D3 F7 G3 H4 E2 B7 C7 B6 B8 C5 D5 B4 B3 D6 D8 E7 B5 A5 C3 A8 C4 B5 C8 A7 A3 E8 A4 D9 F2 H3 H2 G4 F3 J2 F4 F5 H1 J3 J1 G5 D2 D7 B2 C9 C1 B9 G2 PASS B1 PASS #Aloril, map_opening54 vs GNU Go 3.7.10 + E5 C3 E3 G6 C5 G3 C7 F7 E8 F8 F2 E7 H2 D6 C6 D5 D4 D8 H4 G4 H5 G5 J8 C8 B8 E9 H7 B9 B7 C4 B4 B3 B5 A6 A4 H3 J3 H6 J6 G2 G1 B2 D2 A8 A7 A3 A9 C9 C2 G8 H1 H9 J2 H8 J4 A8 D3 F4 E4 A9 E6 D7 B1 F3 J5 G7 J7 J9 F5 F6 A2 PASS B6 PASS A5 PASS PASS #Aloril, map_opening55 vs GNU Go 3.7.10 + E5 G5 F7 D6 F6 #Aloril, play1 vs CrazyStone 0006 + E5 G5 F7 F3 C6 D3 H6 H5 B3 G6 G7 H7 H8 J6 C2 C4 B4 #Aloril, play2 vs CrazyStone 0006 + E5 F4 E4 E3 D3 D4 F5 D2 C3 F2 C5 G4 G7 C7 B6 #Aloril, play3 vs CrazyStone 0006 + E5 F4 E4 E3 D3 F2 G6 #Aloril, play4 vs CrazyStone 0006 + E5 E4 F5 D5 D6 C5 G3 C6 D7 C7 E2 D8 E8 E7 E6 E9 F8 #Aloril, play5 vs CrazyStone 0006 + E5 F4 E4 E3 D3 F2 G6 F5 F6 D2 C3 C7 B6 #Aloril, play6 vs CrazyStone 0006 + E5 G4 E3 F5 E6 #Aloril, play7 vs CrazyStone 0006 + E5 F4 E4 E3 D3 E8 F3 G3 E2 #Aloril, play8 vs CrazyStone 0006 + E5 G4 E3 E7 F6 #Aloril, play9 vs CrazyStone 0006 + E5 G4 E3 F6 E6 E7 F5 G5 F7 G6 D7 E8 D8 F8 F3 C3 B4 #Aloril, play10 vs CrazyStone 0006 + E5 G4 E3 F5 E6 F6 E7 #Aloril, play11 vs CrazyStone 0006 + E5 G4 E3 F6 E6 E7 F5 G5 F7 G6 D7 E8 D8 F8 F3 G7 C5 #Aloril, play12 vs CrazyStone 0006 + E5 G4 E3 E7 F6 F7 G6 #Aloril, play13 vs CrazyStone 0006 + E5 G4 E3 E6 F5 #Aloril, play14 vs CrazyStone 0006 + E5 F4 E4 E3 D3 F2 G6 D2 C3 C7 D8 D7 E8 C8 B6 F5 F6 E6 D5 D6 C5 B7 F7 #Aloril, play15 vs CrazyStone 0006 + E5 G5 F7 F4 D3 E6 F6 E4 D4 F5 D5 H6 D7 B7 C8 B5 B3 B8 B9 #Aloril, play16 vs CrazyStone 0006 + E5 G4 E3 D7 F6 #Aloril, play17 vs CrazyStone 0006 Index: 9x9_white_opening.lst =================================================================== RCS file: /cvsroot/londerings/go/simple_go/9x9_white_opening.lst,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -r1.6 -r1.7 *** 9x9_white_opening.lst 27 Oct 2006 02:17:17 -0000 1.6 --- 9x9_white_opening.lst 1 Nov 2006 15:24:05 -0000 1.7 *************** *** 121,122 **** --- 121,124 ---- E5 G5 F5 G6 F4 C6 G4 E7 C4 D5 D4 B5 B4 D6 H4 H5 J5 J6 J4 A4 A3 A5 B2 F6 C5 B6 E6 F7 B3 D8 E3 PASS D3 PASS F3 PASS G2 PASS F2 PASS H3 PASS #Aloril, map_opening34 vs GNU Go 3.7.10 G6 D5 C7 G4 E6 C4 H5 H4 E5 E4 B6 B5 J4 J3 J5 H3 F5 D6 D7 C6 B7 G5 H6 F4 A5 A4 A6 B3 E7 PASS F7 PASS G7 PASS H8 PASS G8 PASS C8 PASS E8 PASS D9 PASS C9 PASS #Aloril, map_opening35 vs GNU Go 3.7.10 + E5 G5 F6 C6 D4 G6 G7 H7 F8 C4 H8 G2 #Aloril, play1 vs CrazyStone 0006 + E5 G5 E6 C7 E4 C5 G3 F7 E7 F8 E8 D3 E3 D2 E2 C8 H4 H5 D1 C1 E1 C2 J5 J6 F6 G6 F9 G8 G9 H9 E9 H7 J4 G4 H3 D4 C9 B9 D9 B8 F4 D6 D5 D7 D8 F5 #Aloril, play2 vs CrazyStone 0006 Index: config.py =================================================================== RCS file: /cvsroot/londerings/go/simple_go/config.py,v retrieving revision 1.63 retrieving revision 1.64 diff -C2 -r1.63 -r1.64 *** config.py 7 Oct 2006 08:32:24 -0000 1.63 --- config.py 1 Nov 2006 15:24:05 -0000 1.64 *************** *** 4,7 **** --- 4,8 ---- komi = 7.5 + resign_flag = False repeat_check = True use_threads_scoring_system = False Index: game.py =================================================================== RCS file: /cvsroot/londerings/go/simple_go/game.py,v retrieving revision 1.43 retrieving revision 1.44 diff -C2 -r1.43 -r1.44 *** game.py 9 Oct 2006 04:03:23 -0000 1.43 --- game.py 1 Nov 2006 15:24:05 -0000 1.44 *************** *** 72,75 **** --- 72,77 ---- then check for repetition (situational super-ko) """ + if move==RESIGN_MOVE: + return True if move==PASS_MOVE: return True Index: game_experimental.py =================================================================== RCS file: /cvsroot/londerings/go/simple_go/game_experimental.py,v retrieving revision 1.51 retrieving revision 1.52 diff -C2 -r1.51 -r1.52 *** game_experimental.py 9 Oct 2006 04:03:23 -0000 1.51 --- game_experimental.py 1 Nov 2006 15:24:05 -0000 1.52 *************** *** 913,923 **** win, lost = move_results[0][2] win_probability = win / (win + lost) ! final_score = self.score_estimate(100) ! if self.current_board.side==BLACK: ! final_score = -final_score ! if final_score>=0: ! score_str = "W+%.1f" % final_score ! else: ! score_str = "B+%.1f" % -final_score self.statistics_str = "%s %%swin %%s%s%%s prob. %.3f%%s" % \ (score_str, move_as_string(move_results[0][3]), win_probability) --- 913,917 ---- win, lost = move_results[0][2] win_probability = win / (win + lost) ! score_str = self.score_estimate_as_string() self.statistics_str = "%s %%swin %%s%s%%s prob. %.3f%%s" % \ (score_str, move_as_string(move_results[0][3]), win_probability) *************** *** 1136,1140 **** if move_results: score1, score2, result, move = move_results[0] ! if move!=PASS_MOVE and score2 < 0.01 and c_board.uct_game == c_board.random_uct_game: move = RESIGN_MOVE fp = open(saved_moved_file, "w") --- 1130,1135 ---- if move_results: score1, score2, result, move = move_results[0] ! if move!=PASS_MOVE and ((score1 < 0.01 and c_board.uct_game == c_board.random_uct_game) or \ ! (score1 < 0.05 and config.resign_flag)): move = RESIGN_MOVE fp = open(saved_moved_file, "w") *************** *** 1154,1157 **** --- 1149,1160 ---- return score + def chinese_score_position(self): + score = self.current_board.chinese_score_position() + if self.current_board.side==BLACK: + score = score - self.komi + else: + score = score + self.komi + return score + def score_estimate(self, count=100): score_sum = 0.0 *************** *** 1160,1163 **** --- 1163,1176 ---- return score_sum / count + def score_estimate_as_string(self, count=100): + final_score = self.score_estimate(count) + if self.current_board.side==BLACK: + final_score = -final_score + if final_score>=0: + score_str = "W+%.1f" % final_score + else: + score_str = "B+%.1f" % -final_score + return score_str + def evaluate_weakbot_resign(self): if self.check_all_unconditional(True, True): Index: map_opening_vs_gnugo.py =================================================================== RCS file: /cvsroot/londerings/go/simple_go/map_opening_vs_gnugo.py,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -r1.5 -r1.6 *** map_opening_vs_gnugo.py 31 Oct 2006 08:11:51 -0000 1.5 --- map_opening_vs_gnugo.py 1 Nov 2006 15:24:05 -0000 1.6 *************** *** 1,153 **** ! # -*- coding: cp1252 -*- ! #! /usr/bin/env python - # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # - # This program is distributed with GNU Go, a Go program. # - # # - # Write gn...@gn... or see http://www.gnu.org/software/gnugo/ # - # for more information. # - # # - # Copyright 1999, 2000, 2001, 2002, 2003 and 2004 # - # by the Free Software Foundation. # - # # - # This program is free software; you can redistribute it and/or # - # modify it under the terms of the GNU General Public License # - # as published by the Free Software Foundation - version 2. # - # # - # This program is distributed in the hope that it will be # - # useful, but WITHOUT ANY WARRANTY; without even the implied # - # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # - # PURPOSE. See the GNU General Public License in file COPYING # - # for more details. # - # # - # You should have received a copy of the GNU General Public # - # License along with this program; if not, write to the Free # - # Software Foundation, Inc., 59 Temple Place - Suite 330, # - # Boston, MA 02111, USA. # - # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # - - # some comments (like above) and - # lots of code copied from twogtp.py from gnugo-3.6-pre4 - # additions/changes by Aloril 2004,2005,2006 - - # minor changes by Blubb Fallo 2004 - - # Aloril modified to work with simple_go.py - - # Aloril later modified to map random games against gnugo - - usage = """ - - This program plays gnugo against opening book - until it finds position not covered in opening book. - - Usage: - start with command: - python map_opening_vs_gnugo.py - - """ - - import popen2 - import sys - import string - import time - import os - import traceback - import random - import utils - import simple_go - import config - from play_gtp import get_next_filename, Logger, log - import opening_tree - - SUPER_KO = "superko violation" - UNKNOWN_POSITION = "unknown position" - GAME_FINISHED = "game finished" - - def coords_to_sgf(size, board_coords): - global debug - - board_coords = string.lower(board_coords) - if board_coords == "pass": - return "" - letter = board_coords[0] - digits = board_coords[1:] - if letter > "i": - sgffirst = chr(ord(letter) - 1) - else: - sgffirst = letter - sgfsecond = chr(ord("a") + int(size) - int(digits)) - return sgffirst + sgfsecond - - - class GTP_connection: - - # - # Class members: - # outfile File to write to - # infile File to read from - - def __init__(self, command): - try: - infile, outfile = popen2.popen2(command) - except: - print "popen2 failed" - sys.exit(1) - self.infile = infile - self.outfile = outfile - log_name = get_next_filename("gtpa%04i.log") - self.log_fp = open(log_name, "w") - self.log_fp.write(command+"\n") - self.log_fp.flush() - - def exec_cmd(self, cmd): - if cmd[-1]!="\n": - cmd = cmd + "\n\n" - self.outfile.write(cmd) - self.outfile.flush() - if self.log_fp: - self.log_fp.write("Time: %f\n" % time.time()) - self.log_fp.write(cmd + "\n\n") - self.log_fp.flush() - result = "" - while 1: - line = self.infile.readline() - if not line: break - if not result and line=="\n": continue - if self.log_fp: - self.log_fp.write(line) - self.log_fp.flush() - result = result + line - if line=="\n": break - - return result - - - class Match: - def __init__(self, size, command, color): - self.engine_command = command - self.engine = GTP_connection(command) - self.color = color - self.boardsize(size) - - def boardsize(self, size): - if size<5 or size>19: - return "? unacceptable size\n\n" - self.size = size - self.engine.exec_cmd("quit") - self.engine = GTP_connection(self.engine_command) - result = self.engine.exec_cmd("boardsize " + str(size)) - if result[0]=="?": - return result - self.engine.exec_cmd("clear_board") - self.opening_engine = simple_go.Game(size) - self.opening_engine.save_c_state() - self.opening_engine.opening_tree = opening_tree.OpeningTree(self.size, self.color) - self.opening_engine.restore_c_state() - if not self.opening_engine.opening_tree.opening_tree_dict: - msg = "Opening tree not found for " + self.color - print msg - raise ValueError, msg def play(self, print_flag = False): self.boardsize(self.size) --- 1,6 ---- ! from map_opening import * + class MatchGNUGo(Match): def play(self, print_flag = False): self.boardsize(self.size) |