fix the hash conflict bug

This commit is contained in:
Dong Yan 2018-01-12 21:08:07 +08:00
parent 90ffdcbb1f
commit 68cc63144f
2 changed files with 9 additions and 8 deletions

View File

@ -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

View File

@ -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):