2020-01-28 20:07:55 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from ray.rllib.utils.schedules import ConstantSchedule, \
|
|
|
|
LinearSchedule, ExponentialSchedule, PiecewiseSchedule
|
2020-07-30 12:49:32 +02:00
|
|
|
from ray.rllib.utils import check, framework_iterator, try_import_tf, \
|
|
|
|
try_import_torch
|
2020-01-28 20:07:55 +01:00
|
|
|
from ray.rllib.utils.from_config import from_config
|
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
tf1, tf, tfv = try_import_tf()
|
2020-07-30 12:49:32 +02:00
|
|
|
torch, _ = try_import_torch()
|
2020-01-28 20:07:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestSchedules(unittest.TestCase):
|
2020-07-30 12:49:32 +02:00
|
|
|
"""Tests all time-step dependent Schedule classes."""
|
2020-01-28 20:07:55 +01:00
|
|
|
|
|
|
|
def test_constant_schedule(self):
|
|
|
|
value = 2.3
|
|
|
|
ts = [100, 0, 10, 2, 3, 4, 99, 56, 10000, 23, 234, 56]
|
|
|
|
|
2020-01-30 20:27:57 +01:00
|
|
|
config = {"value": value}
|
|
|
|
|
2020-07-30 12:49:32 +02:00
|
|
|
for fw in framework_iterator(
|
|
|
|
frameworks=["tf2", "tf", "tfe", "torch", None]):
|
|
|
|
constant = from_config(ConstantSchedule, config, framework=fw)
|
2020-01-28 20:07:55 +01:00
|
|
|
for t in ts:
|
|
|
|
out = constant(t)
|
|
|
|
check(out, value)
|
|
|
|
|
2020-07-30 12:49:32 +02:00
|
|
|
ts_as_tensors = self._get_framework_tensors(ts, fw)
|
|
|
|
for t in ts_as_tensors:
|
|
|
|
out = constant(t)
|
|
|
|
assert fw != "tf" or isinstance(out, tf.Tensor)
|
|
|
|
check(out, value, decimals=4)
|
|
|
|
|
2020-01-28 20:07:55 +01:00
|
|
|
def test_linear_schedule(self):
|
2020-05-27 04:59:28 -04:00
|
|
|
ts = [0, 50, 10, 100, 90, 2, 1, 99, 23, 1000]
|
2020-07-30 12:49:32 +02:00
|
|
|
expected = [2.1 - (min(t, 100) / 100) * (2.1 - 0.6) for t in ts]
|
2020-01-30 20:27:57 +01:00
|
|
|
config = {"schedule_timesteps": 100, "initial_p": 2.1, "final_p": 0.6}
|
|
|
|
|
2020-07-30 12:49:32 +02:00
|
|
|
for fw in framework_iterator(
|
|
|
|
frameworks=["tf2", "tf", "tfe", "torch", None]):
|
|
|
|
linear = from_config(LinearSchedule, config, framework=fw)
|
|
|
|
for t, e in zip(ts, expected):
|
2020-01-28 20:07:55 +01:00
|
|
|
out = linear(t)
|
2020-07-30 12:49:32 +02:00
|
|
|
check(out, e, decimals=4)
|
|
|
|
|
|
|
|
ts_as_tensors = self._get_framework_tensors(ts, fw)
|
|
|
|
for t, e in zip(ts_as_tensors, expected):
|
|
|
|
out = linear(t)
|
|
|
|
assert fw != "tf" or isinstance(out, tf.Tensor)
|
|
|
|
check(out, e, decimals=4)
|
2020-01-28 20:07:55 +01:00
|
|
|
|
|
|
|
def test_polynomial_schedule(self):
|
2020-05-27 04:59:28 -04:00
|
|
|
ts = [0, 5, 10, 100, 90, 2, 1, 99, 23, 1000]
|
2020-07-30 12:49:32 +02:00
|
|
|
expected = [
|
2020-08-07 16:49:49 -07:00
|
|
|
0.5 + (2.0 - 0.5) * (1.0 - min(t, 100) / 100)**2 for t in ts
|
|
|
|
]
|
2020-01-30 20:27:57 +01:00
|
|
|
config = dict(
|
|
|
|
type="ray.rllib.utils.schedules.polynomial_schedule."
|
|
|
|
"PolynomialSchedule",
|
|
|
|
schedule_timesteps=100,
|
|
|
|
initial_p=2.0,
|
|
|
|
final_p=0.5,
|
|
|
|
power=2.0)
|
|
|
|
|
2020-07-30 12:49:32 +02:00
|
|
|
for fw in framework_iterator(
|
|
|
|
frameworks=["tf2", "tf", "tfe", "torch", None]):
|
|
|
|
polynomial = from_config(config, framework=fw)
|
|
|
|
for t, e in zip(ts, expected):
|
2020-01-28 20:07:55 +01:00
|
|
|
out = polynomial(t)
|
2020-07-30 12:49:32 +02:00
|
|
|
check(out, e, decimals=4)
|
|
|
|
|
|
|
|
ts_as_tensors = self._get_framework_tensors(ts, fw)
|
|
|
|
for t, e in zip(ts_as_tensors, expected):
|
|
|
|
out = polynomial(t)
|
|
|
|
assert fw != "tf" or isinstance(out, tf.Tensor)
|
|
|
|
check(out, e, decimals=4)
|
2020-01-28 20:07:55 +01:00
|
|
|
|
|
|
|
def test_exponential_schedule(self):
|
2020-07-30 12:49:32 +02:00
|
|
|
decay_rate = 0.2
|
2020-01-28 20:07:55 +01:00
|
|
|
ts = [0, 5, 10, 100, 90, 2, 1, 99, 23]
|
2020-07-30 12:49:32 +02:00
|
|
|
expected = [2.0 * decay_rate**(t / 100) for t in ts]
|
|
|
|
config = dict(
|
|
|
|
initial_p=2.0, decay_rate=decay_rate, schedule_timesteps=100)
|
2020-01-30 20:27:57 +01:00
|
|
|
|
2020-07-30 12:49:32 +02:00
|
|
|
for fw in framework_iterator(
|
|
|
|
frameworks=["tf2", "tf", "tfe", "torch", None]):
|
2020-04-03 21:24:25 +02:00
|
|
|
exponential = from_config(
|
2020-07-30 12:49:32 +02:00
|
|
|
ExponentialSchedule, config, framework=fw)
|
|
|
|
for t, e in zip(ts, expected):
|
|
|
|
out = exponential(t)
|
|
|
|
check(out, e, decimals=4)
|
|
|
|
|
|
|
|
ts_as_tensors = self._get_framework_tensors(ts, fw)
|
|
|
|
for t, e in zip(ts_as_tensors, expected):
|
2020-01-28 20:07:55 +01:00
|
|
|
out = exponential(t)
|
2020-07-30 12:49:32 +02:00
|
|
|
assert fw != "tf" or isinstance(out, tf.Tensor)
|
|
|
|
check(out, e, decimals=4)
|
2020-01-28 20:07:55 +01:00
|
|
|
|
|
|
|
def test_piecewise_schedule(self):
|
|
|
|
ts = [0, 5, 10, 100, 90, 2, 1, 99, 27]
|
|
|
|
expected = [50.0, 60.0, 70.0, 14.5, 14.5, 54.0, 52.0, 14.5, 140.0]
|
2020-01-30 20:27:57 +01:00
|
|
|
config = dict(
|
|
|
|
endpoints=[(0, 50.0), (25, 100.0), (30, 200.0)],
|
|
|
|
outside_value=14.5)
|
|
|
|
|
2020-07-30 12:49:32 +02:00
|
|
|
for fw in framework_iterator(
|
|
|
|
frameworks=["tf2", "tf", "tfe", "torch", None]):
|
|
|
|
piecewise = from_config(PiecewiseSchedule, config, framework=fw)
|
2020-01-30 20:27:57 +01:00
|
|
|
for t, e in zip(ts, expected):
|
|
|
|
out = piecewise(t)
|
|
|
|
check(out, e, decimals=4)
|
2020-02-19 21:18:45 +01:00
|
|
|
|
2020-07-30 12:49:32 +02:00
|
|
|
ts_as_tensors = self._get_framework_tensors(ts, fw)
|
|
|
|
for t, e in zip(ts_as_tensors, expected):
|
|
|
|
out = piecewise(t)
|
|
|
|
assert fw != "tf" or isinstance(out, tf.Tensor)
|
|
|
|
check(out, e, decimals=4)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_framework_tensors(ts, fw):
|
|
|
|
if fw == "torch":
|
|
|
|
ts = [torch.tensor(t, dtype=torch.int32) for t in ts]
|
|
|
|
elif fw is not None and "tf" in fw:
|
|
|
|
ts = [tf.constant(t, dtype=tf.int32) for t in ts]
|
|
|
|
return ts
|
|
|
|
|
2020-02-19 21:18:45 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2020-03-12 04:39:47 +01:00
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
sys.exit(pytest.main(["-v", __file__]))
|