Example #1
0
 def setUp(self):
     # Remove the symmetries for reproductibility
     import symmetry
     symmetry.SYMMETRIES = symmetry.SYMMETRIES[0:1]
     from random import seed
     seed(0)
     init_simulation_workers()
Example #2
0
def main():
    print("Starting run (v{})".format(__version__))
    init_directories()
    if conf['THREAD_SIMULATION']:
        init_simulation_workers()
    model_name = "model_1"
    model = create_initial_model(name=model_name)

    while True:
        model = load_latest_model()
        best_model = load_best_model()
        train(model, game_model_name=best_model.name)
        evaluate(best_model, model)
        K.clear_session()
Example #3
0
    def test_save_sgf(self):
        model = DummyModel()
        init_simulation_workers()
        mcts_simulations = 8  # mcts batch size is 8 and we need at least one batch
        game_data = play_game(model,
                              model,
                              mcts_simulations,
                              conf['STOP_EXPLORATION'],
                              self_play=True,
                              num_moves=10)
        save_game_sgf("test_model", 0, game_data)
        destroy_simulation_workers()

        os.remove("games/test_model/game_000.sgf")
        os.removedirs("games/test_model")
Example #4
0
    def setUp(self):
        # Remove the symmetries for reproductibility
        import symmetry
        symmetry.SYMMETRIES = symmetry.SYMMETRIES[0:1]
        size = conf['SIZE']
        tree = {
            'count': 0,
            'mean_value': 0,
            'value': 0,
            'parent': None,
            'virtual_loss': 0,
            'subtree': {
                0: {
                    'count': 0,
                    'p': 1,
                    'value': 0,
                    'mean_value': 0,
                    'virtual_loss': 0,
                    'subtree': {}
                },
                1: {
                    'count': 0,
                    'p': 0,
                    'mean_value': 0,
                    'value': 0,
                    'virtual_loss': 0,
                    'subtree': {}
                }
            }
        }
        tree['subtree'][0]['parent'] = tree
        tree['subtree'][1]['parent'] = tree

        board, player = game_init()
        model = DummyModel()
        self.model = model
        self.board = board
        self.tree = tree
        init_simulation_workers()
Example #5
0
    def run(self):
        # set environment
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = str(self._gpuid)
        logger.info('cuda_visible_device %s',
                    os.environ["CUDA_VISIBLE_DEVICES"])
        if conf['THREAD_SIMULATION']:
            init_simulation_workers()

        # load models
        latest_model, best_model = self.load_model()

        while True:
            if latest_model.name != best_model.name:
                total = 0
                wins = 0
                desc = "Evaluation %s vs %s" % (latest_model.name,
                                                best_model.name)
                tq = tqdm(range(EVALUATE_N_GAMES), desc=desc)
                for game in tq:
                    if self._one_game_only >= 0 and tq != game:
                        continue
                    directory = os.path.join(EVAL_DIR, latest_model.name,
                                             "game_%03d" % game)
                    if os.path.isdir(directory):
                        continue
                    try:
                        os.makedirs(directory)
                    except Exception:
                        continue

                    start = datetime.datetime.now()
                    game_data = play_game(best_model,
                                          latest_model,
                                          MCTS_SIMULATIONS,
                                          stop_exploration=0)
                    stop = datetime.datetime.now()

                    # Some statistics
                    winner_model = game_data['winner_model']
                    if winner_model == latest_model.name:
                        wins += 1
                    total += 1
                    moves = len(game_data['moves'])
                    new_desc = desc + " (winrate:%s%% %.2fs/move)" % (int(
                        wins / total * 100), (stop - start).seconds / moves)
                    tq.set_description(new_desc)

                    # save_game_data(best_model.name, game, game_data)
                    self.save_eval_game(latest_model.name, game, winner_model)
                    if self._one_game_only >= 0:
                        break
            else:
                logger.info("No new trained model")
                if self._forever:
                    logger.info("Sleep for %s seconds", conf['SLEEP_SECONDS'])
                    time.sleep(conf['SLEEP_SECONDS'])
            if not self._forever:
                break
            latest_model, best_model = self.load_model()

        destroy_simulation_workers()