2021-12-15 22:32:52 +01:00
|
|
|
import contextlib
|
2020-01-23 02:02:58 +01:00
|
|
|
from functools import partial
|
|
|
|
|
2019-12-30 15:27:32 -05:00
|
|
|
from ray.rllib.utils.annotations import override, PublicAPI, DeveloperAPI
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.utils.framework import try_import_tf, try_import_tfp, try_import_torch
|
2021-08-03 18:30:02 -04:00
|
|
|
from ray.rllib.utils.deprecation import deprecation_warning
|
2017-12-30 00:24:54 -08:00
|
|
|
from ray.rllib.utils.filter_manager import FilterManager
|
2018-07-01 00:05:08 -07:00
|
|
|
from ray.rllib.utils.filter import Filter
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.utils.numpy import (
|
|
|
|
sigmoid,
|
|
|
|
softmax,
|
|
|
|
relu,
|
|
|
|
one_hot,
|
|
|
|
fc,
|
|
|
|
lstm,
|
|
|
|
SMALL_NUMBER,
|
|
|
|
LARGE_INTEGER,
|
|
|
|
MIN_LOG_NN_OUTPUT,
|
|
|
|
MAX_LOG_NN_OUTPUT,
|
|
|
|
)
|
2022-01-11 19:50:03 +01:00
|
|
|
from ray.rllib.utils.pre_checks.env import check_env
|
2022-01-29 18:41:57 -08:00
|
|
|
from ray.rllib.utils.schedules import (
|
|
|
|
LinearSchedule,
|
|
|
|
PiecewiseSchedule,
|
|
|
|
PolynomialSchedule,
|
|
|
|
ExponentialSchedule,
|
|
|
|
ConstantSchedule,
|
|
|
|
)
|
|
|
|
from ray.rllib.utils.test_utils import (
|
|
|
|
check,
|
|
|
|
check_compute_single_action,
|
|
|
|
check_train_results,
|
|
|
|
framework_iterator,
|
|
|
|
)
|
2020-01-06 21:11:03 -05:00
|
|
|
from ray.tune.utils import merge_dicts, deep_update
|
2017-12-30 00:24:54 -08:00
|
|
|
|
2019-05-20 16:46:05 -07:00
|
|
|
|
2021-09-02 23:02:05 -07:00
|
|
|
def add_mixins(base, mixins, reversed=False):
|
2019-06-07 16:45:36 -07:00
|
|
|
"""Returns a new class with mixins applied in priority order."""
|
|
|
|
|
|
|
|
mixins = list(mixins or [])
|
|
|
|
|
|
|
|
while mixins:
|
2021-09-02 23:02:05 -07:00
|
|
|
if reversed:
|
2019-06-07 16:45:36 -07:00
|
|
|
|
2021-09-02 23:02:05 -07:00
|
|
|
class new_base(base, mixins.pop()):
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
class new_base(mixins.pop(), base):
|
|
|
|
pass
|
2019-06-07 16:45:36 -07:00
|
|
|
|
|
|
|
base = new_base
|
|
|
|
|
|
|
|
return base
|
|
|
|
|
|
|
|
|
2020-01-23 02:02:58 +01:00
|
|
|
def force_list(elements=None, to_tuple=False):
|
|
|
|
"""
|
|
|
|
Makes sure `elements` is returned as a list, whether `elements` is a single
|
|
|
|
item, already a list, or a tuple.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
elements (Optional[any]): The inputs as single item, list, or tuple to
|
|
|
|
be converted into a list/tuple. If None, returns empty list/tuple.
|
|
|
|
to_tuple (bool): Whether to use tuple (instead of list).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Union[list,tuple]: All given elements in a list/tuple depending on
|
|
|
|
`to_tuple`'s value. If elements is None,
|
|
|
|
returns an empty list/tuple.
|
|
|
|
"""
|
|
|
|
ctor = list
|
|
|
|
if to_tuple is True:
|
|
|
|
ctor = tuple
|
2022-01-29 18:41:57 -08:00
|
|
|
return (
|
|
|
|
ctor()
|
|
|
|
if elements is None
|
|
|
|
else ctor(elements)
|
|
|
|
if type(elements) in [list, set, tuple]
|
|
|
|
else ctor([elements])
|
|
|
|
)
|
2020-01-23 02:02:58 +01:00
|
|
|
|
|
|
|
|
2021-12-15 22:32:52 +01:00
|
|
|
class NullContextManager(contextlib.AbstractContextManager):
|
2020-12-26 20:14:18 -05:00
|
|
|
"""No-op context manager"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-01-23 02:02:58 +01:00
|
|
|
force_tuple = partial(force_list, to_tuple=True)
|
|
|
|
|
2018-12-26 03:07:11 -08:00
|
|
|
__all__ = [
|
2019-12-30 15:27:32 -05:00
|
|
|
"add_mixins",
|
|
|
|
"check",
|
2021-11-19 00:41:03 -08:00
|
|
|
"check_env",
|
2020-06-13 17:51:50 +02:00
|
|
|
"check_compute_single_action",
|
2021-09-30 16:39:05 +02:00
|
|
|
"check_train_results",
|
2020-10-02 23:07:44 +02:00
|
|
|
"deep_update",
|
2019-12-30 15:27:32 -05:00
|
|
|
"deprecation_warning",
|
|
|
|
"fc",
|
2020-01-23 02:02:58 +01:00
|
|
|
"force_list",
|
|
|
|
"force_tuple",
|
2020-04-03 21:24:25 +02:00
|
|
|
"framework_iterator",
|
2019-12-30 15:27:32 -05:00
|
|
|
"lstm",
|
|
|
|
"merge_dicts",
|
2020-10-02 23:07:44 +02:00
|
|
|
"one_hot",
|
2019-12-30 15:27:32 -05:00
|
|
|
"override",
|
2020-10-02 23:07:44 +02:00
|
|
|
"relu",
|
|
|
|
"sigmoid",
|
|
|
|
"softmax",
|
2019-12-30 15:27:32 -05:00
|
|
|
"try_import_tf",
|
|
|
|
"try_import_tfp",
|
|
|
|
"try_import_torch",
|
2020-01-28 20:07:55 +01:00
|
|
|
"ConstantSchedule",
|
2019-12-30 15:27:32 -05:00
|
|
|
"DeveloperAPI",
|
2020-01-28 20:07:55 +01:00
|
|
|
"ExponentialSchedule",
|
2019-04-07 00:36:18 -07:00
|
|
|
"Filter",
|
|
|
|
"FilterManager",
|
2019-12-30 15:27:32 -05:00
|
|
|
"LARGE_INTEGER",
|
2020-01-28 20:07:55 +01:00
|
|
|
"LinearSchedule",
|
2020-02-22 23:19:49 +01:00
|
|
|
"MAX_LOG_NN_OUTPUT",
|
|
|
|
"MIN_LOG_NN_OUTPUT",
|
2020-01-28 20:07:55 +01:00
|
|
|
"PiecewiseSchedule",
|
|
|
|
"PolynomialSchedule",
|
2019-12-30 15:27:32 -05:00
|
|
|
"PublicAPI",
|
|
|
|
"SMALL_NUMBER",
|
2018-12-26 03:07:11 -08:00
|
|
|
]
|