Esempio n. 1
0
    def __init__(
        self,
        n_rooms: int,
        fluents: AbstractFluents,
        saved_automaton: str,
        reward: float,
        save_to: Optional[str] = None,
    ):
        """Initialize.

        :param n_rooms: the number of rooms in this environment. From this
            will follow the fluents and automaton states.
        :param fluents: this object contains the fluents valuation function.
            This method will check that the valuated fluents are the expected
            ones.
        :param saved_automaton: path to a saved automaton corresponding
            to a temporal goal. Fluents must match.
        :param reward: reward suplied when reward is reached.
        :param save_to: path where the automaton should be exported.
        """
        # Define the propositional symbols
        expected_fluents = {"bip", "person", "closed"}
        for i in range(1, n_rooms + 1):
            at_room, in_room = f"at{i}", f"in{i}"
            expected_fluents.add(at_room)
            expected_fluents.add(in_room)

        # Load automaton
        with open(saved_automaton, "rb") as f:
            automaton: SimpleDFA = pickle.load(f)

        # Check same fluents in valuations
        if not fluents.fluents == expected_fluents:
            raise ValueError(
                f"Symbols do not match: {fluents.fluents} != {expected_fluents}"
            )
        # NOTE: not checking also for automaton. Assuming correct

        # Super
        TemporalGoal.__init__(
            self,
            formula=None,  # Provinding automaton directly
            reward=reward,
            automaton=automaton,
            labels=fluents,
            extract_fluents=fluents.evaluate,
            reward_shaping=False,
            zero_terminal_state=False,
        )

        # Maybe save
        if save_to:
            self.automaton.to_graphviz().render(save_to)
Esempio n. 2
0
    def __init__(
        self,
        colors: Sequence[str],
        fluents: AbstractFluents,
        reward: Optional[float] = 1.0,
        save_to: Optional[str] = None,
    ):
        """Initialize.

        :param colors: a sequence of colors, these are the positions that
            the agent must reach with the correct order.
        :param fluents: a fluents evaluator. All colors must be fluents, so
            that we know when the agent is in each position.
        :param reward: reward suplied when reward is reached.
        :param save_to: path where the automaton should be exported.
        """
        # Check
        if not all((color in fluents.fluents for color in colors)):
            raise ValueError(
                "Some color has no associated fluent to evaluate it")

        # Make automaton for this sequence
        automaton = self._make_sapientino_automaton(colors)

        # Super
        TemporalGoal.__init__(
            self,
            formula=None,  # Provinding automaton directly
            reward=reward,
            automaton=automaton,
            labels=set(colors),
            extract_fluents=fluents.evaluate,
            reward_shaping=False,
            zero_terminal_state=False,
        )

        # Maybe save
        if save_to:
            self.automaton.to_graphviz().render(save_to)