def test_update(self): t = TTT(3) prev_state = [[1, 1, 0], [-1, -1, 0], [0, 0, 0]] next_state = [[1, 1, 1], [-1, -1, 0], [0, 0, 0]] prev_state = np.array(prev_state).reshape(-1) next_state = np.array(next_state).reshape(-1) result = t.get_result(next_state) self.assertEqual(result, {'terminated': True, 'score': 5}) q = TabularQ(3) q.set_params(alpha=1, gamma=1) encoded_prev_state = t.get_encoded_state(prev_state) prev_state_index = q.get_index(encoded_prev_state) encoded_next_state = t.get_encoded_state(next_state) next_state_index = q.get_index(encoded_next_state) self.assertEqual(next_state_index, None) q.update(encoded_prev_state, 2, encoded_next_state, 5) updated_row = q._Q[prev_state_index, :] check_row = np.array_equal(updated_row, [0, 0, 5, 0, 0, 0, 0, 0, 0]) self.assertTrue(check_row) # test correct inference : q._is_first_mover = True possible_moves = t.get_available_positions(prev_state) inferred = q.infer(encoded_prev_state, possible_moves, 1) self.assertEqual(inferred, 2) pass
def run_game(self, agent1, agent2, size=3): t = TTT(size) for i in range(size * size): agent = agent1 if i % 2 == 0 else agent2 inferred = agent(t.get_state()) t.put(inferred) if t.is_terminated(): break return t.get_result()
def test_result1(self): t3 = TTT(3) s = [[1, -1, -1], [-1, 1, 1], [1, -1, 1]] s = np.array(s).reshape(-1) result = t3.get_result(s) to_equal = { 'terminated': True, 'score': 1, 'winner': 1, 'lines': [[0, 4, 8]] } self.assertDictEqual(result, to_equal)
def test_game1(self): # 0 1 0 # -1 1 0 # -1 1 0 t = TTT(3) result = t.get_result() self.assertDictEqual(result, {'terminated': False, 'score': 0}) t.put(1) result = t.get_result() self.assertDictEqual(result, {'terminated': False, 'score': 0}) t.put(3) result = t.get_result() self.assertDictEqual(result, {'terminated': False, 'score': 0}) t.put(4) result = t.get_result() self.assertDictEqual(result, {'terminated': False, 'score': 0}) t.put(6) result = t.get_result() self.assertDictEqual(result, {'terminated': False, 'score': 0}) t.put(7) result = t.get_result() self.assertDictEqual(result, {'terminated': True, 'score': 5}) return
def _train_both(self,numOfGames): for _ in tqdm(range(numOfGames)): game = TTT(self._size) self._is_first_mover = True # one complete game : while True: encoded_prev_state = game.get_encoded_state() possible_moves = game.get_available_positions() selected_move = self._epsilon_greedy_train(encoded_prev_state,possible_moves) game.put(selected_move) encoded_next_state = game.get_encoded_state() result = game.get_result() self.update(encoded_prev_state,selected_move,encoded_next_state,result['score']) if result['terminated']: break pass pass
def test_deterministic_vs_minimax(self): # gamma, alpha == 1 guarantees that for endstates s and optimal move a, # Q(s,a) = R(s,a) IF Q(s,a) IS NOT 0 # Here, R(s,a) is the score of the terminated state parameters = { "ep_train": 0.5, "ep_infer": 0, "gamma": 1, "alpha": 1, "agent_for": 'both', } q = TabularQ(3) q.set_params(**parameters) q.train(numOfGames=500) s = Settings() minimax = minimax_load(s.path('minimax')) t = TTT(3) Q = q._Q to_check_state_indices = np.where(Q != [0, 0, 0, 0, 0, 0, 0, 0, 0])[0] to_check_state_indices = map(int, to_check_state_indices) for state_index in to_check_state_indices: self.assertFalse( np.array_equal(Q[state_index], np.array([0, 0, 0, 0, 0, 0, 0, 0, 0]))) state = q.get_state(state_index) encoded_state = t.get_encoded_state(state) mover = t.get_mover(state=state) possible_moves = t.get_available_positions(state) if mover == 1: best_move_q = np.argmax(Q[state_index]) if int(Q[state_index, best_move_q]) is not 0: move_inferred = q.infer(encoded_state, possible_moves, mover) q_value_1 = Q[state_index, best_move_q] q_value_2 = Q[state_index, move_inferred] self.assertEqual(q_value_1, q_value_2) elif mover == -1: best_move_q = np.argmin(Q[state_index]) if int(Q[state_index, best_move_q]) is not 0: move_inferred = q.infer(encoded_state, possible_moves, mover) q_value_1 = Q[state_index, best_move_q] q_value_2 = Q[state_index, move_inferred] self.assertEqual(q_value_1, q_value_2) next_state = state.copy() next_state[best_move_q] = mover result = t.get_result(next_state) if result['terminated']: best_score, _ = minimax(state) q_value = Q[state_index, best_move_q] if best_score != q_value: # not yet sampled (s,a) # or withdraw case self.assertEqual(q_value, 0) else: # sampled (s,a) self.assertEqual(best_score, q_value) pass
class GameWindow(tk.Toplevel): """Game UI""" def __init__(self, user_first: bool, size=3, *args, **kwargs): super().__init__(*args, **kwargs) # state variables self._user_first = user_first self._t = TTT(size) self._agent: Callable[[np.ndarray], int] self._num_of_moves = 0 self._state_history = [self._t.get_state()] # UI accessors self._history_scale: tk.Scale self._player_labels: Dict[int, tk.Label] # key : 1,2 self._buttons = [] # UI initialization self.title(f'TTT') self._make_top_frame() self._make_board(size) self._make_bottom_frame(size) return #region Public Methods def set_agent(self, agent: Callable[[np.ndarray], int], name: str) -> None: self._agent = agent return def get_result(self) -> dict: return self._t.get_result() #endregion #region Put UI Components def _make_top_frame(self): frame = tk.Frame(self) if self._user_first: text1 = 'O : User' text2 = 'X : AI' else: text1 = 'O : AI' text2 = 'X : User' label1 = tk.Label(frame, text=text1) label2 = tk.Label(frame, text=text2) label1.pack() label2.pack() frame.pack() return def _make_board(self, size): board = tk.Frame(self) buttons = self._buttons num_of_buttons = size * size for i in range(num_of_buttons): b = tk.Button(board, width=3, height=1, font=('Helvetica', 30), activebackground='white', command=lambda num=i: self._on_click_board(num)) buttons.append(b) b.grid(column=i % size, row=int(i / size)) pass board.pack() return def _make_bottom_frame(self, size): frame = tk.Frame(self) history_scale = tk.Scale(frame, command=self._on_scale_move, orient='horizontal', from_=0, to=0) history_scale.grid(row=0, columnspan=2) self._history_scale = history_scale restart_button = tk.Button(frame, text="Restart", command=self._on_click_reset) exit_button = tk.Button(frame, text="Exit", command=self.destroy) restart_button.grid(row=1, column=0) exit_button.grid(row=1, column=1) frame.pack() return #endregion #region Event Handlers def _on_click_board(self, position: int): state_num = int(self._history_scale.get()) is_rewinded = not (self._num_of_moves == state_num) if is_rewinded: # reset the game to the rewinded one : state_to_force = self._state_history[state_num] self._t.set_state(state_to_force) self._num_of_moves = self._t._num_moves self._state_history = self._state_history[0:(self._num_of_moves + 1)] pass self._t.put(position) current_state = self._t.get_state() self._state_history.append(current_state) self._num_of_moves += 1 self._history_scale.configure(to=self._num_of_moves) self._history_scale.set(self._num_of_moves) """ [issue] If this procedure is called by button.invoke() then it doesn't invoke the scale's command _on_scale_move. So call it manually (and hence, called twice in user's turn) : """ self._on_scale_move(self._num_of_moves) return def _on_scale_move(self, state_num): state_num = int(state_num) first_mover_turn = True if state_num % 2 == 0 else False user_turn = first_mover_turn == self._user_first self._set_board(state_num, user_turn) if self.get_result()['terminated']: return if state_num == len(self._state_history) - 1: if user_turn: pass else: if hasattr(self, '_agent'): self._on_agent_turn(state_num) pass else: # : agent's turn but it's a previous state pass return def _on_click_reset(self): self._num_of_moves = 0 self._state_history = self._state_history[0:1] self._t.set_state(self._state_history[0]) self._history_scale.configure(to=0) self._history_scale.set(0) self._set_board(0, self._user_first == True) return #endregion #region Private Methods def _on_agent_turn(self, state_num: int): # TODO : async progress bar state = self._state_history[state_num] move = self._agent(state) button = self._buttons[move] button.configure(state='normal') button.invoke() return def _set_board(self, state_num: int, user_turn: bool): """Modify board UI""" to_state = self._state_history[state_num] result = self._t.get_result(to_state) terminated = result['terminated'] lines = result['lines'] lines = sum(lines, []) # flattening for p in range(len(to_state)): move = int(to_state[p]) of_line = p in lines self._modify_button(p, move, user_turn, terminated, of_line) return def _modify_button(self, button_position: int, mover: int, move_allowed: bool, terminated=False, of_line=False): button = self._buttons[button_position] args = {'disabledforeground': 'black', 'state': 'disabled'} if mover == 1: args['text'] = '○' args['state'] = 'disabled' elif mover == -1: args['text'] = '×' args['state'] = 'disabled' else: args['text'] = ' ' if move_allowed: args['state'] = 'normal' elif not hasattr(self, '_agent'): args['state'] = 'normal' if terminated: args['state'] = 'disabled' if of_line: if mover == 1: args['disabledforeground'] = 'steelblue' elif mover == -1: args['disabledforeground'] = 'tomato' button.config(**args) return