示例#1
0
    def _validate_action(self, actions: ActionTuple, n_agents: int,
                         name: str) -> ActionTuple:
        """
        Validates that action has the correct action dim
        for the correct number of agents and ensures the type.
        """
        _expected_shape = ((n_agents,
                            self.continuous_size) if n_agents is not None else
                           (self.continuous_size, ))
        if n_agents == 0:
            # print(f"Passed an empty action in _validate_action in base_env.py in mlagents_envs.")
            _continuous = np.zeros((n_agents, self.continuous_size),
                                   dtype=np.float32)
            _discrete = np.zeros((n_agents, self.discrete_size),
                                 dtype=np.int32)
            actions = ActionTuple(continuous=_continuous, discrete=_discrete)

        if actions.continuous.shape != _expected_shape:
            raise UnityActionException(
                f"The behavior {name} needs a continuous input of dimension "
                f"{_expected_shape} for (<number of agents>, <action size>) but "
                f"received input of dimension {actions.continuous.shape}")
        _expected_shape = (n_agents, self.discrete_size)
        if actions.discrete.shape != _expected_shape:
            raise UnityActionException(
                f"The behavior {name} needs a discrete input of dimension "
                f"{_expected_shape} for (<number of agents>, <action size>) but "
                f"received input of dimension {actions.discrete.shape}")
        return actions
示例#2
0
 def _validate_action(
     self, actions: ActionTuple, n_agents: Optional[int], name: str
 ) -> ActionTuple:
     """
     Validates that action has the correct action dim
     for the correct number of agents and ensures the type.
     """
     _expected_shape = (
         (n_agents, self.continuous_size)
         if n_agents is not None
         else (self.continuous_size,)
     )
     if actions.continuous.shape != _expected_shape:
         raise UnityActionException(
             f"The behavior {name} needs a continuous input of dimension "
             f"{_expected_shape} for (<number of agents>, <action size>) but "
             f"received input of dimension {actions.continuous.shape}"
         )
     _expected_shape = (
         (n_agents, self.discrete_size)
         if n_agents is not None
         else (self.discrete_size,)
     )
     if actions.discrete.shape != _expected_shape:
         raise UnityActionException(
             f"The behavior {name} needs a discrete input of dimension "
             f"{_expected_shape} for (<number of agents>, <action size>) but "
             f"received input of dimension {actions.discrete.shape}"
         )
     return actions
示例#3
0
    def set_action_for_agent(
        self, behavior_name: BehaviorName, agent_id: AgentId, action: np.ndarray
    ) -> None:
        self._assert_behavior_exists(behavior_name)
        if behavior_name not in self._env_state:
            return
        spec = self._env_specs[behavior_name]
        expected_shape = (spec.action_size,)
        if action.shape != expected_shape:
            raise UnityActionException(
                f"The Agent {agent_id} with BehaviorName {behavior_name} needs "
                f"an input of dimension {expected_shape} but received input of "
                f"dimension {action.shape}"
            )
        expected_type = np.float32 if spec.is_action_continuous() else np.int32
        if action.dtype != expected_type:
            action = action.astype(expected_type)

        if behavior_name not in self._env_actions:
            self._env_actions[behavior_name] = spec.create_empty_action(
                len(self._env_state[behavior_name][0])
            )
        try:
            index = np.where(self._env_state[behavior_name][0].agent_id == agent_id)[0][
                0
            ]
        except IndexError as ie:
            raise IndexError(
                "agent_id {} is did not request a decision at the previous step".format(
                    agent_id
                )
            ) from ie
        self._env_actions[behavior_name][index] = action
示例#4
0
    def set_action_for_agent(self, agent_group: AgentGroup, agent_id: AgentId,
                             action: np.ndarray) -> None:
        self._assert_group_exists(agent_group)
        if agent_group not in self._env_state:
            return
        spec = self._env_specs[agent_group]
        expected_shape = (spec.action_size, )
        if action.shape != expected_shape:
            raise UnityActionException(
                "The Agent {0} in group {1} needs an input of dimension {2} but received input of dimension {3}"
                .format(agent_id, agent_group, expected_shape, action.shape))
        expected_type = np.float32 if spec.is_action_continuous() else np.int32
        if action.dtype != expected_type:
            action = action.astype(expected_type)

        if agent_group not in self._env_actions:
            self._env_actions[agent_group] = spec.create_empty_action(
                self._env_state[agent_group].n_agents())
        try:
            index = np.where(
                self._env_state[agent_group].agent_id == agent_id)[0][0]
        except IndexError as ie:
            raise IndexError(
                "agent_id {} is did not request a decision at the previous step"
                .format(agent_id)) from ie
        self._env_actions[agent_group][index] = action
示例#5
0
 def set_actions(self, agent_group: AgentGroup, action: np.ndarray) -> None:
     self._assert_group_exists(agent_group)
     if agent_group not in self._env_state:
         return
     spec = self._env_specs[agent_group]
     expected_type = np.float32 if spec.is_action_continuous() else np.int32
     expected_shape = (self._env_state[agent_group].n_agents(),
                       spec.action_size)
     if action.shape != expected_shape:
         raise UnityActionException(
             "The group {0} needs an input of dimension {1} but received input of dimension {2}"
             .format(agent_group, expected_shape, action.shape))
     if action.dtype != expected_type:
         action = action.astype(expected_type)
     self._env_actions[agent_group] = action
示例#6
0
 def set_actions(self, behavior_name: BehaviorName, action: np.ndarray) -> None:
     self._assert_behavior_exists(behavior_name)
     if behavior_name not in self._env_state:
         return
     spec = self._env_specs[behavior_name]
     expected_type = np.float32 if spec.is_action_continuous() else np.int32
     expected_shape = (len(self._env_state[behavior_name][0]), spec.action_size)
     if action.shape != expected_shape:
         raise UnityActionException(
             f"The behavior {behavior_name} needs an input of dimension "
             f"{expected_shape} for (<number of agents>, <action size>) but "
             f"received input of dimension {action.shape}"
         )
     if action.dtype != expected_type:
         action = action.astype(expected_type)
     self._env_actions[behavior_name] = action
示例#7
0
 def set_actions(self, behavior_name: BehaviorName,
                 action: np.ndarray) -> None:
     self._assert_behavior_exists(behavior_name)
     if behavior_name not in self._env_state:
         return
     spec = self._env_specs[behavior_name]
     expected_type = np.float32 if spec.is_action_continuous() else np.int32
     expected_shape = (len(self._env_state[behavior_name][0]),
                       spec.action_size)
     if action.shape != expected_shape:
         raise UnityActionException(
             "The behavior {0} needs an input of dimension {1} but received input of dimension {2}"
             .format(behavior_name, expected_shape, action.shape))
     if action.dtype != expected_type:
         action = action.astype(expected_type)
     self._env_actions[behavior_name] = action
示例#8
0
 def _validate_action(self, actions: np.ndarray, n_agents: Optional[int],
                      name: str) -> np.ndarray:
     """
     Validates that action has the correct action dim
     for the correct number of agents and ensures the type.
     """
     if self.continuous_size > 0:
         _size = self.continuous_size
     else:
         _size = self.discrete_size
     _expected_shape = (n_agents, _size) if n_agents else (_size, )
     if actions.shape != _expected_shape:
         raise UnityActionException(
             f"The behavior {name} needs an input of dimension "
             f"{_expected_shape} for (<number of agents>, <action size>) but "
             f"received input of dimension {actions.shape}")
     _expected_type = np.float32 if self.is_continuous() else np.int32
     if actions.dtype != _expected_type:
         actions = actions.astype(_expected_type)
     return actions
示例#9
0
 def _assert_behavior_exists(self, behavior_name: str) -> None:
     if behavior_name not in self._env_specs:
         raise UnityActionException(
             f"The group {behavior_name} does not correspond to an existing "
             f"agent group in the environment"
         )
示例#10
0
 def _assert_behavior_exists(self, behavior_name: str) -> None:
     if behavior_name not in self._env_specs:
         raise UnityActionException(
             "The group {0} does not correspond to an existing agent group "
             "in the environment".format(behavior_name))
示例#11
0
 def _assert_group_exists(self, agent_group: str) -> None:
     if agent_group not in self._env_specs:
         raise UnityActionException(
             "The group {0} does not correspond to an existing agent group "
             "in the environment".format(agent_group))