# Is env\_id (of an episode) just equal to index of sub-env?

**URL:** https://discuss.ray.io/t/is-env-id-of-an-episode-just-equal-to-index-of-sub-env/4672
**Category:** RLlib
**Created:** [January 12, 2022, 11:07am UTC](https://discuss.ray.io/t/is-env-id-of-an-episode-just-equal-to-index-of-sub-env/4672 "2022-01-12T11:07:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)
#### Post date: [January 12, 2022, 11:07am UTC](https://discuss.ray.io/t/is-env-id-of-an-episode-just-equal-to-index-of-sub-env/4672/1 "2022-01-12T11:07:35Z")

</div>

Hi folks,

RLlib always converts each supported env to a vectorized BaseEnv independent of `"num_envs_per_worker"`. Let’s assume that `"num_envs_per_worker" = 4`, i.e. there are 4 replicates of the env (sub-envs).  
Is my assumption correct that, in general, the env\_id (of an episode) is just the index of the corresponding sub-env?  
If that’s right, then it should be possible to always access all the env’s state and data in `on_episode_step()` via `env_running_this_episode = base_env.envs[env_id]`. I ask this, because I look for a way to log any env/episode data I want to.

---

<div class="post-metadata">

### Author: ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)
#### Post date: [January 12, 2022, 12:35pm UTC](https://discuss.ray.io/t/is-env-id-of-an-episode-just-equal-to-index-of-sub-env/4672/2 "2022-01-12T12:35:32Z")

</div>

Hey @klausk55 , great question! Episode IDs are different from (sub) Env IDs.  
You are right that the sub Env IDs inside the vectorized BaseEnv are (normally) just ints ranging from `0` to `[num_envs_per_worker - 1]`.  
But episodes have independent and rather random IDs, generated when an episode is created:

```auto
From the Episode c'tor:

        ...
        self.episode_id: int = random.randrange(2e9)
        self.env_id = env_id # <- given by caller of the Episode's c'tor
        ....

```

---

<div class="post-metadata">

### Author: ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)
#### Post date: [January 12, 2022, 12:37pm UTC](https://discuss.ray.io/t/is-env-id-of-an-episode-just-equal-to-index-of-sub-env/4672/3 "2022-01-12T12:37:38Z")

</div>

Yeah, you could do this in your implementation of `on_episode_step`:

```auto
on_episode_step(self,
                        *,
                        worker: "RolloutWorker",
                        base_env: BaseEnv,
                        policies: Optional[Dict[PolicyID, Policy]] = None,
                        episode: Episode,
                        **kwargs) -> None:
    ....
    sub_env_running_this_episode = base_env.get_sub_environments()[episode.env_id]
    # `sub_env_running_this_episode` is (most likely) a gym.Env instance now.

```

---

<div class="post-metadata">

### Author: ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)
#### Post date: [January 12, 2022, 12:59pm UTC](https://discuss.ray.io/t/is-env-id-of-an-episode-just-equal-to-index-of-sub-env/4672/4 "2022-01-12T12:59:07Z")

</div>

Thanks @sven1977 for your confirmation!
