From 68cc63144f50e983fd25e50c4b98f5f2a2337914 Mon Sep 17 00:00:00 2001 From: Dong Yan Date: Fri, 12 Jan 2018 21:08:07 +0800 Subject: [PATCH] fix the hash conflict bug --- AlphaGo/game.py | 6 +++--- AlphaGo/go.py | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/AlphaGo/game.py b/AlphaGo/game.py index 28f3424..0d3ca59 100644 --- a/AlphaGo/game.py +++ b/AlphaGo/game.py @@ -37,7 +37,7 @@ class Game: self.komi = 3.75 self.history_length = 8 self.history = [] - self.history_set = set() + self.history_hashtable = set() self.game_engine = go.Go(size=self.size, komi=self.komi) self.board = [utils.EMPTY] * (self.size ** 2) elif self.name == "reversi": @@ -61,7 +61,7 @@ class Game: del self.board[:] self.board = [utils.EMPTY] * (self.size ** 2) del self.history[:] - self.history_set.clear() + self.history_hashtable.clear() if self.name == "reversi": self.board = self.game_engine.get_board() for _ in range(self.history_length): @@ -108,7 +108,7 @@ class Game: if self.name == "reversi": res = self.game_engine.executor_do_move(self.history, self.latest_boards, self.board, color, vertex) if self.name == "go": - res = self.game_engine.executor_do_move(self.history, self.history_set, self.latest_boards, self.board, + res = self.game_engine.executor_do_move(self.history, self.history_hashtable, self.latest_boards, self.board, color, vertex) return res diff --git a/AlphaGo/go.py b/AlphaGo/go.py index 712ba3c..38efd17 100644 --- a/AlphaGo/go.py +++ b/AlphaGo/go.py @@ -101,8 +101,9 @@ class Go: next_board = copy.deepcopy(current_board) next_board[self._flatten(vertex)] = color self._process_board(next_board, color, vertex) - if hash(tuple(next_board)) in history_hashtable: + if tuple(next_board) in history_hashtable: repeat = True + del next_board return repeat def _is_eye(self, current_board, color, vertex): @@ -206,7 +207,7 @@ class Go: history_boards, color = state history_hashtable = set() for board in history_boards: - history_hashtable.add(hash(tuple(board))) + history_hashtable.add(tuple(board)) for action_candidate in action_set[:-1]: # go through all the actions excluding pass if not self._is_valid(state, action_candidate, history_hashtable): @@ -242,15 +243,15 @@ class Go: # since go is MDP, we only need the last board for hashing return tuple(state[0][-1]) - def executor_do_move(self, history, history_set, latest_boards, current_board, color, vertex): - if not self._rule_check(history_set, current_board, color, vertex, is_thinking=False): + def executor_do_move(self, history, history_hashtable, latest_boards, current_board, color, vertex): + if not self._rule_check(history_hashtable, current_board, color, vertex, is_thinking=False): # raise ValueError("!!! We have more than four ko at the same time !!!") return False current_board[self._flatten(vertex)] = color self._process_board(current_board, color, vertex) history.append(copy.deepcopy(current_board)) latest_boards.append(copy.deepcopy(current_board)) - history_set.add(hash(tuple(current_board))) + history_hashtable.add(copy.deepcopy(tuple(current_board))) return True def _find_empty(self, current_board):