def restore(self, state_info): self.current_task = 0 self.episode_steps = 0 self.width = state_info['width'] self.height = state_info['height'] self._map = [[[] for x in range(self.height)] for y in range(self.width)] self._approx_reward_map = [[ -self.turn_penalty for x in range(self.height) ] for y in range(self.width)] for loc in state_info['block_locs']: self._add_item(maze_items.Block(location=loc)) for loc in state_info['water_locs']: self._add_item(maze_items.Water(location=loc)) init_state, tasks = state_info['config'] self.agent = self.agent_cls(location=init_state['loc_a']) self._add_agent(self.agent, "TaxiAgent") self.target = maze_items.Goal(location=init_state['loc_t']) self._add_item(self.target) self.passenger = Passenger(location=init_state['loc_p']) self._add_item(self.passenger) if init_state['is_picked_up']: self.agent.actions['pickup']() self.current_config = state_info['config']
def _reset(self): intersection, holes = add_4_walls(self) # No doors for now # self.door = mi.Door(location=hole, # state=choice(range(1, self.switch_states))) # self._add_item(self.door) # ad blocks on the holes temporarily blocked_holes = [] for i in range(len(holes)): blocked_holes.append(mi.Block(location= holes[i])) self._add_item(blocked_holes[i]) # Add additional blocks and waters # only blocks and water have reset methods, and that sprinkles blocs and waters around super(TrapKey, self)._reset() # empty the wall opening if they have been blocked for i in range(len(holes)): self._remove_item(blocked_holes[i].id) # Add the goal loc = choice(creationutils.empty_locations( self, bad_blocks=[mi.Block, mi.Trap, mi.Water])) # bad blocks where the goal can't be placed self.goal = mi.Goal(location=loc) self._add_item(self.goal) # # no switches for now # side = choice([-1, 1]) # # for adding switch to the opposite side of the goal # def mask_func(x, y): # return side * ((x, y)[1 - dim] - hole[1 - dim]) > 0 # loc = choice(creationutils.empty_locations( # self, bad_blocks=[mi.Block, mi.Door, mi.Goal], mask=mask_func)) # self.sw = mi.Switch(location=loc, nstates=self.switch_states) # self._add_item(self.sw) # add agent loc = choice(creationutils.empty_locations( self, bad_blocks=[mi.Block, mi.Trap])) self.agent = TriggerAgent(location=loc) self._add_agent(self.agent, "TrapKeyAgent") # check if goal can be visited or not visited, _ = creationutils.dijkstra(self, loc, creationutils.agent_movefunc) if self.goal.location not in visited : raise MazeException("No path to goal")
def add_4_walls(game, margin = 1 ): """ creates a vertical and horizontal wall openings in each wall for dividing the game into 4 walls """ size = (game.width, game.height) x_line = randint(margin, (size[0] - 1) - margin ) y_line = randint(margin, (size[1] - 1) - margin ) #intersecting point will be # x_line, y _line # get the four opening here wall_openings = [ (randint(0, x_line - 1), y_line), (randint(x_line + 1, size[0] - 1), y_line), (x_line, randint(0, y_line - 1)), (x_line, randint(y_line + 1, size[1] - 1)), ] # fill the horizontal wall for i in range(size[0]): if (i, y_line) not in wall_openings: loc = [i, y_line] game._add_item(mi.Block(location=loc)) # fill the vertical wall for i in range(size[1]): if (x_line, i) not in wall_openings: loc = [x_line, i] game._add_item(mi.Block(location=loc)) # return the intersection point # dunno why ? loc = [x_line, y_line] return loc, wall_openings
def add_vertical_wall(game): size = (game.width, game.height) dim = choice([0, 1]) line = randint(1, size[1 - dim] - 2) opening = randint(0, size[dim] - 1) for i in range(size[dim]): if i != opening: loc = [line, line] loc[dim] = i game._add_item(mi.Block(location=loc)) loc = [line, line] loc[dim] = opening return loc, dim