src.plangym.vectorization.env
=============================

.. py:module:: src.plangym.vectorization.env

.. autoapi-nested-parse::

   Plangym API implementation.



Classes
-------

.. autoapisummary::

   src.plangym.vectorization.env.VectorizedEnv


Module Contents
---------------

.. py:class:: VectorizedEnv(env_class, name, frameskip = 1, autoreset = True, delay_setup = False, n_workers = 8, **kwargs)

   Bases: :py:obj:`plangym.core.PlangymEnv`, :py:obj:`abc.ABC`


   Base class that defines the API for working with vectorized environments.

   A vectorized environment allows to step several copies of the environment in parallel
   when calling ``step_batch``.

   It creates a local copy of the environment that is the target of all the other
   methods of :class:`PlanEnv`. In practise, a :class:`VectorizedEnv`
   acts as a wrapper of an environment initialized with the provided parameters when calling
   __init__.



   .. py:attribute:: _n_workers


   .. py:attribute:: _env_class


   .. py:attribute:: _env_kwargs


   .. py:attribute:: _plangym_env
      :type:  plangym.core.PlangymEnv | plangym.core.PlanEnv | None
      :value: None



   .. py:attribute:: SINGLETON


   .. py:attribute:: STATE_IS_ARRAY


   .. py:property:: n_workers
      :type: int

      Return the number of parallel processes that run ``step_batch`` in parallel.


   .. py:property:: plan_env
      :type: plangym.core.PlanEnv

      Environment that is wrapped by the current instance.


   .. py:property:: obs_shape
      :type: tuple[int]

      Tuple containing the shape of the observations returned by the Environment.


   .. py:property:: action_shape
      :type: tuple[int]

      Tuple containing the shape of the actions applied to the Environment.


   .. py:property:: action_space
      :type: gymnasium.spaces.Space

      Return the action_space of the environment.


   .. py:property:: observation_space
      :type: gymnasium.spaces.Space

      Return the observation_space of the environment.


   .. py:property:: gym_env
      Return the instance of the environment that is being wrapped by plangym.


   .. py:method:: __getattr__(item)

      Forward attributes to the wrapped environment.



   .. py:method:: split_similar_chunks(vector, n_chunks)
      :staticmethod:


      Split an indexable object into similar chunks.

      :param vector: Target indexable object to be split.
      :param n_chunks: Number of similar chunks.

      :returns: Generator that returns the chunks created after splitting the target object.



   .. py:method:: batch_step_data(actions, states, dt, batch_size)
      :classmethod:


      Make batches of step data to distribute across workers.



   .. py:method:: unpack_transitions(results, return_states)
      :staticmethod:


      Aggregate the results of stepping across diferent workers.



   .. py:method:: create_env_callable(**kwargs)

      Return a callable that initializes the environment that is being vectorized.



   .. py:method:: setup()

      Initialize the target environment with the parameters provided at __init__.



   .. py:method:: step(action, state = None, dt = 1, return_state = None)

      Step the environment applying a given action from an arbitrary state.

      If is not provided the signature matches the `step` method from OpenAI gym.

      :param action: Array containing the action to be applied.
      :param state: State to be set before stepping the environment.
      :param dt: Consecutive number of times to apply the given action.
      :param return_state: Whether to return the state in the returned tuple.                 If None, `step` will return the state if `state` was passed as a parameter.

      :returns: if states is `None` returns `(observs, rewards, ends, infos)` else
                `(new_states, observs, rewards, ends, infos)`.



   .. py:method:: reset(return_state = True)

      Reset the environment.

      Reset the environment and returns the first observation, or the first         (state, obs, info) tuple.

      :param return_state: If true return a also the initial state of the env.

      :returns: Observation of the environment if `return_state` is False. Otherwise,
                return (state, obs) after reset.



   .. py:method:: get_state()

      Recover the internal state of the simulation.

      A state completely describes the Environment at a given moment.

      Returns
          State of the simulation.




   .. py:method:: set_state(state)

      Set the internal state of the simulation.

      :param state: Target state to be set in the environment.



   .. py:method:: render(mode='human')

      Render the environment using OpenGL. This wraps the OpenAI render method.



   .. py:method:: get_image()

      Return a numpy array containing the rendered view of the environment.

      Square matrices are interpreted as a greyscale image. Three-dimensional arrays
      are interpreted as RGB images with channels (Height, Width, RGB)



   .. py:method:: step_with_dt(action, dt = 1)

      Step the environment ``dt`` times with the same action.

      Take ``dt`` simulation steps and make the environment evolve in multiples         of ``self.frameskip`` for a total of ``dt`` * ``self.frameskip`` steps.

      :param action: Chosen action applied to the environment.
      :param dt: Consecutive number of times that the action will be applied.

      :returns: If state is `None` returns `(observs, reward, terminal, info)`
                else returns `(new_state, observs, reward, terminal, info)`.



   .. py:method:: sample_action()

      Return a valid action that can be used to step the Environment.

      Implementing this method is optional, and it's only intended to make the
      testing process of the Environment easier.



   .. py:method:: step_batch(actions, states = None, dt = 1, return_state = None)

      Vectorized version of the ``step`` method.

      It allows to step a vector of states and actions. The signature and
      behaviour is the same as ``step``, but taking a list of states, actions
      and dts as input.

      :param actions: Iterable containing the different actions to be applied.
      :param states: Iterable containing the different states to be set.
      :param dt: int or array containing the frameskips that will be applied.
      :param return_state: Whether to return the state in the returned tuple.                 If None, `step` will return the state if `state` was passed as a parameter.

      :returns: if states is None returns `(observs, rewards, ends, infos)` else
                `(new_states, observs, rewards, ends, infos)`.



   .. py:method:: clone(**kwargs)

      Return a copy of the environment.



   .. py:method:: sync_states(state)
      :abstractmethod:


      Synchronize the workers' states with the state of `self.gym_env`.

      Set all the states of the different workers of the internal :class:`BatchEnv`
      to the same state as the internal :class:`Environment` used to apply the
      non-vectorized steps.



   .. py:method:: make_transitions(actions, states, dt, return_state = None)
      :abstractmethod:


      Implement the logic for stepping the environment in parallel.



