def getMove(self, grid): # place a random tile in a random cell # cells = grid.getAvailableCells() # return cells[randint(0, len(cells) - 1)] if cells else None a = AlphaBeta() moves = grid.getAvailableCells() minBeta = float("inf") bestCell = (-1,-1) for cell in moves: gridCopy = grid.clone() # try inserting a 2 in the cell gridCopy.insertTile(cell, 2) beta1 = a.alphabeta(gridCopy, 2, float("-inf"), float("inf"), False) gridCopy = grid.clone() # try inserting a 4 in the cell gridCopy.insertTile(cell, 4) beta2 = a.alphabeta(gridCopy, 2, float("-inf"), float("inf"), False) # calculate the expected value of beta for each cell beta = beta1*0.9 + beta2*0.1 if beta < minBeta: minBeta = beta bestCell = cell return bestCell
def policy(self, observation, player): # Value is an array. The 0th element corresponds to (0,0), the 1st: (0,1) # the 8th: (1,0), etc. value = [] if (player == -1): observation = reverse(observation) possible_moves = findMovesWithoutEnv(observation) if (len(possible_moves) == 0): # Passes return (-1, -1) if (self.debugging): print(possible_moves) decision_tree = AlphaBeta(self.parent) variation = random.random() if (variation < 1 / self.epsilon): self.epsilon += self.epsilon_increment if (self.debugging): print("Random Move for player " + str(env.to_play)) return random.choice(possible_moves) else: board = OthelloBoard(8) board.board = observation value, move = decision_tree.alphabeta(board, self.depth, -math.inf, math.inf, 1, self.index) if (move == None): return (-1, -1) return move
def human_vs_ai(): cheese_border_size = 15 env = CheeseENV(board_count=cheese_border_size, line_margin=40) ai = AlphaBeta() while True: # 这是维持游戏循环进行的循环 print('new game', flush=True) state = env.reset() # 每局游戏开始之前重置环境 step = 0 while True: # 这是每一局游戏内部接受落子的循环 if env.get_color() == 1: # 黑棋,则由人输入 action = human_action(env) else: # 白棋,则由agent输入· action = ai.get_action(state) if not action: # 若是无效action,跳过 raise TypeError("不应该出现无效action") # 模型训练及保存 step += 1 next_state, reward, done, _ = env.step(action) if done: print( f'''{'black' if env.get_color() == 1 else 'white'} player win!''', flush=True) break state = next_state
def __init__(self): self._board = Goban.Board() self._mycolor = None self._behavior = AlphaBeta() self._nbCoup = 0 self._movePossible1 = {"B2", "C3", "G3", "H2", "A5", "J5", "B8", "H8", "D9", "F8"} self._movePossible2 = {"D2", "F2", "B5", "H5", "C7", "G7", "E8", "A4", "J4"} self._time = 0
def interactive_game(self, human_first=True): """ Start a interactive game :param human_first: If the human should begin. :type human_first: bool """ my_turn = human_first self.actual_depth = self.start_depth while True: self.turn += 1 print(self.board) print('Turn: ', self.turn) if self.board.won('O'): print('Yay, you won... lets go again....') print('###########################') self.reset() continue if self.board.won('X'): print('Lol, you lost... lets go again....') print('###########################') self.reset() continue if not self.board.get_possible_moves(): print('Okay, draw ... lets go again....') print('###########################') self.reset() continue if my_turn: choise = input("Select position (1, 2, 3, 4, 5, 6, 7):") if choise in '1234567': if self.board.play_slot(choise, 'O'): my_turn = False else: print('Yo f****r, dont try to trick me!') else: print('Yo f****r, dont try to trick me!') else: ab = AlphaBeta(self) tree = Node(self.board) start_time = int(time.time()*1000) print("Start searching...", end=' ') sys.stdout.flush() score = ab.iterate(tree, self.actual_depth, -100, 100, 'X') end = int(time.time()*1000) elapsed = end - start_time print("Finished in", elapsed, 'ms. Searching depth:', self.actual_depth, 'moves. Score:', score) if elapsed < self.target_round_time: pass # self.actual_depth += 1 else: pass # self.actual_depth -= 1 if tree.children: candidate = max(tree.children) self.board.play(candidate.my_move, 'X') my_turn = True
def fetch_action(self, board): minmax = AlphaBeta(board.board_status, self.depth, self.play_as, self.play_against, self.state_eval_factory) started = time.time() move, remove = minmax.predict_state() duration = time.time() - started log_string = "MinMax,{0},{1},{2},{3},{4}\n".format(started, duration, self.depth, measure_name(self.state_eval_factory), self.n_game) with open(self.log_file, mode='a+') as fs: fs.write(log_string) print(move) print(remove) board.make_move(move) board.make_remove(remove) return board
def getMove(self, grid): a = AlphaBeta() moves = grid.getAvailableMoves() maxAlpha = float("-inf") maxMove = -1 for move in moves: gridCopy = grid.clone() gridCopy.move(move) alpha = a.alphabeta(gridCopy, 4, float("-inf"), float("inf"), True) # Call alphabeta as Max, what does this return? if alpha > maxAlpha: maxAlpha = alpha maxMove = move # it should return the move corresponding to the node with the highest minimax value # move should be returned in the form of an int: 0, 1, 2, 3 --> up, down, left, right return maxMove
def main(): Eingabe = input("Console 'C' , GUI 'G' or exit?") if Eingabe == "G" or Eingabe== "g": GuiClass.gui() elif Eingabe == "C" or Eingabe== "c": H = Human("HUM") AB = AlphaBeta("AB", 7) MM = MiniMax("MM", 4) G = Game() S = State() G.play(S, H, MM)
def automatic_game(self, start_player=True): """ Play an automatic game to the end :param start_player: If the 'Human' player should start :type start_player: bool """ my_turn = start_player self.actual_depth = self.start_depth self.turn = 0 print('Starting game..') start_time = int(time.time() * 1000) turn_time = int(time.time() * 1000) while True: self.turn += 1 print('Turn: ', self.turn) print(self.board) sys.stdout.flush() if self.board.won('O') or self.board.won('X') or not self.board.get_possible_moves(): print(' Finished') print(self.board) end = int(time.time() * 1000) elapsed = end - start_time print("needed: ", elapsed, 'ms') self.reset() break if my_turn: ab = AlphaBeta(self) tree = Node(self.board) score = ab.iterate_2(tree, self.actual_depth, -100, 100, 'O') print('player O thinks score:', score, 'Search depth:', self.actual_depth) if tree.children: tree.children.sort(key=lambda x: x.beta, reverse=False) candidate = tree.children[0] self.board.play(candidate.my_move, 'O') my_turn = False else: ab = AlphaBeta(self) tree = Node(self.board) score = ab.iterate(tree, self.actual_depth, -100, 100, 'X') print('player X thinks score:', score, 'Search depth:', self.actual_depth) if tree.children: tree.children.sort(key=lambda x: x.alpha, reverse=True) candidate = tree.children[0] self.board.play(candidate.my_move, 'X') my_turn = True if int(time.time() * 1000) - turn_time < self.target_round_time: pass # self.actual_depth += 1 else: pass # self.actual_depth -= 1 turn_time = int(time.time() * 1000)
def declare_action(self, valid_actions, hole_card, round_state): fold_action, call_action, raise_action = list(map(lambda valid_action: valid_action['action'], valid_actions)) + [None] * (3 - len(valid_actions)) if len(round_state['action_histories']['preflop']) == 2: my_role = "SB" self.my_uuid = round_state['action_histories']['preflop'][0]['uuid'] elif len(round_state['action_histories']['preflop']) == 3: my_role = "BB" self.my_uuid = round_state['action_histories']['preflop'][1]['uuid'] if len(round_state['action_histories']['preflop']) <= 3: self.tree.set_my_role(my_role) self.alpha_beta = AlphaBeta(self.tree) win_rate = estimate_hole_card_win_rate(1000, 2, gen_cards(hole_card), gen_cards(round_state['community_card'])) print self.tree.my_role, win_rate if win_rate < self.foldThreshold and fold_action is not None: return fold_action elif win_rate > self.raiseThreshold and raise_action is not None: return raise_action else: return self.explore_game_tree(round_state, win_rate)
def fetch_action(self, board): """Funkcja oblicza najlepszy ruch za pomocą algorytmu min-max""" if self.is_white: minmax = AlphaBeta(board.board_status, self.depth, 2, 3, MeasureOneToTwoFactory) print("AI oblicza ruch...") move, remove = minmax.predict_state() clear() board.make_move(move) board.make_remove(remove) else: minmax = AlphaBeta(board.board_status, self.depth, 3, 2, MeasureOneToTwoFactory) print("AI oblicza ruch...") move, remove = minmax.predict_state() clear() board.make_move(move) board.make_remove(remove) return board
class myPlayer(PlayerInterface): ''' Example of a random player for the go. The only tricky part is to be able to handle the internal representation of moves given by legal_moves() and used by push() and to translate them to the GO-move strings "A1", ..., "J8", "PASS". Easy! ''' def __init__(self): self._board = Goban.Board() self._mycolor = None self._behavior = AlphaBeta() self._nbCoup = 0 self._movePossible1 = {"B2", "C3", "G3", "H2", "A5", "J5", "B8", "H8", "D9", "F8"} self._movePossible2 = {"D2", "F2", "B5", "H5", "C7", "G7", "E8", "A4", "J4"} self._time = 0 def getPlayerName(self): return "Floryannator" def getPlayerMove(self): if self._board.is_game_over(): print("Referee told me to play but the game is over!") return "PASS" startTime = time.time() self._nbCoup += 1 hasPassed = False if(self._board._lastPlayerHasPassed): (black, white) = self._board.compute_score() if(self._mycolor == Goban.Board._WHITE): if(white > black): move = Goban.Board.name_to_flat("PASS") hasPassed = True else: if(black > white): move = Goban.Board.name_to_flat("PASS") hasPassed = True if(not hasPassed): #Début de partie, soit avant nos 12 premiers coups, soit avant 24 pions de placés if self._board._nbBLACK + self._board._nbWHITE < 24: self._behavior.set_heuristic_start() #Milieu de partie, soit après nos 12 premiers coups, soit après 24 pions de placés if self._board._nbBLACK + self._board._nbWHITE > 24: self._behavior.set_heuristic_mid() #Fin de partie, soit 70% du plateau rempli, soit 56 pions placés if self._board._nbBLACK + self._board._nbWHITE > 56: self._behavior.set_heuristic_end() move = self._behavior.choose_move(self._board) self._board.push(move) self._time += time.time() - startTime print("My total time = " + str(self._time)) # New here: allows to consider internal representations of moves print("I am playing ", self._board.move_to_str(move)) print("My current board :") self._board.prettyPrint() # move is an internal representation. To communicate with the interface I need to change if to a string return Goban.Board.flat_to_name(move) def playOpponentMove(self, move): print("Opponent played ", move) # New here # the board needs an internal represetation to push the move. Not a string move = Goban.Board.name_to_flat(move) self._board.push(move) def newGame(self, color): self._mycolor = color self._opponent = Goban.Board.flip(color) self._behavior._color = self._mycolor def endGame(self, winner): if self._mycolor == winner: print("I won!!!") else: print("I lost :(!!")
def test(): 'currently no error checking for valid user moves' AB = AlphaBeta() board = AB.init_board() player = True while not AB.game_over(board): if player: # black - AI print AB.generate_black_moves(board) AB.alphabeta(board, 6, float('-inf'), float('inf'), False) # don't know why False move = AB.BEST_MOVE board = AB.move_black(board, *move) AB.print_board(board) player = False else: print AB.generate_white_moves(board) i = int(raw_input("Enter piece i: ")) j = int(raw_input("Enter piece j: ")) i_new = int(raw_input("Enter new i: ")) j_new = int(raw_input("Enter new j: ")) board = AB.move_white(board, (i, j), (i_new, j_new)) AB.print_board(board) player = True