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
|
|
|
"""
|
|
|
|
Currently disabled since this was done in a very poor way
|
|
|
|
Hashed str representation of objects
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2016-06-17 13:23:49 -07:00
|
|
|
import json
|
|
|
|
import hashlib
|
|
|
|
import os
|
2018-02-27 10:17:33 -08:00
|
|
|
|
2017-02-11 22:17:02 -08:00
|
|
|
import pytest
|
2018-02-27 10:17:33 -08:00
|
|
|
from gym import spaces, logger
|
2017-02-11 22:17:02 -08:00
|
|
|
from gym.envs.tests.spec_list import spec_list
|
2016-06-17 13:23:49 -07:00
|
|
|
|
|
|
|
DATA_DIR = os.path.dirname(__file__)
|
|
|
|
ROLLOUT_STEPS = 100
|
|
|
|
episodes = ROLLOUT_STEPS
|
|
|
|
steps = ROLLOUT_STEPS
|
|
|
|
|
2016-10-21 19:00:36 -07:00
|
|
|
ROLLOUT_FILE = os.path.join(DATA_DIR, 'rollout.json')
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2016-10-31 23:28:25 -07:00
|
|
|
if not os.path.isfile(ROLLOUT_FILE):
|
2019-02-09 02:58:51 +02:00
|
|
|
with open(ROLLOUT_FILE, "w") as outfile:
|
|
|
|
json.dump({}, outfile, indent=2)
|
2016-06-17 13:23:49 -07:00
|
|
|
|
|
|
|
def hash_object(unhashed):
|
2019-02-09 02:58:51 +02:00
|
|
|
return hashlib.sha256(str(unhashed).encode('utf-16')).hexdigest() # This is really bad, str could be same while values change
|
2016-06-17 13:23:49 -07:00
|
|
|
|
|
|
|
def generate_rollout_hash(spec):
|
2019-02-09 02:58:51 +02:00
|
|
|
spaces.seed(0)
|
|
|
|
env = spec.make()
|
|
|
|
env.seed(0)
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
observation_list = []
|
|
|
|
action_list = []
|
|
|
|
reward_list = []
|
|
|
|
done_list = []
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
total_steps = 0
|
|
|
|
for episode in range(episodes):
|
|
|
|
if total_steps >= ROLLOUT_STEPS: break
|
|
|
|
observation = env.reset()
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
for step in range(steps):
|
|
|
|
action = env.action_space.sample()
|
|
|
|
observation, reward, done, _ = env.step(action)
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
action_list.append(action)
|
|
|
|
observation_list.append(observation)
|
|
|
|
reward_list.append(reward)
|
|
|
|
done_list.append(done)
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
total_steps += 1
|
|
|
|
if total_steps >= ROLLOUT_STEPS: break
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
if done: break
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
observations_hash = hash_object(observation_list)
|
|
|
|
actions_hash = hash_object(action_list)
|
|
|
|
rewards_hash = hash_object(reward_list)
|
|
|
|
dones_hash = hash_object(done_list)
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2019-02-09 02:58:51 +02:00
|
|
|
env.close()
|
|
|
|
return observations_hash, actions_hash, rewards_hash, dones_hash
|
2016-06-17 13:23:49 -07:00
|
|
|
|
2017-02-11 22:17:02 -08:00
|
|
|
@pytest.mark.parametrize("spec", spec_list)
|
2016-06-17 13:23:49 -07:00
|
|
|
def test_env_semantics(spec):
|
2019-02-09 02:58:51 +02:00
|
|
|
logger.warn("Skipping this test. Existing hashes were generated in a bad way")
|
|
|
|
return
|
|
|
|
with open(ROLLOUT_FILE) as data_file:
|
|
|
|
rollout_dict = json.load(data_file)
|
|
|
|
|
|
|
|
if spec.id not in rollout_dict:
|
|
|
|
if not spec.nondeterministic:
|
|
|
|
logger.warn("Rollout does not exist for {}, run generate_json.py to generate rollouts for new envs".format(spec.id))
|
|
|
|
return
|
|
|
|
|
|
|
|
logger.info("Testing rollout for {} environment...".format(spec.id))
|
|
|
|
|
|
|
|
observations_now, actions_now, rewards_now, dones_now = generate_rollout_hash(spec)
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
if rollout_dict[spec.id]['observations'] != observations_now:
|
|
|
|
errors.append('Observations not equal for {} -- expected {} but got {}'.format(spec.id, rollout_dict[spec.id]['observations'], observations_now))
|
|
|
|
if rollout_dict[spec.id]['actions'] != actions_now:
|
|
|
|
errors.append('Actions not equal for {} -- expected {} but got {}'.format(spec.id, rollout_dict[spec.id]['actions'], actions_now))
|
|
|
|
if rollout_dict[spec.id]['rewards'] != rewards_now:
|
|
|
|
errors.append('Rewards not equal for {} -- expected {} but got {}'.format(spec.id, rollout_dict[spec.id]['rewards'], rewards_now))
|
|
|
|
if rollout_dict[spec.id]['dones'] != dones_now:
|
|
|
|
errors.append('Dones not equal for {} -- expected {} but got {}'.format(spec.id, rollout_dict[spec.id]['dones'], dones_now))
|
|
|
|
if len(errors):
|
|
|
|
for error in errors:
|
|
|
|
logger.warn(error)
|
|
|
|
raise ValueError(errors)
|