2019-09-18 13:35:17 +00:00
|
|
|
import os
|
|
|
|
|
|
2019-10-08 11:06:38 +00:00
|
|
|
import pytest
|
2019-10-07 14:26:03 +00:00
|
|
|
import numpy as np
|
|
|
|
|
|
2019-10-25 08:59:15 +00:00
|
|
|
from torchy_baselines import A2C, CEMRL, PPO, SAC, TD3
|
2019-10-07 14:26:03 +00:00
|
|
|
from torchy_baselines.common.noise import NormalActionNoise
|
|
|
|
|
|
|
|
|
|
action_noise = NormalActionNoise(np.zeros(1), 0.1 * np.ones(1))
|
2019-09-18 13:35:17 +00:00
|
|
|
|
2019-09-21 15:17:09 +00:00
|
|
|
|
2019-09-20 14:43:19 +00:00
|
|
|
def test_td3():
|
|
|
|
|
model = TD3('MlpPolicy', 'Pendulum-v0', policy_kwargs=dict(net_arch=[64, 64]),
|
2019-10-07 14:26:03 +00:00
|
|
|
learning_starts=100, verbose=1, create_eval_env=True, action_noise=action_noise)
|
2019-09-24 12:15:12 +00:00
|
|
|
model.learn(total_timesteps=1000, eval_freq=500)
|
2019-09-18 20:12:32 +00:00
|
|
|
model.save("test_save")
|
|
|
|
|
model.load("test_save")
|
2019-11-21 15:54:30 +00:00
|
|
|
os.remove("test_save.zip")
|
2019-09-18 20:12:32 +00:00
|
|
|
|
2019-09-21 15:17:09 +00:00
|
|
|
|
2019-09-18 20:12:32 +00:00
|
|
|
def test_cemrl():
|
|
|
|
|
model = CEMRL('MlpPolicy', 'Pendulum-v0', policy_kwargs=dict(net_arch=[16]), pop_size=2, n_grad=1,
|
2019-10-07 14:26:03 +00:00
|
|
|
learning_starts=100, verbose=1, create_eval_env=True, action_noise=action_noise)
|
2019-09-24 12:15:12 +00:00
|
|
|
model.learn(total_timesteps=1000, eval_freq=500)
|
2019-09-18 20:12:32 +00:00
|
|
|
model.save("test_save")
|
|
|
|
|
model.load("test_save")
|
2019-11-21 15:54:30 +00:00
|
|
|
os.remove("test_save.zip")
|
2019-09-18 13:35:17 +00:00
|
|
|
|
2019-09-21 15:17:09 +00:00
|
|
|
|
2019-10-25 08:59:15 +00:00
|
|
|
@pytest.mark.parametrize("model_class", [A2C, PPO])
|
2019-10-08 11:06:38 +00:00
|
|
|
@pytest.mark.parametrize("env_id", ['CartPole-v1', 'Pendulum-v0'])
|
2019-10-25 08:59:15 +00:00
|
|
|
def test_onpolicy(model_class, env_id):
|
|
|
|
|
model = model_class('MlpPolicy', env_id, policy_kwargs=dict(net_arch=[16]), verbose=1, create_eval_env=True)
|
2019-09-18 13:35:17 +00:00
|
|
|
model.learn(total_timesteps=1000, eval_freq=500)
|
2019-11-21 15:54:30 +00:00
|
|
|
model.save("test_save")
|
|
|
|
|
model.load("test_save")
|
|
|
|
|
os.remove("test_save.zip")
|
2019-09-24 12:15:12 +00:00
|
|
|
|
|
|
|
|
|
2019-12-02 10:47:52 +00:00
|
|
|
@pytest.mark.parametrize("ent_coef", ['auto', 0.01])
|
|
|
|
|
def test_sac(ent_coef):
|
2019-09-24 12:15:12 +00:00
|
|
|
model = SAC('MlpPolicy', 'Pendulum-v0', policy_kwargs=dict(net_arch=[64, 64]),
|
2019-12-02 10:47:52 +00:00
|
|
|
learning_starts=100, verbose=1, create_eval_env=True, ent_coef=ent_coef,
|
2019-10-07 14:26:03 +00:00
|
|
|
action_noise=NormalActionNoise(np.zeros(1), np.zeros(1)))
|
2019-09-24 12:15:12 +00:00
|
|
|
model.learn(total_timesteps=1000, eval_freq=500)
|
2019-11-21 15:54:30 +00:00
|
|
|
model.save("test_save")
|
|
|
|
|
model.load("test_save")
|
|
|
|
|
os.remove("test_save.zip")
|