class_docstr: Class for converting a gym environment, a controller style, and others into a black-box optimization benchmark.
class_docstr_tok: ["Class","for","converting","a","gym","environment",",","a","controller","style",",","and","others","into","a","black-box","optimization","benchmark","."]
docstr: Transforms an action into an action of type as expected by the gym step function.
docstr_tok: ["Transforms","an","action","into","an","action","of","type","as","expected","by","the","gym","step","function","."]
code: def action_cast(self, a, env):
      """Transforms an action into an action of type as expected by the gym step function."""
      if self.action_type == tuple:
          a = self.discretize(a, env)
          return tuple(a)
      if type(a) == np.float64:
          a = np.asarray((a,))
      if self.discrete:
          a = self.discretize(a, env)
      else:
          if (
              type(a) != self.action_type
          ):  # , f"{a} does not have type {self.action_type}"
              a = self.action_type(a)
          try:
              if (
                  env.action_space.low is not None
                  and env.action_space.high is not None
              ):
                  # Projection to [0, 1]
                  a = 0.5 * (1.0 + np.tanh(a))
                  # Projection to the right space.
                  a = (
                      env.action_space.low
                      + (env.action_space.high - env.action_space.low)
                      * a
                  )
          except AttributeError:
              pass  # Sometimes an action space has no low and no high.
          if self.subaction_type is not None:
              if type(a) == tuple:
                  a = tuple(int(_a + 0.5) for _a in a)
              else:
                  for i in range(len(a)):
                      a[i] = self.subaction_type(a[i])
      if not np.isscalar(a):
          a = np.asarray(a, dtype=env.action_space.sample().dtype)
      assert (
          type(a) == self.action_type
      ), f"{a} should have type {self.action_type} "
      # assert env.action_space.contains(env.action_space.sample())
      try:
          assert env.action_space.contains(a), (
              f"In {self.name}, high={env.action_space.high} low={env.action_space.low} {a} "
              f"is not sufficiently close to {[env.action_space.sample() for _ in range(10)]}"
              f"Action space = {env.action_space} (sample has type {type(env.action_space.sample())})"
              f"and a={a} with type {type(a)}"
          )
      except AttributeError:
          pass  # Not all env can do "contains".
      return a
  
imports: import os
  import copy
  import scipy.stats
  import typing
  import numpy
  import gym
  import nevergrad
  from nevergrad.parametrization import parameter
  from ..base import ExperimentFunction
label_desc: Use image node_id 11 for calling the GymMulti obj's underlying member method code with example usage: obj.action_cast(a, env) and returns: a, tuple