Exemple #1
0
def start():
    parser = create_parser()
    args = parser.parse_args()
    config_type = args.type

    config = Config(config_type=config_type)
    setup(config, args)

    logger.info('Config type: %s' % (config_type))

    if args.cmd == 'self':
        from cchess_alphazero.worker import self_play
        config.opts.light = True  # use lighten environment
        return self_play.start(config)
    elif args.cmd == 'self2':
        from cchess_alphazero.worker import self_play2
        config.opts.light = True  # use lighten environment
        return self_play2.start(config)
    elif args.cmd == 'opt':
        from cchess_alphazero.worker import optimize
        return optimize.start(config)
    elif args.cmd == 'play':
        from cchess_alphazero.play_games import play
        config.opts.light = False
        pwhc = PlayWithHumanConfig()
        pwhc.update_play_config(config.play)
        logger.info(f"AI move first : {args.ai_move_first}")
        play.start(config, not args.ai_move_first)
def test_optimise():
    from cchess_alphazero.worker.optimize import start
    from cchess_alphazero.config import Config
    from cchess_alphazero.lib.logger import setup_logger
    c = Config('mini')
    c.resource.create_directories()
    setup_logger(c.resource.main_log_path)
    start(c)
Exemple #3
0
def test_download():
    from cchess_alphazero.lib.web_helper import download_file
    from cchess_alphazero.config import Config
    c = Config('mini')
    url = 'http://alphazero.52coding.com.cn/model_best_weight.h5'
    path = '/Users/liuhe/Downloads/model_best_weight.h5'
    res = download_file(url, path)
    print(res)
Exemple #4
0
def fixbug():
    from cchess_alphazero.config import Config
    from cchess_alphazero.lib.data_helper import get_game_data_filenames, read_game_data_from_file, write_game_data_to_file
    import cchess_alphazero.environment.static_env as senv
    c = Config('distribute')
    files = get_game_data_filenames(c.resource)
    cnt = 0
    fix = 0
    draw_cnt = 0
    for filename in files:
        try:
            data = read_game_data_from_file(filename)
        except:
            print(f"error: {filename}")
            os.remove(filename)
            continue
        state = data[0]
        real_data = [state]
        need_fix = True
        draw = False
        action = None
        value = None
        is_red_turn = True
        for item in data[1:]:
            action = item[0]
            value = -item[1]
            if value == 0:
                need_fix = False
                draw = True
                draw_cnt += 1
                break
            state = senv.step(state, action)
            is_red_turn = not is_red_turn
            real_data.append([action, value])
        if not draw:
            game_over, v, final_move = senv.done(state)
            if final_move:
                v = -v
                is_red_turn = not is_red_turn
            if not is_red_turn:
                v = -v
            if not game_over:
                v = 1
            # print(game_over, v, final_move, state)
            if v == data[1][1]:
                need_fix = False
            else:
                need_fix = True
        if need_fix:
            write_game_data_to_file(filename, real_data)
            # print(filename)
            fix += 1
        cnt += 1
        if cnt % 1000 == 0:
            print(cnt, fix, draw_cnt)
    print(f"all {cnt}, fix {fix}, draw {draw_cnt}")
Exemple #5
0
def test_request():
    from cchess_alphazero.lib.web_helper import http_request
    from cchess_alphazero.config import Config
    c = Config('mini')
    url = 'http://alphazero.52coding.com.cn/api/add_model'
    digest = 'd6fce85e040a63966fa7651d4a08a7cdba2ef0e5975fc16a6d178c96345547b3'
    elo = 0
    data = {'digest': digest, 'elo': elo}
    res = http_request(url, post=True, data=data)
    print(res)
Exemple #6
0
def test_upload():
    from cchess_alphazero.lib.web_helper import upload_file
    from cchess_alphazero.config import Config
    c = Config('mini')
    url = 'http://alphazero.52coding.com.cn/api/upload_game_file'
    path = '/Users/liuhe/Documents/Graduation Project/ChineseChess-AlphaZero/data/play_data/test.json'
    filename = 'test.json'
    data = {'digest': 'test', 'username': '******'}
    res = upload_file(url, path, filename=filename, data=data)
    print(res)
Exemple #7
0
def start():
    parser = create_parser()
    args = parser.parse_args()
    config_type = args.type

    config = Config(config_type=config_type)
    setup(config, args)

    logger.info('Config type: %s' % (config_type))
    config.opts.piece_style = args.piece_style
    config.opts.bg_style = args.bg_style
    config.internet.distributed = args.distributed

    if args.cmd == 'self':
        if args.ucci:
            import cchess_alphazero.worker.play_with_ucci_engine as self_play
        else:
            if mp.get_start_method() == 'spawn':
                import cchess_alphazero.worker.self_play_windows as self_play
            else:
                from cchess_alphazero.worker import self_play
        return self_play.start(config)
    elif args.cmd == 'opt':
        from cchess_alphazero.worker import optimize
        return optimize.start(config)
    elif args.cmd == 'play':
        if args.cli:
            import cchess_alphazero.play_games.play_cli as play
        else:
            from cchess_alphazero.play_games import play
        config.opts.light = False
        pwhc = PlayWithHumanConfig()
        pwhc.update_play_config(config.play)
        logger.info(f"AI move first : {args.ai_move_first}")
        play.start(config, not args.ai_move_first)
    elif args.cmd == 'eval':
        if args.elo == False:
            from cchess_alphazero.worker import evaluator
        else:
            import cchess_alphazero.worker.compute_elo as evaluator
        config.eval.update_play_config(config.play)
        evaluator.start(config)
    elif args.cmd == 'sl':
        if args.onegreen:
            import cchess_alphazero.worker.sl_onegreen as sl
            sl.start(config, args.skip)
        else:
            from cchess_alphazero.worker import sl
            sl.start(config)

    elif args.cmd == 'ob':
        from cchess_alphazero.play_games import ob_self_play
        pwhc = PlayWithHumanConfig()
        pwhc.update_play_config(config.play)
        ob_self_play.start(config, args.ucci, args.ai_move_first)
Exemple #8
0
def test_sl():
    from cchess_alphazero.worker import sl
    from cchess_alphazero.config import Config
    from cchess_alphazero.environment.lookup_tables import ActionLabelsRed, flip_policy, flip_move
    c = Config('mini')
    labels_n = len(ActionLabelsRed)
    move_lookup = {move: i for move, i in zip(ActionLabelsRed, range(labels_n))}
    slworker = sl.SupervisedWorker(c)
    p1 = slworker.build_policy('0001', False)
    print(p1[move_lookup['0001']])
    p2 = slworker.build_policy('0001', True)
    print(p2[move_lookup[flip_move('0001')]])
def test_light_env():
    from cchess_alphazero.environment.env import CChessEnv
    from cchess_alphazero.config import Config
    c = Config('mini')
    env = CChessEnv(c)
    env.reset()
    print(env.observation)
    env.step('0001')
    print(env.observation)
    env.step('7770')
    print(env.observation)
    env.render()
    print(env.input_planes()[0 + 7:3 + 7])
Exemple #10
0
def plot_model():
    from keras.utils import plot_model
    from cchess_alphazero.agent.model import CChessModel
    from cchess_alphazero.config import Config
    from cchess_alphazero.lib.model_helper import save_as_best_model
    config = Config('distribute')
    model = CChessModel(config)
    model.build()
    save_as_best_model(model)
    plot_model(model.model,
               to_file='model.png',
               show_shapes=True,
               show_layer_names=True)
Exemple #11
0
def start():
    parser = create_parser()
    args = parser.parse_args()
    config_type = args.type

    config = Config(config_type=config_type)
    setup(config, args)

    logger.info('Config type: %s' % (config_type))
    config.opts.piece_style = args.piece_style
    config.opts.bg_style = args.bg_style
    config.internet.distributed = args.distributed

    # # use multiple GPU
    gpus = torch.cuda.device_count()
    if gpus > 1:
        config.opts.use_multiple_gpus = True
        config.opts.gpu_num = gpus
        config.opts.use_gpu = True
        # logger.info(f"User GPU {config.opts.device_list}")
    elif gpus == 1:
        config.opts.use_gpu = True

    if args.cmd == 'self':
        # if args.ucci:
        #     import cchess_alphazero.worker.play_with_ucci_engine as self_play
        # else:
        # if mp.get_start_method() == 'spawn':
        #     import cchess_alphazero.worker.self_play_windows as self_play
        # else:
        #     from cchess_alphazero.worker import self_play
        import cchess_alphazero.worker.self_play_windows as self_play
        return self_play.start(config)
    elif args.cmd == 'opt':
        from cchess_alphazero.worker import optimize
        return optimize.start(config)
    elif args.cmd == 'play':
        if args.cli:
            import cchess_alphazero.play_games.play_cli as play
        else:
            from cchess_alphazero.play_games import play
        config.opts.light = False
        pwhc = PlayWithHumanConfig()
        pwhc.update_play_config(config.play)
        logger.info(f"AI move first : {args.ai_move_first}")
        play.start(config, not args.ai_move_first)
    elif args.cmd == 'pk':
        import cchess_alphazero.worker.pk_player_windows as self_play
        return self_play.start(config)
Exemple #12
0
def test_onegreen2():
    from cchess_alphazero.environment.env import CChessEnv
    import cchess_alphazero.environment.static_env as senv
    from cchess_alphazero.config import Config
    c = Config('mini')
    init = '9999299949999999249999869999999958999999519999999999999999997699'
    env = CChessEnv(c)
    env.reset(init)
    print(env.observation)
    env.render()
    move = senv.parse_onegreen_move('8685')
    env.step(move)
    print(env.observation)
    env.render()
    move = senv.parse_onegreen_move('7666')
    env.step(move)
    print(env.observation)
    env.render()
def test_config():
    from cchess_alphazero.config import Config
    c = Config('mini')
    c.resource.create_directories()
    print(c.resource.project_dir, c.resource.data_dir)
Exemple #14
0

def setup_parameters(config):
    if len(sys.argv) > 1:
        config.internet.username = sys.argv[1]
        print(f'用户名设置为:{config.internet.username}')
    num_cores = mp.cpu_count()
    max_processes = 2
    if len(sys.argv) > 2:
        max_processes = int(sys.argv[2])
    search_threads = 20
    print(
        f"max_processes = {max_processes}, search_threads = {search_threads}")
    config.play.max_processes = max_processes
    config.play.search_threads = search_threads


if __name__ == "__main__":
    sys.setrecursionlimit(10000)
    config_type = 'distribute'
    config = Config(config_type=config_type)
    config.opts.device_list = '0'
    config.resource.create_directories()
    setup_logger(config.resource.main_log_path)
    config.internet.distributed = False
    config.opts.log_move = True
    config.opts.has_history = True
    setup_parameters(config)
    # config.internet.download_url = 'http://alphazero-1251776088.cossh.myqcloud.com/model/128x7/model_best_weight.h5'
    self_play.start(config)
Exemple #15
0
if _PATH_ not in sys.path:
    sys.path.append(_PATH_)

from cchess_alphazero.lib.logger import setup_logger
from cchess_alphazero.config import Config, PlayWithHumanConfig
import cchess_alphazero.worker.self_play_windows as self_play


def setup_parameters(config):
    num_cores = mp.cpu_count()
    max_processes = num_cores // 2 if num_cores < 20 else 10
    search_threads = 10 if num_cores < 10 else (num_cores // 10) * 10
    print(
        f"max_processes = {max_processes}, search_threads = {search_threads}")
    config.play.max_processes = max_processes
    config.play.search_threads = search_threads


if __name__ == "__main__":
    mp.freeze_support()
    sys.setrecursionlimit(10000)
    config_type = 'distribute'
    config = Config(config_type=config_type)
    config.device_list = '0'
    config.resource.create_directories()
    setup_logger(config.resource.main_log_path)
    config.internet.distributed = True
    setup_parameters(config)
    self_play.start(config)