예제 #1
0
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', 'ATL']
    plane_to_airport = {
        'P1': 'SFO',
        'P2': 'JFK',
        'P3': 'ATL'
    }
    cargo_to_airport = {
        'C1': 'SFO',
        'C2': 'JFK',
        'C3': 'ATL',
    }
    pos = []
    neg = []
    for plane, this_airport in plane_to_airport.items():
        for other_airport in airports:
            if other_airport == this_airport:
                pos.append(expr('At({}, {})'.format(plane, this_airport)))
            else:
                neg.append(expr('At({}, {})'.format(plane, other_airport)))

    for cargo, this_airport in cargo_to_airport.items():
        for other_airport in airports:
            if this_airport == other_airport:
                pos.append(expr('At({}, {})'.format(cargo, this_airport)))
            else:
                neg.append(expr('At({}, {})'.format(cargo, other_airport)))

    for cargo in cargos:
        for plane in planes:
            neg.append(expr('In({}, {})'.format(cargo, plane)))

    init = FluentState(pos, neg)

    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C3, SFO)'),
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #2
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([], [])
        #----------------------insert---------------------
        # Lifted from example_have_cake.py
        # 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)
예제 #3
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(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(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, SFO)'),
        expr('At(P3, JFK)'),
    ]
    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 air_cargo_p2() -> AirCargoProblem:
    # TODO implement Problem 2 definition
    '''
    Init(At(C1, SFO) ∧ At(C2, JFK) ∧ At(C3, ATL)
        ∧ At(P1, SFO) ∧ At(P2, JFK) ∧ At(P3, ATL)

    Goal(At(C1, JFK) ∧ At(C2, SFO) ∧ At(C3, SFO))
    '''
    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)')
    ]

    negs = []
    for ca in cargos:
        for pl in planes:
            neg = expr('In({}, {})'.format(ca, pl))
            if neg not in negs and neg not in pos:
                negs.append(neg)
            for ap in airports:
                neg = expr('At({}, {})'.format(pl, ap))
                if neg not in negs and neg not in pos:
                    negs.append(neg)
                neg = expr('At({}, {})'.format(ca, ap))
                if neg not in negs and neg not in pos:
                    negs.append(neg)

    init = FluentState(pos, negs)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C3, SFO)'),
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #5
0
def air_cargo_p3() -> AirCargoProblem:
    # TODO implement Problem 3 definition
    cargos=['C1','C2','C3','C4']
    planes=['P1','P2']
    airports=['SFO','JFK','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=[expr('In(C1,P1)'),expr('In(C1,P2)'),
         expr('In(C2,P1)'),expr('In(C2,P2)'),     
         expr('In(C3,P1)'),expr('In(C3,P2)'),
         expr('In(C4,P1)'),expr('In(C4,P2)'),
         expr('At(C1,JFK)'),expr('At(C1,ATL)'),expr('At(C1,ORD)'),
         expr('At(C2,SFO)'),expr('At(C2,ATL)'),expr('At(C2,ORD)'),
         expr('At(C3,SFO)'),expr('At(C3,JFK)'),expr('At(C3,ORD)'),
         expr('At(C4,SFO)'),expr('At(C4,JFK)'),expr('At(C4,ATL)'),
         expr('At(P1,JFK)'),expr('At(P1,ATL)'),expr('At(P1,ORD)'),
         expr('At(P2,SFO)'),expr('At(P2,ATL)'),expr('At(P2,ORD)')]
    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)
예제 #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
     """
     # TODO implement
     current_state = []
     for idx,p in enumerate(state):
         if p == 'T':
             current_state.append(self.state_map[idx])
     for effect in action.effect_add:
         current_state.append(effect)
     for effect in action.effect_rem:
         current_state.remove(effect)
     new_state = FluentState(current_state, [])
     
     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
        """
        # TODO implement
        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)
예제 #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
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())

        # Act on the KB with 'action'
        action.act(kb, action.args)

        # Create a new fluent state from the KB
        new_state = FluentState(kb.clauses, [])

        return encode_state(new_state, self.state_map)
예제 #9
0
 def result(self, state: str, action: 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)
     #Check if current fluent could persist or not by seeing if effect_rem will impact current fluent
     for fluent in action.effect_add:
         if fluent not in new_state.pos:
             new_state.pos.append(fluent)
     #If add effect from action not in new state fluent, add it
     for fluent in old_state.neg:
         if fluent not in action.effect_add:
             new_state.neg.append(fluent)
     #Identify if negative fluent that could not be effect_add by action?
     for fluent in action.effect_rem:
         if fluent not in new_state.neg:
             new_state.neg.append(fluent)
     #Make sure remove effect from action is put into fluent of next state
     return encode_state(new_state, self.state_map)
예제 #10
0
def air_cargo_p3() -> AirCargoProblem:
    # TODO implement Problem 3 definition
    cargos = ['C1', 'C2', 'C3', 'C4']
    planes = ['P1', 'P2']
    airports = ['SFO', 'JFK', 'ATL', 'ORD']
    pos = [expr('At({}, {})'.format(i,j)) for i,j in zip(cargos,airports)] + \
    [expr('At({}, {})'.format(i,j)) for i,j in zip(planes,airports[:len(planes)])]

    neg = [expr('At({}, {})'.format(i,j)) for i in cargos for j in airports] + \
    [expr('At({}, {})'.format(i,j))  for i in planes for j in airports] + \
    [expr('In({}, {})'.format(i,j)) for i in cargos for j in planes]
    neg = list(set(neg).difference(set(pos)))
    init = FluentState(pos, neg)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C3, JFK)'),
        expr('At(C4, SFO)'),
    ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #11
0
def air_cargo_p3() -> AirCargoProblem:
    # TODO COMPLETED:  implement Problem 3 definition
    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 = get_neg_clauses(cargos,planes,airports,pos)
    init = FluentState(pos, neg)
    goal = [expr('At(C1, JFK)'),
            expr('At(C2, SFO)'),
            expr('At(C3, JFK)'),
            expr('At(C4, SFO)'),
            ]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #12
0
def air_cargo_p1():
    """ Implementation of air cargo transport problem 1

        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(C1, JFK)'),
        expr('In(C1, P1)'),
        expr('In(C1, P2)'),
        expr('At(C2, SFO)'),
        expr('In(C2, P1)'),
        expr('In(C2, 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)
예제 #13
0
def air_cargo_p2() -> AirCargoProblem:
    """
    Given:
        Init(At(C1, SFO) ∧ At(C2, JFK) ∧ At(C3, ATL)
        ∧ At(P1, SFO) ∧ At(P2, JFK) ∧ At(P3, ATL)
        ∧ Cargo(C1) ∧ Cargo(C2) ∧ Cargo(C3)
        ∧ Plane(P1) ∧ Plane(P2) ∧ Plane(P3)
        ∧ Airport(JFK) ∧ Airport(SFO) ∧ Airport(ATL))
        Goal(At(C1, JFK) ∧ At(C2, SFO) ∧ At(C3, SFO))
    """
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['SFO', 'JFK', 'ATL']

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

    neg = [
        At('C1', 'JFK'),
        At('C1', 'ATL'),
        At('C2', 'SFO'),
        At('C2', 'ATL'),
        At('C3', 'SFO'),
        At('C3', 'JFK')
    ]

    neg += [
        At('P1', 'JFK'),
        At('P1', 'ATL'),
        At('P2', 'SFO'),
        At('P2', 'ATL'),
        At('P3', 'SFO'),
        At('P3', 'JFK')
    ]

    neg += [In(c, p) for p in planes for c in cargos]

    init = FluentState(pos, neg)
    goal = [At('C1', 'JFK'), At('C2', 'SFO'), At('C3', 'SFO')]

    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #14
0
def air_cargo_p2() -> AirCargoProblem:
    # TODO implement Problem 2 definition
    # 5/10/17 Bhinesh Patel - copy and modified air_cargo_p1
    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)'),
           expr('At(C3, SFO)'),
           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, SFO)'),
           expr('At(P3, JFK)'),
           ]
    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 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 = []
    neg = [expr("In({}, {})".format(c, p)) for p in planes for c in cargos]

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

    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") or (c == "C4" and a == "ORD")):
                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")):
                neg.append(expr("At({}, {})".format(p, a)))

    init = FluentState(pos, neg)

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

    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #16
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
        """

        fluent_state = decode_state(state, self.state_map)
        new_pos = set(fluent_state.pos)
        new_pos = new_pos.difference(action.effect_rem)
        new_pos = new_pos.union(action.effect_add)

        new_neg = set(fluent_state.neg)
        new_neg = new_neg.difference(action.effect_add)
        new_neg = new_neg.union(action.effect_rem)

        new_state = FluentState(list(new_pos), list(new_neg))
        return encode_state(new_state, self.state_map)
예제 #17
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
        """
        kb = PropKB(decode_state(state, self.state_map).sentence())
        # Not sure why action.args should be given here instead
        # of using it as default.
        action.act(kb, action.args)
        # FAIL, kb does not have a method to go back to a FluentState?
        pos_list = [
            clause for clause in kb.clauses if '~' != clause.__repr__()[0]
        ]
        neg_list = [
            clause for clause in kb.clauses if '~' == clause.__repr__()[0]
        ]
        return encode_state(FluentState(pos_list, neg_list), self.state_map)
예제 #18
0
def air_cargo_p2() -> AirCargoProblem:
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', 'ATL']

    # generate list of all fluents
    all = set(expr_in(c, p) for (c, p) in product(cargos, planes)) \
          | set(expr_at(p, a) for (p, a) in product(planes, airports)) \
          | set(expr_at(c, a) for (c, a) in product(cargos, airports))

    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')
    ]
    init = FluentState(pos, list(all - set(pos)))
    goal = [expr_at('C1', 'JFK'), expr_at('C2', 'SFO'), expr_at('C3', 'SFO')]
    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #19
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([], [])
        # Create knowledge base
        kb = PropKB()
        # Add the current state to the KB
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        # Change the KB according to action
        action.act(kb, action.args)
        # Add those changes to the FluentState
        for i in kb.clauses:
            new_state.pos.append(i)
        # Return the state in string formate (encode)
        return encode_state(new_state, self.state_map)
예제 #20
0
def air_cargo_p1() -> AirCargoProblem:
    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)')]
    init = FluentState(pos, neg)
    goal = [expr('At(C1, JFK)'),
            expr('At(C2, SFO)')]

    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #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([], [])
        kb = PropKB()
        kb.tell(decode_state(state, self.state_map).pos_sentence())
        action.check_precond(kb, action.args)
        action.act(kb, action.args)
        for clause in kb.clauses:
            if kb.ask_if_true:
                new_state.pos.append(clause)
            else:
                new_state.neg.append(clause)
        return encode_state(new_state, self.state_map)
예제 #22
0
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 = build_negative(pos, cargos, planes, airports)
    init = FluentState(pos, neg)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C3, JFK)'),
        expr('At(C4, 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
        """
        new_state = FluentState([], [])
        old_state = decode_state(state, self.state_map)

        # copy over fluent if not in the effect_rem section
        # of the executed action
        for fluent in old_state.pos:
            #TODO print("result", "fluent", fluent)
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)

        # copy over fluent if not in the effect_add section
        # of the executed action
        for fluent in old_state.neg:
            if fluent not in action.effect_add:
                new_state.neg.append(fluent)

        # create new fluents from action
        for fluent in action.effect_add:
            # avoiding duplicates
            # (contradictions/mutexes are already handled by the preconditions when
            # creating the list of possible actions)
            if fluent not in new_state.pos:
                new_state.pos.append(fluent)

        # remove effects as a consequence of this action
        for fluent in action.effect_rem:
            # Avoid duplicates
            if fluent not in new_state.neg:
                new_state.neg.append(fluent)

        return encode_state(new_state, self.state_map)
def air_cargo_p3() -> AirCargoProblem:
    # TODO implement Problem 3 definition
    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_cargo_plane = [
        expr("In({}, {})".format(c, p)) for c in cargos for p in planes
    ]
    neg_cargo_airport = []
    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') or (c == 'C4' and a == 'ORD')):
                neg_cargo_airport.append(expr("At({}, {})".format(c, a)))

    neg_plane_airport = []
    for p in planes:
        for a in airports:
            if not ((p == 'P1' and a == 'SFO') or (p == 'P2' and a == 'JFK')):
                neg_plane_airport.append(expr("At({}, {})".format(p, a)))

    init = FluentState(pos,
                       neg_cargo_plane + neg_cargo_airport + neg_plane_airport)
    goal = [
        expr('At(C1, JFK)'),
        expr('At(C3, JFK)'),
        expr('At(C2, SFO)'),
        expr('At(C4, 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([], [])

        # EG: state=FTTTFF
        # self.state_map=[At(C1,SFO),At(P1, ORD),In(C2,P1),etc..], it is a list contain positive and negative
        # fluent of our current Problem. This value won't be changed since our problem won't be changed
        # Get the current state
        current_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 current 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 current_state.pos:
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)
        for fluent in current_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 current 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 encode_state(new_state, self.state_map)
예제 #26
0
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)')
    ]

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

    # The cargos aren't in any of the planes
    neg = [expr('In({}, {})'.format(c, p)) for c in cargos for p in planes]

    # The planes are not at the other airports
    neg += [expr('At(P1, {})'.format(a)) for a in ['JFK', 'ATL', 'ORD']]
    neg += [expr('At(P2, {})'.format(a)) for a in ['SFO', 'ATL', 'ORD']]

    # The cargos are not at the other airports
    neg += [expr('At(C1, {})'.format(a)) for a in ['JFK', 'ATL', 'ORD']]
    neg += [expr('At(C2, {})'.format(a)) for a in ['SFO', 'ATL', 'ORD']]
    neg += [expr('At(C3, {})'.format(a)) for a in ['SFO', 'JFK', 'ORD']]
    neg += [expr('At(C4, {})'.format(a)) for a in ['SFO', 'JFK', 'ATL']]

    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)
예제 #27
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([], [])
        #get current state
        curr_state = decode_state(state, self.state_map)

        # the effect defines the result of executing the action.
        # so each fluent (positive) is checked over the new state.
        for fluent in curr_state.pos:
            if fluent not in action.effect_rem:
                new_state.pos.append(fluent)

        # each fluent (negative) is checked over the new state.
        for fluent in curr_state.neg:
            if fluent not in action.effect_add:
                new_state.neg.append(fluent)

        # check fluent over action (positive)
        # if positive(add) effect, then these fluents are added
        # to the new state's positive clause
        for fluent in action.effect_add:
            if fluent not in new_state.pos:
                new_state.pos.append(fluent)

        # check fluent over action (negative)
        # if negative (add) effect, then these fluents are added
        # to the new state's negative clause
        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)
예제 #28
0
def air_cargo_p2() -> AirCargoProblem:
    cargos = air_cargo_p1().cargos[:]
    cargos.append('C3')

    planes = air_cargo_p1().planes[:]
    planes.append('P3')

    airports = air_cargo_p1().airports
    airports.append('ATL')

    pos = decode_state(air_cargo_p1().initial_state_TF,
                       air_cargo_p1().state_map).pos
    pos.extend([
        expr('At(C3, ATL)'),
        expr('At(P3, ATL)'),
    ])

    neg = decode_state(air_cargo_p1().initial_state_TF,
                       air_cargo_p1().state_map).neg
    neg.extend([
        expr('At(C3, JFK)'),
        expr('At(C3, SFO)'),
        expr('In(C3, P1)'),
        expr('In(C3, P2)'),
        expr('In(C3, P3)'),
        expr('At(C1, ATL)'),
        expr('In(C1, P3)'),
        expr('At(C2, ATL)'),
        expr('In(C2, P3)'),
        expr('At(P3, JFK)'),
        expr('At(P3, SFO)'),
        expr('At(P1, ATL)'),
        expr('At(P2, ATL)'),
    ])
    init = FluentState(pos, neg)

    goal = air_cargo_p1().goal[:]
    goal.append(expr('At(C3, SFO)'))

    return AirCargoProblem(cargos, planes, airports, init, goal)
예제 #29
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
        # create an empty new state
        new_state = FluentState([], [])
        # decode the old state
        old_state = decode_state(state, self.state_map)
        # loop through the fluents in the old state - positive
        for fluent in old_state.pos:
            # if pos fluent not in the removal effect
            if fluent not in action.effect_rem:
                # add the fluent to the new state - positive
                new_state.pos.append(fluent)
        # loop through the fluents in the action - additive
        for fluent in action.effect_add:
            # if pos fluent not in the new state - positive
            if fluent not in new_state.pos:
                # add the fluent to the new state - positive
                new_state.pos.append(fluent)
        # loop through the fluents in the old state - neg
        for fluent in old_state.neg:
            # if neg fluent not in the additive effect
            if fluent not in action.effect_add:
                # add the fluent to the new state - neg
                new_state.neg.append(fluent)
        # loop through the fluents in the action - removal
        for fluent in action.effect_rem:
            # if neg fluent not in the new state - neg
            if fluent not in new_state.neg:
                # add the fluent to the new state - neg
                new_state.neg.append(fluent)
        # return the resulting state encoded
        return encode_state(new_state, self.state_map)
예제 #30
0
def air_cargo_p2() -> AirCargoProblem:
    # TODO implement Problem 2 definition
    # Identify initial state and goal for Problem 2
    cargos = ['C1', 'C2', 'C3']
    planes = ['P1', 'P2', 'P3']
    airports = ['JFK', 'SFO', '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)