def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # TODO implement
        new_state = decode_state(state, self.state_map)
        new_state.pos = [
            f for f in new_state.pos if f not in action.effect_rem
        ]
        # Use effect_add
        new_state.pos += action.effect_add

        return encode_state(new_state, self.state_map)

        new_state = FluentState([], [])
        current_state = decode_state(state, self.state_map)

        for state in current_state.pos:
            if state not in action.effect_rem:
                new_state.pos.append(state)

        for state in action.effect_add:
            if state not in new_state.pos:
                new_state.pos.append(state)

        for state in current_state.neg:
            if state not in action.effect_add:
                new_state.neg.append(state)

        for state in action.effect_rem:
            if state not in new_state.neg:
                new_state.neg.append(state)

        return encode_state(new_state, self.state_map)
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['ATL', 'JFK', 'SFO']
    pos = [
        expr('At(C1, SFO)'),
        expr('At(C2, JFK)'),
        expr('At(C3, ATL)'),
        expr('At(P1, SFO)'),
        expr('At(P2, JFK)'),
        expr('At(P3, ATL)'),
    ]

    neg = get_negatives(cargos, planes, airports, pos)

    init = FluentState(pos, neg)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C3, SFO)'),
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #3
0
    def __init__(self, cargos, planes, airports, initial: FluentState, goal: list):
        """

        :param cargos: list of str
            cargos in the problem
        :param planes: list of str
            planes in the problem
        :param airports: list of str
            airports in the problem
        :param initial: FluentState object
            positive and negative literal fluents (as expr) describing initial state
        :param goal: list of expr
            literal fluents required for goal test
        """
        self.state_map = initial.pos + initial.neg
        self.initial_state_TF = encode_state(initial, self.state_map)
        self.goal_state_TF = encode_state(FluentState(goal, []), self.state_map)
        Problem.__init__(self, self.initial_state_TF, goal=goal)
        self.cargos = cargos
        self.planes = planes
        self.airports = airports
        self.actions_list = self.get_actions()
예제 #4
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # TODO implement
        new_state = FluentState([], [])
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())

        action(kb, action.args)

        for clause in kb.clauses:
            new_state.pos.append(clause)
        for clause in action.effect_rem:
            new_state.neg.append(clause)

        return encode_state(new_state, self.state_map)
예제 #5
0
def air_cargo_p2() -> AirCargoProblem:
    # TODO implement Problem 2 definition
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['SFO', 'JFK', 'ATL']
    pos = [
        expr('At(P1, SFO)'),
        expr('At(P2, JFK)'),
        expr('At(P3, ATL)'),
        expr('At(C1, SFO)'),
        expr('At(C2, JFK)'),
        expr('At(C3, ATL)')
    ]
    neg = [
        expr('At(P1, JFK)'),
        expr('At(P1, ATL)'),
        expr('At(P2, SFO)'),
        expr('At(P2, ATL)'),
        expr('At(P3, SFO)'),
        expr('At(P3, JFK)'),
        expr('At(C1, JFK)'),
        expr('At(C1, ATL)'),
        expr('At(C2, SFO)'),
        expr('At(C2, ATL)'),
        expr('At(C3, SFO)'),
        expr('At(C3, JFK)'),
        expr('In(C1, P1)'),
        expr('In(C1, P2)'),
        expr('In(C1, P3)'),
        expr('In(C2, P1)'),
        expr('In(C2, P2)'),
        expr('In(C2, P3)'),
        expr('In(C3, P1)'),
        expr('In(C3, P2)'),
        expr('In(C3, P3)')
    ]
    init = FluentState(pos, neg)
    goal = [expr('At(C1, JFK)'), expr('At(C2, SFO)'), expr('At(C3, SFO)')]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #6
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        current_state = decode_state(state, self.state_map)

        new_pos = set(current_state.pos)
        new_neg = set(current_state.neg)

        for add in action.effect_add:
            new_pos.add(add)
            new_neg.discard(add)
        for rem in action.effect_rem:
            new_pos.discard(rem)
            new_neg.add(rem)

        return encode_state(FluentState(list(new_pos), list(new_neg)), self.state_map)
def air_cargo_p3() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3', 'C4']
    planes = ['P1', 'P2']
    airports = ['JFK', 'SFO', 'ATL', 'ORD']
    pos = [
        expr('At(C1, SFO)'),
        expr('At(C2, JFK)'),
        expr('At(C3, ATL)'),
        expr('At(C4, ORD)'),
        expr('At(P1, SFO)'),
        expr('At(P2, JFK)')
    ]
    # -----------
    neg = []
    for c in cargos:
        for p in planes:
            # all cargos not in the planes
            neg.append(expr('In({}, {})'.format(c, p)))

        for a in airports:
            # adding to 'neg' those combinations that not in 'pos'
            if not expr('At({}, {})'.format(c, a)) in pos:
                neg.append(expr('At({}, {})'.format(c, a)))

    for p in planes:
        for a in airports:
            # adding to 'neg' those combinations that not in 'pos'
            if not expr('At({}, {})'.format(p, a)) in pos:
                neg.append(expr('At({}, {})'.format(p, a)))
    # -------------

    init = FluentState(pos, neg)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C3, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C4, SFO)')
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #8
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # TODO implement
        new_state = FluentState([], [])

        old_state = decode_state(state, self.state_map)

        new_state.pos = list(
            set(old_state.pos) - set(action.effect_rem)
            | set(action.effect_add))
        new_state.neg = list(
            set(old_state.neg) - set(action.effect_add)
            | set(action.effect_rem))

        return encode_state(new_state, self.state_map)
예제 #9
0
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['SFO', 'JFK', 'ATL']

    pos = []
    neg = []
    for x in range(0, 3):
        pos.append(expr('At(' + cargos[x] + ',' + airports[x] + ')'))
        pos.append(expr('At(' + planes[x] + ',' + airports[x] + ')'))
        for y in range(0, 3):
            neg.append(expr('In(' + cargos[x] + ',' + planes[y] + ')'))
            if x != y:
                neg.append(expr('At(' + cargos[x] + ',' + airports[y] + ')'))
                neg.append(expr('At(' + planes[x] + ',' + airports[y] + ')'))
    init = FluentState(pos, neg)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C3, SFO)'),
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #10
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).
        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """

        # Lifted from example_have_cake.py
        new_state = FluentState([], [])

        # Get the current state
        old_state = decode_state(state, self.state_map)

        # Per the teaching materials, the precondition and effect of an action are each conjunctions of literals
        # (positive or negated atomic sentences). The precondition defines the states in which the action can be
        # executed, and the effect defines the result of executing the action. So, for each fluent (positive or
        # negative) in the old state, we need to look in the action's effects (both the add and remove effects) to
        # determine how the fluent will be carried over to the new state, as a positive or a negative fluent.
        for fluent in old_state.pos:
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)
        for fluent in old_state.neg:
            if fluent not in action.effect_add:
                new_state.neg.append(fluent)

        # The action may have positive and negative effects INDEPENDENTLY of the old state. If they're add effects,
        # we need to make sure these fluents are added to the new state's positive sentences. Similarly, if they're
        # remove effects, these fluents need to be added to the new state's negative sentences.
        for fluent in action.effect_add:
            if fluent not in new_state.pos:
                new_state.pos.append(fluent)
        for fluent in action.effect_rem:
            if fluent not in new_state.neg:
                new_state.neg.append(fluent)

        # Return the new state
        return encode_state(new_state, self.state_map)
예제 #11
0
def air_cargo_p1() -> AirCargoProblem:
    '''
    Problem 1 initial state and goal:

    Init(At(C1, SFO) ∧ At(C2, JFK)
        ∧ At(P1, SFO) ∧ At(P2, JFK)
        ∧ Cargo(C1) ∧ Cargo(C2)
        ∧ Plane(P1) ∧ Plane(P2)
        ∧ Airport(JFK) ∧ Airport(SFO))
    Goal(At(C1, JFK) ∧ At(C2, SFO))
    '''
    cargos = ['C1', 'C2']
    planes = ['P1', 'P2']
    airports = ['JFK', 'SFO']

    pos = [
        expr('At(C1, SFO)'),
        expr('At(C2, JFK)'),
        expr('At(P1, SFO)'),
        expr('At(P2, JFK)')
    ]

    neg = [
        expr('At(C2, SFO)'),
        expr('In(C2, P1)'),
        expr('In(C2, P2)'),
        expr('At(C1, JFK)'),
        expr('In(C1, P1)'),
        expr('In(C1, P2)'),
        expr('At(P1, JFK)'),
        expr('At(P2, SFO)')
    ]

    # set initial state as fluent of positive and negative assertions
    init = FluentState(pos_list=pos, neg_list=neg)

    goal = [expr('At(C1, JFK)'), expr('At(C2, SFO)')]

    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #12
0
    def result(self, state: str, action: Action):  # here
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """

        old_state = decode_state(state, self.state_map)
        pos_list = old_state.pos
        neg_list = old_state.neg
        for rem in action.effect_rem:
            pos_list.remove(rem)
            neg_list.append(rem)
        for add in action.effect_add:
            pos_list.append(add)
            neg_list.remove(add)

        new_state = FluentState(pos_list, neg_list)
        return encode_state(new_state, self.state_map)
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', 'ATL']

    pos = [
        expr("At(C1, SFO)"),
        expr("At(C2, JFK)"),
        expr("At(C3, ATL)"),
        expr("At(P1, SFO)"),
        expr("At(P2, JFK)"),
        expr("At(P3, ATL)")
    ]
    neg = [
        expr("At(C1, JFK)"),
        expr("At(C1, ATL)"),
        expr("In(C1, P1)"),
        expr("In(C1, P2)"),
        expr("In(C1, P3)"),
        expr("At(C2, SFO)"),
        expr("At(C2, ATL)"),
        expr("In(C2, P1)"),
        expr("In(C2, P2)"),
        expr("In(C2, P3)"),
        expr("At(C3, JFK)"),
        expr("At(C3, SFO)"),
        expr("In(C3, P1)"),
        expr("In(C3, P2)"),
        expr("In(C3, P3)"),
        expr("At(P1, JFK)"),
        expr("At(P1, ATL)"),
        expr("At(P2, SFO)"),
        expr("At(P2, ATL)"),
        expr("At(P3, JFK)"),
        expr("At(P3, SFO)")
    ]
    init = FluentState(pos, neg)
    goal = [expr('At(C1, JFK)'), expr('At(C2, SFO)'), expr('At(C3, SFO)')]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #14
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        new_state = FluentState([], [])
        old_state = decode_state(state, self.state_map)
        new_state.pos.extend(
            [f for f in old_state.pos if f not in action.effect_rem])
        new_state.pos.extend(
            [f for f in action.effect_add if f not in new_state.pos])

        new_state.neg.extend(
            [f for f in old_state.neg if f not in action.effect_add])
        new_state.neg.extend(
            [f for f in action.effect_rem if f not in new_state.neg])

        return encode_state(new_state, self.state_map)
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', 'OTP']
    pos = [expr('At(C1, SFO)'),
           expr('At(C2, JFK)'),
           expr('At(P1, SFO)'),
           expr('At(P2, JFK)'),
           expr('At(P3, OTP)'),
           expr('At(C3, OTP)'),
           ]
    neg = [expr('At(C2, SFO)'),
           expr('At(C2, OTP)'),
           expr('In(C2, P1)'),
           expr('In(C2, P2)'),
           expr('In(C2, P3)'),
           expr('At(C3, SFO)'),
           expr('At(C3, JFK)'),
           expr('In(C3, P1)'),
           expr('In(C3, P2)'),
           expr('In(C3, P3)'),
           expr('At(C1, JFK)'),
           expr('At(C1, OTP)'),
           expr('In(C1, P1)'),
           expr('In(C1, P2)'),
           expr('In(C1, P3)'),
           expr('At(P1, JFK)'),
           expr('At(P1, OTP)'),
           expr('At(P2, SFO)'),
           expr('At(P2, OTP)'),
           expr('At(P3, JFK)'),
           expr('At(P3, SFO)'),
           ]
    init = FluentState(pos, neg)
    goal = [expr('At(C1, JFK)'),
            expr('At(C2, OTP)'),
            expr('At(C3, SFO)'),
            ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
    def result(self, state: str, action: Action):
        """ Returns the state that results from executing the given
        action in the given state. The action is one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """

        new_state = FluentState([], [])
        old_state = decode_state(state, self.state_map)

        # If a positive fluent has not been removed by the effect of current action then
        # append that to positive fluents in the new_state.
        for fluent in old_state.pos:
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)

        # If a new positive fluent is added by the effect of current action then
        # append that to positive fluents in the new_state.
        for fluent in action.effect_add:
            if fluent not in new_state.pos:
                new_state.pos.append(fluent)

        # If a negative fluent has not been removed by the effect of current action then
        # append that to negative fluents in the new_state.
        for fluent in old_state.neg:
            if fluent not in action.effect_add:
                new_state.neg.append

        # If a negative fluent has been removed by the effect of current action then
        # append that to negative fluents in the new_state.
        for fluents in action.effect_rem:
            if fluent not in new_state.neg:
                new_state.neg.append(fluent)

        return encode_state(new_state, self.state_map)
예제 #17
0
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', 'ATL']
    pos = [
        at_e('C1', 'SFO'),
        at_e('C2', 'JFK'),
        at_e('C3', 'ATL'),
        at_e('P1', 'SFO'),
        at_e('P2', 'JFK'),
        at_e('P3', 'ATL')
    ]
    neg = [
        at_e('C2', 'SFO'),
        at_e('C2', 'ATL'),
        in_e('C2', 'P1'),
        in_e('C2', 'P2'),
        in_e('C2', 'P3'),
        at_e('C1', 'JFK'),
        at_e('C1', 'ATL'),
        in_e('C1', 'P1'),
        in_e('C1', 'P2'),
        in_e('C1', 'P3'),
        at_e('C3', 'JFK'),
        at_e('C3', 'SFO'),
        in_e('C3', 'P1'),
        in_e('C3', 'P2'),
        in_e('C3', 'P3'),
        at_e('P1', 'JFK'),
        at_e('P1', 'ATL'),
        at_e('P2', 'SFO'),
        at_e('P2', 'ATL'),
        at_e('P3', 'JFK'),
        at_e('P3', 'SFO')
    ]
    init = FluentState(pos, neg)
    goal = [at_e('C1', 'JFK'), at_e('C2', 'SFO'), at_e('C3', 'SFO')]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #18
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        This routine simply takes the current state, pos and neg, and copies to a new version depending
        on the action's effects which can be add or rem.

        So if the action has a add effect the new state will contain that effect in its pos section
        If the action has a rem effect then the new state will contain that effect in its neg section

        More formally put (p368) - The result of executing action a in state s is defined as a state s' which is represented
        by the set of fluents formed by starting with s, removing the fluents that appear as negative literals in the
        action's effects (what we wil call the delete list or DEL(a), and addign the fluents that are positie literals
        in the action's effects (what we will call the add list or ADD(a).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # TODO implement
        #print('Running result() function...')
        new_state = FluentState([], [])
        old_state = decode_state(state, self.state_map)
        for fluent in old_state.pos:
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)
        for fluent in action.effect_add:
            if fluent not in new_state.pos:
                new_state.pos.append(fluent)
        for fluent in old_state.neg:
            if fluent not in action.effect_add:
                new_state.neg.append(fluent)
        for fluent in action.effect_rem:
            if fluent not in new_state.neg:
                new_state.neg.append(fluent)

        return encode_state(new_state, self.state_map)
def air_cargo_p2() -> AirCargoProblem:
    cargos = ["C1", "C2", "C3"]
    planes = ["P1", "P2", "P3"]

    airports = ["JFK", "SFO", "ATL"]

    pos = [
        expr("At(C1, SFO)"),
        expr("At(C2, JFK)"),
        expr("At(C3, ATL)"),
        expr("At(P1, SFO)"),
        expr("At(P2, JFK)"),
        expr("At(P3, ATL)")
    ]

    neg = []
    # for c in cargos:
    #     for p in planes:
    #         neg.append(expr("In({}, {})".format(c, p)))
    neg = [expr("In({}, {})".format(c, p)) for p in planes for c in cargos]

    for c in cargos:
        for a in airports:
            if not ((c == "C1" and a == "SFO") or (c == "C2" and a == "JFK") or
                    (c == "C3" and a == "ATL")):
                neg.append(expr("At({}, {})".format(c, a)))

    for p in planes:
        for a in airports:
            if not ((p == "P1" and a == "SFO") or (p == "P2" and a == "JFK") or
                    (p == "P3" and a == "ATL")):
                neg.append(expr("At({}, {})".format(p, a)))

    init = FluentState(pos, neg)

    goal = [expr("At(C1, JFK)"), expr("At(C2, SFO)"), expr("At(C3, SFO)")]

    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #20
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # TODO implement
        new_state = FluentState([], [])
        #This code is used from Cake example file
        #The idea of this function is to define next state in our problem
        #which is going to "happen" after applying current action
        old_state = decode_state(state, self.state_map)

        #Going through positive fluents in previous (old) state
        for fluent in old_state.pos:
            #if that fluent is not in removal_effect from action -> add it to positve fluents for new state
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)

        #going through fluents that are added by the action
        for fluent in action.effect_add:
            #if that fluent is not already in positive fluents of new state -> add it
            if fluent not in new_state.pos:
                new_state.pos.append(fluent)
        #going through negative fluents from old state
        for fluent in old_state.neg:
            #if that negative fluent is not in adding fluents of the action -> add it to negative fluents for new state
            if fluent not in action.effect_add:
                new_state.neg.append(fluent)
        #going through fluents that are being removed by the action
        for fluent in action.effect_rem:
            #if that fluent is not already in negatives for the new state -> add it
            if fluent not in new_state.neg:
                new_state.neg.append(fluent)
        return encode_state(new_state, self.state_map)
예제 #21
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # TODO implement
        new_state = FluentState([], [])

        old_state = decode_state(state, self.state_map)

        for pos_fluent in old_state.pos:
            if pos_fluent not in action.effect_rem:  # positive effect
                new_state.pos.append(pos_fluent)

        for pos_fluent in action.effect_add:  # negative effect
            if pos_fluent not in new_state.pos:
                new_state.pos.append(pos_fluent)

        for neg_fluent in old_state.neg:
            if neg_fluent not in action.effect_add:  # negative effect
                new_state.neg.append(neg_fluent)

        for neg_fluent in action.effect_rem:  # positive effect
            if neg_fluent not in new_state.neg:
                new_state.neg.append(neg_fluent)
        '''
        old_state = decode_state(state, self.state_map)
        pos, neg = set(old_state.pos), set(old_state.neg)
        pos = pos - set(action.effect_rem) | set(action.effect_add)
        neg = neg - set(action.effect_add) | set(action.effect_rem)
        new_state = FluentState(list(pos), list(neg))
        '''

        return encode_state(new_state, self.state_map)
def air_cargo_p2() -> AirCargoProblem:
    cargos = ["C1", "C2", "C3"]
    planes = ["P1", "P2", "P3"]
    airports = ["JFK", "ATL", "SFO"]
    pos = [
        expr("At(C1, SFO)"),
        expr("At(C2, JFK)"),
        expr("At(C3, ATL)"),
        expr("At(P1, SFO)"),
        expr("At(P2, JFK)"),
        expr("At(P3, ATL)"),
    ]
    neg = [
        expr("At(C1, JFK)"),
        expr("At(C1, ATL)"),
        expr("At(C2, SFO)"),
        expr("At(C2, ATL)"),
        expr("At(C3, JFK)"),
        expr("At(C3, SFO)"),
        expr("At(P1, JFK)"),
        expr("At(P1, ATL)"),
        expr("At(P2, SFO)"),
        expr("At(P2, ATL)"),
        expr("At(P3, JFK)"),
        expr("At(P3, SFO)"),
        expr("In(C1, P1)"),
        expr("In(C1, P2)"),
        expr("In(C1, P3)"),
        expr("In(C2, P1)"),
        expr("In(C2, P2)"),
        expr("In(C2, P3)"),
        expr("In(C3, P1)"),
        expr("In(C3, P2)"),
        expr("In(C3, P3)"),
    ]
    init = FluentState(pos_list=pos, neg_list=neg)
    goal = [expr("At(C1, JFK)"), expr("At(C2, SFO)"), expr("At(C3, SFO)")]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #23
0
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
    
        new_state = FluentState([], [])
        old_state = decode_state(state, self.state_map)
        


        for fluent in old_state.pos:
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)


        for fluent in action.effect_add:
            if fluent not in new_state.pos:
                new_state.pos.append(fluent)


        for fluent in old_state.neg:
            if fluent not in action.effect_add:
                new_state.neg.append(fluent)


        for fluent in action.effect_rem:
            if fluent not in new_state.neg:
                new_state.neg.append(fluent)




        return encode_state(new_state, self.state_map)
예제 #24
0
def air_cargo_p1() -> AirCargoProblem:
    '''
    Init(At(C1, SFO) ∧ At(C2, JFK)
    	∧ At(P1, SFO) ∧ At(P2, JFK)
    	∧ Cargo(C1) ∧ Cargo(C2)
    	∧ Plane(P1) ∧ Plane(P2)
    	∧ Airport(JFK) ∧ Airport(SFO))
    Goal(At(C1, JFK) ∧ At(C2, SFO))
    '''
    cargos = ['C1', 'C2']
    planes = ['P1', 'P2']
    airports = ['JFK', 'SFO']

    # Track where the planes and cargo are at
    pos = [
        expr('At(C1, SFO)'),
        expr('At(C2, JFK)'),
        expr('At(P1, SFO)'),
        expr('At(P2, JFK)'),
    ]

    # Track where the planes and cargo are not at
    neg = [
        expr('At(C2, SFO)'),
        expr('In(C2, P1)'),
        expr('In(C2, P2)'),
        expr('At(C1, JFK)'),
        expr('In(C1, P1)'),
        expr('In(C1, P2)'),
        expr('At(P1, JFK)'),
        expr('At(P2, SFO)'),
    ]
    init = FluentState(pos, neg)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # TODO implement
        new_state = FluentState([], [])
        old_state = decode_state(state, self.state_map)

        for fluent in old_state.pos:
            if fluent not in action.effect_rem:
                new_state.pos.append(
                    fluent
                )  # add positive fluents which are in the old state and should not be removed

        for fluent in action.effect_add:
            if fluent not in new_state.pos:
                new_state.pos.append(
                    fluent
                )  # add positive fluents which should be added and have not already been added

        for fluent in old_state.neg:
            if fluent not in action.effect_add:
                new_state.neg.append(
                    fluent
                )  # add negative fluents which are in the old state and should not be added

        for fluent in action.effect_rem:
            if fluent not in new_state.neg:
                new_state.neg.append(
                    fluent
                )  # add negative fluents which should be removed but have not already been removed from the negative state

        return encode_state(new_state, self.state_map)
예제 #26
0
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', 'ATL']
    pos = [
        expr('At(C1, SFO)'),
        expr('At(C2, JFK)'),
        expr('At(C3, ATL)'),
        expr('At(P1, SFO)'),
        expr('At(P2, JFK)'),
        expr('At(P3, ATL)'),
    ]
    neg = [
        expr('At(C3, JFK)'),  # A LITTLE CONFUSED ON THIS
        expr('At(C3, SFO)'),  # GOT NEGATION OF WHERE CARGOS AT
        expr('In(C3, P1)'),
        expr('In(C3, P2)'),
        expr('In(C3, P3)'),
        expr('At(C2, SFO)'),
        expr('At(C2, ATL)'),
        expr('In(C2, P1)'),
        expr('In(C2, P2)'),
        expr('In(C2, P3)'),
        expr('At(C1, JFK)'),
        expr('At(C1, ATL)'),
        expr('In(C1, P1)'),
        expr('In(C1, P2)'),
        expr('In(C1, P3)'),
        expr('At(P1, JFK)'),
        expr('At(P1, ATL)'),
        expr('At(P2, SFO)'),
        expr('At(P2, ATL)'),
        expr('At(P3, JFK)'),
        expr('At(P3, SFO)')
    ]
    init = FluentState(pos, neg)
    goal = [expr('At(C1, JFK)'), expr('At(C2, SFO)'), expr('At(C3, SFO)')]
    return AirCargoProblem(cargos, planes, airports, init, goal)
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # TODO implement

        # Get the current state
        curr_state = decode_state(state, self.state_map)
        new_state = FluentState([], [])

        # For each fluent in current state, we need to check the action's effects (added or removed effects),
        # in order to how the positive or negative fluent can be carried over to the new state.

        for f in curr_state.neg:
            if f not in action.effect_add:
                new_state.neg.append(f)

        for f in curr_state.pos:
            if f not in action.effect_rem:
                new_state.pos.append(f)

        #  The action may have positive and negative effects separated, e.g. we need to check if the fluents are
        #  added to the new state's positive clauses if the actions have add effects. It is also the same for negative
        #  clauses
        for f in action.effect_rem:
            if f not in new_state.neg:
                new_state.neg.append(f)

        for f in action.effect_add:
            if f not in new_state.pos:
                new_state.pos.append(f)

        return encode_state(new_state, self.state_map)
    def result(self, state: str, action: Action):
        """ Return the state that results from executing the given
        action in the given state. The action must be one of
        self.actions(state).

        :param state: state entering node
        :param action: Action applied
        :return: resulting state after action
        """
        # This from example_have_cake.py should work
        new_state = FluentState([], [])
        old_state = decode_state(state, self.state_map)
        """
        for fluent in old_state.pos:
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)
        for fluent in action.effect_add:
            if fluent not in new_state.pos:
                new_state.pos.append(fluent)
        for fluent in old_state.neg:
            if fluent not in action.effect_add:
                new_state.neg.append(fluent)
        for fluent in action.effect_rem:
            if fluent not in new_state.neg:
                new_state.neg.append(fluent)
        """
        # This may be a better way, how much depends on the size
        pos_fluents = set(old_state.pos)
        pos_fluents.difference_update(action.effect_rem)
        pos_fluents.update(action.effect_add)
        new_state.pos.extend(pos_fluents)
        neg_fluents = set(old_state.neg)
        neg_fluents.difference_update(action.effect_add)
        neg_fluents.update(action.effect_rem)
        new_state.neg.extend(neg_fluents)
        #"""

        return encode_state(new_state, self.state_map)
예제 #29
0
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', 'ATL']

    at_list = [ \
                ('C1','SFO'), \
                ('C2','JFK'), \
                ('C3','ATL'), \
                ('P1','SFO'), \
                ('P2','JFK'), \
                ('P3','ATL'), \
                ]

    pos = [expr('At({}, {})'.format(a, b)) for a, b in at_list]
    neg = []
    for p in planes:
        for ap in airports:
            if (p, ap) in at_list:
                continue
            neg.append(expr('At({}, {})'.format(p, ap)))
    for c in cargos:
        for ap in airports:
            if (c, ap) in at_list:
                continue
            neg.append(expr('At({}, {})'.format(c, ap)))
    for c in cargos:
        for p in planes:
            if (c, p) in at_list:
                continue
            neg.append(expr('In({}, {})'.format(c, p)))
    init = FluentState(pos, neg)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C3, SFO)'),
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #30
0
def air_cargo_p2() -> AirCargoProblem:
    # TODO implement Problem 2 definition
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', 'ATL']

    pos = [
        expr('At(C1, SFO)'),
        expr('At(P1, SFO)'),
        expr('At(C2, JFK)'),
        expr('At(P2, JFK)'),
        expr('At(C3, ATL)'),
        expr('At(P3, ATL)'),
    ]
    neg = [
        expr('At(C1, JFK)'),
        expr('At(P1, JFK)'),
        expr('At(C1, ATL)'),
        expr('At(P1, ATL)'),
        expr('At(C2, SFO)'),
        expr('At(P2, SFO)'),
        expr('At(C2, ATL)'),
        expr('At(P2, ATL)'),
        expr('At(C3, SFO)'),
        expr('At(P3, SFO)'),
        expr('At(C3, JFK)'),
        expr('At(P3, JFK)'),
    ]

    neg += [expr('In({},{})'.format(c, p)) for c in cargos for p in planes]

    init = FluentState(pos, neg)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C3, SFO)'),
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)