# Toy example for using ExternalEnv API

**URL:** <https://discuss.ray.io/t/toy-example-for-using-externalenv-api/3335>\
**Category:** RLlib\
**Created:** [August 24, 2021, 8:36am UTC](https://discuss.ray.io/t/toy-example-for-using-externalenv-api/3335 "2021-08-24T08:36:09Z")\
**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:** [August 24, 2021, 8:36am UTC](https://discuss.ray.io/t/toy-example-for-using-externalenv-api/3335/1 "2021-08-24T08:36:09Z")

</div>

Hello people,

Is my toy example a correct and possible way how one can use the _ExternalEnv_ API?

test.py:

```auto
from typing import Dict
import ray
from ray.rllib.agents.ppo.ppo import PPOTrainer
from ray.tune.logger import pretty_print
from ray.tune.registry import register_env

from gym.spaces import Discrete, Box

from toy_external_env import ToyExternalEnv

def env_creator(config: Dict):
    action_space = Discrete(2)
    observation_space = Box(-10, 10, (4,))
    return ToyExternalEnv(action_space, observation_space)

if __name__ == " __main__":
    
    ray.init()

    register_env("TEE", env_creator)

    trainer = PPOTrainer(config= {"framework": "tf2"}, env="TEE")
    results = trainer.train()
    pretty_print(results)
    print("Training end")

```

toy\_external\_env.py:

```auto
from typing import Any, Tuple
from ray.rllib.env.external_env import ExternalEnv
from ray.rllib.utils.annotations import override

import gym
import numpy as np

class ToyExternalEnv(ExternalEnv):

    def __init__ (self, action_space: gym.Space, observation_space: gym.Space,
                 max_concurrent: int = 100):
        super(). __init__ (action_space, observation_space,
                         max_concurrent=max_concurrent)

        # self.simulator = DES()
        self.simulator = gym.make("CartPole-v0")

    @override(ExternalEnv)
    def run(self):
        obs = self.simulator.reset()
        self.simulator.render()
        eid = self.start_episode()

        while True:
            action = self.get_action(eid, obs)
            obs, reward, done, info = self.simulator.step(action)
            self.simulator.render()
            self.log_returns(eid, reward, info)
            if done:
                self.end_episode(eid, obs)
                obs = self.simulator.reset()
                eid = self.start_episode()

```

---

<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:** [September 9, 2021, 12:15pm UTC](https://discuss.ray.io/t/toy-example-for-using-externalenv-api/3335/2 "2021-09-09T12:15:56Z")

</div>

Do all agree with me or are there any objections (other ideas) how one can use the _ExternalEnv API_?

---

<div class="post-metadata">

**Author:** ![hermmanhender](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/hermmanhender/32/5691_2.png) [@hermmanhender](https://discuss.ray.io/u/hermmanhender)\
**Post date:** [February 18, 2022, 7:50pm UTC](https://discuss.ray.io/t/toy-example-for-using-externalenv-api/3335/3 "2022-02-18T19:50:18Z")

</div>

Hello @klausk55 , I’m new in RLlib and I’m trying to run an ExternalEnv with Tune. Could you try the code here showed? Wer you find a better way to do that?  
Tomorrow I will try the way that you expose anyway. Thanks!

---

<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:** [February 24, 2022, 10:19am UTC](https://discuss.ray.io/t/toy-example-for-using-externalenv-api/3335/4 "2022-02-24T10:19:11Z")

</div>

Hey @hermmanhender,

my experiences in combination with Tune are almost zero, but yes this code example (toy example) here worked for me when I’d tried it with RLlib alone.  
I can’t tell you whether there is a better way or not, perhaps there is one 😅  
I’d started to do my simulations with an _ExternalEnv_ (in the same manner as above), but in the meantime I’ve switched to use a normal _MultiagentEnv_ resp. _GymEnv_ and integrated my discrete event simulation therein.
