Initializing environment is very easy in Gym and can be done via:
```python
import gym
env = gym.make('CartPole-v0')
```
## Interacting with the Environment
This example will run an instance of `CartPole-v0` environment for 1000 timesteps, rendering the environment at each step. You should see a window pop up rendering the classic [cart-pole](https://www.youtube.com/watch?v=J7E6_my3CHk&ab_channel=TylerStreeter) problem
```python
import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
env.render() # by default `mode="human"`(GUI), you can pass `mode="rbg_array"` to retrieve an image instead
env.step(env.action_space.sample()) # take a random action
-`observation` (**object**) : an environment specific object representation your observation of the environment after the step is taken. Its often aliased as the next state after the action has been taken
-`reward`(**float**) : immediate reward achieved by the previous action. Actual value and range will varies between environments, but the final goal is always to increase your total reward
-`done`(**boolean**): whether it’s time to `reset` the environment again. Most (but not all) tasks are divided up into well-defined episodes, and `done` being `True` indicates the episode has terminated. (For example, perhaps the pole tipped too far, or you lost your last life.)
-`info`(**dict**) : This provides general information helpful for debugging or additional information depending on the environment, such as the raw probabilities behind the environment’s last state change
## Additional Environment API
-`action_space`: this attribute gives the format of valid actions. It is of datatype `Space` provided by Gym. (For ex: If the action space is of type `Discrete` and gives the value `Discrete(2)`, this means there are two valid discrete actions 0 & 1 )
-`observation_space`: this attribute gives the format of valid observations. It if of datatype `Space` provided by Gym. (For ex: if the observation space is of type `Box` and the shape of the object is `(4,)`, this denotes a valid observation will be an array of 4 numbers). We can check the box bounds as well with attributes
- There are multiple types of Space types inherently available in gym:
-`Box` describes an n-dimensional continuous space. Its a bounded space where we can define the upper and lower limit which describe the valid values our observations can take.
-`Discrete` describes a discrete space where { 0, 1, ......., n-1} are the possible values our observation/action can take.
-`Dict` represents a dictionary of simple spaces.
-`Tuple` represents a tuple of simple spaces
-`MultiBinary` creates a n-shape binary space. Argument n can be a number or a `list` of numbers
-`MultiDiscrete` consists of a series of `Discrete` action spaces with different number of actions in each element
-`reward_range`: returns a tuple corresponding to min and max possible rewards. Default range is set to `[-inf,+inf]`. You can set it if you want a narrower range
-`close()` : Override close in your subclass to perform any necessary cleanup
-`seed()`: Sets the seed for this env's random number generator
### Unwrapping an environment
If you have a wrapped environment, and you want to get the unwrapped environment underneath all the layers of wrappers (so that you can manually call a function or change some underlying aspect of the environment), you can use the `.unwrapped` attribute. If the environment is already a base environment, the `.unwrapped` attribute will just return itself.
```python
base_env = env.unwrapped
```
### Vectorized Environment
Vectorized Environments are a way of stacking multiple independent environments, so that instead of training on one environment, our agent can train on multiple environments at a time. Each `observation` returned from a vectorized environment is a batch of observations for each sub-environment, and `step` is also expected to receive a batch of actions for each sub-environment.
**NOTE:** All sub-environments should share the identical observation and action spaces. A vector of multiple different environments is not supported
Gym Vector API consists of two types of vectorized environments:
-`AsyncVectorEnv` runs multiple environments in parallel. It uses `multiprocessing` processes, and pipes for communication.