def __init__(self, game_properties): self.logger = logging.getLogger("AI AGENT") self.logger.setLevel(logging.WARN) self.logger.info("AI Agent created.") self.map = Map(size=game_properties["size"], mapa=game_properties["map"]) self.logger.debug(self.map) self.update_wallpass_next = False self.cur_pos = None self.walls = None self.enemies = None self.powerups = None self.bonus = None self.exit = None self.have_powerup = None self.level = None self.lives = 3 #this shouldnt be hardcoded # self.enemy_past_pos = {} self.pursuing_enemy = None # self.eval_enemy = None self.depth_limit = 100 self.loop = 0 self.search_domain = BombermanSearch(self.map) self.decisions_queue = [] self.rounds_pursuing_limit = 3 # Limit of rounds we can be pursuing the same enemy self.wait_time = 200 # time to wait when in loop pursuing enemy self.last_enemy_dir = None self.perform_last_resort = False
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect(f"ws://{server_address}/player") as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() game_properties = json.loads(msg) # You can create your own map representation or use the game representation: mapa = Map(game_properties["map"]) print(mapa) while True: try: state = json.loads( await websocket.recv() ) # receive game state, this must be called timely or your game will get out of sync with the server elif event.key == pygame.K_d: import pprint pprint.pprint(state) print(Map(f"levels/{state['level']}.xsb")) await websocket.send( json.dumps({"cmd": "key", "key": key}) ) # send key command to server - you must implement this send in the AI agent break
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect( f"ws://{server_address}/player") as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) # Next 3 lines are not needed for AI agent SCREEN = pygame.display.set_mode((299, 123)) SPRITES = pygame.image.load("data/pad.png").convert_alpha() SCREEN.blit(SPRITES, (0, 0)) while True: try: update = json.loads( await websocket.recv() ) # receive game update, this must be called timely or your game will get out of sync with the server if "map" in update: # we got a new level game_properties = update mapa = Map(update["map"]) else: # we got a current map state update state = update # Next lines are only for the Human Agent, the key values are nonetheless the correct ones! key = "" for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: key = "w" elif event.key == pygame.K_LEFT: key = "a" elif event.key == pygame.K_DOWN: key = "s" elif event.key == pygame.K_RIGHT: key = "d" elif event.key == pygame.K_d: import pprint pprint.pprint(state) print(Map(f"levels/{state['level']}.xsb")) await websocket.send( json.dumps({ "cmd": "key", "key": key }) ) # send key command to server - you must implement this send in the AI agent break except websockets.exceptions.ConnectionClosedOK: print("Server has cleanly disconnected us") return # Next line is not needed for AI agent pygame.display.flip()
def __init__(self, obs_type, positive_rewards, agent_name, ghosts, level_ghosts, lives, timeout, map_files=None, training=True): self.positive_rewards = positive_rewards self.agent_name = agent_name self.training = training self.train_env_params = [p for p in ENV_PARAMS if p.test_runs > 1] maps = [] if not map_files: for mf in {param.map for param in ENV_PARAMS}: maps.append(Map(mf)) else: for mf in map_files: maps.append(Map(mf)) mapfile = maps[0].filename self._game = Game(mapfile, ghosts, level_ghosts, lives, timeout) self.pacman_obs = obs_type(maps, lives) self.observation_space = self.pacman_obs.space self.action_space = gym.spaces.Discrete(len(self.keys)) self._current_score = 0 self.current_lives = self._game._initial_lives self._last_pos = None self._current_params = EnvParams(ghosts, level_ghosts, mapfile, 10) self.idle_steps = 0 self.total_energy = len(self._game.map.energy) self.energy_reward_increment = (self.MAX_ENERGY_REWARD - self.MIN_ENERGY_REWARD) / ( self.total_energy - 1) self._current_energy_reward = self.MIN_ENERGY_REWARD self.difficulty = self.INITIAL_DIFFICULTY self.wins_count = 0
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect( f"ws://{server_address}/player") as websocket: # receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() game_properties = json.loads(msg) # you can create your own map representation or use the game representation: mapa = Map(size=game_properties["size"], mapa=game_properties["map"]) # init bomberman agent properties bomberman = Bomberman() logger.debug("STARTING GAME") while True: try: while websocket.messages: await websocket.recv() logger.debug(f"Websocket messages: {websocket.messages}") state = json.loads( await websocket.recv() ) # receive game state, this must be called timely or your game will get out of sync with the server if "lives" not in state or not state["lives"]: logger.debug("GAME OVER!") return mapa.walls = state["walls"] # update our bomberman state bomberman.update_state(state, mapa) # choose next move of bomberman key = bomberman.next_move() if key is None: logger.debug("RANDOM KEY") moves = ["w", "a", "s", "d"] key = random.choice(moves) logger.debug(f"P: {bomberman.pos} | K: {key}") await websocket.send( json.dumps({ "cmd": "key", "key": key }) ) # send key command to server - you must implement this send in the AI agent except websockets.exceptions.ConnectionClosedOK: print("Server has cleanly disconnected us") return
def __init__(self, level=1, lives=LIVES, timeout=TIMEOUT, size=MAP_SIZE): logger.info(f"Game(level={level}, lives={lives})") self.initial_level = level self._running = False self._timeout = timeout self._score = 0 self._state = {} self._initial_lives = lives self.map = Map(size=size, empty=True) self._enemies = []
def next_level(self, level): """Update all state variables to a new level.""" self._total_steps += self._step self._step = 0 self._lastkeypress = "" self.level = level try: self.map = Map(f"levels/{level}.xsb") logger.info("NEXT LEVEL: %s", level) except FileNotFoundError: logger.info("No more levels... You WIN!") self.stop() return
def __init__(self, mapfile, n_ghosts=GHOSTS, lives=LIVES, timeout=TIMEOUT): logger.info("Game({}, {}, {})".format(mapfile, n_ghosts, lives)) self._running = False self._timeout = timeout self._state = {} self._n_ghosts = n_ghosts self._initial_lives = lives self.map = Map(mapfile) self._highscores = [] if os.path.isfile(mapfile + ".score"): with open(mapfile + ".score", 'r') as infile: self._highscores = json.load(infile)
def start(self, player_name): logger.debug("Reset world") self._player_name = player_name self._running = True self.map = Map(self.map.filename) self._step = 0 self._ghosts = [Ghost(self.map) for g in range(0, self._n_ghosts)] self._pacman = self.map.pacman_spawn self._energy = self.map.energy self._boost = self.map.boost self._lastkeypress = "d" self._score = INITIAL_SCORE self._lives = self._initial_lives
def actions(self, current_state): ''' RECEIVES: the current state RETURNS: a list containing all the valid actions for the state Calculates all the valid actions for a state: -> Receives a dictionary containing the current state -> For every direction in "wasd" calculates the next state (positions of the keeper and boxes) -> Verifies wether the boxes are in a deadlock -> Return a list of valid actions (directions) ''' valid_directions = [] for direction in DIRECTIONS: next_state = calc_next_state(current_state, direction) keeper = next_state['keeper'] boxes = next_state['boxes'] valid_directions.append(direction) # Check wether we are placing a box outside of the map or # placing a box on top of another box if Map.is_blocked(self.level_map, keeper) or len(list(set(boxes))) < len(boxes): valid_directions.remove(direction) continue for box in boxes: if box in self.deadlocks_pos: valid_directions.remove(direction) continue elif self.isDeadlock(box): self.deadlocks_pos.append(box) self.deadlocks.append(next_state) valid_directions.remove(direction) return list(set(valid_directions))
async def agent_loop(puzzle, solution, server_address="localhost:8000", agent_name="student"): async with websockets.connect(f"ws://{server_address}/player") as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) while True: try: update = json.loads( await websocket.recv() ) # receive game update, this must be called timely or your game will get out of sync with the server if "map" in update: # we got a new level mapa = Map(update["map"]) keys = "" await puzzle.put(update) if not solution.empty(): keys = await solution.get() key = "" if len(keys): key = keys[0] keys = keys[1:] await websocket.send( json.dumps({"cmd": "key", "key": key}) ) except websockets.exceptions.ConnectionClosedOK: print("Server has cleanly disconnected us") sys.exit(0) return
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect( "ws://{}/player".format(server_address)) as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() game_properties = json.loads(msg) mapa = Map(game_properties['map']) #init agent properties key = 'a' cur_x, cur_y = None, None while True: r = await websocket.recv() state = json.loads(r) #receive game state if not state['lives']: print("GAME OVER") return x, y = state['pacman'] if x == cur_x and y == cur_y: if key in "ad": key = random.choice("ws") elif key in "ws": key = random.choice("ad") cur_x, cur_y = x, y #send new key await websocket.send(json.dumps({"cmd": "key", "key": key}))
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect(f"ws://{server_address}/player") as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() game_properties = json.loads(msg) # You can create your own map representation or use the game representation: mapa = Map(size=game_properties["size"], mapa=game_properties["map"]) # Create instance of Agent agent = Agent(mapa) while True: try: # receive game state, this must be called timely or your game will get out of sync with the server state = json.loads(await websocket.recv()) # Para comer todas as mensagens q estao entaladas while len(websocket.messages) > 0: state = json.loads(await websocket.recv()) # Para so enviar o score if len(state) > 1: agent.update_agent(state) key = agent.exec() await websocket.send( json.dumps({"cmd": "key", "key": key}) ) # send key command to server - you must implement this send in the AI agent except websockets.exceptions.ConnectionClosedOK: return
async def solver(puzzle, solution): domain = Sokoban() while True: game_properties = await puzzle.get() mapa = Map(game_properties["map"]) domain.set_mapa(mapa) p = SearchProblem(domain, mapa) t = SearchTree(p, 'a*') while True: await asyncio.sleep( 0) # this should be 0 in your code and this is REQUIRED break keys = await t.search() print(game_properties["map"]) print('terminals: {} non-terminals: {} depth: {}'.format( t.terminals, t.non_terminals, t.length)) print(len(keys), keys) await solution.put(keys)
async def solver(puzzle, solution): i=0 while True: start = time.time() game_properties = await puzzle.get() mapa = Map(game_properties["map"]) initial, goal, walls, available = map_conditions(mapa) coord_box = [b.args for b in initial] coord_goal = [g.args for g in goal] wall_deadlocks = static_wall_deadlocks([c for c in available if not c in coord_box], coord_goal, walls) strips = STRIPS(available, walls, wall_deadlocks, coord_goal) p = SearchProblem(strips, initial, goal) t = SearchTree(p, mapa.keeper) await t.search() keys = t.plan await solution.put(keys) i+=1 print("nv:", i,' time:', time.time() - start) print('Nos terminais', t.terminals) print('Nos nao terminais', t.non_terminals)
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect( f"ws://{server_address}/player") as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() game_properties = json.loads(msg) print("GAME PROPERTIES AS SEEN BY CLIENT: ") print(game_properties) # You can create your own map representation or use the game representation: mapa = Map(size=game_properties["size"], mapa=game_properties["map"]) # Next 3 lines are not needed for AI agent SCREEN = pygame.display.set_mode((299, 123)) SPRITES = pygame.image.load("data/pad.png").convert_alpha() SCREEN.blit(SPRITES, (0, 0)) while True: try: state = json.loads( await websocket.recv() ) # receive game state, this must be called timely or your game will get out of sync with the server # Next lines are only for the Human Agent, the key values are nonetheless the correct ones! key = "" for event in pygame.event.get(): if event.type == pygame.QUIT or not state["lives"]: pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: key = "w" elif event.key == pygame.K_LEFT: key = "a" elif event.key == pygame.K_DOWN: key = "s" elif event.key == pygame.K_RIGHT: key = "d" elif event.key == pygame.K_a: key = "A" elif event.key == pygame.K_b: key = "B" await websocket.send( json.dumps({ "cmd": "key", "key": key }) ) # send key command to server - you must implement this send in the AI agent break except websockets.exceptions.ConnectionClosedOK: print("Server has cleanly disconnected us") return # Next line is not needed for AI agent pygame.display.flip()
def next_level(self, level): if level > len(LEVEL_ENEMIES): logger.info("You WIN!") self.stop() return logger.info("NEXT LEVEL") self.map = Map(level=level, size=self.map.size, enemies=len(LEVEL_ENEMIES[level])) self._step = 0 self._bomberman = Bomberman(self.map.bomberman_spawn, self._initial_lives) self._bombs = [] self._powerups = [] self._bonus = [] self._exit = [] self._lastkeypress = "" self._bomb_radius = 3 self._enemies = [ t(p) for t, p in zip(LEVEL_ENEMIES[level], self.map.enemies_spawn) ]
def next_level(self, level): if level > len(LEVEL_ENEMIES): logger.info("You WIN!") self.stop() return logger.info("NEXT LEVEL") self.map = Map(level=level, size=self.map.size, enemies=len(LEVEL_ENEMIES[level])) self._bomberman.respawn() self._step = 0 self._bombs = [] self._powerups = [] self._bonus = [] self._exit = [] self._lastkeypress = "" self._enemies = [ t(p) for t, p in zip(LEVEL_ENEMIES[level], self.map.enemies_spawn) ] logger.debug("Enemies: %s", [(e._name, e.pos) for e in self._enemies]) logger.debug("Walls: %s", self.map.walls)
def tester(filename): p = SokobanDomain(filename) mapa = Map(filename) initial = {"player": mapa.keeper, "boxes": mapa.boxes} goal = { "boxes": mapa.filter_tiles([Tiles.MAN_ON_GOAL, Tiles.BOX_ON_GOAL, Tiles.GOAL]) } problema = SearchProblem(p, initial, goal) t = SearchTree(problema, 'a*', mapa) return t
async def agent_loop(server_address="localhost:8000", agent_name="student"): async with websockets.connect( "ws://{}/player".format(server_address)) as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() game_properties = json.loads(msg) mapa = Map(game_properties['map']) #init agent properties acts = [] currentlives = 99 while True: r = await websocket.recv() state = json.loads(r) #receive game state if not state['lives']: print("GAME OVER") return #You recalculate if: # -You die (lives decrement) # -Ghost gets on your path (WIP!!!) # -Run out of acts if not acts or state['lives'] < currentlives: print("----------START SEARCH-----------") cur_x, cur_y = state['pacman'] closest = list(state['energy'] + state['boost']) target_x, target_y = min( closest, key=lambda p: abs(cur_x - p[0]) + abs(cur_y - p[1])) d = PacDomain(mapa, state) f = formulatePacman(d, state['pacman'], (target_x, target_y)) t = solvePacman(f, 'astar') acts = t[1] coords = t[0] print("----------CONCLUDED SEARCH-----------") print("Output:") print(acts) print(t[0]) key = acts.pop(0) await websocket.send(json.dumps({"cmd": "key", "key": key})) currentlives = state['lives']
def __init__(self, level: Map, game_settings): self.max_steps = game_settings['timeout'] self.boxes = level.boxes self.goals = level.filter_tiles([Tiles.GOAL, Tiles.BOX_ON_GOAL]) self.keeper = level.keeper self.initial_state = { 'boxes': self.boxes, 'keeper': self.keeper, 'goals': self.goals } self.path_finder: SokobanSolver = SokobanSolver(level_map=level, method='mixed')
async def main_loop(q): main_group = pygame.sprite.OrderedUpdates() images = pygame.image.load("data/sprites/spritemap.png") logging.info("Waiting for map information from server") state = await q.get() #first state message includes map information newgame_json = json.loads(state) mapa = Map(newgame_json["map"]) for entry in newgame_json["highscores"]: print(entry) GAME_SPEED = newgame_json["fps"] SCREEN = pygame.display.set_mode(scale(mapa.size)) draw_background(mapa, SCREEN) main_group.add(PacMan(pos=scale(mapa.pacman_spawn), images=images)) for i in range(newgame_json["ghosts"]): main_group.add( Ghost(pos=scale(mapa.ghost_spawn), images=images, index=i)) state = dict() while True: pygame.event.pump() if pygame.key.get_pressed()[pygame.K_ESCAPE]: asyncio.get_event_loop().stop() main_group.clear(SCREEN, clear_callback) if "score" in state: text = str(state["score"]) draw_info(SCREEN, text.zfill(6), (0, 0)) text = str(state["player"]) draw_info(SCREEN, text, (4000, 0)) if "energy" in state: for x, y in state["energy"]: draw_energy(SCREEN, x, y) if "boost" in state: for x, y in state["boost"]: draw_energy(SCREEN, x, y, True) main_group.draw(SCREEN) main_group.update(state) pygame.display.flip() try: state = json.loads(q.get_nowait()) except asyncio.queues.QueueEmpty: await asyncio.sleep(1. / GAME_SPEED) continue
async def solver(puzzle, solution): while True: game_properties = await puzzle.get() mapa = Map(game_properties["map"]) print(mapa) agent = SokobanAgent(mapa, game_properties) keys = await agent.search() keys.remove('') print(keys) await solution.put(keys)
async def solver(map_queue, solver_queue): while True: game_properties = await map_queue.get() mapa = Map(game_properties["map"]) t = TreeSearch(mapa, game_properties['map']) while True: await asyncio.sleep(0) break keys = await t.search() await solver_queue.put(keys)
def sokobanSolver(self, filename): p = SokobanDomain(filename) mapa = Map(filename) initial = {"player": mapa.keeper, "boxes": mapa.boxes} goal = { "boxes": mapa.filter_tiles( [Tiles.MAN_ON_GOAL, Tiles.BOX_ON_GOAL, Tiles.GOAL]) } problema = SearchProblem(p, initial, goal) t = SearchTree(problema, 'a*', mapa) t.search() self.plan = t.get_plan(t.solution) return t
async def solver(puzzle, solution): domain = Sokoban() while True: game_properties = await puzzle.get() mapa = Map(game_properties["map"]) p = SearchProblem(domain, mapa) t = SearchTree(p, 'greedy') while True: await asyncio.sleep( 0) # this should be 0 in your code and this is REQUIRED break keys = await t.search() # print(keys) await solution.put(keys)
async def agent_loop(model, pacman_obs, agent_name, server_address="localhost:8000"): async with websockets.connect("ws://{}/player".format(server_address)) as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) msg = await websocket.recv() print("Available frame time: {} ms".format(1. / GAME_SPEED * 1000)) game_properties = json.loads(msg) game_map = Map(game_properties['map']) keys = PacmanEnv.keys while True: r = await websocket.recv() start_time = time.time() state = json.loads(r) if 'lives' in state: if not state['lives']: print("GAME OVER - Score: {}".format(state['score'])) return else: print("WIN - Score: {}".format(state['score'])) break obs = pacman_obs.get_obs(state, game_map) action, _states = model.predict(obs, deterministic=False) key = keys[action] elapsed_time = time.time() - start_time # print(elapsed_time * 1000, 1. / GAME_SPEED * 1000) assert elapsed_time <= 1. / GAME_SPEED, \ "The agent needed {} ms to take an action.".format(elapsed_time * 1000) await websocket.send(json.dumps({"cmd": "key", "key": key}))
async def solver(puzzle, solution): while True: game_properties = await puzzle.get() mapa = Map(game_properties["map"]) p = SokobanDomain(game_properties["map"]) initial = {"player": mapa.keeper, "boxes": mapa.boxes} goal = {"boxes": mapa.filter_tiles([Tiles.MAN_ON_GOAL, Tiles.BOX_ON_GOAL, Tiles.GOAL])} problema = SearchProblem(p, initial, goal) t = SearchTree(problema, 'a*', mapa) print(mapa) while True: t.search() await asyncio.sleep(0) # this should be 0 in your code and this is REQUIRED break keys = t.get_plan(t.solution) #print(keys) await solution.put(keys)
async def agent_loop(self,server_address="localhost:8000", agent_name="student"): async with websockets.connect(f"ws://{server_address}/player") as websocket: # Receive information about static game properties await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) # Next 3 lines are not needed for AI agent SCREEN = pygame.display.set_mode((299, 123)) SPRITES = pygame.image.load("data/pad.png").convert_alpha() SCREEN.blit(SPRITES, (0, 0)) while True: try: update = json.loads( await websocket.recv() ) # receive game update, this must be called timely or your game will get out of sync with the server state = None if "map" in update: # we got a new level game_properties = update mapa = Map(update["map"]) else: # we got a current map state update #global state state = update print(state) # Next lines are only for the Human Agent, the key values are nonetheless the correct ones! self.pygame(pygame.event.get(),state) except websockets.exceptions.ConnectionClosedOK: print("Server has cleanly disconnected us") return # Next line is not needed for AI agent pygame.display.flip()
def pygame(self, getPygameEvent, state): key = "" for event in getPygameEvent: if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: key = "w" elif event.key == pygame.K_LEFT: key = "a" elif event.key == pygame.K_DOWN: key = "s" elif event.key == pygame.K_RIGHT: key = "d" elif event.key == pygame.K_d: import pprint pprint.pprint(state) print(Map(f"levels/{state['level']}.xsb")) send_web_socket(key) break
from mapa import Map mp = Map() mp.infile('mapa_in_02abr.dat') mp.create_label() mp.calcfaixa(step=10) mp.geramapa()