def _gen_grid(self, width, height):

        self.grid = Grid(width, height)

        # Generate the surrounding walls
        self.grid.wall_rect(0, 0, width, height)

        # Place a goal square in the bottom-right corner
        # self.put_obj(Goal(), width - 2, height - 2)

        for i in range(6):
            self.put_obj(Wall(), 3, i + 1)

        self.put_obj(Door('blue'), 3, 5)

        self.put_obj(Ball('red'), 4, 1)
        self.put_obj(Key('green'), 4, 2)
        self.put_obj(Box('grey'), 4, 3)
        self.put_obj(Ball('blue'), 4, 4)

        # Place the agent
        if self.agent_start_pos is not None:
            self.agent_pos = self.agent_start_pos
            self.agent_dir = self.agent_start_dir
        else:
            self.place_agent()

        self.mission = "get to the green goal square"
Esempio n. 2
0
    def _gen_grid(self, width, height):
        # We catch RecursionError to deal with rare cases where
        # rejection sampling gets stuck in an infinite loop
        while True:
            try:
                super()._gen_grid(width, height)

                # Generate the mission
                self.gen_mission()

                # Validate the instructions
                self.validate_instrs(self.instrs)

            except RecursionError as error:
                print('Timeout during mission generation:', error)
                continue

            except RejectSampling as error:
                print('Sampling rejected:', error)
                continue

        # Generate the surrounding walls
        self.grid.wall_rect(0, 0, width, height)

        # Generate the 4 doors at random positions
        doorPos = []
        doorPos.append((self._rand_int(2, width - 2), 0))
        doorPos.append((self._rand_int(2, width - 2), height - 1))
        doorPos.append((0, self._rand_int(2, height - 2)))
        doorPos.append((width - 1, self._rand_int(2, height - 2)))

        # Generate the door colors
        doorColors = []
        while len(doorColors) < len(doorPos):
            color = self._rand_elem(COLOR_NAMES)
            if color in doorColors:
                continue
            doorColors.append(color)

        # Place the doors in the grid
        for idx, pos in enumerate(doorPos):
            color = doorColors[idx]
            self.grid.set(*pos, Door(color))

        # Randomize the agent start position and orientation
        self.place_agent(size=(width, height))

        # Select a random target door
        doorIdx = self._rand_int(0, len(doorPos))
        self.target_pos = doorPos[doorIdx]
        self.target_color = doorColors[doorIdx]

        # Generate the mission string
        self.mission = 'go to the %s door' % self.target_color
        # Generate the surface form for the instructions
        self.surface = self.instrs.surface(self)
        self.mission = self.surface
Esempio n. 3
0
    def decode(array):
        """
        Decode an array grid encoding back into a grid
        """

        width, height, channels = array.shape
        assert channels == 3

        grid = Grid(width, height)
        for i in range(width):
            for j in range(height):
                typeIdx, colorIdx, state = array[i, j]

                if typeIdx == OBJECT_TO_IDX['unseen'] or \
                        typeIdx == OBJECT_TO_IDX['empty']:
                    continue

                objType = IDX_TO_OBJECT[typeIdx]
                color = IDX_TO_COLOR[colorIdx]
                # State, 0: open, 1: closed, 2: locked
                is_open = state == 0
                is_locked = state == 2

                if objType == 'wall':
                    v = Wall(color)
                elif objType == 'floor':
                    v = Floor(color)
                elif objType == 'ball':
                    v = Ball(color)
                elif objType == 'key':
                    v = Key(color)
                elif objType == 'box':
                    v = Box(color)
                elif objType == 'door':
                    v = Door(color, is_open, is_locked)
                elif objType == 'goal':
                    v = Goal()
                elif objType == 'lava':
                    v = Lava()
                elif objType == 'agent':
                    v = None
                else:
                    assert False, "unknown obj type in decode '%s'" % objType

                grid.set(i, j, v)

        return grid
Esempio n. 4
0
 def _add_door(self, coords):
     """Add door at coords = (x,y)"""
     x, y = coords
     assert isinstance(self.get(x,y), Wall), 'You can\'t put a door outside of a wall!'
     self.set(x, y, Door(color='purple', is_open=self.doors_open, is_locked=False))
Esempio n. 5
0
 def _add_door(self, coords):
     """Add door at coords = (x,y)"""
     #TODO assert (x,y) is a wall
     self.set(*coords, Door(color='purple', is_open=self.doors_open, is_locked=False))
Esempio n. 6
0
    def _gen_grid(self, width, height):
        roomList = []

        # Choose a random number of rooms to generate
        numRooms = self._rand_int(self.minNumRooms, self.maxNumRooms + 1)

        while len(roomList) < numRooms:
            curRoomList = []

            entryDoorPos = (self._rand_int(0, width - 2),
                            self._rand_int(0, width - 2))

            # Recursively place the rooms
            self._placeRoom(numRooms,
                            roomList=curRoomList,
                            minSz=4,
                            maxSz=self.maxRoomSize,
                            entryDoorWall=2,
                            entryDoorPos=entryDoorPos)

            if len(curRoomList) > len(roomList):
                roomList = curRoomList

        # Store the list of rooms in this environment
        assert len(roomList) > 0
        self.rooms = roomList

        # Create the grid
        self.grid = Grid(width, height)
        wall = Wall()

        prevDoorColor = None

        # For each room
        for idx, room in enumerate(roomList):
            room.name = self.room_names[idx]

            topX, topY = room.top
            sizeX, sizeY = room.size

            # Draw the top and bottom walls
            for i in range(0, sizeX):
                self.grid.set(topX + i, topY, wall)
                self.grid.set(topX + i, topY + sizeY - 1, wall)

            # Draw the left and right walls
            for j in range(0, sizeY):
                self.grid.set(topX, topY + j, wall)
                self.grid.set(topX + sizeX - 1, topY + j, wall)

            # If this isn't the first room, place the entry door
            if idx > 0:
                # Pick a door color different from the previous one
                doorColors = set(COLOR_NAMES)
                if prevDoorColor:
                    doorColors.remove(prevDoorColor)
                # Note: the use of sorting here guarantees determinism,
                # This is needed because Python's set is not deterministic
                doorColor = self._rand_elem(sorted(doorColors))

                entryDoor = Door(doorColor)
                self.grid.set(*room.entryDoorPos, entryDoor)
                prevDoorColor = doorColor

                prevRoom = roomList[idx - 1]
                prevRoom.exitDoorPos = room.entryDoorPos

        # Randomize the starting agent position and direction
        self.place_agent(roomList[0].top, roomList[0].size)

        # Create rooms dict
        rooms_dict = {}
        for r in self.rooms:
            if r.name not in rooms_dict:
                rooms_dict[r.name] = {}
                rooms_dict[r.name]["P"] = {}
            rooms_dict[r.name]["T"] = r.temperature
            rooms_dict[r.name]["A"] = (r.size[0] - 2) * (r.size[1] - 2)
            rooms_dict[r.name]["heat"] = 0
            rooms_dict[r.name]["mask"] = np.zeros((self.width, self.height))
            rooms_dict[r.name]["mask"][r.y1 + 1:r.y2 - 1,
                                       r.x1 + 1:r.x2 - 1] = 1
            P_out = 2 * (r.x2 - r.x1 + r.y2 - r.y1 - 4)
            for r2 in self.rooms:
                if r2 == r:
                    pass
                else:
                    P = 0
                    if r.x1 + 1 == r2.x2 or r.x2 == r2.x1 + 1:
                        overlap = min(r2.y2 - 1, r.y2 - 1) - max(r2.y1,
                                                                 r.y1) - 1
                        if overlap > 0:
                            P += overlap
                            P_out -= overlap
                    if r.y1 + 1 == r2.y2 or r.y2 == r2.y1 + 1:
                        overlap = min(r2.x2 - 1, r.x2 - 1) - max(r2.x1,
                                                                 r.x1) - 1
                        if overlap > 0:
                            P += overlap
                            P_out -= overlap
                    rooms_dict[r.name]["P"][r2.name] = P
            rooms_dict[r.name]["P_out"] = P_out
            rooms_dict[r.name]["T_out"] = self.t_out
        self.rooms_dict = rooms_dict

        # Place the heating tiles in the house
        self.temperatures = np.ones((self.width, self.height)) * self.t_out
        for r, data in self.rooms_dict.items():
            self.temperatures[data["mask"] == 1] = data["T"]

        for i, cell in enumerate(self.grid.grid):
            x, y = divmod(i, width)

            if cell is None:
                self.grid.grid[i] = HeatingTile(
                    temperature=self.temperatures[x, y])
            self.grid.grid[i].temperature = self.temperatures[x, y]

        # # Place the final goal in the last room
        # self.goal_pos = self.place_obj(Goal(), roomList[-1].top,
        #                                roomList[-1].size)

        self.mission = 'save the world'