Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenDRAG committed Mar 21, 2021
1 parent 28b16bc commit 8d680fe
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
18 changes: 9 additions & 9 deletions tianshou/policy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def __init__(
self.agent_id = 0
self.updating = False
self.action_scaling = action_scaling
# can be either "clip", "tanh", or empty string for doing nothing
assert action_bound_method in ["", "clip", "tanh"]
# can be one of ("clip", "tanh", ""), empty string means no bounding
self.action_bound_method = action_bound_method
assert action_bound_method in ("", "clip", "tanh")
self._compile()

def set_agent_id(self, agent_id: int) -> None:
Expand Down Expand Up @@ -121,24 +121,24 @@ def forward(
pass

def map_action(self, act: Union[Batch, np.ndarray]) -> Union[Batch, np.ndarray]:
"""Map raw network output action to action range in gym's env.action_space.
"""Map raw network output to action range in gym's env.action_space.
This function is called in :meth:`~tianshou.data.Collector.collect` and only
affects action sending to env. Remapped action will not be stored in buffer
and thus can be viewed as a part of env (a black box action transformation).
Basically it assumes the original action range is [-1, 1] or (-inf, inf), and
maps them to [action_space.low, action_space.high] by cliping, applying tanh
activation, or linear scaling.
Action mapping includes 2 standard procedures: bounding and scaling. Bounding
procedure expects original action range is (-inf, inf) and maps them to [-1, 1],
while scaling procedure expects original action range is (-1, 1) and maps them
to [action_space.low, action_space.high]. Bounding procedure is applied first.
:param act: a data batch or numpy.ndarray which is the action taken by
policy.forward.
:return: action in the same form of input "act" but remap to the target action
space.
"""
if isinstance(self.action_space, gym.spaces.Box) and \
isinstance(act, np.ndarray):
if isinstance(self.action_space, gym.spaces.Box):
# currently this action mapping only supports np.ndarray action
if self.action_bound_method == "clip":
act = np.clip(act, -1, 1)
Expand All @@ -148,7 +148,7 @@ def map_action(self, act: Union[Batch, np.ndarray]) -> Union[Batch, np.ndarray]:
assert np.all(act >= -1) and np.all(act <= 1), \
"action scaling only accepts raw action range = [-1, 1]"
low, high = self.action_space.low, self.action_space.high
act = low + (high - low) * (act + 1) / 2
act = low + (high - low) * (act + 1.) / 2.
return act

def process_fn(
Expand Down
2 changes: 1 addition & 1 deletion tianshou/policy/modelfree/a2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class A2CPolicy(PGPolicy):
either "clip" (for simply clipping the action), "tanh" (for applying tanh
squashing) for now, or empty string for no bounding. Default to "clip".
:param Optional[gym.Space] action_space: env's action space, mandatory if you want
to use option action_scaling/action_bound_method. Default to None.
to use option 'action_scaling' or 'action_bound_method'. Default to None.
.. seealso::
Expand Down
2 changes: 1 addition & 1 deletion tianshou/policy/modelfree/ddpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DDPGPolicy(BasePolicy):
either "clip" (for simply clipping the action), "tanh" (for applying tanh
squashing) for now, or empty string for no bounding. Default to "clip".
:param Optional[gym.Space] action_space: env's action space, mandatory if you want
to use option action_scaling/action_bound_method. Default to None.
to use option 'action_scaling' or 'action_bound_method'. Default to None.
.. seealso::
Expand Down
2 changes: 1 addition & 1 deletion tianshou/policy/modelfree/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PGPolicy(BasePolicy):
either "clip" (for simply clipping the action), "tanh" (for applying tanh
squashing) for now, or empty string for no bounding. Default to "clip".
:param Optional[gym.Space] action_space: env's action space, mandatory if you want
to use option action_scaling/action_bound_method. Default to None.
to use option 'action_scaling' or 'action_bound_method'. Default to None.
.. seealso::
Expand Down
2 changes: 1 addition & 1 deletion tianshou/policy/modelfree/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PPOPolicy(PGPolicy):
either "clip" (for simply clipping the action), "tanh" (for applying tanh
squashing) for now, or empty string for no bounding. Default to "clip".
:param Optional[gym.Space] action_space: env's action space, mandatory if you want
to use option action_scaling/action_bound_method. Default to None.
to use option 'action_scaling' or 'action_bound_method'. Default to None.
.. seealso::
Expand Down
2 changes: 1 addition & 1 deletion tianshou/policy/modelfree/sac.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SACPolicy(DDPGPolicy):
either "clip" (for simply clipping the action), "tanh" (for applying tanh
squashing) for now, or empty string for no bounding. Default to "tanh".
:param Optional[gym.Space] action_space: env's action space, mandatory if you want
to use option action_scaling/action_bound_method. Default to None.
to use option 'action_scaling' or 'action_bound_method'. Default to None.
.. seealso::
Expand Down
2 changes: 1 addition & 1 deletion tianshou/policy/modelfree/td3.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TD3Policy(DDPGPolicy):
either "clip" (for simply clipping the action), "tanh" (for applying tanh
squashing) for now, or empty string for no bounding. Default to "clip".
:param Optional[gym.Space] action_space: env's action space, mandatory if you want
to use option action_scaling/action_bound_method. Default to None.
to use option 'action_scaling' or 'action_bound_method'. Default to None.
.. seealso::
Expand Down

0 comments on commit 8d680fe

Please sign in to comment.