Ejemplo n.º 1
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.º 2
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:
            print(
Ejemplo n.º 3
0
        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
        break
Ejemplo n.º 4
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