def get_state_from_val(domain_file, problem_file, plan_file, objects,
                       init_state):
    param = VAL_PATH + " -v " + domain_file + " " + problem_file + " " + plan_file + " > " + GEN_VAL_FILE
    p = subprocess.Popen([param], shell=True)
    p.wait()
    all_states = []

    curr_state = set(copy.deepcopy(init_state))

    # Scanning the VAL output
    f = open(GEN_VAL_FILE, 'r')
    for line in f:
        # Adding a new policy rule
        if "Checking next happening (time " in line or "Plan executed successfully" in line:
            _state = State(set_to_state(curr_state), objects)
            all_states.append(copy.deepcopy(_state))

        if "Deleting " in line:
            pred = line.replace("Deleting ",
                                "").replace("(", "").replace(")\n",
                                                             "").split(" ")
            pred = "|".join(pred)
            curr_state.discard(pred)

        if "Adding " in line:
            pred = line.replace("Adding ",
                                "").replace("(", "").replace(")\n",
                                                             "").split(" ")
            pred = "|".join(pred)
            curr_state.add(pred)

    f.close()

    return all_states
def create_custom_query_init_state(init_state):
    w = init_state['width']
    h = init_state['height']
    walls = init_state['walls']
    stones = init_state['stones']
    goals = init_state['goals']
    player_loc = init_state['player']
    state = State({}, {})
    keys = [
        "at", "clear", "is-goal", "is-stone", "is-goal", "is-nongoal",
        "is-player"
    ]
    for key in keys:
        state.state[key] = []

    stone_num = 0
    for x in range(1, w + 1):
        for y in range(1, h + 1):
            loc = 'pos-' + "{0:0=2d}".format(x) + '-' + "{0:0=2d}".format(y)
            state.objects[loc] = "location"
            if loc in stones:
                stone_num += 1
                stone = "stone-" + "{0:0=2d}".format(stone_num)
                state.objects[stone] = "thing"
                state.state['at'].append(tuple([stone, loc]))
                state.state['is-stone'].append(tuple([stone]))
            elif loc in player_loc:
                state.state['at'].append(tuple(["player-01", loc]))
            elif loc not in walls:
                state.state['clear'].append(tuple([loc]))
            if loc in goals:
                state.state['is-goal'].append(tuple([loc]))
            else:
                state.state['is-nongoal'].append(tuple([loc]))
            state.objects[loc] = "location"
    state.objects['player-01'] = "thing"
    state.state["is-player"] = [tuple(["player-01"])]
    return state
示例#3
0
def gym_to_iaa_dict(state):
    """
    Converts gym state to iaa state (dict) format
    """

    ret_state = {}
    objects = {}
    for lit in list(state):
        pred_name = lit.predicate.name
        if pred_name not in ret_state.keys():
            ret_state[pred_name] = []
        vars_temp = []
        for v in lit.variables:
            vars_temp.append(v.name)
            if v.var_type in objects.keys():
                if v.name not in objects[v.var_type]:
                    objects[v.var_type].append(v.name)
            else:
                objects[v.var_type] = [v.name]

        ret_state[pred_name].append(tuple(vars_temp))
    return State(ret_state, objects)
    def get_state_from_render(self, img, init_state):
        """
        get all initial info from init_state
        detect and filter contours from image
        iterate over each contour and create Cells
        for each cell, detect its type and store the results
        add the detected cell literals to state
        """
        cell_width = 35
        temp_state = {}
        for pred in init_state.state:
            if pred not in ["at", "unlocked"]:
                temp_state[pred] = init_state.state[pred]

        temp_state['at'] = []
        temp_state['unlocked'] = [('room-0', )]
        key_locations = {}
        key_for_room = {}
        for (key, loc) in temp_state['keyat']:
            key_locations[loc] = key
        for (key, room) in temp_state['keyforroom']:
            key_for_room[key] = room

        detected_state = State(temp_state, init_state.objects)
        filtered_contours = get_contours(img, range_min=1500, range_max=2000)
        px_coords = []
        py_coords = []
        img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
        grid = []
        for c in filtered_contours:
            _m = cv2.moments(c)
            pc_x = int(_m["m10"] / _m["m00"])
            pc_y = int(_m["m01"] / _m["m00"])
            px_coords.append(pc_y)
            py_coords.append(pc_x)
            mean_color = calculate_color(img_lab, c)
            cell_temp = Cell(pc_y, pc_x, c, mean_color)
            grid.append(cell_temp)

        px_coords = list(sorted(px_coords))
        grid = sorted(grid, key=lambda x: x.pc_x)
        i = 0
        j = 1
        while i < len(px_coords) and j < len(px_coords):
            if abs(px_coords[i] - px_coords[j]) < cell_width:
                px_coords[j] = px_coords[i]
                grid[j].pc_x = px_coords[j]
                j += 1
                continue
            i = j
            j += 1

        py_coords = list(sorted(py_coords))
        grid = sorted(grid, key=lambda x: x.pc_y)
        i = 0
        j = 1
        while i < len(py_coords) and j < len(py_coords):
            if abs(py_coords[i] - py_coords[j]) < 35:
                py_coords[j] = py_coords[i]
                grid[j].pc_y = py_coords[j]
                j += 1
                continue
            i = j
            j += 1

        cell_type_dict = {}
        px_coords = sorted(list(set(px_coords)))
        py_coords = sorted(list(set(py_coords)))
        keysat = set()
        for i in range(len(grid)):
            grid[i].c_x = px_coords.index(grid[i].pc_x)
            grid[i].c_y = py_coords.index(grid[i].pc_y)
            cell_location = 'loc-' + str(grid[i].c_x) + '-' + str(grid[i].c_y)
            if self.ref_colors is not None:
                grid[i].assign_cell_type(self.ref_colors)
                cell_type_dict[str(grid[i].c_x) + ',' +
                               str(grid[i].c_y)] = grid[i].cell_type

                if grid[i].cell_type == "player":
                    detected_state.state['at'].append(tuple([cell_location]))

                if grid[i].cell_type == "player_with_key":
                    detected_state.state['at'].append(tuple([cell_location]))
                    keysat.add(cell_location)
                if grid[i].cell_type in ["locked_with_key", "key_only"]:
                    keysat.add(cell_location)

        # remove keys at for keys that have been picked
        detected_state.state['keyat'] = set(detected_state.state['keyat'])
        if sorted(keysat) != sorted(list((key_locations.keys()))):
            # if key is not present in location, that room is unlocked and keyat is removed
            for loc in set(key_locations.keys()).difference(set(keysat)):
                detected_state.state['unlocked'].append(
                    tuple([key_for_room[key_locations[loc]]]))
                detected_state.state['keyat'].remove(
                    tuple([key_locations[loc], loc]))
        if 'keyat' in detected_state.state.keys() and len(
                detected_state.state['keyat']) > 0:
            detected_state.state['keyat'] = list(detected_state.state['keyat'])
        elif 'keyat' in detected_state.state.keys() and len(
                detected_state.state['keyat']) == 0:
            detected_state.state.pop('keyat')
        print("Number of cells found:" + str(len(grid)))
        return detected_state
    def get_state_from_render(self, img, init_state):
        """
        get all initial info from init_state
        detect and filter contours from image
        iterate over each contour and create Cells
        for each cell, detect its type and store the results
        add the detected cell literals to state
        """

        cell_width = 35
        temp_state = {}
        for pred in init_state.state:
            if "at" not in pred and "clear" not in pred:
                temp_state[pred] = init_state.state[pred]

        temp_state['at'] = []
        temp_state['clear'] = []

        detected_state = State(temp_state, init_state.objects)
        filtered_contours = get_contours(img, range_min=1100, range_max=1500)
        if VERBOSE:
            print("Number of contours:" + str(len(filtered_contours)))
        px_coords = []
        py_coords = []
        img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
        grid = []
        for c in filtered_contours:
            _m = cv2.moments(c)
            pc_x = int(_m["m10"] / _m["m00"])
            pc_y = int(_m["m01"] / _m["m00"])
            px_coords.append(pc_x)
            py_coords.append(pc_y)
            mean_color = calculate_color(img_lab, c)
            cell_temp = Cell(pc_x, pc_y, c, mean_color)
            grid.append(cell_temp)

        px_coords = list(sorted(px_coords))
        grid = sorted(grid, key=lambda x: x.pc_x)
        i = 0
        j = 1
        while i < len(px_coords) and j < len(px_coords):
            if abs(px_coords[i] - px_coords[j]) < cell_width:
                px_coords[j] = px_coords[i]
                grid[j].pc_x = px_coords[j]
                j += 1
                continue
            i = j
            j += 1

        py_coords = list(sorted(py_coords))
        grid = sorted(grid, key=lambda x: x.pc_y)
        i = 0
        j = 1
        while i < len(py_coords) and j < len(py_coords):
            if abs(py_coords[i] - py_coords[j]) < 35:
                py_coords[j] = py_coords[i]
                grid[j].pc_y = py_coords[j]
                j += 1
                continue
            i = j
            j += 1

        px_coords = sorted(list(set(px_coords)))
        py_coords = sorted(list(set(py_coords)))
        for i in range(len(grid)):
            grid[i].c_x = px_coords.index(grid[i].pc_x) + 1
            grid[i].c_y = py_coords.index(grid[i].pc_y) + 1
            cell_location = 'pos-' + "{0:0=2d}".format(
                grid[i].c_x) + '-' + "{0:0=2d}".format(grid[i].c_y)
            if VERBOSE:
                print(str(grid[i].c_x) + "," + str(grid[i].c_y))
            if self.ref_colors is not None:
                grid[i].assign_cell_type(self.ref_colors)
                if VERBOSE:
                    print("Assigned " + str(grid[i].c_x) + ',' +
                          str(grid[i].c_y) + ' as ' + grid[i].cell_type)

                if grid[i].cell_type == "player":
                    detected_state.state['at'].append(
                        tuple(["player-01", cell_location]))

                if grid[i].cell_type == "clear" or grid[i].cell_type == "goal":
                    detected_state.state['clear'].append(tuple([cell_location
                                                                ]))

                if grid[i].cell_type == "wall":
                    continue

                if "stone" in grid[i].cell_type:
                    detected_state.state['at'].append(
                        tuple(["stone-01", cell_location]))

                if "goal-at-stone" in grid[i].cell_type:
                    if "at-goal" not in detected_state.state.keys():
                        detected_state.state['at-goal'] = [
                            tuple([grid[i].cell_type])
                        ]
                    else:
                        detected_state.state['at-goal'].append(
                            tuple([grid[i].cell_type]))

                if grid[i].cell_type == "goal":
                    continue
        if VERBOSE:
            print("Number of cells found:" + str(len(grid)))
        return detected_state
示例#6
0
    def generate_random_init_states(self, n, render):
        # make an lxb environment and add blocks surrounding it
        sample_init_state = {
            'at': set(),
            'clear': set(),
            'is-stone': set(),
            'is-goal': set(),
            'is-nongoal': set(),
            'is-player': [('player-01', )],
            'move': [('dir-down', ), ('dir-left', ), ('dir-right', ),
                     ('dir-up', )],
            'move-dir': set(),
            'at-goal': set()
        }

        self.random_states = []
        s = 0
        while s <= n:
            l = 4
            b = 4
            num_stones = 1
            num_goals = num_stones + 0
            num_blocked_cells = random.choice(range(int(3 * l * b / 10)))
            all_cells = list(itertools.product(range(1, l), range(1, b)))
            blocked_cells = random.sample(all_cells, num_blocked_cells)
            unblocked_cells = list(
                set(all_cells).difference(set(blocked_cells)))
            stone_positions = random.sample(unblocked_cells, num_stones)
            goal_positions = random.sample(unblocked_cells, num_goals)
            player_location = [
                random.choice(
                    list(
                        set(unblocked_cells).difference(set(stone_positions))))
            ]
            init_state = copy.deepcopy(sample_init_state)
            init_state['at'].add(
                ('player-01',
                 'pos-' + "{0:0=2d}".format(player_location[0][0]) + '-' +
                 "{0:0=2d}".format(player_location[0][1])))

            for i, pos in enumerate(stone_positions):
                stone = 'stone-' + "{0:0=2d}".format(i + 1)
                loc = 'pos-' + "{0:0=2d}".format(
                    pos[0]) + '-' + "{0:0=2d}".format(pos[1])
                init_state['is-stone'].add(tuple([stone]))
                init_state['at'].add(tuple([stone, loc]))
                if pos in goal_positions:
                    init_state['at-goal'].add(tuple([stone]))

            for pos in all_cells:
                location = 'pos-' + "{0:0=2d}".format(
                    pos[0]) + '-' + "{0:0=2d}".format(pos[1])
                location_left = 'pos-' + "{0:0=2d}".format(
                    pos[0] - 1) + '-' + "{0:0=2d}".format(pos[1])
                location_right = 'pos-' + "{0:0=2d}".format(
                    pos[0] + 1) + '-' + "{0:0=2d}".format(pos[1])
                location_up = 'pos-' + "{0:0=2d}".format(
                    pos[0]) + '-' + "{0:0=2d}".format(pos[1] - 1)
                location_down = 'pos-' + "{0:0=2d}".format(
                    pos[0]) + '-' + "{0:0=2d}".format(pos[1] + 1)

                left_cell = (pos[0] - 1, pos[1])
                right_cell = (pos[0] + 1, pos[1])
                up_cell = (pos[0], pos[1] - 1)
                down_cell = (pos[0], pos[1] + 1)

                if pos in goal_positions:
                    init_state['is-goal'].add(tuple([location]))
                else:
                    init_state['is-nongoal'].add(tuple([location]))
                if pos in unblocked_cells:
                    if pos not in player_location and pos not in stone_positions:
                        init_state['clear'].add(tuple([location]))
                    if left_cell in unblocked_cells or left_cell in stone_positions or left_cell in player_location:
                        init_state['move-dir'].add(
                            tuple([location, location_left, 'dir-left']))
                    if right_cell in unblocked_cells or right_cell in stone_positions or right_cell in player_location:
                        init_state['move-dir'].add(
                            tuple([location, location_right, 'dir-right']))
                    if up_cell in unblocked_cells or up_cell in stone_positions or up_cell in player_location:
                        init_state['move-dir'].add(
                            tuple([location, location_up, 'dir-up']))
                    if down_cell in unblocked_cells or down_cell in stone_positions or down_cell in player_location:
                        init_state['move-dir'].add(
                            tuple([location, location_down, 'dir-down']))

            for k in init_state.keys():
                init_state[k] = list(init_state[k])

            test_state = State(init_state, {})
            success = self.set_initial_state(test_state, render=render)
            if success:
                state_objects = dict()
                state_objects['direction'] = [
                    'dir-down', 'dir-left', 'dir-right', 'dir-up'
                ]
                state_objects['thing'] = ['player-01']
                state_objects['location'] = []
                for k in range(num_stones):
                    state_objects['thing'].append('stone-' +
                                                  "{0:0=2d}".format(k + 1))
                for pos in all_cells:
                    location = 'pos-' + "{0:0=2d}".format(
                        pos[0]) + '-' + "{0:0=2d}".format(pos[1])
                    state_objects['location'].append(location)

                if len(init_state['at-goal']) == 0:
                    del init_state['at-goal']
                t = State(init_state, state_objects)
                if t not in self.random_states:
                    self.random_states.append(t)
                    s += 1
            else:
                print("incorrect random state!")
示例#7
0
    def generate_random_init_states(self, n, render):
        sample_init_state = {
            'at': [],
            'unlocked': [],
            'locinroom': [],
            'keyforroom': [],
            'keyat': [],
            'moveto': [],
            'pick': [],
        }
        i = 0
        max_keys = 3
        num_keys = random.choice(range(1, max_keys))
        num_rooms = num_keys + 2
        while i <= n:
            # locations = []
            length = 4
            width = 4
            keys = []
            rooms = []
            locations = []
            for l in range(length):
                for w in range(width):
                    locations.append('loc-' + str(l) + '-' + str(w))
            for k in range(num_keys):
                keys.append('key-' + str(k))
            for r in range(num_rooms):
                rooms.append('room-' + str(r))
            init_state = copy.deepcopy(sample_init_state)
            # place keys randomly
            init_state['unlocked'].append(tuple([rooms[0]]))
            key_locations = random.sample(locations, len(keys))

            for key, key_loc in zip(keys, key_locations):
                init_state['keyat'].append(tuple([key, key_loc]))
            # place rooms randomly
            room_locations = random.sample(locations, len(rooms))
            j = 0
            for loc in locations:
                if loc not in room_locations:
                    init_state['locinroom'].append(tuple([loc, 'room-0']))
                else:
                    init_state['locinroom'].append(tuple([loc, rooms[j]]))
                    j += 1

            # put player in random non-key place
            at_loc = random.choice(
                list(set(locations).difference(room_locations)))
            init_state['at'].append(tuple([at_loc]))
            # assign keys to rooms
            random.shuffle(keys)
            temp_rooms = rooms[1:]
            random.shuffle(temp_rooms)
            random.shuffle(locations)

            for key, room in list(zip(keys, temp_rooms)):
                init_state['keyforroom'].append(tuple([key, room]))

            state_objects = {'location': [], 'key': [], 'room': []}

            for loc in locations:
                state_objects['location'].append(loc)
                init_state['moveto'].append(tuple([loc]))
            for key in keys:
                state_objects['key'].append(key)
                init_state['pick'].append(tuple([key]))

            for room in rooms:
                state_objects['room'].append(room)
            test_state = State(init_state, state_objects)
            assert len(test_state.state['at']) == 1, "Multiple AT!!"
            success = self.set_initial_state(test_state, render=render)
            if success:
                print("Found")
                i += 1
                self.random_states.append(test_state)
            else:
                print("Failed")