def test_c_ladder_alpha_beta(name = "kgs/simple_ladders2.sgf", pos_str = "C3", depth = 1, seed = 1, undo_count = 0, limit = 0, print_pos = True): c_board.set_random_seed(seed) g = load_sgf.load_file(name, game_class = CGame) undo_count0 = undo_count while undo_count: g.undo_move() undo_count = undo_count - 1 pos = string_as_move(pos_str) block_color = g.current_board.goban[pos] if g.current_board.side==block_color: g.make_move(PASS_MOVE) if print_pos: print g.current_board print pos_str #g.probabilistic_alpha_beta(string_as_move(pos), 80) nodes0 = c_board.get_trymove_counter() t0 = time.time() score = c_board.alpha_beta_search_random(pos, g.c_color(), depth, -2, 2, limit) t1 = time.time() nodes1 = c_board.get_trymove_counter() moves = [] t_used = t1-t0 t_used = round(t_used, int(-math.log10(t_used) + 2)) nodes_s = (nodes1-nodes0)/(t1-t0) if nodes_s >= 1000000: nodes_s = "%sM" % round(nodes_s/1000000., 1) else: nodes_s = "%sK" % round(nodes_s/1000.) if nodes_s >= 10000: nodes_s = nodes_s.replace(".0", "") limit_s = str(limit/1000000.) limit_s = limit_s.replace(".0", "") s = "%-6s %-4s %-5s %-6s %-9s %-7s %s" % (limit_s, undo_count0, depth, score, nodes1-nodes0, t_used, nodes_s) print s return s, score
def play_random_game(seed=0, size=9): g = CGame(size) c_board.set_random_seed(seed) while not g.has_2_passes(): move = g.select_random_move() g.make_move(move) print g print g.current_board
def test_ld(filename, pos): config.time_per_move_limit = 100000000 config.games_per_move_limit = 2**63 g = load_sgf.load_file(filename) print g.current_board c_board.clear_result_table() c_board.set_random_seed(1) g.select_uct_move(pos=string_as_move(pos))
def test_tromp_taylor_scoring(): c_board.clear_result_table() c_board.set_random_seed(1) config.time_per_move_limit = 100000000 config.games_per_move_limit = 10000 g = load_sgf.load_file("kgs/tromp_taylor_scoring_error.sgf") g.undo_move() #g.undo_move() print g.current_board print g.make_move(g.generate_move()) return g
def test_area_scoring(): c_board.clear_result_table() c_board.set_random_seed(1) config.time_per_move_limit = 100000000 config.games_per_move_limit = 100000 g = load_sgf.load_file("kgs/endgame_test.sgf") g.set_komi(7.5) g.undo_move() #g.make_move(string_as_move("C1")) #g.make_move(string_as_move("G1")) #g.make_move(string_as_move("D5")) print g.current_board #g.generate_move() return g
def test_simple_atari(count): random.seed(1) c_board.set_random_seed(1) g = load_sgf.load_file("kgs/simple_atari0.sgf", game_class=RandomGame) print g.current_board move_list = g.list_moves() pattern_dict = {} for move in move_list: pattern = MovePattern(move) pattern_dict[move] = pattern #print len(pattern_dict), pattern for seed in range(1, count + 1): best_score = None for move in move_list: pattern = pattern_dict[move] score = pattern.best_theory() if score > best_score: best_score = score best_patterns = [pattern] elif score == best_score: best_patterns.append(pattern) ## print len(best_patterns), "%.3f: %.3f" % best_score, ## for pattern in best_patterns: ## print move_as_string(pattern.move), ## print pattern = random.choice(best_patterns) pattern = pattern_dict[random.choice(pattern_dict.keys())] move = pattern.move start_len = len(g.move_history) g.make_move(move) result = play_game(g, string_as_move("A1")) #print g.current_board while len(g.move_history) > start_len: g.undo_move() pattern.apply_result(result) ## print seed, result, "%.3f: %.3f" % pattern.best_theory(), str(pattern) ## print result_lst = [] print "-" * 60 for move in move_list: pattern = pattern_dict[move] if pattern.total_count: result_lst.append((pattern.best_theory(), pattern)) result_lst.sort() for score, pattern in result_lst: print str(pattern)
def test_komi(komi): config.time_per_move_limit = 100000000 config.games_per_move_limit = 1000000 print "."*60 print "Komi:", komi #g = load_sgf.load_file("kgs/minue622-SimpleBot_1h_komi_analysis6.sgf"); g.make_move(PASS_MOVE) #g = Game(7) #g = load_sgf.load_file("kgs/Fomalhaut-SimpleBot_2006-08-06_tourn.sgf") #g = load_sgf.load_file("kgs/StoneCrazy-SimpleBot_2006-08-06_tour.sgf") #g = load_sgf.load_file("kgs/yose_test0.sgf") g = load_sgf.load_file("kgs/t.sgf") print g.current_board c_board.clear_result_table() g.set_komi(komi) c_board.set_random_seed(1) print g.generate_move()
def test_komi(komi): config.time_per_move_limit = 100000000 config.games_per_move_limit = 1000000 print "." * 60 print "Komi:", komi #g = load_sgf.load_file("kgs/minue622-SimpleBot_1h_komi_analysis6.sgf"); g.make_move(PASS_MOVE) #g = Game(7) #g = load_sgf.load_file("kgs/Fomalhaut-SimpleBot_2006-08-06_tourn.sgf") #g = load_sgf.load_file("kgs/StoneCrazy-SimpleBot_2006-08-06_tour.sgf") #g = load_sgf.load_file("kgs/yose_test0.sgf") g = load_sgf.load_file("kgs/t.sgf") print g.current_board c_board.clear_result_table() g.set_komi(komi) c_board.set_random_seed(1) print g.generate_move()
def test_simple_atari(count): random.seed(1) c_board.set_random_seed(1) g = load_sgf.load_file("kgs/simple_atari0.sgf", game_class = RandomGame) print g.current_board move_list = g.list_moves() pattern_dict = {} for move in move_list: pattern = MovePattern(move) pattern_dict[move] = pattern #print len(pattern_dict), pattern for seed in range(1, count+1): best_score = None for move in move_list: pattern = pattern_dict[move] score = pattern.best_theory() if score > best_score: best_score = score best_patterns = [pattern] elif score == best_score: best_patterns.append(pattern) ## print len(best_patterns), "%.3f: %.3f" % best_score, ## for pattern in best_patterns: ## print move_as_string(pattern.move), ## print pattern = random.choice(best_patterns) pattern = pattern_dict[random.choice(pattern_dict.keys())] move = pattern.move start_len = len(g.move_history) g.make_move(move) result = play_game(g, string_as_move("A1")) #print g.current_board while len(g.move_history) > start_len: g.undo_move() pattern.apply_result(result) ## print seed, result, "%.3f: %.3f" % pattern.best_theory(), str(pattern) ## print result_lst = [] print "-"*60 for move in move_list: pattern = pattern_dict[move] if pattern.total_count: result_lst.append((pattern.best_theory(), pattern)) result_lst.sort() for score, pattern in result_lst: print str(pattern)
def test_pattern(): c_board.set_random_seed(1) g = load_sgf.load_file("kgs/simple_ladder_full.sgf", game_class = CGame) for i in range(21): g.undo_move() print g.current_board #c_board.add_pattern_result_table(string_as_move("C5"), CBLACK, 3, 1) uct_search(1, g) print c_board.dump_pattern_result_table("patterns.dat") ## s = "B:B9 W:E2 B:J7 W:B8 B:E5 W:G7 B:D8 W:A1 B:D2 W:A8 B:G6 W:G2 B:D4 W:B1 B:D6 W:J5 B:B2 W:D1 B:J6 W:H6 B:F3 W:C1 B:H3 W:E4 B:F6 W:G9 B:E3 W:G3 B:G4 W:D9 B:H8 W:F7 B:A5 W:A3 B:A9 W:F8 B:J4 W:E9 B:H7 W:B5 B:D7 W:E6 B:H9 W:C6 B:J9 W:B6 B:H2 W:H1 B:E8 W:J3 B:E7 W:A2 B:G5 W:H5 B:J1 W:C8 B:J2 W:F2 B:A6 W:F4 B:G8 W:F1 B:G1 W:C9 B:H4 W:C7 B:B7 W:J5 B:A4 W:C5 B:D5 W:A9 B:F5 W:F9 B:E4 W:H1 B:A7 W:H5 B:E1 W:B1 B:A2 W:A1 B:D1 B:H6 W:J5 B:C1 W:A1 B:B1 B:G1 W:E2 B:F1 W:G2 B:H5 W:G3 B:F2 W:G2 B:B9 W:A8 B:F9 W:C9 B:F8 W:G7 B:F7 W:C7 B:C8 W:B5 B:G3 W:C5 B:C3 W:D9 B:C4 W:A9 B:B8 W:A9 B:B6 B:A8 B:C6 W:C5 B:B5 B:E9 W:C9 B:D9" ## for m in s.split(): ## m = string_as_move(m.split(":")[1]) ## if not g.make_move(m): ## print "illegal move:", move_as_string(m) ## break ## print g.current_board return g
def test_pattern(): c_board.set_random_seed(1) g = load_sgf.load_file("kgs/simple_ladder_full.sgf", game_class=CGame) for i in range(21): g.undo_move() print g.current_board #c_board.add_pattern_result_table(string_as_move("C5"), CBLACK, 3, 1) uct_search(1, g) print c_board.dump_pattern_result_table("patterns.dat") ## s = "B:B9 W:E2 B:J7 W:B8 B:E5 W:G7 B:D8 W:A1 B:D2 W:A8 B:G6 W:G2 B:D4 W:B1 B:D6 W:J5 B:B2 W:D1 B:J6 W:H6 B:F3 W:C1 B:H3 W:E4 B:F6 W:G9 B:E3 W:G3 B:G4 W:D9 B:H8 W:F7 B:A5 W:A3 B:A9 W:F8 B:J4 W:E9 B:H7 W:B5 B:D7 W:E6 B:H9 W:C6 B:J9 W:B6 B:H2 W:H1 B:E8 W:J3 B:E7 W:A2 B:G5 W:H5 B:J1 W:C8 B:J2 W:F2 B:A6 W:F4 B:G8 W:F1 B:G1 W:C9 B:H4 W:C7 B:B7 W:J5 B:A4 W:C5 B:D5 W:A9 B:F5 W:F9 B:E4 W:H1 B:A7 W:H5 B:E1 W:B1 B:A2 W:A1 B:D1 B:H6 W:J5 B:C1 W:A1 B:B1 B:G1 W:E2 B:F1 W:G2 B:H5 W:G3 B:F2 W:G2 B:B9 W:A8 B:F9 W:C9 B:F8 W:G7 B:F7 W:C7 B:C8 W:B5 B:G3 W:C5 B:C3 W:D9 B:C4 W:A9 B:B8 W:A9 B:B6 B:A8 B:C6 W:C5 B:B5 B:E9 W:C9 B:D9" ## for m in s.split(): ## m = string_as_move(m.split(":")[1]) ## if not g.make_move(m): ## print "illegal move:", move_as_string(m) ## break ## print g.current_board return g
def test_c_ladder_alpha_beta(name="kgs/simple_ladders2.sgf", pos_str="C3", depth=1, seed=1, undo_count=0, limit=0, print_pos=True): c_board.set_random_seed(seed) g = load_sgf.load_file(name, game_class=CGame) undo_count0 = undo_count while undo_count: g.undo_move() undo_count = undo_count - 1 pos = string_as_move(pos_str) block_color = g.current_board.goban[pos] if g.current_board.side == block_color: g.make_move(PASS_MOVE) if print_pos: print g.current_board print pos_str #g.probabilistic_alpha_beta(string_as_move(pos), 80) nodes0 = c_board.get_trymove_counter() t0 = time.time() score = c_board.alpha_beta_search_random(pos, g.c_color(), depth, -2, 2, limit) t1 = time.time() nodes1 = c_board.get_trymove_counter() moves = [] t_used = t1 - t0 t_used = round(t_used, int(-math.log10(t_used) + 2)) nodes_s = (nodes1 - nodes0) / (t1 - t0) if nodes_s >= 1000000: nodes_s = "%sM" % round(nodes_s / 1000000., 1) else: nodes_s = "%sK" % round(nodes_s / 1000.) if nodes_s >= 10000: nodes_s = nodes_s.replace(".0", "") limit_s = str(limit / 1000000.) limit_s = limit_s.replace(".0", "") s = "%-6s %-4s %-5s %-6s %-9s %-7s %s" % ( limit_s, undo_count0, depth, score, nodes1 - nodes0, t_used, nodes_s) print s return s, score
def test_ld_all_blocks(filename, colors): config.time_per_move_limit = 100000000 config.games_per_move_limit = 100000 g = load_sgf.load_file(filename) cb = g.current_board print cb pos_lst = [] for block in cb.iterate_blocks(colors): pos = block.get_origin() pos_lst.append(pos) for pos in pos_lst: print "test:", move_as_string(pos), cb.side c_board.clear_result_table() c_board.set_random_seed(1) g.select_uct_move(pos=pos) g.make_move(PASS_MOVE) print "test opposite:", move_as_string(pos), cb.side c_board.clear_result_table() c_board.set_random_seed(1) g.select_uct_move(pos=pos) g.undo_move()
def ld_score_moves(filename=None, g=None): #config.debug_output = open("ld_score.log", "a") score_dict = {} config.time_per_move_limit = 100000000 config.games_per_move_limit = 10000 if filename: g = load_sgf.load_file(filename) cb = g.current_board dprintnl(cb) side0 = cb.side pos_lst = [] for block in cb.iterate_blocks(BLACK + WHITE): #for block in cb.iterate_blocks(WHITE): pos = block.get_origin() pos_lst.append(pos) for pos in pos_lst: block_color = cb.goban[pos] score_opponent = 0.0 score_our = {} for color_switch in (False, True): if not color_switch: dprintnl("test:", move_as_string(pos), cb.side) else: g.make_move(PASS_MOVE) dprintnl("test opposite:", move_as_string(pos), cb.side) c_board.clear_result_table() c_board.set_random_seed(1) g.select_uct_move(pos=pos) for move in g.list_moves(): dprintnl(move_as_string(pos), "move:", move_as_string(move)) g.make_move(move) result = c_board.get_result_table(g.c_color()) g.undo_move() score = result[1] / float(sum(result)) if cb.side == side0: #dprintnl("defend:", score) score_our[move] = score else: #dprintnl("attack:", score) score_opponent = max(score, score_opponent) if color_switch: g.undo_move() dprintnl("summary:") dprintnl(score_opponent, max(score_our.values())) #dprintnl(score_our) score_our_lst = [] for m, value in score_our.items(): score_our_lst.append((value, m)) score_our_lst.sort() score_our_lst = score_our_lst[-10:] dprintnl(score_our_lst) for score, move in score_our_lst: if block_color == side0: #defend score_diff = score - (1 - score_opponent) else: score_diff = score_opponent - (1 - score) dprintnl("score diff:", score_diff, move_as_string(move), score) score_diff *= cb.blocks[pos].size() score_dict[move] = score_dict.get(move, 0.0) + score_diff dprintnl("endof", move_as_string(pos)) dprintnl() move_values = [] for move in score_dict: move_values.append((score_dict[move], move)) move_values.sort() move_values.reverse() for score, move in move_values: dprintnl(score, move_as_string(move)) #for score, move in move_values[:5]: # print score, move_as_string(move) #config.debug_output.flush() if move_values: return move_values[0][1] return PASS_MOVE
#g.make_move(string_as_move("G1")) #g.make_move(string_as_move("D5")) print g.current_board #g.generate_move() return g def gm0(): config.games_per_move_limit = 100000 c_board.clear_result_table() print g.make_move(g.generate_move()) g.undo_move() def gm(): print g.make_move(ld_score_moves(g=g)) if __name__ == "__main__": #test_uct(1) #show_result_tree() c_board.set_random_seed(1) g = CGame(9) #fp = open("patterns.dat") #line = fp.readline() #pattern, g, real_liberties = create_board_from_pattern(line) #uct_search(1, g) #test_wrong_count() g = test_area_scoring() pass
g.set_komi(7.5) g.undo_move() #g.make_move(string_as_move("C1")) #g.make_move(string_as_move("G1")) #g.make_move(string_as_move("D5")) print g.current_board #g.generate_move() return g def gm0(): config.games_per_move_limit = 100000 c_board.clear_result_table() print g.make_move(g.generate_move()) g.undo_move() def gm(): print g.make_move(ld_score_moves(g=g)) if __name__=="__main__": #test_uct(1) #show_result_tree() c_board.set_random_seed(1) g = CGame(9) #fp = open("patterns.dat") #line = fp.readline() #pattern, g, real_liberties = create_board_from_pattern(line) #uct_search(1, g) #test_wrong_count() g = test_area_scoring() pass
def ld_score_moves(filename = None, g = None): #config.debug_output = open("ld_score.log", "a") score_dict = {} config.time_per_move_limit = 100000000 config.games_per_move_limit = 10000 if filename: g = load_sgf.load_file(filename) cb = g.current_board dprintnl(cb) side0 = cb.side pos_lst = [] for block in cb.iterate_blocks(BLACK+WHITE): #for block in cb.iterate_blocks(WHITE): pos = block.get_origin() pos_lst.append(pos) for pos in pos_lst: block_color = cb.goban[pos] score_opponent = 0.0 score_our = {} for color_switch in (False, True): if not color_switch: dprintnl("test:", move_as_string(pos), cb.side) else: g.make_move(PASS_MOVE) dprintnl("test opposite:", move_as_string(pos), cb.side) c_board.clear_result_table() c_board.set_random_seed(1) g.select_uct_move(pos=pos) for move in g.list_moves(): dprintnl(move_as_string(pos), "move:", move_as_string(move)) g.make_move(move) result = c_board.get_result_table(g.c_color()) g.undo_move() score = result[1] / float(sum(result)) if cb.side==side0: #dprintnl("defend:", score) score_our[move] = score else: #dprintnl("attack:", score) score_opponent = max(score, score_opponent) if color_switch: g.undo_move() dprintnl("summary:") dprintnl(score_opponent, max(score_our.values())) #dprintnl(score_our) score_our_lst = [] for m, value in score_our.items(): score_our_lst.append((value, m)) score_our_lst.sort() score_our_lst = score_our_lst[-10:] dprintnl(score_our_lst) for score, move in score_our_lst: if block_color==side0: #defend score_diff = score - (1 - score_opponent) else: score_diff = score_opponent - (1 - score) dprintnl("score diff:", score_diff, move_as_string(move), score) score_diff *= cb.blocks[pos].size() score_dict[move] = score_dict.get(move, 0.0) + score_diff dprintnl("endof", move_as_string(pos)) dprintnl() move_values = [] for move in score_dict: move_values.append((score_dict[move], move)) move_values.sort() move_values.reverse() for score, move in move_values: dprintnl(score, move_as_string(move)) #for score, move in move_values[:5]: # print score, move_as_string(move) #config.debug_output.flush() if move_values: return move_values[0][1] return PASS_MOVE