def main(): tm = TronModel.TM(2901, 4) mapList = [ "center_block.txt", "diagonal_blocks.txt", "divider.txt", "empty_room.txt", "hunger_games.txt", "joust.txt", "small_room.txt" ] for i in range(20000): # mapChoice = random.choice(mapList) # loss, lengths = trainOneGame(TronProblem("maps/" + mapChoice, 0), tm) loss, lengths = trainOneGame( TronProblem("maps/" + "center_block.txt", 0), tm) print(i) if (i % 10 == 0): print(loss) print(lengths) print('\n')
def separated(self, state, asp): myboard = np.array(state.board) locs = state.player_locs myloc = locs[state.ptm] myQ = Queue.Queue() myQ.put(myloc) myvisited = {myloc} enemyloc = locs[state.ptm - 1] while not myQ.empty(): mycur = myQ.get() allmoves = list(asp.get_available_actions(state)) for direction in allmoves: neighbor = TronProblem.move(mycur, direction) if neighbor in myvisited: continue if neighbor == enemyloc: # if enemy is accessible, then not seperated return False elif myboard[neighbor] == ' ' or myboard[neighbor] == '*': myQ.put(neighbor) myvisited.add(neighbor) return True
def alpha_beta_cutoff(asp, cutoff_turn, eval_func): """ An implementation of the alpha_beta_cutoff algorithm that is specifically written for the TronProblem (it only considers safe actions) """ state = asp.get_start_state() player = state.player_to_move() best_action = 'D' best_value = -float('inf') a = -float('inf') b = float('inf') for action in TronProblem.get_safe_actions(state.board, state.player_locs[player]): next_state = asp.transition(state, action) value = abchelper(asp, next_state, cutoff_turn - 1, a, b, False, eval_func, player) if value > best_value: best_value = value best_action = action a = max(a, best_value) if b <= a: break return best_action
def main(): random.seed(1) np.random.seed(1) parser = argparse.ArgumentParser(prog="gamerunner", usage="%(prog)s [options]") parser.add_argument("-map", type=str, help=HelpMessage.MAP, default=Argument_Defaults.MAP) parser.add_argument( "-max_wait", type=float, help=HelpMessage.MAX_WAIT, default=Argument_Defaults.MAX_WAIT, ) parser.add_argument( "-bots", type=str, nargs="+", help=HelpMessage.BOTS, default=Argument_Defaults.BOTS, ) parser.add_argument( "-image_delay", type=float, help=HelpMessage.IMAGE_DELAY, default=Argument_Defaults.IMAGE_DELAY, ) parser.add_argument("-no_image", dest="show_image", help=HelpMessage.NO_IMAGE, action="store_false") parser.set_defaults(show_image=True) parser.add_argument("-multi_test", type=int, help=HelpMessage.MULTI_TEST) parser.add_argument("-no_color", dest="colored", help=HelpMessage.COLOR, action="store_false") parser.set_defaults(colored=True) args = parser.parse_args() wait = args.max_wait bots = support.determine_bot_functions(args.bots) delay = args.image_delay verbose = args.show_image multi = args.multi_test colored = args.colored visualizer = None if verbose: visualizer = TronProblem.visualize_state if multi is not None: winners = defaultdict(int) bots = support.determine_bot_functions(args.bots) for i in range(multi): game = TronProblem(args.map, 0) outcome = run_game(game, bots, visualizer, delay, wait, colored) winner = outcome.index(1) winners[winner] += 1 for bot in bots: bot.cleanup() for winner, wins in list(winners.items()): print("Player %s won %d out of %d times" % (winner + 1, wins, multi)) else: game = TronProblem(args.map, 0) outcome = run_game(game, bots, visualizer, delay, wait, colored) winner = outcome.index(1) + 1 print("Player %s won!" % winner)
def fillboard(self, state, me, enemy): board = state.board locs = state.player_locs myloc = locs[me] enemyloc = locs[enemy] floodboard = np.array(board) if floodboard[myloc] == '*': floodboard[myloc] = 'A' #my powerup else: floodboard[myloc] = 'M' #'M' for me if floodboard[enemyloc] == '*': floodboard[enemyloc] = 'B' #enemy powerup else: floodboard[enemyloc] = 'E' #'E' for enemy myvisited = {myloc} enemyvisited = {enemyloc} myQ = Queue.Queue() myQ.put(myloc) enemyQ = Queue.Queue() enemyQ.put(enemyloc) myneighbors = set() enemyneighbors = set() # keep going until no new positions are added to the queues while not myQ.empty() or not enemyQ.empty(): while not myQ.empty(): #I fill first mycur = myQ.get() valid_moves = list(TronProblem.get_safe_actions(board, mycur)) for direction in valid_moves: neighbor = TronProblem.move(mycur, direction) if neighbor not in myvisited and not ( floodboard[neighbor] == 'E' or floodboard[neighbor] == 'B'): if floodboard[neighbor] == '*': #if tile is a powerup floodboard[ neighbor] = 'A' #Let 'A' represent a 'M' space with powerup else: floodboard[neighbor] = 'M' myvisited.add(neighbor) myneighbors.add(neighbor) while not enemyQ.empty(): #Then enemy fills enemycur = enemyQ.get() valid_moves = list( TronProblem.get_safe_actions(board, enemycur)) for direction in valid_moves: neighbor = TronProblem.move(enemycur, direction) if neighbor not in enemyvisited and not ( floodboard[neighbor] == 'M' or floodboard[neighbor] == 'A'): if floodboard[neighbor] == '*': floodboard[ neighbor] = 'B' #Let 'B' represent a 'E' space with powerup else: floodboard[neighbor] = 'E' enemyvisited.add(neighbor) enemyneighbors.add(neighbor) map( myQ.put, myneighbors ) #put all the newfound neighbors onto queue for next iteration myneighbors = set() #reset neighbors set map(enemyQ.put, enemyneighbors) enemyneighbors = set() numM = np.count_nonzero( floodboard == 'M') #count the number of tiles I reach first numMpwr = np.count_nonzero( floodboard == 'A') #count the number of powerup tiles I reach first numE = np.count_nonzero( floodboard == 'E') #count the number of tiles that enemy reaches first numEpwr = np.count_nonzero( floodboard == 'B') #count the number of powerup tiles that enemy reaches first val = (numM + numMpwr) - ( numE + numEpwr) #maximize the difference between our spaces return val
def fillboard(self, state, me, enemy): board = state.board locs = state.player_locs myloc = locs[me] enemyloc = locs[enemy] enemyspace = self.get_surroundings(state, enemyloc, 2) floodboard = np.array(board) closebonus = 0 if floodboard[myloc] == '*': floodboard[myloc] = 'A' #my powerup closebonus += 2 else: floodboard[myloc] = 'M' #'M' for me if floodboard[enemyloc] == '*': floodboard[enemyloc] = 'B' #enemy powerup else: floodboard[enemyloc] = 'E' #'E' for enemy for direction in list(TronProblem.get_safe_actions(board, myloc)): neighbor = TronProblem.move(myloc, direction) if floodboard[neighbor] == '*': closebonus += 1 myvisited = {myloc} enemyvisited = {enemyloc} myQ = Queue.Queue() myQ.put(myloc) enemyQ = Queue.Queue() enemyQ.put(enemyloc) myneighbors = set() enemyneighbors = set() colored = False # KEY # A powerup # M colored tile me # N non-colored tile me # keep going until no new positions are added to the queues while not myQ.empty() or not enemyQ.empty(): while not myQ.empty(): #I fill first mycur = myQ.get() valid_moves = list(TronProblem.get_safe_actions(board, mycur)) for direction in valid_moves: neighbor = TronProblem.move(mycur, direction) if neighbor not in myvisited and not ( floodboard[neighbor] == 'E' or floodboard[neighbor] == 'F' or floodboard[neighbor] == 'B'): if floodboard[neighbor] == '*': #if tile is a powerup if colored: floodboard[ neighbor] = 'A' #Let 'A' represent a 'M' space with powerup else: floodboard[neighbor] = 'C' #not colored mine else: if colored: floodboard[neighbor] = 'M' else: floodboard[neighbor] = 'N' myvisited.add(neighbor) myneighbors.add(neighbor) while not enemyQ.empty(): #Then enemy fills enemycur = enemyQ.get() valid_moves = list( TronProblem.get_safe_actions(board, enemycur)) for direction in valid_moves: neighbor = TronProblem.move(enemycur, direction) if neighbor not in enemyvisited and not ( floodboard[neighbor] == 'M' or floodboard[neighbor] == 'N' or floodboard[neighbor] == 'A'): if floodboard[neighbor] == '*': if colored: floodboard[ neighbor] = 'B' #Let 'B' represent a 'E' space with powerup else: floodboard[neighbor] = 'D' #not colored enemy else: if colored: floodboard[neighbor] = 'E' else: floodboard[neighbor] = 'F' enemyvisited.add(neighbor) enemyneighbors.add(neighbor) colored = not colored map( myQ.put, myneighbors ) #put all the newfound neighbors onto queue for next iteration myneighbors = set() #reset neighbors set map(enemyQ.put, enemyneighbors) enemyneighbors = set() numM = np.count_nonzero( floodboard == 'M') #count the number of colored tiles I reach first numN = np.count_nonzero( floodboard == 'N') #count the number of non-colored tiles I reach first numMpwr = np.count_nonzero( floodboard == 'A') #count the number of powerup tiles I reach first numNpwr = np.count_nonzero(floodboard == 'C') numE = np.count_nonzero( floodboard == 'E') #count the number of colored tiles that enemy reaches first numF = np.count_nonzero( floodboard == 'F' ) #count the number of non-colored tiles that enemy reaches first numEpwr = np.count_nonzero( floodboard == 'B') #count the number of powerup tiles that enemy reaches first numFpwr = np.count_nonzero(floodboard == 'D') val = (min(numM + numMpwr * self.powerup_val, numN + numNpwr * self.powerup_val) + (numMpwr + numNpwr) * self.powerup_val) - ( min(numE + numEpwr * self.powerup_val, numF + numFpwr * self.powerup_val) + (numEpwr + numFpwr) * self.powerup_val ) #maximize the difference between our spaces return val + closebonus
def _get_safe_actions_random(board, loc): actions = list(TronProblem.get_safe_actions(board, loc)) random.shuffle(actions) return actions