def minimax(tabuleiro, depth, alpha, beta, maximizingPlayer, current_move, player): best_move = current_move score = tabuleiro.calculate_board_score(player) if depth == 0 or score == inf: return score, current_move possible_moves = utils.get_possible_moves(tabuleiro.get_board()) if maximizingPlayer: maxEval = -inf for move in possible_moves: evalue, pos = minimax(utils.make_move(copy.deepcopy(tabuleiro), move, 1), depth-1, alpha, beta, False, move, player) if maxEval < evalue: maxEval = evalue best_move = move alpha = max(alpha, evalue) if alpha >= beta: break return maxEval, best_move else: minEval = inf for move in possible_moves: evalue, pos = minimax(utils.make_move(copy.deepcopy(tabuleiro), move, 2), depth - 1, alpha, beta, True, move, player) if minEval > evalue: minEval = evalue best_move = move beta = min(beta, evalue) if alpha >= beta: break return minEval, best_move
def move(self, board) -> Tuple[int, int, Action]: if random() < self.epsilon: possible_moves = get_possible_moves(board, self.mark) if possible_moves: chosen_move = choice(possible_moves) move_hash = ALL_MOVES.index(chosen_move) else: move_hash = 0 chosen_move = ALL_MOVES[move_hash] else: predictions = self.model.predict(encode_board(board, self.mark))[0] possible_moves_hashes = get_encoded_possible_moves( board, self.mark) move_hash = next((x for _, x in sort_predictions(predictions) if x in possible_moves_hashes), 0) chosen_move = ALL_MOVES[move_hash] if self.training: self.tau += 1 if self.tau > self.max_tau: self.tau = 0 self.update_target_model() self.memorize(board, move_hash, 0, False) return chosen_move
def move(self, board) -> Tuple[int, int, Action]: mark = self.mark if self.training: move_hash = 0 chosen_move = ALL_MOVES[move_hash] if random() < self.epsilon: possible_moves = get_possible_moves(board, mark) if possible_moves: chosen_move = choice(possible_moves) move_hash = ALL_MOVES.index(chosen_move) else: predictions = self.model.predict(encode_board(board, mark))[0] possible_moves_hashes = get_encoded_possible_moves(board, mark) move_hash = next((x for _, x in sort_predictions(predictions) if x in possible_moves_hashes), 0) chosen_move = ALL_MOVES[move_hash] self.buffer.append((board, move_hash)) return chosen_move else: predictions = self.model.predict(encode_board(board, mark))[0] possible_moves_hashes = get_encoded_possible_moves(board, mark) move_hash = next((x for _, x in sort_predictions(predictions) if x in possible_moves_hashes), 0) return ALL_MOVES[move_hash]
def minimax(game_state, turn, maximizer_turn, round_num=0, top=False): node_key = hash_game_state(game_state) if node_key in MINIMAX_TREE: return MINIMAX_TREE[node_key] possible_moves = get_possible_moves(game_state) if not possible_moves or get_winner(game_state)[0] is not None: return _score(game_state, maximizer_turn), None, round_num if turn == maximizer_turn: best = -2 # Lower than lowest possible score. best_move = None best_move_depth = 10 # Larger than deepest possible tree. for i, move in enumerate(possible_moves): move_score, _, move_depth = minimax( _simulate_move(game_state, move, turn), toggle_turn(turn), maximizer_turn, round_num + 1 ) # break ties by preferring faster wins is_faster = move_depth < best_move_depth if move_score > best or (move_score == best and is_faster): best_move = move best = move_score best_move_depth = move_depth # Print a progress bar at the top level if top: progress = 100. * (i + 1.) / len(possible_moves) sys.stdout.write( 'Computer is thinking... Progress: %.02f%%\r' % progress) sys.stdout.flush() ret = (best, best_move, best_move_depth) MINIMAX_TREE[node_key] = ret return ret else: best = 2 # Higher than highest possible score best_move = None best_move_depth = 10 # Larger than deepest possible tree. for move in possible_moves: move_score, _, move_depth = minimax( _simulate_move(game_state, move, turn), toggle_turn(turn), maximizer_turn, round_num + 1 ) # break ties by preferring faster wins is_faster = move_depth < best_move_depth if move_score < best or (move_score == best and is_faster): best_move = move best = move_score best_move_depth = move_depth ret = (best, best_move, best_move_depth) MINIMAX_TREE[node_key] = ret return ret
def get_ordered_next_games(self, mark: Mark, alpha: float, beta: float, desc: bool = True): possible_games = [(move, self.move(*move, mark)) for move in get_possible_moves(self.board, mark)] # add some random noise to randomize results possible_games.sort(key=lambda x: (x[1].maybe_evaluate(mark, 0, alpha, beta, 0), random.random()), reverse=desc) return possible_games
def random(game_state, turn): '''Random Algorithm''' possible_moves = get_possible_moves(game_state) return choice(possible_moves)
def move(self, board) -> Tuple[int, int, Action]: possible_moves = get_possible_moves(board, self.mark) return choice(possible_moves) if possible_moves else ALL_MOVES[0]
def move(self, board) -> Tuple[int, int, Action]: possible_moves = get_possible_moves(board, self.mark) possible_moves.sort(key=lambda m: (evaluate_move( board, self.mark, m, self.eval_rows, self.eval_cols, self. eval_diagonals), random.random())) return possible_moves[-1]
# -*- coding: utf-8 -*- import board import utils from math import inf import minimax tabuleiro = board.Board() lista = tabuleiro.calculate_board_score(1) #print(lista) moves = utils.get_possible_moves(tabuleiro.get_board()) print(moves) score, move = minimax.minimax(tabuleiro, 3, -inf, inf, True, [0, 0], 1) print("SCORE: ") print(score) print("Movimento") print(move)