Beispiel #1
0
    def fromFeaturesToPropositional(self, features, action, *args, **kwargs):
        res = set()

        if action == 4:
            # res.add(self.get)
            cur_action = "get"
        elif action == 5:
            # res.add(self.use)
            cur_action = "use"
        else:
            cur_action = ""

        l = features[0]
        if l < len(self.locations):
            location_sym = self.locations[l]
            # res.add(location_sym)

            s = Symbol(cur_action + "_" + location_sym)
            if s in self.alphabet.symbols: res.add(s)

        for entity, used in features[1].items():
            if used == 0: continue
            if entity in RESOURCES:
                s = Symbol("get" + "_" + entity)
            elif entity in TOOLS:
                s = Symbol("use" + "_" + entity)
            else:
                raise Exception
            if s in self.alphabet.symbols: res.add(s)

        return res
Beispiel #2
0
    def __init__(self,
                 input_space,
                 bricks_cols=3,
                 bricks_rows=3,
                 lines_num=3,
                 gamma=0.99,
                 on_the_fly=False):
        assert lines_num == bricks_cols or lines_num == bricks_rows
        self.line_symbols = [Symbol("l%s" % i) for i in range(lines_num)]
        lines = self.line_symbols

        parser = LDLfParser()

        string_formula = get_breakout_lines_formula(lines)
        print(string_formula)
        f = parser(string_formula)
        reward = 10000

        super().__init__(BreakoutGoalFeatureExtractor(input_space,
                                                      bricks_cols=bricks_cols,
                                                      bricks_rows=bricks_rows),
                         set(lines),
                         f,
                         reward,
                         gamma=gamma,
                         on_the_fly=on_the_fly)
Beispiel #3
0
 def p_propositional(self, p):
     """propositional : propositional EQUIVALENCE propositional
                      | propositional IMPLIES propositional
                      | propositional OR propositional
                      | propositional AND propositional
                      | NOT propositional
                      | FALSE
                      | TRUE
                      | ATOM"""
     if len(p) == 4:
         if p[2] == Symbols.EQUIVALENCE.value:
             p[0] = PLEquivalence([p[1], p[3]])
         elif p[2] == Symbols.IMPLIES.value:
             p[0] = PLImplies([p[1], p[3]])
         elif p[2] == Symbols.OR.value:
             p[0] = PLOr([p[1], p[3]])
         elif p[2] == Symbols.AND.value:
             p[0] = PLAnd([p[1], p[3]])
         else:
             raise ValueError
         # else:
         #     p[0] = p[2]
     elif len(p) == 3:
         p[0] = PLNot(p[2])
     elif len(p) == 2:
         if p[1] == Symbols.TRUE.value:
             p[0] = PLTrue()
         elif p[1] == Symbols.FALSE.value:
             p[0] = PLFalse()
         else:
             p[0] = PLAtomic(Symbol(p[1]))
     else:
         raise ValueError
 def p_formula_atom(self, p):
     """formula : ATOM
                | TRUE
                | FALSE"""
     if p[1] == Symbols.TRUE.value:
         p[0] = PLTrue()
     elif p[1] == Symbols.FALSE.value:
         p[0] = PLFalse()
     else:
         p[0] = PLAtomic(Symbol(p[1]))
Beispiel #5
0
    def __init__(self, input_space, gamma=0.99, on_the_fly=False):

        # the formula
        self.location_syms = [Symbol(l[0]) for l in LOCATIONS]
        ngnu = "(<(get | use) -> (%s) >tt | [true]ff)" % " | ".join(
            map(str, self.location_syms))
        formula_string = "[true*]" + ngnu

        super().__init__(input_space,
                         formula_string,
                         gamma=gamma,
                         on_the_fly=on_the_fly)
 def p_formula(self, p):
     """formula : formula EQUIVALENCE formula
                | formula IMPLIES formula
                | formula OR formula
                | formula AND formula
                | formula UNTIL formula
                | formula RELEASE formula
                | EVENTUALLY formula
                | ALWAYS formula
                | NEXT formula
                | WEAK_NEXT formula
                | NOT formula
                | TRUE
                | FALSE
                | ATOM"""
     if len(p) == 2:
         if p[1] == Symbols.TRUE.value:
             p[0] = LTLfTrue()
         elif p[1] == Symbols.FALSE.value:
             p[0] = LTLfFalse()
         else:
             p[0] = LTLfAtomic(Symbol(p[1]))
     elif len(p) == 3:
         if p[1] == Symbols.NEXT.value:
             p[0] = LTLfNext(p[2])
         elif p[1] == Symbols.WEAK_NEXT.value:
             p[0] = LTLfWeakNext(p[2])
         elif p[1] == Symbols.EVENTUALLY.value:
             p[0] = LTLfEventually(p[2])
         elif p[1] == Symbols.ALWAYS.value:
             p[0] = LTLfAlways(p[2])
         elif p[1] == Symbols.NOT.value:
             p[0] = LTLfNot(p[2])
     elif len(p) == 4:
         l, o, r = p[1:]
         if o == Symbols.EQUIVALENCE.value:
             p[0] = LTLfEquivalence([l, r])
         elif o == Symbols.IMPLIES.value:
             p[0] = LTLfImplies([l, r])
         elif o == Symbols.OR.value:
             p[0] = LTLfOr([l, r])
         elif o == Symbols.AND.value:
             p[0] = LTLfAnd([l, r])
         elif o == Symbols.UNTIL.value:
             p[0] = LTLfUntil([l, r])
         elif o == Symbols.RELEASE.value:
             p[0] = LTLfRelease([l, r])
         else:
             raise ValueError
     else:
         raise ValueError
Beispiel #7
0
    def __init__(self,
                 input_space,
                 gamma=0.99,
                 on_the_fly=False,
                 relaxed=True):
        self.color_syms = [Symbol(c) for c in COLORS] + [Symbol("no_color")]
        self.bip = Symbol("bip")

        parser = LDLfParser()

        if not relaxed:
            # the formula
            sb = str(self.bip)
            not_bip = ";(!%s)*;" % sb
            and_bip = lambda x: str(x) + " & " + sb
            # every color-bip in sequence, no bip between colors.
            formula_string = "<(!%s)*;" % sb + not_bip.join(
                map(and_bip, self.color_syms[:-1])) + ">tt"
        else:
            sb = str(self.bip)
            not_bip = ";true*;"
            and_bip = lambda x: str(x) + " & " + sb
            # every color-bip in sequence, no bip between colors.
            formula_string = "<true*;" + not_bip.join(
                map(and_bip, self.color_syms[:-1])) + ">tt"

        print(formula_string)
        f = parser(formula_string)

        reward = 1

        super().__init__(SapientinoTEFeatureExtractor(input_space),
                         set(self.color_syms).union({self.bip}),
                         f,
                         reward,
                         gamma=gamma,
                         on_the_fly=on_the_fly)
Beispiel #8
0
    def __init__(self, on_the_fly=False):
        self.row_symbols = [Symbol(r) for r in ["r0", "r1", "r2"]]
        rows = self.row_symbols

        parser = LDLfParser()
        f = parser(
            "<(!r0 & !r1 & !r2)*;(r0 & !r1 & !r2)*;(r0 & r1 & !r2)*; r0 & r1 & r2>tt"
        )
        reward = 10000

        super().__init__(BreakoutRowBottomUpGoalFeatureExtractor(),
                         set(rows),
                         f,
                         reward,
                         on_the_fly=on_the_fly)
Beispiel #9
0
    def __init__(self,
                 input_space,
                 bricks_cols=3,
                 bricks_rows=3,
                 lines_num=3,
                 gamma=0.99,
                 on_the_fly=False):
        self.line_symbols = [Symbol("l%s" % i) for i in range(lines_num)]
        lines = self.line_symbols

        parser = LDLfParser()
        f = parser(
            "<(!l0 & !l1 & !l2)*;(l0 & !l1 & !l2);(l0 & !l1 & !l2)*;(l0 & l1 & !l2); (l0 & l1 & !l2)*; l0 & l1 & l2>tt"
        )
        reward = 10000

        super().__init__(BreakoutGoalFeatureExtractor(input_space,
                                                      bricks_cols=bricks_cols,
                                                      bricks_rows=bricks_rows),
                         set(lines),
                         f,
                         reward,
                         gamma=gamma,
                         on_the_fly=on_the_fly)
Beispiel #10
0
 def fromStrings(cls, symbol_strings: Set[str]):
     f_symbols = frozenset(Symbol(s) for s in symbol_strings)
     return alphabet_factory.new(f_symbols)
Beispiel #11
0
 def __init__(self):
     super().__init__(Symbol(Symbols.LAST.value))
Beispiel #12
0
 def __init__(self):
     super().__init__(Symbol(Symbols.END.value))
Beispiel #13
0
 def __init__(self):
     super().__init__(Symbol(Symbols.LOGICAL_FALSE.value))
 def __init__(self):
     PLAtomic.__init__(self, Symbol(Symbols.FALSE.value))
Beispiel #15
0
 def __init__(self):
     LTLfAtomic.__init__(self, Symbol("End"))
Beispiel #16
0
 def __init__(self):
     super().__init__(Symbol(Symbols.FALSE.value))
     self.a = PLFalse()
Beispiel #17
0
 def __init__(self):
     super().__init__(Symbol(Symbols.TRUE.value))
     self.a = PLTrue()
 def fromStringSets(l:List[Set[str]]):
     return FiniteTrace([PLInterpretation(frozenset({Symbol(string) for string in s})) for s in l])
Beispiel #19
0
 def fromStrings(self, strings: List[str]):
     return self(set(Symbol(s) for s in strings))
def to_automaton_(f, labels:Set[Symbol]=None):
    """
    DEPRECATED
    From a LDLfFormula, build the automaton.
    :param f:               a LDLfFormula;
    :param labels:          a set of Symbol, the fluents of our domain. If None, retrieve them from the formula;
    :param determinize:     True if you need to determinize the NFA, obtaining a DFA;
    :param minimize:        True if you need to minimize the DFA (if determinize is False this flag has no effect.)
    :return:                a NFA or a DFA which accepts the same traces that makes the formula True.
    """

    nnf = f.to_nnf()

    if labels is None:
        # if the labels of the formula are not specified in input,
        # retrieve them from the formula
        labels = nnf.find_labels()

    # the alphabet is the powerset of the set of fluents
    alphabet = powerset(labels)
    initial_state = MacroState({nnf})
    final_states = {MacroState()}
    delta = set()

    d = f.delta(PLFalseInterpretation(), epsilon=True)
    if d.truth(d):
        final_states.add(initial_state)

    states = {MacroState(), initial_state}

    states_changed, delta_changed = True, True

    while states_changed or delta_changed:

        states_changed, delta_changed = False, False
        for actions_set in alphabet:
            states_list = list(states)
            for q in states_list:

                # delta function applied to every formula in the macro state Q
                delta_formulas = [f.delta(actions_set) for f in q]

                # find the list of atoms, which are "true" atoms (i.e. propositional atoms) or LDLf formulas
                atomics = [s for subf in delta_formulas for s in find_atomics(subf)]

                # "freeze" the found atoms as symbols and build a mapping from symbols to formulas
                symbol2formula = {Symbol(str(f)): f for f in atomics if f != PLTrue() and f != PLFalse()}

                # build a map from formula to a "freezed" propositional Atomic Formula
                formula2atomic_formulas = {
                    f: PLAtomic(Symbol(str(f)))
                    if f != PLTrue() and f != PLFalse()# and not isinstance(f, PLAtomic)
                    else f for f in atomics
                }

                # the final list of Propositional Atomic Formulas, one for each formula in the original macro state Q
                transformed_delta_formulas = [_transform_delta(f, formula2atomic_formulas) for f in delta_formulas]

                # the empty conjunction stands for true
                if len(transformed_delta_formulas) == 0:
                    conjunctions = PLTrue()
                elif len(transformed_delta_formulas) == 1:
                    conjunctions = transformed_delta_formulas[0]
                else:
                    conjunctions = PLAnd(transformed_delta_formulas)

                # the model in this case is the smallest set of symbols s.t. the conjunction of "freezed" atomic formula
                # is true.
                models = frozenset(conjunctions.minimal_models(Alphabet(symbol2formula)))

                if len(models) == 0:
                    continue
                for min_model in models:
                    q_prime = MacroState(
                        {symbol2formula[s] for s in min_model.true_propositions})

                    len_before = len(states)
                    states.add(q_prime)
                    if len(states) == len_before + 1:
                        states_list.append(q_prime)
                        states_changed = True

                    len_before = len(delta)
                    delta.add((q, actions_set, q_prime))
                    if len(delta) == len_before + 1:
                        delta_changed = True

                    # check if q_prime should be added as final state
                    if len(q_prime) == 0:
                        final_states.add(q_prime)
                    else:
                        subf_deltas = [subf.delta(PLFalseInterpretation(), epsilon=True) for subf in q_prime]
                        if len(subf_deltas)==1:
                            q_prime_delta_conjunction = subf_deltas[0]
                        else:
                            q_prime_delta_conjunction = PLAnd(subf_deltas)

                        if q_prime_delta_conjunction.truth(PLFalseInterpretation()):
                            final_states.add(q_prime)


    alphabet = PythomataAlphabet({PLInterpretation(set(sym)) for sym in alphabet})
    delta = frozenset((i, PLInterpretation(set(a)), o) for i, a, o in delta)


    nfa = NFA.fromTransitions(
        alphabet=alphabet,
        states=frozenset(states),
        initial_state=initial_state,
        accepting_states=frozenset(final_states),
        transitions=delta
    )

    return nfa