Example #1
0
    def test_step(self, word):
        """Test the behaviour of the method 'step'."""
        simulator = AutomatonSimulator(self.dfa)

        for symbol in word:
            previous_states = simulator.cur_state
            current_states = simulator.step(symbol)
            expected_current_states = reduce(
                set.union,
                [self.dfa.get_successors(s, symbol) for s in previous_states],
                set(),
            )
            assert simulator.cur_state == current_states == expected_current_states
Example #2
0
    def test_reset(self, word):
        """Test the behaviour of the method 'reset'."""
        simulator = AutomatonSimulator(self.dfa)

        assert not simulator.is_started
        assert simulator.cur_state == {self.dfa.initial_state}

        for symbol in word:
            simulator.step(symbol)
            assert simulator.is_started

        initial_state = simulator.reset()
        assert not simulator.is_started
        assert simulator.cur_state == initial_state == {self.dfa.initial_state}
Example #3
0
    def test_initialization(self):
        """Test the initialization of a simulator."""
        simulator = AutomatonSimulator(self.dfa)

        assert simulator.automaton == self.dfa
        assert simulator.cur_state == {self.dfa.initial_state}
        assert not simulator.is_started
Example #4
0
    def test_accepts(self, word):
        """Test the behaviour of the method 'accepts'."""
        simulator = AutomatonSimulator(self.dfa)

        assert simulator.accepts(word) == self.dfa.accepts(word)

        for index, symbol in enumerate(word):
            simulator.step(symbol)
            assert simulator.accepts(word[index:]) == self.dfa.accepts(word)
Example #5
0
    def test_is_failed(self):
        """Test the behaviour of the method 'is_failed'."""
        simulator = AutomatonSimulator(self.dfa)

        assert not simulator.is_failed()
        simulator.step("a")
        assert not simulator.is_failed()
        simulator.step("b")
        assert not simulator.is_failed()
        simulator.step("c")
        assert not simulator.is_failed()
        simulator.step("d")
        assert simulator.is_failed()
Example #6
0
    def test_is_true(self):
        """Test the behaviour of the method 'is_true'."""
        simulator = AutomatonSimulator(self.dfa)

        assert not simulator.is_true()
        simulator.step({"a": True})
        assert not simulator.is_true()
        simulator.step({"b": True})
        assert not simulator.is_true()
        simulator.step({"c": True})
        assert simulator.is_true()
Example #7
0
    def __init__(
        self,
        env_name,
        fluents,
        reward,
        logdir,
        load=None,
        verbose=True,
    ):
        """Initialize.

        :param env_name: a gym atari environment name.
        :param fluents: the list of propositional atoms that are known at
            each step.
        :param reward: (float) this reward is returned when the execution
            reaches a final state (at the first instant an execution satisfies
            the restraining specification).
        :param logdir: the automaton just parsed is saved here.
        :param load: if provided, the automaton is not computed but loaded
            from this file. A path to a rb.pickle file.
        :param verbose: verbose flag (automaton conversion may take a while).
        """

        # Read
        data = selector.read_back(env_name)
        json_rb = data["restraining_bolt"] + data["constraints"]

        # Parsing
        restraining_spec = " & ".join(json_rb)
        formula = LDLfParser()(restraining_spec)  # type: LDLfFormula

        # Check: all atoms must be evaluated
        atoms = formula.find_labels()
        all_predicted = all((a in fluents for a in atoms))
        if not all_predicted:
            raise ValueError("One of the atoms " + str(atoms) +
                             " is not in fluents")

        # Parse
        if load is None:

            # Conversion (slow op)
            if verbose:
                print("> Parsing", env_name, "restraining specification")
            automaton = formula.to_automaton()  # type: SymbolicAutomaton
            automaton = automaton.determinize().complete()
            if verbose:
                print("> Parsed")

            # Save and visualize the automaton
            graphviz = automaton.to_graphviz()
            graphviz.render("rb.gv",
                            directory=logdir,
                            view=False,
                            cleanup=False)
            with open(os.path.join(logdir, "rb.pickle"), "wb") as f:
                pickle.dump(automaton, f)

        # Load
        else:
            with open(load, "rb") as f:
                automaton = pickle.load(f)
            if verbose:
                print(">", load, "loaded")

            # Visualize the automaton loaded (debugging)
            graphviz = automaton.to_graphviz()
            graphviz.render("loaded_rb.gv",
                            directory=logdir,
                            view=False,
                            cleanup=False)

        # Runner
        simulator = AutomatonSimulator(automaton)

        # Store
        self.env_name = env_name
        self.fluents = fluents
        self._str = restraining_spec
        self._formula = formula
        self._automaton = automaton
        self._simulator = simulator
        self._reward = reward
        self._last_state = None