Exemplo n.º 1
0
        def apply_action(
            self, action_proto: Event
        ) -> Tuple[bool, Optional[ActionSpace], bool]:
            """Apply an action."""
            if not action_proto.HasField("int64_value"):
                raise ValueError("Invalid action, int64_value expected.")

            choice_index = action_proto.int64_value
            if choice_index < 0 or choice_index >= self.num_actions:
                raise ValueError("Out-of-range")

            # Get the action
            action = actions[choice_index]
            # Apply the action to this session and check if we changed anything
            old_choices = self.choices.copy()
            action(self)
            logger.debug("Applied action %s", action)

            # Reset the internal variables if this action has caused a change in the
            # choices
            if old_choices != self.choices:
                self.reset_cached()

            # The action has not changed anything yet. That waits until an
            # observation is taken
            return False, None, False
Exemplo n.º 2
0
    def apply_action(
            self, action: Event) -> Tuple[bool, Optional[ActionSpace], bool]:
        if not action.HasField("int64_value"):
            raise ValueError("Invalid action. int64_value expected.")

        choice_index = action.int64_value
        if choice_index < 0 or choice_index >= len(
                self.action_space.space.named_discrete.name):
            raise ValueError("Out-of-range")

        logger.info("Applied action %d", choice_index)

        act = self.action_space.space.named_discrete.name[choice_index]
        if self.mode not in ["size", "select"]:
            raise RuntimeError("Invalid mode set: {}".format(self.mode))
        if act == "toggle_mode":
            if self.mode == "size":
                self.mode = "select"
            elif self.mode == "select":
                self.mode = "size"
        if act == "toggle_thread":
            self.thread[self.cursor] = not self.thread[self.cursor]
        if act == "down":
            # always loop around
            if self.mode == "size":
                self.resize(-1)
            elif self.mode == "select":
                next_cursor = (self.cursor - 1) % len(self.order)
                self.cursor = next_cursor
        if act == "up":
            # always loop around
            if self.mode == "size":
                self.resize(1)
            elif self.mode == "select":
                next_cursor = (self.cursor + 1) % len(self.order)
                self.cursor = next_cursor

        return False, None, False