opendilab / DI-engine

OpenDILab Decision AI Engine. The Most Comprehensive Reinforcement Learning Framework B.P.
https://di-engine-docs.readthedocs.io
Apache License 2.0
3k stars 366 forks source link

How to define close function in custom environment #815

Closed WangJuan6 closed 3 months ago

WangJuan6 commented 3 months ago

Dear Author, I referenced the documentation to construct the custom environment, but this documentation does not specify how to define the close function. I define the close function simply by:

def close(self):
        self.controller.stop()

This function will close the environment window.

When the done is True, it seems that this close function is not being called. Besides, the code in the following location:

https://github.com/opendilab/DI-engine/blob/7f951592a3d7b39c8fda44081fc40002f0ee27fc/ding/envs/env_manager/base_env_manager.py#L335

reinitializes the environment, and a new window is opened. Since the old window is not closed, memory overhead is increased. I need to call the close function to close the old window to limit memory overhead. I am not sure if my understanding is correct. Can you help me to solve this problem? Thanks

WangJuan6 commented 3 months ago

Sorry, I tried running the code for a while and I found I made a mistake: the environment reinitialization is not related to the value of Done.

When I run the code by python ding/example/ppo_with_complex_obs.py, the codes in the main function:

https://github.com/opendilab/DI-engine/blob/7f951592a3d7b39c8fda44081fc40002f0ee27fc/ding/example/ppo_with_complex_obs.py#L182

and

https://github.com/opendilab/DI-engine/blob/7f951592a3d7b39c8fda44081fc40002f0ee27fc/ding/example/ppo_with_complex_obs.py#L185

will initialize the environment. But, the code in

https://github.com/opendilab/DI-engine/blob/7f951592a3d7b39c8fda44081fc40002f0ee27fc/ding/envs/env_manager/base_env_manager.py#L335

will also reinitialize the new environment. The new environments will used to train and evaluate algorithms. So, the increased memory overhead comes from the environment initialized in the main function.

I am not sure if my understanding is correct, but I hope it can be of help.

Thanks

PaParaZz1 commented 3 months ago

For your case, you need to implement you custom env like a lazy_init way. This form is just to set necessary member variables in __init__ method instead of init environment. And the real initialization happens in the reset method. You can refer to the code here.

BTW, you can print(id(self)) in env to check whether a new env is launched or just a old env is reset.

WangJuan6 commented 3 months ago

Sorry, you mentioned it in the documentation, but I forgot. This problem has been solved, thank you so much.