Ejemplo n.º 1
0
def test_foxwq():
    for sgf in ["data/fox sgf error.sgf", "data/fox sgf works.sgf"]:
        file = os.path.join(os.path.dirname(__file__), sgf)
        move_tree = KaTrainSGF.parse_file(file)
        katrain = KaTrainBase(force_package_config=True, debug_level=0)
        game = Game(katrain, MagicMock(), move_tree)

        assert [] == move_tree.placements
        assert [] == game.root.placements
        while game.current_node.children:
            assert 1 == len(game.current_node.children)
            game.redo(1)
Ejemplo n.º 2
0
    def test_ai_strategies(self):
        katrain = KaTrainBase(force_package_config=True, debug_level=0)
        engine = KataGoEngine(katrain, katrain.config("engine"))

        game = Game(katrain, engine)
        n_rounds = 3
        for _ in range(n_rounds):
            for strategy in AI_STRATEGIES:
                settings = katrain.config(f"ai/{strategy}")
                move, played_node = generate_ai_move(game, strategy, settings)
                katrain.log(f"Testing strategy {strategy} -> {move}", OUTPUT_INFO)
                assert move.coords is not None
                assert played_node == game.current_node

        assert game.current_node.depth == len(AI_STRATEGIES) * n_rounds

        for strategy in AI_STRATEGIES:
            game = Game(katrain, engine)
            settings = katrain.config(f"ai/{strategy}")
            move, played_node = generate_ai_move(game, strategy, settings)
            katrain.log(f"Testing strategy on first move {strategy} -> {move}", OUTPUT_INFO)
            assert game.current_node.depth == 1
Ejemplo n.º 3
0
from katrain.core.base_katrain import KaTrainBase
from katrain.core.game import Game, KaTrainSGF
from katrain.core.engine import KataGoEngine
from rank_utils import rank_game


def format_rank(rank):
    if rank <= 0:
        return f"{1-rank:5.1f}d"
    else:
        return f"{rank:5.1f}k"


if __name__ == "__main__":
    kt = KaTrainBase(force_package_config=True)
    e_config = kt.config("engine")
    e_config["max_visits"] = e_config[
        "fast_visits"] = 1  # since it's just policy anyway
    engine = KataGoEngine(kt, e_config)

    for filename in glob.glob("sgf_ogs/*.sgf"):
        game = Game(kt, engine, move_tree=KaTrainSGF.parse_file(filename))
        size = game.board_size
        len_segment = 80

        ranks = rank_game(game, len_segment)
        if not ranks:
            continue
        print("* File name: {0:s}".format(filename))
        for start, end, rank in ranks:
Ejemplo n.º 4
0
        assert rank[-1] == "k", f"unexpected rank {rank}"
        return 1 - int(rank[:-1])


def polyfit(x, y, degree=1):
    coeffs = np.polyfit(x, y, degree)
    correlation = np.corrcoef(x, y)[0, 1]
    results = {
        "coef": coeffs.tolist(),
        "r": correlation,
        "rsq": correlation**2
    }
    return results


katrain = KaTrainBase(force_package_config=True, debug_level=0)
combined_settings = {**katrain.config("engine"), **settings}
engine = KataGoEngine(katrain, {**katrain.config("engine"), **settings})
thresholds = katrain.config("trainer/eval_thresholds")

games = []
n = 0
for sgf in os.listdir("sgftest/"):
    if sgf.lower().endswith("sgf"):
        print(sgf)
        with open(os.path.join("sgftest", sgf)) as f:
            move_tree = KaTrainSGF.parse_sgf(f.read())
        games.append(
            Game(katrain, engine, move_tree=move_tree, analyze_fast=False))
    n += 1
    if n >= 30000:  # small test=3
Ejemplo n.º 5
0
 def test_ai_rank_estimation(self):
     katrain = KaTrainBase(force_package_config=True, debug_level=0)
     for strategy in AI_STRATEGIES:
         settings = katrain.config(f"ai/{strategy}")
         rank = ai_rank_estimation(strategy, settings)
         assert -20 <= rank <= 9