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","."]
code: def __init__(
      self,
      name: str = "CartPole-v0",
      control: str = "conformant",
      neural_factor: tp.Optional[int] = 1,
      randomized: bool = True,
      optimization_scale: int = 0,
      greedy_bias: bool = False,
      sparse_limit: tp.Optional[
          int
      ] = None,  # if not None, we penalize solutions with more than sparse_limit weights !=0
  ) -> None:
      self.num_calls = 0
      self.optimization_scale = optimization_scale
      self.stochastic_problem = "stoc" in name
      self.greedy_bias = greedy_bias
      self.sparse_limit = sparse_limit
      if "conformant" in control or control == "linear":
          assert neural_factor is None
      if os.name == "nt":
          raise ng.errors.UnsupportedExperiment(
              "Windows is not supported"
          )
      # self.env = None  # self.create_env() let us have no self.env
  
      # Build various attributes.
      self.short_name = name  # Just the environment name.
      env = self.create_env()
      self.name = name + "__" + control + "__" + str(neural_factor)
      if sparse_limit is not None:
          self.name += f"__{sparse_limit}"
      if randomized:
          self.name += "_unseeded"
      self.randomized = randomized
      try:
          try:
              self.num_time_steps = (
                  env._max_episode_steps
              )  # I know! This is a private variable.
          except (
              AttributeError
          ):  # Second chance! Some environments use self.horizon.
              self.num_time_steps = env.horizon
      except (
          AttributeError
      ):  # Not all environements have a max number of episodes!
          assert any(x in name for x in NO_LENGTH), name
          if (
              "LANM" not in name
          ):  # Most cases: let's say 5000 time steps.
              self.num_time_steps = (
                  200 if control == "conformant" else 5000
              )
          else:  # LANM is a special case with 3000 time steps.
              self.num_time_steps = 3000
      self.gamma = 0.995 if "LANM" in name else 1.0
      self.neural_factor = neural_factor
  
      # Infer the action space.
      self.arities = []
      if isinstance(env.action_space, gym.spaces.Tuple):
          assert all(
              isinstance(p, gym.spaces.MultiDiscrete)
              for p in env.action_space
          ), f"{name} has a too complicated structure."
          self.arities = [p.nvec for p in env.action_space]
          if control == "conformant":
              output_dim = sum(len(a) for a in self.arities)
          else:
              output_dim = sum(sum(a) for a in self.arities)
          output_shape = (output_dim,)
          discrete = True
      elif isinstance(env.action_space, gym.spaces.Discrete):
          output_dim = env.action_space.n
          output_shape = (output_dim,)
          discrete = True
          assert output_dim is not None, env.action_space.n
      else:  # Continuous action space
          output_shape = env.action_space.shape
          if output_shape is None:
              output_shape = tuple(
                  np.asarray(env.action_space.sample()).shape
              )
          # When the shape is not available we might do:
          # output_shape = tuple(np.asarray(env.action_space.sample()).shape)
          discrete = False
          output_dim = np.prod(output_shape)
  
      self.discrete = discrete
  
      # Infer the observation space.
      assert (
          env.observation_space is not None or "llvm" in name
      ), "An observation space should be defined."
      if (
          env.observation_space is not None
          and env.observation_space.dtype == int
      ):
          # Direct inference for corner cases:
          # if "int" in str(type(o)):
          input_dim = env.observation_space.n
          assert input_dim is not None, env.observation_space.n
          self.discrete_input = True
      else:
          input_dim = (
              np.prod(env.observation_space.shape)
              if env.observation_space is not None
              else 0
          )
          if input_dim is None:
              o = env.reset()
              input_dim = np.prod(np.asarray(o).shape)
          self.discrete_input = False
  
      # Infer the action type.
      a = env.action_space.sample()
      self.action_type = type(a)
      self.subaction_type = None
      if hasattr(a, "__iter__"):
          self.subaction_type = type(a[0])
  
      # Prepare the policy shape.
      if neural_factor is None:
          assert (
              control == "linear" or "conformant" in control
          ), f"{control} has neural_factor {neural_factor}"
          neural_factor = 1
      self.output_shape = output_shape
      self.num_stacking = 1
      self.memory_len = (
          neural_factor * input_dim if "memory" in control else 0
      )
      self.extended_input_len = (
          (input_dim + output_dim) * self.num_stacking
          if "stacking" in control
          else 0
      )
      input_dim = input_dim + self.memory_len + self.extended_input_len
      self.extended_input = np.zeros(self.extended_input_len)
      output_dim = output_dim + self.memory_len
      self.input_dim = input_dim
      self.output_dim = output_dim
      self.num_neurons = neural_factor * (
          input_dim - self.extended_input_len
      )
      self.num_internal_layers = 1 if "semi" in control else 3
      internal = (
          self.num_internal_layers * (self.num_neurons**2)
          if "deep" in control
          else 0
      )
      unstructured_neural_size = (
          output_dim * self.num_neurons
          + self.num_neurons * (input_dim + 1)
          + internal,
      )
      neural_size = unstructured_neural_size
      if self.greedy_bias:
          neural_size = (unstructured_neural_size[0] + 1,)
          assert "multi" not in control
          assert "structured" not in control
      assert (
          control in CONTROLLERS or control == "conformant"
      ), f"{control} not known as a form of control"
      self.control = control
      if "neural" in control:
          self.first_size = self.num_neurons * (self.input_dim + 1)
          self.second_size = self.num_neurons * self.output_dim
          self.first_layer_shape = (
              self.input_dim + 1,
              self.num_neurons,
          )
          self.second_layer_shape = (self.num_neurons, self.output_dim)
      shape_dict = {
          "conformant": (self.num_time_steps,) + output_shape,
          "stochastic_conformant": (self.num_time_steps,)
          + output_shape,
          "linear": (input_dim + 1, output_dim),
          "multi_neural": (min(self.num_time_steps, 50),)
          + unstructured_neural_size,
      }
      shape = tuple(map(int, shape_dict.get(control, neural_size)))
      self.policy_shape = shape if "structured" not in control else None
  
      # Create the parametrization.
      parametrization = parameter.Array(shape=shape).set_name(
          "ng_default"
      )
      if sparse_limit is not None:
          parametrization1 = parameter.Array(shape=shape)
          repetitions = int(np.prod(shape))
          assert isinstance(repetitions, int), f"{repetitions}"
          parametrization2 = ng.p.Choice([0, 1], repetitions=repetitions)  # type: ignore
          parametrization = ng.p.Instrumentation(  # type: ignore
              weights=parametrization1,
              enablers=parametrization2,
          )
          parametrization.set_name("ng_sparse" + str(sparse_limit))
          assert (
              "conformant" not in control
              and "structured" not in control
          )
  
      if (
          "structured" in control
          and "neural" in control
          and "multi" not in control
      ):
          parametrization = parameter.Instrumentation(  # type: ignore
              parameter.Array(
                  shape=tuple(map(int, self.first_layer_shape))
              ),
              parameter.Array(
                  shape=tuple(map(int, self.second_layer_shape))
              ),
          ).set_name("ng_struct")
      elif "conformant" in control:
          try:
              if (
                  env.action_space.low is not None
                  and env.action_space.high is not None
              ):
                  low = np.repeat(
                      np.expand_dims(env.action_space.low, 0),
                      self.num_time_steps,
                      axis=0,
                  )
                  high = np.repeat(
                      np.expand_dims(env.action_space.high, 0),
                      self.num_time_steps,
                      axis=0,
                  )
                  init = 0.5 * (low + high)
                  parametrization = parameter.Array(init=init)
                  parametrization.set_bounds(low, high)
          except (
              AttributeError
          ):  # Not all env.action_space have a low and a high.
              pass
          if self.subaction_type == int:
              parametrization.set_integer_casting()
          parametrization.set_name("conformant")
  
      # Now initializing.
      super().__init__(
          self.sparse_gym_multi_function if sparse_limit is not None else self.gym_multi_function,  # type: ignore
          parametrization=parametrization,
      )
      self.greedy_coefficient = 0.0
      self.parametrization.function.deterministic = False
      self.archive: tp.List[tp.Any] = []
      self.mean_loss = 0.0
      self.num_losses = 0
  
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 4 to create a new GymMulti object from inherited base classes: ExperimentFunction with example: obj = GymMulti(name, control, neural_factor, randomized, optimization_scale, greedy_bias, sparse_limit)