# \[RLlib\] How to access the environment of 'remote worker environments'?

**URL:** https://discuss.ray.io/t/rllib-how-to-access-the-environment-of-remote-worker-environments/247
**Category:** RLlib
**Created:** [December 15, 2020, 5:13pm UTC](https://discuss.ray.io/t/rllib-how-to-access-the-environment-of-remote-worker-environments/247 "2020-12-15T17:13:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zalador](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/zalador/32/157_2.png) [@zalador](https://discuss.ray.io/u/zalador)
#### Post date: [December 15, 2020, 5:13pm UTC](https://discuss.ray.io/t/rllib-how-to-access-the-environment-of-remote-worker-environments/247/1 "2020-12-15T17:13:03Z")

</div>

Hello,

My training setup consists of 1 worker with multiple remote environments but I am having trouble with accessing the environment. Following the curriculum example on docs(dot)ray(dot)io, remote vector environments do not have a ‘env’ attribute directly. As far as I can tell, remote vector environments are grouped together as a list per worker. There is a function ‘get\_unwrapped()’ in the [BaseEnv](https://github.com/ray-project/ray/blob/5a142d5bd6b0e143030d951da6cbd64108ffe24a/rllib/env/base_env.py#L18) class but that is not implemented for the remote environments. I tried accessing the ‘env’ [attribute](https://github.com/ray-project/ray/blob/5a142d5bd6b0e143030d951da6cbd64108ffe24a/rllib/env/remote_vector_env.py#L46) directly but the remote aspect of actors complicate things (at least for me).

Please see a simple reproduction script with the following error.

```auto
import ray, gym
from ray import tune
from ray.rllib.agents.ppo import DEFAULT_CONFIG as PPO_DEFAULT_CONFIG, PPOTrainer, PPOTFPolicy
from ray.rllib.agents.callbacks import DefaultCallbacks

class MyEnv(gym.Env):
    def __init__ (self, env_config):
        self.action_space = gym.spaces.Box(0, 1, shape=(1,))
        self.observation_space = gym.spaces.Box(0, 1, shape=(2,))
        self.phase = 1
    def reset(self):
        self.steps = 0
        return self.observation_space.sample()
    def step(self, action):
        self.steps += 1
        return self.observation_space.sample(), 0, self.steps > 10, {}
    def set_phase(self, phase):
        print("Phase set: {}".format(phase))
        self.phase = phase

class MyCallbacks(DefaultCallbacks):
    def on_train_result(self, **kwargs):
        """ Curriculum learning as seen in Ray docs """
        result = kwargs["result"]
        if result["episode_reward_mean"] > 200:
            phase = 0
        else:
            phase = 1
        trainer = kwargs["trainer"]

        trainer.workers.foreach_worker(
            lambda ev: ev.foreach_env(
                lambda env: env.set_phase(phase))) # Problem when remote_worker_envs = True

config = PPO_DEFAULT_CONFIG.copy()
config["env"] = MyEnv
config["num_workers"] = 0
config["num_envs_per_worker"] = 2
config["remote_worker_envs"] = True # Culprit
config["callbacks"] = MyCallbacks

ray.init(num_gpus=1)
ray.tune.run(
    PPOTrainer,
    config=config,
)

```

`AttributeError: 'RemoteVectorEnv' object has no attribute 'set_phase'`  
Instead, `remote_worker_envs = False` runs fine.

I tried accessing the env via the actors by:

```python
    def on_train_result(self, **kwargs):
        """ Curriculum learning as seen in Ray docs """
        result = kwargs["result"]
        if result["episode_reward_mean"] > 200:
            phase = 0
        else:
            phase = 1
        trainer = kwargs["trainer"]

        def update_phase(env):
            for actor in env.actors:
                actor.env.set_phase(phase)

        trainer.workers.foreach_worker(
            lambda ev: ev.foreach_env(
                lambda env: update_phase(env)))

```

But this gives the following error:  
`AttributeError: 'ActorHandle' object has no attribute 'env'`

The question is again: how do you access the individual environments?

Thanks in advance

---

<div class="post-metadata">

### Author: ![SebastianBo1995](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@SebastianBo1995](https://discuss.ray.io/u/SebastianBo1995)
#### Post date: [July 12, 2021, 3:54pm UTC](https://discuss.ray.io/t/rllib-how-to-access-the-environment-of-remote-worker-environments/247/2 "2021-07-12T15:54:23Z")

</div>

I am having the same question. I need to acess each environment of the remote workers after n ammount of episodes for debugging resons. Meaning, I need to manipulate the environment classes directly.  
Is this somehow possible?

---

<div class="post-metadata">

### Author: ![zalador](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/zalador/32/157_2.png) [@zalador](https://discuss.ray.io/u/zalador)
#### Post date: [July 12, 2021, 6:43pm UTC](https://discuss.ray.io/t/rllib-how-to-access-the-environment-of-remote-worker-environments/247/3 "2021-07-12T18:43:11Z")

</div>

Sadly I did not make any progress on this but I’m still interested in hearing if anyone does.

---

<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: [July 15, 2021, 10:13am UTC](https://discuss.ray.io/t/rllib-how-to-access-the-environment-of-remote-worker-environments/247/4 "2021-07-15T10:13:05Z")

</div>

Hey @zalador @SebastianBo1995 , I created a PR to fix this limitation. Could you take a look here?

> <https://github.com/ray-project/ray/pull/17118>
>
> RLlib currently does not support using custom Env APIs when using the \`remote\_wo…rker\_envs=True\` setting (which parallelizes stepping through the different sub-envs within a vectorized env). This PR fixes this limitation and adds an example script to demonstrate how this can be done.
> 
> Also see this discussion here:
> https://discuss.ray.io/t/rllib-how-to-access-the-environment-of-remote-worker-environments/247
> 
> 
> 
> \## Why are these changes needed?
> 
> 
> 
> \## Related issue number
> 
> 
> 
> \## Checks
> 
> \- \[x\] I've run \`scripts/format.sh\` to lint the changes in this PR.
> \- \[x\] I've included any doc changes needed for https://docs.ray.io/en/master/.
> \- \[x\] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
> \- Testing Strategy
> - \[x\] Unit tests
> - \[\] Release tests
> - \[\] This PR is not tested :(

There is also an example script that verifies it’s working with RLlib’s set\_task API (similar to your set\_phase method, which should also work, of course).

---

<div class="post-metadata">

### Author: ![zalador](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/zalador/32/157_2.png) [@zalador](https://discuss.ray.io/u/zalador)
#### Post date: [July 16, 2021, 7:47pm UTC](https://discuss.ray.io/t/rllib-how-to-access-the-environment-of-remote-worker-environments/247/5 "2021-07-16T19:47:08Z")

</div>

Hi,  
I am unable to test this for the foreseeable future. Thank you for the fix, hopefully someone else can verify.
