forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timesteps.py
More file actions
67 lines (55 loc) · 1.83 KB
/
Copy pathtest_timesteps.py
File metadata and controls
67 lines (55 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import unittest
import ray
import ray.rllib.algorithms.ppo as ppo
from ray.rllib.examples.envs.classes.random_env import RandomEnv
from ray.rllib.utils.test_utils import check
class TestTimeSteps(unittest.TestCase):
@classmethod
def setUpClass(cls):
ray.init()
@classmethod
def tearDownClass(cls):
ray.shutdown()
def test_timesteps(self):
"""Test whether PG can be built with both frameworks."""
config = (
ppo.PPOConfig()
.api_stack(
enable_env_runner_and_connector_v2=False,
enable_rl_module_and_learner=False,
)
.experimental(_disable_preprocessor_api=True)
.environment(RandomEnv)
.env_runners(num_env_runners=0)
.training(
model={
"fcnet_hiddens": [1],
"fcnet_activation": None,
}
)
)
obs = np.array(1)
obs_batch = np.array([1])
algo = config.build()
policy = algo.get_policy()
for i in range(1, 21):
algo.compute_single_action(obs)
check(int(policy.global_timestep), i)
for i in range(1, 21):
policy.compute_actions(obs_batch)
check(int(policy.global_timestep), i + 20)
# Artificially set ts to 100Bio, then keep computing actions and
# train.
crazy_timesteps = int(1e11)
policy.on_global_var_update({"timestep": crazy_timesteps})
# Run for 10 more ts.
for i in range(1, 11):
policy.compute_actions(obs_batch)
check(int(policy.global_timestep), i + crazy_timesteps)
algo.train()
algo.stop()
if __name__ == "__main__":
import pytest
import sys
sys.exit(pytest.main(["-v", __file__]))