def main(args): if args.player1 == "human": agent1 = Human(1, surface) elif args.player1 == "minimax": agent1 = Minimax(1, args.minimax_depth[0], args.variant) elif args.player1 == "mcts": agent1 = MCTS(1, args.mcts_depth[0], args.mcts_rollouts[0],\ args.variant, args.heuristic_rollouts[0], \ args.input_file[0] if args.input_file else None, args.output_file[0] if args.output_file else None, args.ucb_const[0]) if args.player2 == "human": agent2 = Human(-1, surface) elif args.player2 == "minimax": agent2 = Minimax(-1, args.minimax_depth[1], args.variant) elif args.player2 == "mcts": agent2 = MCTS(1, args.mcts_depth[1], args.mcts_rollouts[1],\ args.variant, args.heuristic_rollouts[1], args.input_file[1] if len(args.input_file) == 2 else None,\ args.output_file[1] if len(args.output_file) == 2 else None, args.ucb_const[1]) for i in range(args.num_games): play_game(agent1, agent2, surface, args.variant, args.wait_between) if type(agent1) == MCTS: agent1.reset(1) if type(agent2) == MCTS: agent2.reset(-1) if args.alternate_sides: agent1.switch_sides() agent2.switch_sides() temp = agent1 agent1 = agent2 agent2 = temp if type(agent1) == MCTS: agent1.store_root() if type(agent2) == MCTS: agent2.store_root()
def alphaBeta(self, state, alpha, beta, depth): if state in self.table: return self.table[state].minimax_value state.updateWinCheck() if state.isTerminal(): u = state.stateUtility() self.table[state] = Minimax(u, None) return u elif depth >= self.depth_cutoff: e = state.stateEval() self.table[state] = Minimax(e, None) return e elif state.turn == "MAX": best_minimax_so_far = np.NINF best_move_for_state = None children_states = state.genNextBoards() for i in range(len(state.moves)): if state.moves[i]: child_state = children_states[i] minimax_of_child = self.alphaBeta(child_state, alpha, beta, depth + 1) if minimax_of_child > best_minimax_so_far: best_minimax_so_far = minimax_of_child best_move_for_state = i if best_minimax_so_far >= beta: return best_minimax_so_far alpha = max(alpha, best_minimax_so_far) self.table[state] = Minimax(best_minimax_so_far, best_move_for_state) return best_minimax_so_far else: best_minimax_so_far = np.Inf best_move_for_state = None children_states = state.genNextBoards() for i in range(len(state.moves)): if state.moves[i]: child_state = children_states[i] minimax_of_child = self.alphaBeta(child_state, alpha, beta, depth + 1) if minimax_of_child < best_minimax_so_far: best_minimax_so_far = minimax_of_child best_move_for_state = i if best_minimax_so_far <= alpha: return best_minimax_so_far beta = min(beta, best_minimax_so_far) self.table[state] = Minimax(best_minimax_so_far, best_move_for_state) return best_minimax_so_far
def play(q_agent, human=0, helper=False, helper_depth=20): """ Play human game against the QAgent. `human` can be set to 0 or 1 to specify whether human moves first or second. """ connect4 = Connect4() helper = Minimax(max_depth=helper_depth) if helper else None while True: for l in range(0, 42, 7): row = ''.join([f"{connect4.board[l + i]}|" for i in range(7)]) print(row[:13]) print('-+-+-+-+-+-+-') actions = connect4.available_actions(connect4.board) if connect4.player == human: print("Your Move.") while True: column = int(input().strip()) - 1 if not column in actions: print( 'That place is already filled or invalid. Still your move.' ) else: break # column, values = helper.get_move(connect4.board) else: print("QAgent's Move.") if helper: action, values = helper.get_move(connect4.board) if values.count(1000) >= 1 or values.count(-1000) >= 1: column = action else: column = q_agent.choose_action(connect4.board, epsilon=False) else: column = q_agent.choose_action(connect4.board, epsilon=False) print(f"QAgent put a chip in column {column + 1}.") connect4.move(column) if connect4.result is not None: print("\nGAME OVER\n") winner = "Human" if connect4.result == human else "QAgent" print(f"Winner is {winner}") break if input("Play again?\n").lower() == "y": play(q_agent)
def __init__(self, game, depth, breadths, evaluate, tag=None): assert depth == len(breadths) self.game = game self.depth = depth self.evaluate = evaluate self.tag = tag self.minimax = Minimax(game, depth, breadths, evaluate)
def move(self, state): print("{0}'s turn. {0} is {1}, {2}".format(self.name, self.color, self.ctr)) self.ctr += 1 m = Minimax(state) best_move, value = m.bestMove(self.difficulty, state, self.color) return best_move
def move(self, state, timeLimit): print("{0}'s turn. {0} is {1}".format(self.name, self.color)) if self.difficulty == 6: m = AlphaBeta(state) start = time.clock() best_move, value, depth = m.bestMove(30, state, self.color, timeLimit) print("Alpha: ", value) print("Elapsed:", time.clock() - start) print("Depth Reached:", depth) return best_move, depth elif self.difficulty == 7: m = Greedy(state) time.sleep(randrange(8, 17, 1) / 10.0) best_move = m.best_move(state, self.color) print("guess greedy worked") return best_move, 1 else: m = Minimax(state) start = time.clock() best_move, value, depth = m.bestMove(30, state, self.color, timeLimit) print("Alpha: ", value) print("Elapsed:", time.clock() - start) print("Depth Reached:", depth) return best_move, depth
def _run_minimax(self, max_depth) -> State: minimax = Minimax(State(self._grid), terminal_test_func=self._create_terminal_test(max_depth), child_states_func=self._child_states, eval_func=self._eval) best_child, _ = minimax.decision() return best_child
def ai_vs_human(self, team): self.menubar.add_command(label="AI Go", command=self.ai_move) self.ai_team = team self.only_allow_valid_moves = True # AI time limit defined here self.m = Minimax(self.time_limit, True) self.master.update()
def move(self, state): super().move(self) print("{0}'s turno. {0} es {1}".format(self.nombre, self.color)) m = Minimax(state) mejor_mov, valor = m.mejorMov(self.dificultad, state, self.color) return mejor_mov
async def _prepare_player(self, name): async with self._session.post(f'{self._api_url}/game', params={'team_name': name}) as resp: res = (await resp.json())['data'] self._player_num = 1 if res['color'] == 'RED' else 2 self._player = {'color': res['color'], 'token': res['token']} self.minimax = Minimax(self._player_num) print("PLAYER_NUM", self._player_num)
def move(self, state): print("{0}'s turn. {0} is {1}".format(self.name, self.color)) m = Minimax(state) best_move, _ = m.bestMove(self.difficulty, state, self.color, self.heuristic) #SetMoveMi(best_move) print(self.name + ": " + str(best_move)) return best_move
def move(self, state): print("{0}'s turn. {0} is {1}".format(self.name, self.color)) # sleeping for about 1 second makes it looks like he's thinking #time.sleep(random.randrange(8, 17, 1)/10.0) #return random.randint(0, 6) m = Minimax(state) best_move, value = m.bestMove(self.difficulty, state, self.color) return best_move
def test_next_move_gives_the_best_move_with_diagonals(self): minimax = Minimax(1) game = Game(4, 4) game.board = [ [0, -1, 1, -1], [0, 1, -1, -1], [0, -1, 1, -1], [0, 0, 0, 1], ] self.assertEqual(minimax.next_move(game), 0)
def test_next_move_gives_the_best_defence_move(self): minimax = Minimax(1) game = Game(4, 4) game.board = [ [0, 0, 0, 0], [0, 0, 0, 0], [0, -1, -1, -1], [0, 0, 1, 1], ] self.assertEqual(minimax.next_move(game), 2)
def __init__(self, mode, square_side, depth, ai_mode, omni): self.square_side = square_side player1 = Player(1, self.square_side) player2 = Player(2, self.square_side) self.player_turn = 1 self.players = [player1, player2] self.mode = mode self.min_pos = square_side / 2 self.max_pos = square_side * 8 - square_side / 2 self.turn = 1 if '3' == self.mode and omni: ai2 = ai_mode[1] else: ai2 = '3' self.ai_player2 = Minimax(depth, ai_mode[0], ai2) if '3' == self.mode: if omni: self.ai_player1 = Minimax(depth, ai2, ai_mode[0]) else: self.ai_player1 = Minimax(depth, ai2, '3')
def ai_vs_ai(self): m = Minimax(self.time_limit, True) i = 0 while self.board.check_win() == Board.EMPTY: if i % 2 == 0: team = Board.GREEN else: team = Board.RED self.board = m.search(self.board, team)[0] i += 1 self.display_board(self.board) self.master.update()
def minimax(self, state): if state in self.table: return self.table[state].minimax_value state.updateWinCheck() if state.isTerminal(): u = state.stateUtility() self.table[state] = Minimax(u, None) return u elif state.turn == "MAX": best_minimax_so_far = np.NINF best_move_for_state = None children_states = state.genNextBoards() for i in range(len(state.moves)): if state.moves[i]: child_state = children_states[i] minimax_of_child = self.minimax(child_state) if minimax_of_child > best_minimax_so_far: best_minimax_so_far = minimax_of_child best_move_for_state = i self.table[state] = Minimax(best_minimax_so_far, best_move_for_state) return best_minimax_so_far else: best_minimax_so_far = np.Inf best_move_for_state = None children_states = state.genNextBoards() for i in range(len(state.moves)): if state.moves[i]: child_state = children_states[i] minimax_of_child = self.minimax(child_state) if minimax_of_child < best_minimax_so_far: best_minimax_so_far = minimax_of_child best_move_for_state = i self.table[state] = Minimax(best_minimax_so_far, best_move_for_state) return best_minimax_so_far
def move(self, state): print(f"{self.name}'s turn. {self.name} is {self.color}") # time.sleep(random.randrange(8, 17, 1) / 10.0) # return random.randint(0, 6) m = Minimax(state) best_move, value = m.best_move(state=state, depth=self.difficulty, alpha=-math.inf, beta=math.inf, maximizing_player=True) return best_move
def make_move(self): changed_board = self.change_board(settings.board) m = Minimax(changed_board) x = m.bestMove(self.depth, changed_board, 'x' if self.player == 1 else 'o') x = x[0] output = self.name + " played in column " + str(x) + "." print(output) return x
def __init__(self): pg.init() self.domino = Domino() self.images = Images() self.player1 = Player() self.player2 = Player() self.h3 = Minimax() self.clock = pg.time.Clock() self.page = 1 self.result = "Venceu" self.conf = pg.Rect(575, 30, 20, 20) self.show_resolutions = False self.resolutions = [] self.aux_field = [] # Salvando a font que será usada self.min_font = pg.font.Font(pg.font.get_default_font(), 10) self.font = pg.font.Font(pg.font.get_default_font(), 20) self.max_font = pg.font.Font(pg.font.get_default_font(), 30) # Salvando as cores que serão utilizadas self.black = (0, 0, 0) self.white = (255, 255, 255) self.green = (0, 128, 0) # Salvando a lista de botões self.buttons = [] # Salvando peças clicavéis e peça marcada self.clickable_pieces = [] self.marked_piece = [] # Tamanho da tela self.height = 480 self.offset = [[0, 0], [100, 100], [300, 100]] self.index = 0 self.width = 640 # Criando a tela e Adicionando título e icone self.screen = pg.display.set_mode((self.width, self.height)) pg.display.set_caption("Dominó") pg.display.set_icon(pg.image.load('images\logo.png')) # Preenchendo background self.screen.fill(self.black) pg.display.update()
def ai(self,event): print "AI" self.new_game() ai = Minimax() while not self.board.has_lost(): ai.state = self.board move = ai.get_move() self.board.move(move) if self.board.valid_move: new_tile = Tile(None) new_tile.set_start_value() self.board.set_new_tile(new_tile) self.update_board() else: self.update_board() break self.game_ended()
def minimax_player( self, state, depth=2500000, team=1, heuristic_parameter=True ): # creates first successors to implement minimax algorithm new_shape_x = np.asarray(state[1]).shape player1 = Minimax(n=new_shape_x, default_team=team, advance_option=heuristic_parameter) print('default_team', team, player1.default_team) if team == -1: state = player1.convert_board_state(state) best_move = player1.decision_maker(state, depth) chosen_succ, utility = best_move if team == -1: chosen_succ = player1.convert_board_state(chosen_succ) return chosen_succ
def __init__(self, render=True, ros_node=None): self.board = C4Board(render) self.max = Minimax() if not ros_node: rospy.init_node("connect4", anonymous=True) self.ur5_commander = rospy.Publisher("behaviors_cmd", String, queue_size=10) self.token_grabber = rospy.Publisher("gripper", Int16, queue_size=10) self.computer_vision = rospy.Publisher("c4_ready", Int16, queue_size=10) rospy.Subscriber("arm_status", String, self.status_callback) rospy.Subscriber("opponent_move", Int16, self.player_move) self.status = True self.turn = "Start" self.player_action = None
def run_bot(self): if (self.active_player.mode == "Minimax"): (frm_x, frm_y), (to_x, to_y) = Minimax( self.TARGETS, self.board.to_ozer_board(), TIMELIMIT / 1000, active_player=self.active_player.pion).result self.move(self.board.board[frm_x][frm_y], self.board.board[to_x][to_y]) self.next_turn() elif (self.active_player.mode == "LocSearch"): (frm_x, frm_y), (to_x, to_y) = MinimaxLocalSearch( self.TARGETS, self.board.to_ozer_board(), TIMELIMIT / 1000, active_player=self.active_player.pion, n_restart=5).result self.move(self.board.board[frm_x][frm_y], self.board.board[to_x][to_y]) self.next_turn()
def move(self, state): print("{0}'s turn. {0} is {1}".format(self.name, self.color)) # sleeping for about 1 second makes it looks like he's thinking #time.sleep(random.randrange(8, 17, 1)/10.0) #return random.randint(0, 6) port = serial.Serial("COM12", 9600) port.isOpen() m = Minimax(state) #m = Minimax(super_board) best_move, value = m.bestMove(self.difficulty, state, self.color) #print(state) #input(best_move + 1) print(best_move + 1) inp = 7 - best_move k = str.encode(str(inp)) #time.sleep(2) time.sleep(1) #k = bytes(inp) port.write(k) port.write(b'1') #print(k) i = 0 while True: response = port.read() if response == b'9': print(response) i = 1 if (i == 1): break port.close() return best_move
def main(): team = [0] game = generate_game(seed) cur_state = game.first_state() mm = Minimax(game) for tokens in game.pieces: print(tokens) first_value = mm.find(cur_state) while not game.is_over(cur_state): print(cur_state) value = mm.find(cur_state) print(value) moves = mm.get_moves(cur_state) move = moves[0] print(move) cur_state = game.apply(cur_state, move) assert first_value == value
def parse_agent(agent_type, filename): if agent_type == 'mcts_a0': model_file = 'best_policy_8_8_5.model' if filename: model_file = filename # load the trained policy_value_net in either Theano/Lasagne, PyTorch or TensorFlow # best_policy = PolicyValueNet(width, height, model_file = model_file) # mcts_player = MCTSPlayer(best_policy.policy_value_fn, c_puct=5, n_playout=400) # load the provided model (trained in Theano/Lasagne) into a MCTS player written in pure numpy try: policy_param = pickle.load(open(model_file, 'rb')) except: policy_param = pickle.load( open(model_file, 'rb'), encoding='bytes') # To support python3 best_policy = PolicyValueNetNumpy(width, height, policy_param) player = MCTSPlayer( best_policy.policy_value_fn, c_puct=5, n_playout=400 ) # set larger n_playout for better performance elif agent_type == 'mcts_pure': player = MCTS_Pure(c_puct=5, n_playout=1000) elif agent_type == 'minmax': player = Minimax() elif agent_type == 'dqn': model_file = 'output/v_1/epoch_100/agent_2.pkl' if filename: model_file = filename player = DQNPlayer(model_file) elif agent_type == 'human': player = Human() else: player = Human() print('Illegal Agent Type. Defaulting to human player.') return player
def __init__(self, color, prune=3): self.depthLimit = prune evaluator = Evaluator() self.minimaxObj = Minimax(evaluator.score) self.color = color
def intelligentFunction2(turn, board): m = Minimax() best_move, value = m.bestMove(4, board, 2) return best_move
from game import Game from minimax import Minimax from alpha_beta import AlphaBeta from math import floor f = open('input.txt','r') boardSize = int(f.readline()) algo = f.readline().rstrip() originPlayer = f.readline().rstrip() searchDepth = int(f.readline()) boardValues = [["*" for i in range(boardSize)]for j in range (boardSize)] originBoardState = [["*" for i in range(boardSize)]for j in range (boardSize)] game = Game(boardValues, boardSize) minimax = Minimax(searchDepth, game, originPlayer) alphabeta = AlphaBeta(searchDepth, game, originPlayer) # Set boardValues for i in range(boardSize): line = f.readline().rstrip() line = line.split(" ") for j in range(boardSize): boardValues[i][j] = int(line[j]) # Set boardState for i in range(boardSize): line = f.readline().rstrip() for j in range(boardSize): originBoardState[i][j] = line[j] f.close()