def play(max_episode=10): episode = 0 start_mark = 'O' env = TicTacToeEnv() agents = [BaseAgent('O'), BaseAgent('X')] while episode < max_episode: env.set_start_mark(start_mark) state = env.reset() _, mark = state done = False while not done: env.show_turn(True, mark) agent = agent_by_mark(agents, mark) ava_actions = env.available_actions() action = agent.act(state, ava_actions) state, reward, done, info = env.step(action) env.render() if done: env.show_result(True, mark, reward) break else: _, mark = state # rotate start start_mark = next_mark(start_mark) episode += 1
def play(show_number): env = TicTacToeEnv(show_number=show_number) agents = [MinimaxAgent('O'), HumanAgent('X')] episode = 0 while True: state = env.reset() _, mark = state done = False env.render() while not done: agent = agent_by_mark(agents, mark) env.show_turn(True, mark) ava_actions = env.available_actions() if mark=='O': n,action=agent.act(state, ava_actions) else: action = agent.act(state, ava_actions) if action is None: sys.exit() state, reward, done, info = env.step(action) print('') env.render() if done: env.show_result(True, mark, reward) break else: _, _ = state mark = next_mark(mark) episode += 1
def _bench(max_episode, model_file, show_result=True): """Benchmark given model. Args: max_episode (int): Episode count to benchmark. model_file (str): Learned model file name to benchmark. show_result (bool): Output result to stdout. Returns: (dict): Benchmark result. """ minfo = load_model(model_file) agents = [BaseAgent('O'), TDAgent('X', 0, 0)] show = False start_mark = 'O' env = TicTacToeEnv() env.set_start_mark(start_mark) episode = 0 results = [] for i in tqdm(range(max_episode)): env.set_start_mark(start_mark) state = env.reset() _, mark = state done = False while not done: agent = agent_by_mark(agents, mark) ava_actions = env.available_actions() action = agent.act(state, ava_actions) state, reward, done, info = env.step(action) if show: env.show_turn(True, mark) env.render(mode='human') if done: if show: env.show_result(True, mark, reward) results.append(reward) break else: _, mark = state # rotation start start_mark = next_mark(start_mark) episode += 1 o_win = results.count(1) x_win = results.count(-1) draw = len(results) - o_win - x_win mfile = model_file.replace(CWD + os.sep, '') minfo.update( dict(base_win=o_win, td_win=x_win, draw=draw, model_file=mfile)) result = json.dumps(minfo) if show_result: print(result) return result
def _play(load_file, vs_agent, show_number): """Play with learned model. Make TD agent and adversarial agnet to play with. Play and switch starting mark when the game finished. TD agent behave no exploring action while in play mode. Args: load_file (str): vs_agent (object): Enemy agent of TD agent. show_number (bool): Whether show grid number for visual hint. """ load_model(load_file) env = TicTacToeEnv(show_number=show_number) td_agent = TDAgent('X', 0, 0) # prevent exploring start_mark = 'O' agents = [vs_agent, td_agent] while True: # start agent rotation env.set_start_mark(start_mark) state = env.reset() _, mark = state done = False # show start board for human agent if mark == 'O': env.render(mode='human') while not done: agent = agent_by_mark(agents, mark) human = isinstance(agent, HumanAgent) env.show_turn(True, mark) ava_actions = env.available_actions() if human: action = agent.act(ava_actions) if action is None: sys.exit() else: action = agent.act(state, ava_actions) state, reward, done, info = env.step(action) env.render(mode='human') if done: env.show_result(True, mark, reward) break else: _, mark = state # rotation start start_mark = next_mark(start_mark)
def play(show_number): env = TicTacToeEnv(show_number=show_number) agents = [HumanAgent(HUMAN_MARK)] episode = 0 j = 0 while True: state = env.reset() _, mark = state done = False env.render() i = 0 if j == 0: Papa = Node(state, None, [1, 2, 3, 4, 5, 6, 7, 8, 9], 0) Papa.fill() j += 1 action = Papa.maxAddress current = Papa.children[Papa.maxAddress] print("X's Turn") while not done: pre_action = action pre_current = current ava_actions = env.available_actions() if i % 2 == 0 and i != 0: print("X's Turn") print("Previous Action: ", pre_action) action, current = pre_current.reach_child(pre_action) print("Playing: ", action) elif i % 2 == 1: print("O's Turn") action = HumanAgent.act(state, ava_actions) i += 1 if action is None: sys.exit() state, reward, done, info = env.step(action - 1) print('') env.render() if done: env.show_result(True, mark, reward) break else: _, _ = state mark = next_mark(mark) episode += 1