浏览代码

discrete/contionuous unity envs train

/develop/action-spec-gym
Andrew Cohen 4 年前
当前提交
eef14922
共有 3 个文件被更改,包括 15 次插入12 次删除
  1. 4
      ml-agents/mlagents/trainers/env_manager.py
  2. 10
      ml-agents/mlagents/trainers/tests/simple_test_envs.py
  3. 13
      ml-agents/mlagents/trainers/torch/utils.py

4
ml-agents/mlagents/trainers/env_manager.py


def action_buffers_from_numpy_dict(
action_dict: Dict[str, np.ndarray]
) -> ActionBuffers:
continuous: np.ndarray = [np.array([])]
discrete: np.ndarray = [np.array([])]
continuous: np.ndarray = None
discrete: np.ndarray = None
if "continuous_action" in action_dict:
continuous = action_dict["continuous_action"]
if "discrete_action" in action_dict:

10
ml-agents/mlagents/trainers/tests/simple_test_envs.py


def _take_action(self, name: str) -> bool:
deltas = []
_act = self.action[name]
for _disc in _act.discrete[0]:
deltas.append(1 if _disc else -1)
for _cont in _act.continuous[0]:
deltas.append(_cont)
if _act.discrete is not None:
for _disc in _act.discrete[0]:
deltas.append(1 if _disc else -1)
if _act.continuous is not None:
for _cont in _act.continuous[0]:
deltas.append(_cont)
for i, _delta in enumerate(deltas):
_delta = clamp(_delta, -self.step_size, self.step_size)
self.positions[name][i] += _delta

13
ml-agents/mlagents/trainers/torch/utils.py


tensor_list: List[torch.Tensor], action_spec: ActionSpec
) -> "AgentAction":
continuous: torch.Tensor = None
discrete: List[torch.Tensor] = None
discrete: List[torch.Tensor] = None # type: ignore
_offset = 0
if action_spec.continuous_size > 0:
continuous = tensor_list[0]

@staticmethod
def from_dict(buff: Dict[str, np.ndarray]) -> "AgentAction":
continuous: torch.Tensor = None
discrete: List[torch.Tensor] = None
discrete: List[torch.Tensor] = None # type: ignore
discrete_tensor = ModelUtils.list_to_tensor(buff["discrete_action"])
discrete_tensor = ModelUtils.list_to_tensor(
buff["discrete_action"], dtype=torch.long
)
discrete = [
discrete_tensor[..., i] for i in range(discrete_tensor.shape[-1])
]

@property
def discrete_tensor(self):
return torch.stack(self.discrete_list, dim=-1)
return torch.cat([_disc.unsqueeze(-1) for _disc in self.discrete_list], dim=1)
@property
def all_discrete_tensor(self):

all_log_prob_list: List[torch.Tensor] = None,
) -> "ActionLogProbs":
continuous: torch.Tensor = None
discrete: List[torch.Tensor] = None
discrete: List[torch.Tensor] = None # type: ignore
_offset = 0
if action_spec.continuous_size > 0:
continuous = log_prob_list[0]

@staticmethod
def from_dict(buff: Dict[str, np.ndarray]) -> "ActionLogProbs":
continuous: torch.Tensor = None
discrete: List[torch.Tensor] = None
discrete: List[torch.Tensor] = None # type: ignore
if "continuous_log_probs" in buff:
continuous = ModelUtils.list_to_tensor(buff["continuous_log_probs"])
if "discrete_log_probs" in buff:

正在加载...
取消
保存