def test_match(use_gpu, search_limit, initial_life=1, reuse=False, show_info=False): # print test game print("\n<test game>") ckpt_path = "ckpt/model" tree = search.Tree(ckpt_path, use_gpu, 0, reuse=reuse) fbg = FizzBuzzGame(initial_life) continue_game = True while (continue_game): num = tree.search(fbg, search_limit, use_dirichlet=False, show_info=show_info) continue_game = fbg.play(num) fbg.print_record()
elif arg.find("clean") >= 0: clean = True elif arg.find("main_time") >= 0: main_time = float(arg[arg.find("=") + 1:]) elif arg.find("byoyomi") >= 0: byoyomi = float(arg[arg.find("=") + 1:]) elif arg.find("cpu") >= 0: use_gpu = False if launch_mode == 0: gtp.call_gtp(main_time, byoyomi, quick, clean, use_gpu) elif launch_mode == 1: b = Board() if not random: tree = search.Tree("models/bsk-model-1.ckpt", use_gpu) while b.move_cnt < BVCNT * 2: prev_move = b.prev_move if random: move = b.random_play() elif quick: move = rv2ev(np.argmax(tree.evaluate(b)[0][0])) b.play(move, False) else: move, _ = tree.search(b, 0, clean=clean) b.play(move, False) b.showboard() if prev_move == PASS and move == PASS: break
clean = True elif arg.find("main_time") >= 0: main_time = float(arg[arg.find("=") + 1:]) elif arg.find("byoyomi") >= 0: byoyomi = float(arg[arg.find("=") + 1:]) elif arg.find("cpu") >= 0: use_gpu = False if launch_mode == 0: gtp.call_gtp(main_time, byoyomi, quick, clean, use_gpu) elif launch_mode == 1: b = Board() if not random: # 读取训练模型,声明tree对象 tree = search.Tree("model.ckpt", use_gpu) tree.main_time=main_time # 下棋主循环 while b.move_cnt < BVCNT * 2: # 记录上一步走的棋,来判断游戏是否结束 prev_move = b.prev_move if random: # 随机下一步 move = b.random_play() elif quick: #得到棋盘评价最高的点,选取该点 #返回两行,一行策略,一行价值 move = rv2ev(np.argmax(tree.evaluate(b)[0][0])) b.play(move, False)
elif arg.find("main_time") >= 0: main_time = float(arg[arg.find("=") + 1:]) elif arg.find("byoyomi") >= 0: byoyomi = float(arg[arg.find("=") + 1:]) elif arg.find("cpu") >= 0: use_gpu = False elif arg.find("path") >= 0: ckpt_path = arg[arg.find("=") + 1:] if launch_mode == 0: gtp.call_gtp(main_time, byoyomi, quick, clean, use_gpu, ckpt_path) elif launch_mode == 1: b = Board() if not random: tree = search.Tree(ckpt_path, use_gpu) while b.move_cnt < BVCNT * 2: prev_move = b.prev_move if random: move = b.random_play() elif quick: move = rv2ev(np.argmax(tree.evaluate(b)[0][0])) b.play(move, False) else: move, _ = tree.search(b, 0, clean=clean) b.play(move, False) b.showboard() if prev_move == PASS and move == PASS: break
def feed_match(feed, match_cnt, search_limit, ckpt_path, initial_life=1, use_gpu=True, gpu_idx=0, reuse=False, show_info=True): # delete old feed feed.forget() tree = search.Tree(ckpt_path, use_gpu, gpu_idx, reuse) fbg = FizzBuzzGame(initial_life) prob_leaf = np.full((FEATURE_SIZE), 0.0) correct_cnt = 0 play_cnt = 0 lengths = [] print("") for i in range(match_cnt): fbg.clear() tree.clear() continue_game = True while (continue_game and fbg.next_num <= DRAW_NB): # show log only in the first match show_info = show_info and i == 0 num = tree.search(fbg, search_limit, True, show_info) prob = tree.node[tree.root_id].visit_cnt prob = prob if prob.sum() == 0 else prob.astype(float) / prob.sum() feed.append(fbg.feature(), prob, 0) if fbg.legal(num, fbg.next_num): correct_cnt += 1 play_cnt += 1 continue_game = fbg.play(num) stdout.write("\r%03d/%03d games" % (i + 1, match_cnt)) stdout.flush() is_draw = np.count_nonzero(fbg.lives) == PLAYER_NB continue_length = DRAW_NB if is_draw else fbg.next_num - 2 lengths.append(continue_length) result = 0 if is_draw else 1 feed.append(fbg.feature(), prob_leaf, result) for j in range(fbg.next_num): id_ = -(j + 1) feed._result[id_] = result result = -result print("") accuracy = float(correct_cnt) / play_cnt * 100 # percent ave_length = float(sum(lengths)) / len(lengths) print("match: accuracy=%.1f[%%] average length=%.1f" % (accuracy, ave_length)) log_file = open("match_log.txt", "a") log_file.write("%.2f\t%.2f\n" % (accuracy, ave_length)) log_file.close() return accuracy