Exemple #1
0
def test_sample():
    space = Commandline([
        CommandlineFlag(name="a", flag="-a", description=""),
        CommandlineFlag(name="b", flag="-b", description=""),
        CommandlineFlag(name="c", flag="-c", description=""),
    ])
    assert space.sample() in {0, 1, 2}
Exemple #2
0
def test_commandline():
    space = Commandline([
        CommandlineFlag(name="a", flag="-a", description=""),
        CommandlineFlag(name="b", flag="-b", description=""),
        CommandlineFlag(name="c", flag="-c", description=""),
    ])

    assert space.commandline([0, 1, 2]) == "-a -b -c"
    assert space.from_commandline(space.commandline([0, 1, 2])) == [0, 1, 2]
Exemple #3
0
 def _make_action_space(self, name: str, entries: List[str]) -> Commandline:
     flags = [
         CommandlineFlag(name=entry,
                         flag=_FLAGS[entry],
                         description=_DESCRIPTIONS[entry])
         for entry in entries
     ]
     return Commandline(items=flags, name=name)
Exemple #4
0
def test_contains():
    space = Commandline([
        CommandlineFlag(name="a", flag="-a", description=""),
        CommandlineFlag(name="b", flag="-b", description=""),
        CommandlineFlag(name="c", flag="-c", description=""),
    ])
    assert space.contains(0)
    assert space.contains(1)
    assert space.contains(2)
    assert not space.contains(-11)
    assert not space.contains(1.5)
    assert not space.contains(4)
Exemple #5
0
    def __init__(self,
                 env: CompilerEnv,
                 flags: Iterable[str],
                 name: Optional[str] = None):
        """Constructor.

        :param env: The environment to wrap.

        :param flags: A list of entries from :code:`env.action_space.flags`
            denoting flags that are available in this wrapped environment.

        :param name: The name of the new action space.
        """
        super().__init__(env)
        self._flags = flags

        if not flags:
            raise TypeError("No flags provided")
        if not isinstance(env.action_space, Commandline):
            raise TypeError("Can only wrap Commandline action space. "
                            f"Received: {type(env.action_space).__name__}")

        self._forward_translation: List[int] = [
            self.action_space[f] for f in flags
        ]
        self._reverse_translation: Dict[int, int] = {
            v: i
            for i, v in enumerate(self._forward_translation)
        }

        # Redefine the action space using this smaller set of flags.
        self.action_space = Commandline(
            items=[
                CommandlineFlag(
                    name=env.action_space.names[a],
                    flag=env.action_space.flags[a],
                    description=env.action_space.descriptions[a],
                ) for a in (env.action_space.flags.index(f) for f in flags)
            ],
            name=
            f"{type(self).__name__}<{name or env.action_space.name}, {len(flags)}>",
        )
Exemple #6
0
    def __init__(
        self,
        env: CompilerEnv,
        terminal=CommandlineFlag(
            name="end-of-episode",
            flag="# end-of-episode",
            description="End the episode",
        ),
    ):
        """Constructor.

        :param env: The environment to wrap.

        :param terminal: The flag to use as the terminal action. Optional.
        """
        super().__init__(env)

        if not isinstance(env.action_space, Commandline):
            raise TypeError(
                f"Unsupported action space: {type(env.action_space).__name__}")

        # Redefine the action space, inserting the terminal action at the start.
        self.action_space = Commandline(
            items=[
                CommandlineFlag(
                    name=name,
                    flag=flag,
                    description=description,
                ) for name, flag, description in zip(
                    env.action_space.names,
                    env.action_space.flags,
                    env.action_space.descriptions,
                )
            ] + [terminal],
            name=f"{type(self).__name__}<{env.action_space.name}>",
        )