2019-04-19 23:20:26 +02:00
import gym
2021-07-28 22:21:47 -04:00
import warnings
2016-12-27 12:15:00 -08:00
2019-04-19 23:20:26 +02:00
class TimeLimit ( gym . Wrapper ) :
def __init__ ( self , env , max_episode_steps = None ) :
2017-01-03 23:51:25 -08:00
super ( TimeLimit , self ) . __init__ ( env )
2021-07-28 22:21:47 -04:00
warnings . warn ( " Gym \' s internal preprocessing wrappers are now deprecated. While they will continue to work for the foreseeable future, we strongly recommend using SuperSuit instead: https://github.com/PettingZoo-Team/SuperSuit " )
2019-06-08 12:36:02 -04:00
if max_episode_steps is None and self . env . spec is not None :
2019-04-19 23:20:26 +02:00
max_episode_steps = env . spec . max_episode_steps
2019-06-08 12:36:02 -04:00
if self . env . spec is not None :
self . env . spec . max_episode_steps = max_episode_steps
2017-01-03 23:51:25 -08:00
self . _max_episode_steps = max_episode_steps
2019-04-19 23:20:26 +02:00
self . _elapsed_steps = None
2016-12-27 12:15:00 -08:00
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
def step ( self , action ) :
2021-07-29 02:26:34 +02:00
assert (
self . _elapsed_steps is not None
) , " Cannot call env.step() before calling reset() "
2016-12-27 12:15:00 -08:00
observation , reward , done , info = self . env . step ( action )
self . _elapsed_steps + = 1
2019-04-19 23:20:26 +02:00
if self . _elapsed_steps > = self . _max_episode_steps :
2021-07-29 02:26:34 +02:00
info [ " TimeLimit.truncated " ] = not done
2019-04-19 23:20:26 +02:00
done = True
2016-12-27 12:15:00 -08:00
return observation , reward , done , info
2019-02-26 20:07:22 -05:00
def reset ( self , * * kwargs ) :
2016-12-27 12:15:00 -08:00
self . _elapsed_steps = 0
2019-02-26 20:07:22 -05:00
return self . env . reset ( * * kwargs )