def transform_to_dot_flloat(f): parser = LTLfParser() parsed_formula = parser(f) dfa = parsed_formula.to_automaton(determinize=True) dfa.minimize().trim().to_dot("./automaton.dot")
def setup_class(cls): cls.parser = LTLfParser() cls.a, cls.b, cls.c = "A", "B", "C" cls.alphabet_abc = {cls.a, cls.b, cls.c} cls.i_ = PLInterpretation(set()) cls.i_a = PLInterpretation({cls.a}) cls.i_b = PLInterpretation({cls.b}) cls.i_ab = PLInterpretation({cls.a, cls.b})
def setup_class(cls): cls.parser = LTLfParser() cls.a, cls.b, cls.c = "a", "b", "c" cls.alphabet_abc = {cls.a, cls.b, cls.c} cls.i_ = {} cls.i_a = {cls.a: True} cls.i_b = {cls.b: True} cls.i_ab = {cls.a: True, cls.b: True}
def setup_class(cls): cls.parser = LTLfParser() cls.trace = FiniteTrace.from_symbol_sets([ {"A"}, {"A"}, {"B"}, {"B"}, {"C"}, {"C"}, ])
def intersection(prb_dfa, ltl_dfa): """ Build the intersection DFA automaton between problem DFA and LTL DFA. :param prb_dfa: problem DFA :param ltl_dfa: LTL formula DFA :return: SimpleDFA object """ parser = LTLfParser() alphabet = prb_dfa.alphabet states = set({}) initial_state = "[" + prb_dfa.initial_state + "," + str( ltl_dfa.initial_state) + "]" accepting_states = set({}) transition_function = {} explore = {(prb_dfa.initial_state, str(ltl_dfa.initial_state))} while len(explore) is not 0: state = explore.pop() state_intersection = "[" + state[0] + "," + state[1] + "]" states.add(state_intersection) if prb_dfa.is_accepting(state[0]) and ltl_dfa.is_accepting( int(state[1])): accepting_states.add(state_intersection) for letter in alphabet: successor = prb_dfa.get_successors(state[0], letter) if len(successor) is not 0: prob_state = successor.pop() ltl_state = state[1] props = [propositions[prob_state]] transitions = ltl_dfa.get_transitions_from(int(state[1])) for transition in transitions: formula = ltl_extract_formula(transition) if parser(str(formula).replace("~", "!")).truth(props, 0): ltl_state = str(ltl_extract_next_state(transition)) break new_state_intersection = "[" + prob_state + "," + ltl_state + "]" if new_state_intersection not in states: explore.add((prob_state, ltl_state)) if state_intersection not in transition_function: transition_function[state_intersection] = { letter: new_state_intersection } else: transition_function[state_intersection][ letter] = new_state_intersection return SimpleDFA(states, alphabet, initial_state, accepting_states, transition_function)
def test_QuotedFormula(): from flloat.base import QuotedFormula from flloat.parser.ltlf import LTLfParser from flloat.ltlf import LTLfAtomic, LTLfAnd f = LTLfParser()("!(G a)") qf = QuotedFormula(f) atomf = LTLfAnd([LTLfAtomic(f), LTLfAtomic(f)]) assert qf.wrapped is f dir_qf = dir(qf) for member in dir(f): assert member in dir_qf assert hasattr(qf, member)
def test_hash_consistency_after_pickling(): from flloat.parser.ltlf import LTLfParser import pickle parser = LTLfParser() formula = "F (a & !b)" old_obj = parser(formula) h = hash(old_obj) pickle.dump(old_obj, open("temp", "wb")) new_obj = pickle.load(open("temp", "rb")) assert new_obj._hash is None assert h == hash(new_obj) os.remove("temp")
def setup_class(cls): cls.parser = LTLfParser() cls.i_, cls.i_a, cls.i_b, cls.i_ab = ( {}, { "a": True }, { "b": True }, { "a": True, "b": True }, ) cls.true = PLTrue() cls.false = PLFalse()
def test_ltlf_example_readme(): from flloat.parser.ltlf import LTLfParser from flloat.semantics.traces import FiniteTrace parser = LTLfParser() formula = "F (A & !B)" parsed_formula = parser(formula) t1 = FiniteTrace.from_symbol_sets([{}, {"A"}, {"A"}, {"A", "B"}]) assert parsed_formula.truth(t1, 0) t2 = FiniteTrace.from_symbol_sets([{}, {"A", "B"}, {"B"}]) assert not parsed_formula.truth(t2, 0) dfa = parsed_formula.to_automaton() assert dfa.accepts(t1.trace) assert not dfa.accepts(t2.trace)
def _init_symbolic_dfa(self, ltlf_formula: str): """ Initialize the SymbolicDFA :param ltlf_formula: the LTLf formula """ parser = LTLfParser() self.parsed_formula = parser(ltlf_formula) symbolic_dfa = self.parsed_formula.to_automaton() for state in symbolic_dfa.states: self.states.add(state) self.set_initial_state(symbolic_dfa.initial_state) for state in symbolic_dfa.accepting_states: self.set_accepting_state(state, is_accepting=True) for transition in symbolic_dfa.get_transitions(): self.add_transition(transition)
def test_ltlf_example_readme(): from flloat.parser.ltlf import LTLfParser parser = LTLfParser() formula = "F (a & !b)" parsed_formula = parser(formula) t1 = [ {"a": False, "b": False}, {"a": True, "b": False}, {"a": True, "b": False}, {"a": True, "b": True}, {"a": False, "b": False}, ] assert parsed_formula.truth(t1, 0) t2 = [{"a": False, "b": False}, {"a": True, "b": True}, {"a": False, "b": True}] assert not parsed_formula.truth(t2, 0) dfa = parsed_formula.to_automaton() assert dfa.accepts(t1) assert not dfa.accepts(t2)
def setup_class(cls): cls.parser = LTLfParser() cls.trace = [ { "a": True }, { "a": True }, { "b": True }, { "b": True }, { "c": True }, { "c": True }, ]
def test_parser(): parser = LTLfParser() a, b, c = [LTLfAtomic(c) for c in "abc"] assert parser("!a | b <-> !(a & !b) <-> a->b") == LTLfEquivalence([ LTLfOr([LTLfNot(a), b]), LTLfNot(LTLfAnd([a, LTLfNot(b)])), LTLfImplies([a, b]), ]) assert parser("(X a) & (WX !b)") == LTLfAnd( [LTLfNext(a), LTLfWeakNext(LTLfNot(b))]) assert parser("(F (a&b)) <-> !(G (!a | !b) )") == LTLfEquivalence([ LTLfEventually(LTLfAnd([a, b])), LTLfNot(LTLfAlways(LTLfOr([LTLfNot(a), LTLfNot(b)]))), ]) assert parser("(a U b U c) <-> !(!a R !b R !c)") == LTLfEquivalence([ LTLfUntil([a, b, c]), LTLfNot(LTLfRelease([LTLfNot(a), LTLfNot(b), LTLfNot(c)])), ])
def test_nnf(): parser = LTLfParser() a, b, c = [LTLfAtomic(c) for c in "abc"] f = parser("!(a & !b)") assert f.to_nnf() == LTLfOr([LTLfNot(a), b]) f = parser("!(!a | b)") assert f.to_nnf() == LTLfAnd([a, LTLfNot(b)]) f = parser("!(a <-> b)") assert f.to_nnf() == LTLfAnd( [LTLfOr([LTLfNot(a), LTLfNot(b)]), LTLfOr([a, b])]) # Next and Weak Next f = parser("!(X (a & b))") assert f.to_nnf() == LTLfWeakNext(LTLfOr([LTLfNot(a), LTLfNot(b)])) f = parser("!(WX (a & b))") assert f.to_nnf() == LTLfNext(LTLfOr([LTLfNot(a), LTLfNot(b)])) # Eventually and Always f = parser("!(F (a | b))") assert f.to_nnf() == LTLfAlways(LTLfAnd([LTLfNot(a), LTLfNot(b)])).to_nnf() # Until and Release f = parser("!(a U b)") assert f.to_nnf() == LTLfRelease([LTLfNot(a), LTLfNot(b)]) f = parser("!(a R b)") assert f.to_nnf() == LTLfUntil([LTLfNot(a), LTLfNot(b)]) f = parser("!(F (a | b))") assert f.to_nnf() == LTLfAlways(LTLfAnd([LTLfNot(a), LTLfNot(b)])).to_nnf() f = parser("!(G (a | b))") assert f.to_nnf() == LTLfEventually(LTLfAnd([LTLfNot(a), LTLfNot(b)])).to_nnf()
def test_nnf(): parser = LTLfParser() a, b, c = [LTLfAtomic(c) for c in "ABC"] f = parser("!(A & !B)") assert f.to_nnf() == LTLfOr([LTLfNot(a), b]) f = parser("!(!A | B)") assert f.to_nnf() == LTLfAnd([a, LTLfNot(b)]) f = parser("!( (A->B) <-> (!A | B))") assert f.to_nnf() == LTLfAnd([ LTLfAnd([a, LTLfNot(b)]), LTLfOr([LTLfNot(a), b]), ]) # Next and Weak Next f = parser("!(X (A & B))") assert f.to_nnf() == LTLfWeakNext(LTLfOr([LTLfNot(a), LTLfNot(b)])) f = parser("!(WX (A & B))") assert f.to_nnf() == LTLfNext(LTLfOr([LTLfNot(a), LTLfNot(b)])) # Eventually and Always f = parser("!(F (A | B))") assert f.to_nnf() == LTLfAlways(LTLfAnd([LTLfNot(a), LTLfNot(b)])).to_nnf() f = parser("!(F (A | B))") assert f.to_nnf() == LTLfAlways(LTLfAnd([LTLfNot(a), LTLfNot(b)])).to_nnf() f = parser("!(G (A | B))") assert f.to_nnf() == LTLfEventually(LTLfAnd([LTLfNot(a), LTLfNot(b)])).to_nnf() # Until and Release f = parser("!(A U B)") assert f.to_nnf() == LTLfRelease([LTLfNot(a), LTLfNot(b)]) f = parser("!(A R B)") assert f.to_nnf() == LTLfUntil([LTLfNot(a), LTLfNot(b)])
def testuntil(t1, name): root, p1, p2, goal1, goal2 = skeleton() parser = LTLfParser() goalspec = 'a U b' ltlformula = parser(goalspec) # t1 = [{ # 'a': False, 'b': False # }] print(name) print('-' * 50) print(ltlformula.truth(t1), t1) # py_trees.logging.level = py_trees.logging.Level.DEBUG # output = py_trees.display.ascii_tree(root.root) # print(output) # setup_nodes(t1[0]['a'], t1[0]['b'], goal1, goal2) for k in range(len(t1)): setup_nodes(t1[k]['a'], t1[k]['b'], goal1, goal2) root.tick() post_handler(p1, p2, goal1, goal2) if root.root.status == Status.FAILURE: print(False, Status.FAILURE) else: print(True, Status.SUCCESS) print('-' * 50)
def test_parser(): parser = LTLfParser() a, b, c = [LTLfAtomic(c) for c in "ABC"] assert parser("!A | B <-> !(A & !B) <-> A->B") == LTLfEquivalence([ LTLfOr([LTLfNot(a), b]), LTLfNot(LTLfAnd([a, LTLfNot(b)])), LTLfImplies([a, b]) ]) assert parser("(X A) & (WX !B)") == LTLfAnd([ LTLfNext(a), LTLfWeakNext(LTLfNot(b)) ]) assert parser("(F (A&B)) <-> !(G (!A | !B) )") == LTLfEquivalence([ LTLfEventually(LTLfAnd([a, b])), LTLfNot(LTLfAlways(LTLfOr([LTLfNot(a), LTLfNot(b)]))) ]) assert parser("(A U B U C) <-> !(!A R !B R !C)") == LTLfEquivalence([ LTLfUntil([a, b, c]), LTLfNot(LTLfRelease([LTLfNot(a), LTLfNot(b), LTLfNot(c)])) ])
def construct_formula(constraint): parser = LTLfParser() formula = parser(constraint) return formula
def setup_class(cls): cls.parser = LTLfParser() cls.i_, cls.i_a, cls.i_b, cls.i_ab = \ PLFalseInterpretation(), PLInterpretation({"A"}), PLInterpretation({"B"}), PLInterpretation({"A", "B"}) cls.true = PLTrue() cls.false = PLFalse()
LTLfEventually, LTLfAlways, LTLfUntil, LTLfRelease, LTLfNext, LTLfWeakNext, LTLfTrue, LTLfFalse, ) from flloat.parser.ltlf import LTLfParser from flloat.pl import PLAtomic, PLTrue, PLFalse, PLAnd, PLOr from tests.strategies import propositional_words from .conftest import LTLfFixtures from .parsing import ParsingCheck parser = LTLfParser() def test_parser(): parser = LTLfParser() a, b, c = [LTLfAtomic(c) for c in "abc"] assert parser("!a | b <-> !(a & !b) <-> a->b") == LTLfEquivalence([ LTLfOr([LTLfNot(a), b]), LTLfNot(LTLfAnd([a, LTLfNot(b)])), LTLfImplies([a, b]), ]) assert parser("(X a) & (WX !b)") == LTLfAnd( [LTLfNext(a), LTLfWeakNext(LTLfNot(b))])
def older(): # from examples.decompose.utils import DummyNode from flloat.parser.ltlf import LTLfParser # parse the formula parser = LTLfParser() # formula = "r U (t U (b U p))" # F(b) U F(p)" # formula = "p U (b U (t U r))" formula = "((F r U t) U F b) U F p" parsed_formula = parser(formula) print(parsed_formula) # evaluate over finite traces t1 = [ { "r": True, "t": False, "b": False, "p": False }, { "r": True, "t": False, "b": False, "p": False }, { "r": False, "t": True, "b": False, "p": False }, { "r": False, "t": False, "b": True, "p": False }, { "r": False, "t": False, "b": False, "p": True }, ] # t1 = [ # {"r": False, "t": True, "b": False, "p": False}, # {"r": True, "t": False, "b": False, "p": False}, # {"r": False, "t": False, "b": False, "p": False}, # {"r": False, "t": False, "b": True, "p": False}, # {"r": False, "t": False, "b": False, "p": True} # ] print('t1', parsed_formula.truth(t1, 0)) # t2 = [ # {"a": False, "b": False}, # {"a": True, "b": True}, # {"a": False, "b": True}, # ] # assert not parsed_formula.truth(t2, 0) # # from LTLf formula to DFA dfa = parsed_formula.to_automaton() # print(dir(dfa)) print(dfa.get_transitions(), dfa.size) # assert dfa.accepts(t1) # assert not dfa.accepts(t2) # # print the automaton graph = dfa.to_graphviz() graph.render("./1") # requires Graphviz installed on your system.
def cozmo_application(robot: cozmo.robot.Robot): init_robot_world(robot) print("Problem DFA Automaton ...") problem_automaton = problem_dfa() problem_automaton.to_graphviz().render("problem_automata") parser = LTLfParser() formula = "(G !vs_w2) & F (vs_w1 & X G(!at_h))" #formula = "F(vs_w1 & vs_w2 & X G(at_h))" parsed_formula = parser(formula) # From LTLf formula to DFA : deterministic minimized automaton print("LTL formula DFA Automaton ...") dfa = parsed_formula.to_automaton() dfa.to_graphviz().render("ltl_automata") # DFA intersection print("DFA intersection Automaton ...") automaton_intersection = intersection(problem_automaton, dfa) automaton_intersection.to_graphviz().render("intersection_automata") # DFA Game #print("Robot task: visit way point 1, way point 2 and back to home") print("Robot task: visit way point 1 and remain there") print("LTL formula:" + formula) print("-----------------------") print("Start DFA Game") print("-----------------------") winning_condition.init_dfa_game(automaton_intersection) dfa_game = winning_condition.get_dfa_game() print("Winning states waiting ....") winning_states = winning_condition.winnning_states( automaton_intersection.accepting_states) print("Winning states:") print(winning_states) print("Winning strategy waiting ...") winning_strategy.init_winning_condition(winning_states, automaton_intersection) print("Start winning strategy:") state = dfa_game.initial_state final = dfa_game.accepting_states last_env_state = "s_init" while state not in final: print("-----------------------") print("Current state:" + state) omega_action = winning_strategy.omega_function(state) if omega_action is None: print("No action from winning strategy: problem unsolvable") return print("Current action:" + omega_action) print("-----------------------") print("Send action to the robot ...") env_state = action_list[omega_action](robot) if env_state != "err": if env_state is None: print("Environment state after the action execution:" + last_env_state) env_state = last_env_state else: last_env_state = env_state print("Environment state after the action execution:" + env_state) action = omega_action + "," + env_state transitions = dfa_game.get_transitions_from(state) for transition in transitions: if transition[1] == action: state = transition[2] break else: print("Error from environment") return print("-----------------------") print("DFA Game completed successfully")