Example #1
0
def create_tree():
    result_lst = read_results(cgos_dir)
    hash_tree = {}
    i = 0
    for result in result_lst:
        if result["P" + opponent_color]==opponent:
            random.seed(1)
            g = load_sgf.load_file(cgos_dir + result["File"])
            cboard = g.current_board
            #print cboard
            print result
            black_won = result["RE"][0]=="B"
            next_move = PASS_MOVE
            while True:
                key = g.search_key()
                moves = hash_tree.get(key, {})
                counts = moves.get(next_move, [0, 0])
                counts[black_won] += 1
                moves[next_move] = counts
                hash_tree[key] = moves
                if len(g.move_history)==0:
                    break
                next_move = g.move_history[-1]
                g.undo_move()
            i += 1
            print i, len(hash_tree), len(hash_tree)/float(i)
            #print hash_tree
            #random.seed(1)
            #g = Game(9)
            #print hash_tree[g.search_key()]
            #break

    fp = open(opening_file(), "w")
    cPickle.dump(hash_tree, fp)
Example #2
0
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
Example #3
0
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))
Example #4
0
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))
Example #5
0
def test_same(move_str):
    g = load_sgf.load_file("kgs/simple_atari0.sgf", game_class=RandomGame)
    cboard = g.current_board
    print cboard
    g.make_move(string_as_move(move_str))
    result_sum = 0
    for i in range(1000):
        result_sum += play_game_c(g, string_as_move("A1"))
    print move_str, result_sum
Example #6
0
def test_wrong_count():
    global g
    #g = load_sgf.load_file("../../kgs/Aloriless-SimpleBot_uct_brown_wrong_count.sgf")
    #g = load_sgf.load_file("kgs/Aloriless-SimpleBot_uct_weak_ha3_won.sgf")
    #g = load_sgf.load_file("kgs/Aloriless-SimpleBot_uct_shape_still_wrong_scoring.sgf")
    #g = load_sgf.load_file("kgs/Aloriless-SimpleBot_still_loses_trivially.sgf")
    g = load_sgf.load_file("kgs/Aloriless-SimpleBot_again_loses_trivially.sgf")
    c_board.set_komi(3.5)
    print c_board.score_board()
Example #7
0
def one_game(file_name = file_names % 1):
    g = load_sgf.load_file(file_name)
    cboard = g.current_board
    while True:
        print "#", file_name, len(g.move_history)
        sys.stdout.flush()
        yield cboard
        if not g.undo_move():
            break
Example #8
0
def test_wrong_count():
    global g
    #g = load_sgf.load_file("../../kgs/Aloriless-SimpleBot_uct_brown_wrong_count.sgf")
    #g = load_sgf.load_file("kgs/Aloriless-SimpleBot_uct_weak_ha3_won.sgf")
    #g = load_sgf.load_file("kgs/Aloriless-SimpleBot_uct_shape_still_wrong_scoring.sgf")
    #g = load_sgf.load_file("kgs/Aloriless-SimpleBot_still_loses_trivially.sgf")
    g = load_sgf.load_file("kgs/Aloriless-SimpleBot_again_loses_trivially.sgf")
    c_board.set_komi(3.5)
    print c_board.score_board()
Example #9
0
def test_same(move_str):
    g = load_sgf.load_file("kgs/simple_atari0.sgf", game_class = RandomGame)
    cboard = g.current_board
    print cboard
    g.make_move(string_as_move(move_str))
    result_sum = 0
    for i in range(1000):
        result_sum += play_game_c(g, string_as_move("A1"))
    print move_str, result_sum
Example #10
0
def one_game(file_name=file_names % 1):
    g = load_sgf.load_file(file_name)
    cboard = g.current_board
    while True:
        print "#", file_name, len(g.move_history)
        sys.stdout.flush()
        yield cboard
        if not g.undo_move():
            break
Example #11
0
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
Example #12
0
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
Example #13
0
def test_block_capture(name, pos_str, n):
    pos = string_as_move(pos_str)
    g = load_sgf.load_file(name, game_class=CGame)
    print g.current_board
    print pos_str, n
    t0 = time.time()
    res1 = c_board.test_block(pos, CWHITE, n)
    print "White:", res1
    res2 = c_board.test_block(pos, CBLACK, n)
    print "Black:", res2
    print "Diff:", res2 - res1
    t1 = time.time()
    print "Time:", t1 - t0
Example #14
0
def test_block_capture(name, pos_str, n):
    pos = string_as_move(pos_str)
    g = load_sgf.load_file(name, game_class = CGame)
    print g.current_board
    print pos_str, n
    t0 = time.time()
    res1 = c_board.test_block(pos, CWHITE, n)
    print "White:", res1
    res2 = c_board.test_block(pos, CBLACK, n)
    print "Black:", res2
    print "Diff:", res2-res1
    t1 = time.time()
    print "Time:", t1-t0
Example #15
0
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
Example #16
0
def test_ladder_alpha_beta(name = "kgs/simple_ladders2.sgf", pos_str = "C3", depth = 1, seed = 1):
    random.seed(seed)
    g = load_sgf.load_file(name, game_class = BGame)
    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)
    print g.current_board
    print pos_str
    #g.probabilistic_alpha_beta(string_as_move(pos), 80)
    nodes0 = g.node_count
    score, moves = g.probabilistic_alpha_beta(pos, depth)
    nodes1 = g.node_count
    print score, move_list_as_string(moves), nodes1-nodes0
Example #17
0
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
Example #18
0
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)
Example #19
0
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()
Example #20
0
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()
Example #21
0
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)
Example #22
0
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
Example #23
0
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
Example #24
0
def test_ladder(n, name = "kgs/simple_ladders2.sgf"):
    for pos_str, moves_string in (("C3", "D3 C4 C5 D4 E4 D5 D6 E5 F5 E6 E7 F6 G6 F7 F8 G7 H7 G8 H8 G9 F9 H9 J9"),
                              ("G3", "F3 G4 G5 F4 E4 F5 F6 E5 D5 E6 E7 D6 C6 D7 D8 C7 PASS")):
        print "="*60
        g = load_sgf.load_file(name, game_class = CGame)
        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)

        for move in string_as_move_list(moves_string):
            color = g.current_board.side
            print g.current_board
            score, move2 = g.monte_carlo_local_moves(pos, n)
            g.make_move(move)
            print color, move_as_string(move), score, move_as_string(move2)
            print "-"*60
Example #25
0
def test_ladder_alpha_beta(name="kgs/simple_ladders2.sgf",
                           pos_str="C3",
                           depth=1,
                           seed=1):
    random.seed(seed)
    g = load_sgf.load_file(name, game_class=BGame)
    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)
    print g.current_board
    print pos_str
    #g.probabilistic_alpha_beta(string_as_move(pos), 80)
    nodes0 = g.node_count
    score, moves = g.probabilistic_alpha_beta(pos, depth)
    nodes1 = g.node_count
    print score, move_list_as_string(moves), nodes1 - nodes0
Example #26
0
def test_ladder(n, name="kgs/simple_ladders2.sgf"):
    for pos_str, moves_string in (
        ("C3",
         "D3 C4 C5 D4 E4 D5 D6 E5 F5 E6 E7 F6 G6 F7 F8 G7 H7 G8 H8 G9 F9 H9 J9"
         ), ("G3", "F3 G4 G5 F4 E4 F5 F6 E5 D5 E6 E7 D6 C6 D7 D8 C7 PASS")):
        print "=" * 60
        g = load_sgf.load_file(name, game_class=CGame)
        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)

        for move in string_as_move_list(moves_string):
            color = g.current_board.side
            print g.current_board
            score, move2 = g.monte_carlo_local_moves(pos, n)
            g.make_move(move)
            print color, move_as_string(move), score, move_as_string(move2)
            print "-" * 60
Example #27
0
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
Example #28
0
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()
Example #29
0
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()
Example #30
0
def test_block_capture_local_moves(name, pos_str, n, local_moves=True):
    t0_all = time.time()
    pos = string_as_move(pos_str)
    g = load_sgf.load_file(name, game_class=CGame)
    block_color = g.current_board.goban[pos]
    if g.current_board.side == block_color:
        g.make_move(PASS_MOVE)
    pass_count = 0
    while pass_count < 2:
        print g.current_board
        print pos_str, n
        t0 = time.time()
        score, move = g.monte_carlo_local_moves(pos, n, local_moves)
        t1 = time.time()
        if pass_count == 0:
            print "Attack",
        else:
            print "Defend",
        print "result:", move_as_string(move), score
        print "Time:", t1 - t0
        g.make_move(PASS_MOVE)
        pass_count = pass_count + 1
    t1_all = time.time()
    print "Total time:", t1_all - t0_all
Example #31
0
def test_block_capture_local_moves(name, pos_str, n, local_moves = True):
    t0_all = time.time()
    pos = string_as_move(pos_str)
    g = load_sgf.load_file(name, game_class = CGame)
    block_color = g.current_board.goban[pos]
    if g.current_board.side==block_color:
        g.make_move(PASS_MOVE)
    pass_count = 0
    while pass_count < 2:
        print g.current_board
        print pos_str, n
        t0 = time.time()
        score, move = g.monte_carlo_local_moves(pos, n, local_moves)
        t1 = time.time()
        if pass_count==0:
            print "Attack",
        else:
            print "Defend",
        print "result:", move_as_string(move), score
        print "Time:", t1-t0
        g.make_move(PASS_MOVE)
        pass_count = pass_count + 1
    t1_all = time.time()
    print "Total time:", t1_all-t0_all
Example #32
0
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
Example #33
0
 def load(self):
     self.engine = load_sgf.load_file("tmp.sgf")
     return self.ok()
Example #34
0
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
Example #35
0
def one_position(file_name=file_names % 1):
    g = load_sgf.load_file(file_name)
    yield g.current_board
Example #36
0
 def load(self):
     self.engine = load_sgf.load_file("tmp.sgf")
     return self.ok()
Example #37
0
def one_position(file_name = file_names % 1):
    g = load_sgf.load_file(file_name)
    yield g.current_board
Example #38
0
def t():
    g = load_sgf.load_file("uct_game.sgf", game_class=CGame)
    while len(g.move_history) > 58:
        g.undo_move()
    return g
Example #39
0
def t():
    g = load_sgf.load_file("uct_game.sgf", game_class = CGame)
    while len(g.move_history) > 58:
        g.undo_move()
    return g