2020-05-05 14:28:38 +00:00
|
|
|
import gym
|
|
|
|
|
import numpy as np
|
2020-06-10 10:09:04 +00:00
|
|
|
import pytest
|
2020-07-16 14:12:16 +00:00
|
|
|
from gym import spaces
|
2020-05-05 14:28:38 +00:00
|
|
|
|
2020-05-05 14:32:08 +00:00
|
|
|
from stable_baselines3.common.vec_env import DummyVecEnv, VecCheckNan
|
2020-05-05 14:28:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class NanAndInfEnv(gym.Env):
|
|
|
|
|
"""Custom Environment that raised NaNs and Infs"""
|
2020-07-16 14:12:16 +00:00
|
|
|
|
|
|
|
|
metadata = {"render.modes": ["human"]}
|
2020-05-05 14:28:38 +00:00
|
|
|
|
|
|
|
|
def __init__(self):
|
2022-04-25 10:01:38 +00:00
|
|
|
super().__init__()
|
2020-05-05 14:28:38 +00:00
|
|
|
self.action_space = spaces.Box(low=-np.inf, high=np.inf, shape=(1,), dtype=np.float64)
|
|
|
|
|
self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=(1,), dtype=np.float64)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def step(action):
|
|
|
|
|
if np.all(np.array(action) > 0):
|
2020-07-16 14:12:16 +00:00
|
|
|
obs = float("NaN")
|
2020-05-05 14:28:38 +00:00
|
|
|
elif np.all(np.array(action) < 0):
|
2020-07-16 14:12:16 +00:00
|
|
|
obs = float("inf")
|
2020-05-05 14:28:38 +00:00
|
|
|
else:
|
|
|
|
|
obs = 0
|
|
|
|
|
return [obs], 0.0, False, {}
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def reset():
|
|
|
|
|
return [0.0]
|
|
|
|
|
|
2020-07-16 14:12:16 +00:00
|
|
|
def render(self, mode="human", close=False):
|
2020-05-05 14:28:38 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_nan():
|
|
|
|
|
"""Test VecCheckNan Object"""
|
|
|
|
|
|
|
|
|
|
env = DummyVecEnv([NanAndInfEnv])
|
|
|
|
|
env = VecCheckNan(env, raise_exception=True)
|
|
|
|
|
|
|
|
|
|
env.step([[0]])
|
|
|
|
|
|
2020-06-10 10:09:04 +00:00
|
|
|
with pytest.raises(ValueError):
|
2020-07-16 14:12:16 +00:00
|
|
|
env.step([[float("NaN")]])
|
2020-05-05 14:28:38 +00:00
|
|
|
|
2020-06-10 10:09:04 +00:00
|
|
|
with pytest.raises(ValueError):
|
2020-07-16 14:12:16 +00:00
|
|
|
env.step([[float("inf")]])
|
2020-05-05 14:28:38 +00:00
|
|
|
|
2020-06-10 10:09:04 +00:00
|
|
|
with pytest.raises(ValueError):
|
2020-05-05 14:28:38 +00:00
|
|
|
env.step([[-1]])
|
|
|
|
|
|
2020-06-10 10:09:04 +00:00
|
|
|
with pytest.raises(ValueError):
|
2020-05-05 14:28:38 +00:00
|
|
|
env.step([[1]])
|
|
|
|
|
|
|
|
|
|
env.step(np.array([[0, 1], [0, 1]]))
|
2020-06-10 10:09:04 +00:00
|
|
|
|
|
|
|
|
env.reset()
|