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: Single simulation with parametrization x.
docstr_tok: ["Single","simulation","with","parametrization","x","."]
code: def gym_simulate(
      self,
      x: np.ndarray,
      seed: int,
      limited_fidelity: bool = True,
  ):
      """Single simulation with parametrization x."""
      current_time_index = 0
      current_reward = 0.0
      current_observations: tp.List[tp.Any] = []
      current_actions: tp.List[tp.Any] = []
      try:
          if self.policy_shape is not None:
              x = x.reshape(self.policy_shape)
      except:
          assert (
              False
          ), f"x has shape {x.shape} and needs {self.policy_shape} for control {self.control}"
      assert (
          seed == 0 or self.control != "conformant" or self.randomized
      )
      env = self.create_env()
      env.seed(seed=seed)
      o = env.reset()
      control = self.control
      if (
          "conformant" in control
      ):  # Conformant planning: we just optimize a sequence of actions. No reactivity.
          return self.gym_conformant(x, env)
      if (
          "scrambled" in control
      ):  # We shuffle the variables, typically so that progressive methods optimize
          # everywhere in parallel instead of focusing on one single layer for years.
          x = x.copy()
          np.random.RandomState(1234).shuffle(x)
      if (
          "noisy" in control
      ):  # We add a randomly chosen but fixed perturbation of the x, i.e. we do not
          # start at 0.
          x = x + 0.01 * np.random.RandomState(1234).normal(
              size=x.shape
          )
      reward = 0.0
      memory = np.zeros(self.memory_len)
      for i in range(self.num_time_steps):
          # Actual loop over time steps!
          if self.discrete_input:
              obs = np.zeros(
                  shape=self.input_dim
                  - self.extended_input_len
                  - len(memory)
              )
              obs[o] = 1
              o = obs
          previous_o = np.asarray(o)
          o = np.concatenate(
              [previous_o.ravel(), memory.ravel(), self.extended_input]
          )
          assert len(o) == self.input_dim, (
              f"o has shape {o.shape} whereas input_dim={self.input_dim} "
              f"({control} / {env} {self.name})"
          )
          a, memory = self.neural(
              x[i % len(x)] if "multi" in control else x, o
          )
          a = self.action_cast(a, env)
          try:
              o, r, done, _ = self.step(
                  a, env
              )  # Outputs = observation, reward, done, info.
              current_time_index += 1
              if (
                  "multifidLANM" in self.name
                  and current_time_index > 500
                  and limited_fidelity
              ):
                  done = True
              current_reward *= self.gamma
              current_reward += r
              current_observations += [np.asarray(o).copy()]
              current_actions += [np.asarray(a).copy()]
              if (
                  done and "stacking" in self.control
              ):  # Only the method which do a stacking of heuristic + memory into the
                  # observation need archiving.
                  self.archive_observations(
                      current_actions,
                      current_observations,
                      current_reward,
                  )
          except AssertionError:  # Illegal action.
              return 1e20 / (
                  1.0 + i
              )  # We encourage late failures rather than early failures.
          if "stacking" in control:
              attention_a = self.heuristic(
                  o, current_observations
              )  # Best so far, or something like that heuristically derived.
              a = (
                  attention_a
                  if attention_a is not None
                  else 0.0 * np.asarray(a)
              )
              previous_o = previous_o.ravel()
              additional_input = np.concatenate(
                  [np.asarray(a).ravel(), previous_o]
              )
              shift = len(additional_input)
              self.extended_input[
                  : (len(self.extended_input) - shift)
              ] = self.extended_input[shift:]
              self.extended_input[
                  (len(self.extended_input) - shift) :
              ] = additional_input
          reward += r
          if done:
              break
      return -reward
  
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 14 for calling the GymMulti obj's underlying member method code with example usage: obj.gym_simulate(x, seed, limited_fidelity) and returns: unknown, self, unknown