rlmodule 0.0.5__tar.gz → 0.1.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rlmodule-0.1.5/LICENSE +30 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/PKG-INFO +49 -4
- rlmodule-0.1.5/README.md +46 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/pyproject.toml +7 -5
- rlmodule-0.1.5/rlmodule/__init__.py +4 -0
- rlmodule-0.1.5/rlmodule/skrl/torch/__init__.py +9 -0
- rlmodule-0.1.5/rlmodule/skrl/torch/examples/gymnasium/skrl_gymnasium_pendulum.py +267 -0
- rlmodule-0.1.5/rlmodule/skrl/torch/network/__init__.py +8 -0
- rlmodule-0.1.5/rlmodule/skrl/torch/output_layer/__init__.py +5 -0
- rlmodule-0.1.5/rlmodule/source/build.py +33 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/rlmodule/source/deprecated.py +23 -18
- {rlmodule-0.0.5 → rlmodule-0.1.5}/rlmodule/source/model.py +167 -189
- rlmodule-0.1.5/rlmodule/source/network.py +696 -0
- rlmodule-0.1.5/rlmodule/source/network_cfg.py +56 -0
- rlmodule-0.1.5/rlmodule/source/nvidia_utils/__init__.py +1 -0
- rlmodule-0.1.5/rlmodule/source/nvidia_utils/configclass.py +436 -0
- rlmodule-0.1.5/rlmodule/source/nvidia_utils/dict.py +121 -0
- rlmodule-0.1.5/rlmodule/source/nvidia_utils/string.py +97 -0
- rlmodule-0.1.5/rlmodule/source/output_layer.py +150 -0
- rlmodule-0.1.5/rlmodule/source/output_layer_cfg.py +38 -0
- rlmodule-0.1.5/rlmodule/source/rlmodel.py +363 -0
- rlmodule-0.1.5/rlmodule/source/rlmodel_cfg.py +40 -0
- rlmodule-0.1.5/rlmodule/source/utils.py +121 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/rlmodule.egg-info/PKG-INFO +49 -4
- rlmodule-0.1.5/rlmodule.egg-info/SOURCES.txt +30 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/rlmodule.egg-info/requires.txt +3 -3
- rlmodule-0.0.5/LICENSE +0 -21
- rlmodule-0.0.5/README.md +0 -2
- rlmodule-0.0.5/rlmodule/__init__.py +0 -3
- rlmodule-0.0.5/rlmodule/skrl/examples/gymnasium/skrl_gymnasium_pendulum.py +0 -120
- rlmodule-0.0.5/rlmodule/source/modules.py +0 -1003
- rlmodule-0.0.5/rlmodule/source/rlmodel.py +0 -771
- rlmodule-0.0.5/rlmodule.egg-info/SOURCES.txt +0 -17
- {rlmodule-0.0.5 → rlmodule-0.1.5}/rlmodule/skrl/__init__.py +0 -0
- {rlmodule-0.0.5/rlmodule/skrl → rlmodule-0.1.5/rlmodule/skrl/torch}/examples/gymnasium/skrl_gymnasium_pendulum_no_module.py +0 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/rlmodule/source/__init__.py +0 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/rlmodule.egg-info/dependency_links.txt +0 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/rlmodule.egg-info/top_level.txt +0 -0
- {rlmodule-0.0.5 → rlmodule-0.1.5}/setup.cfg +0 -0
rlmodule-0.1.5/LICENSE
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Copyright (c) 2024 by Fabrica AI Corp.
|
|
2
|
+
|
|
3
|
+
All rights reserved.
|
|
4
|
+
|
|
5
|
+
SPDX-License-Identifier: BSD-3-Clause
|
|
6
|
+
|
|
7
|
+
Redistribution and use in source and binary forms, with or without modification,
|
|
8
|
+
are permitted provided that the following conditions are met:
|
|
9
|
+
|
|
10
|
+
1. Redistributions of source code must retain the above copyright notice,
|
|
11
|
+
this list of conditions and the following disclaimer.
|
|
12
|
+
|
|
13
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
14
|
+
this list of conditions and the following disclaimer in the documentation
|
|
15
|
+
and/or other materials provided with the distribution.
|
|
16
|
+
|
|
17
|
+
3. Neither the name of the copyright holder nor the names of its contributors
|
|
18
|
+
may be used to endorse or promote products derived from this software without
|
|
19
|
+
specific prior written permission.
|
|
20
|
+
|
|
21
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
22
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
23
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
24
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
25
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
26
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
27
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
28
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
29
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
30
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: rlmodule
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.1.5
|
|
4
4
|
Summary: Flexible reinforcement learning models instantiators library
|
|
5
5
|
Author: Lopatovsky
|
|
6
|
+
Author-email: lopatovsky@gmail.com
|
|
6
7
|
Maintainer: Lopatovsky
|
|
7
8
|
License: MIT License
|
|
8
9
|
Project-URL: Homepage, https://github.com/FabricaAI/rlmodule
|
|
@@ -24,11 +25,55 @@ License-File: LICENSE
|
|
|
24
25
|
Requires-Dist: gym
|
|
25
26
|
Requires-Dist: gymnasium
|
|
26
27
|
Requires-Dist: torch>=1.9
|
|
27
|
-
Requires-Dist: skrl>=1.
|
|
28
|
+
Requires-Dist: skrl>=1.3.0
|
|
28
29
|
Provides-Extra: skrl
|
|
29
|
-
Requires-Dist: skrl>=1.
|
|
30
|
+
Requires-Dist: skrl>=1.3.0; extra == "skrl"
|
|
30
31
|
Provides-Extra: all
|
|
31
|
-
Requires-Dist: skrl>=1.
|
|
32
|
+
Requires-Dist: skrl>=1.3.0; extra == "all"
|
|
32
33
|
|
|
33
34
|
# rlmodule
|
|
34
35
|
Flexible reinforcement learning models instantiators library
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## How to run
|
|
39
|
+
|
|
40
|
+
### Install rlmodule from local code
|
|
41
|
+
|
|
42
|
+
- Make sure you are in base rlmodule dit.
|
|
43
|
+
|
|
44
|
+
- Start virtual env.
|
|
45
|
+
```
|
|
46
|
+
python3 -m venv venv
|
|
47
|
+
source venv/bin/activate
|
|
48
|
+
```
|
|
49
|
+
- Install library from local code
|
|
50
|
+
```
|
|
51
|
+
pip install -e .
|
|
52
|
+
```
|
|
53
|
+
Note: sometimes installation may fail, if there is a run/ dir present, you may need to remove it (TODO: fix)
|
|
54
|
+
```
|
|
55
|
+
rm -rf runs
|
|
56
|
+
```
|
|
57
|
+
### Run chosen example
|
|
58
|
+
```
|
|
59
|
+
python3 rlmodule/skrl/examples/gymnasium/skrl_gymnasium_pendulum.py
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
## Update new version to PIP
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
pip install build twine
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
rm -rf runs
|
|
71
|
+
python -m build
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
twine upload dist/*
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
rlmodule-0.1.5/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# rlmodule
|
|
2
|
+
Flexible reinforcement learning models instantiators library
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
## How to run
|
|
6
|
+
|
|
7
|
+
### Install rlmodule from local code
|
|
8
|
+
|
|
9
|
+
- Make sure you are in base rlmodule dit.
|
|
10
|
+
|
|
11
|
+
- Start virtual env.
|
|
12
|
+
```
|
|
13
|
+
python3 -m venv venv
|
|
14
|
+
source venv/bin/activate
|
|
15
|
+
```
|
|
16
|
+
- Install library from local code
|
|
17
|
+
```
|
|
18
|
+
pip install -e .
|
|
19
|
+
```
|
|
20
|
+
Note: sometimes installation may fail, if there is a run/ dir present, you may need to remove it (TODO: fix)
|
|
21
|
+
```
|
|
22
|
+
rm -rf runs
|
|
23
|
+
```
|
|
24
|
+
### Run chosen example
|
|
25
|
+
```
|
|
26
|
+
python3 rlmodule/skrl/examples/gymnasium/skrl_gymnasium_pendulum.py
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
## Update new version to PIP
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
pip install build twine
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
rm -rf runs
|
|
38
|
+
python -m build
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
twine upload dist/*
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "rlmodule"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.1.5"
|
|
4
4
|
description = "Flexible reinforcement learning models instantiators library"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.6"
|
|
7
7
|
license = {text = "MIT License"}
|
|
8
8
|
authors = [
|
|
9
|
-
{name = "Lopatovsky"},
|
|
9
|
+
{name = "Lopatovsky"},
|
|
10
|
+
{email = "lopatovsky@gmail.com"},
|
|
10
11
|
]
|
|
11
12
|
maintainers = [
|
|
12
13
|
{name = "Lopatovsky"},
|
|
13
14
|
]
|
|
15
|
+
|
|
14
16
|
keywords = ["reinforcement-learning", "machine-learning", "reinforcement", "machine", "learning", "rl", "rl-models", "rl-modules", "models", "modules"]
|
|
15
17
|
classifiers = [
|
|
16
18
|
"License :: OSI Approved :: MIT License",
|
|
@@ -24,18 +26,18 @@ dependencies = [
|
|
|
24
26
|
"gym",
|
|
25
27
|
"gymnasium",
|
|
26
28
|
"torch>=1.9",
|
|
27
|
-
"skrl>=1.
|
|
29
|
+
"skrl>=1.3.0",
|
|
28
30
|
]
|
|
29
31
|
|
|
30
32
|
[project.optional-dependencies]
|
|
31
33
|
|
|
32
34
|
# TODO(ll) only examples should depend on skrl, not the main code.
|
|
33
35
|
skrl = [
|
|
34
|
-
"skrl>=1.
|
|
36
|
+
"skrl>=1.3.0",
|
|
35
37
|
]
|
|
36
38
|
|
|
37
39
|
all = [
|
|
38
|
-
"skrl>=1.
|
|
40
|
+
"skrl>=1.3.0",
|
|
39
41
|
]
|
|
40
42
|
|
|
41
43
|
# urls
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import gymnasium as gym
|
|
2
|
+
|
|
3
|
+
import torch.nn as nn
|
|
4
|
+
|
|
5
|
+
from rlmodule.skrl.torch import build_model, SharedRLModelCfg, RLModelCfg, SeparatedRLModelCfg
|
|
6
|
+
from rlmodule.skrl.torch.network import MlpCfg
|
|
7
|
+
from rlmodule.skrl.torch.output_layer import DeterministicLayerCfg, GaussianLayerCfg
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# import the skrl components to build the RL system
|
|
11
|
+
from skrl.agents.torch.ppo import PPO, PPO_RNN, PPO_DEFAULT_CONFIG
|
|
12
|
+
|
|
13
|
+
from skrl.envs.wrappers.torch.gymnasium_envs import GymnasiumWrapper
|
|
14
|
+
#from skrl.envs.wrappers.torch import wrap_env
|
|
15
|
+
from skrl.memories.torch import RandomMemory
|
|
16
|
+
from skrl.resources.preprocessors.torch import RunningStandardScaler
|
|
17
|
+
from skrl.resources.schedulers.torch import KLAdaptiveRL
|
|
18
|
+
from skrl.trainers.torch import SequentialTrainer
|
|
19
|
+
from skrl.utils import set_seed
|
|
20
|
+
from skrl.envs.wrappers.torch.gymnasium_envs import GymnasiumWrapper
|
|
21
|
+
|
|
22
|
+
# todo set seed
|
|
23
|
+
# seed for reproducibility
|
|
24
|
+
set_seed() # e.g. `set_seed(42)` for fixed seed
|
|
25
|
+
|
|
26
|
+
#TODO next
|
|
27
|
+
# (Done) check how to do own @configclass .. or use one from isaac by default (print something to know version)
|
|
28
|
+
# (Done) RNN code modify to use data class
|
|
29
|
+
# (Done) RnnMLP
|
|
30
|
+
# (Done) Custom function Network
|
|
31
|
+
# (Done) Move things logically
|
|
32
|
+
# (Done) Handle input shapes better?
|
|
33
|
+
# (Done except Cnn) Convert all rest modules to use Configclass
|
|
34
|
+
# (Done) Rearrange library in the way it can be used for other rllibs and for torch/jax?
|
|
35
|
+
# Create examples - mlp, rnn,gru,lstm, Lstmmlp, shared - separate , custom net by fcion
|
|
36
|
+
# - examples from modules make in class header not function
|
|
37
|
+
# WRITE README tutorial
|
|
38
|
+
# Run & fix pre-commit
|
|
39
|
+
# annotate cfgs in modules - why doesn't work
|
|
40
|
+
# extensive comments
|
|
41
|
+
# Launch new version to pip
|
|
42
|
+
# Import new version in Isaac-lab
|
|
43
|
+
# Change structure of source
|
|
44
|
+
# lazy linear? what is it ?
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# def example_module(cfg):
|
|
49
|
+
# cfg = RnnMlpCfg(
|
|
50
|
+
# input_size = env.observation_space,
|
|
51
|
+
# rnn = LstmCfg(
|
|
52
|
+
# num_envs = env.num_envs,
|
|
53
|
+
# num_layers = 1,
|
|
54
|
+
# hidden_size = 32,
|
|
55
|
+
# sequence_length = 16,
|
|
56
|
+
# ),
|
|
57
|
+
# mlp = MlpCfg(
|
|
58
|
+
# hidden_units = [64, 64, 64],
|
|
59
|
+
# activations = [nn.ReLU, nn.ReLU, nn.ReLU],
|
|
60
|
+
# ),
|
|
61
|
+
# )
|
|
62
|
+
# return RnnMlp(cfg)
|
|
63
|
+
|
|
64
|
+
def get_shared_model(env):
|
|
65
|
+
# instantiate the agent's models (function approximators).
|
|
66
|
+
|
|
67
|
+
net_cfg = MlpCfg(
|
|
68
|
+
input_size = env.observation_space,
|
|
69
|
+
hidden_units = [64, 64, 64],
|
|
70
|
+
activations = [nn.ReLU, nn.ReLU, nn.ReLU],
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# 2
|
|
74
|
+
# net_cfg = LstmCfg(
|
|
75
|
+
# input_size = env.observation_space,
|
|
76
|
+
# num_envs = env.num_envs,
|
|
77
|
+
# num_layers = 1,
|
|
78
|
+
# hidden_size = 32,
|
|
79
|
+
# sequence_length = 16,
|
|
80
|
+
# )
|
|
81
|
+
|
|
82
|
+
# 3
|
|
83
|
+
# net_cfg = RnnMlpCfg(
|
|
84
|
+
# input_size = env.observation_space,
|
|
85
|
+
# rnn = LstmCfg(
|
|
86
|
+
# num_envs = env.num_envs,
|
|
87
|
+
# num_layers = 1,
|
|
88
|
+
# hidden_size = 32,
|
|
89
|
+
# sequence_length = 16,
|
|
90
|
+
# ),
|
|
91
|
+
# mlp = MlpCfg(
|
|
92
|
+
# hidden_units = [64, 64, 64],
|
|
93
|
+
# activations = [nn.ReLU, nn.ReLU, nn.ReLU],
|
|
94
|
+
# ),
|
|
95
|
+
# )
|
|
96
|
+
|
|
97
|
+
# 3.5
|
|
98
|
+
# net_cfg = RnnMlpCfg(
|
|
99
|
+
# input_size = env.observation_space,
|
|
100
|
+
# module = RnnMlpWithForwardedInput,
|
|
101
|
+
# rnn = LstmCfg(
|
|
102
|
+
# num_envs = env.num_envs,
|
|
103
|
+
# num_layers = 1,
|
|
104
|
+
# hidden_size = 32,
|
|
105
|
+
# sequence_length = 16,
|
|
106
|
+
# ),
|
|
107
|
+
# mlp = MlpCfg(
|
|
108
|
+
# hidden_units = [64, 64, 64],
|
|
109
|
+
# activations = [nn.ReLU, nn.ReLU, nn.ReLU],
|
|
110
|
+
# ),
|
|
111
|
+
# )
|
|
112
|
+
|
|
113
|
+
# 4
|
|
114
|
+
# net_cfg = NetworkCfg( input_size = env.observation_space,
|
|
115
|
+
# module = example_module)
|
|
116
|
+
|
|
117
|
+
# variant IV - all config
|
|
118
|
+
# net also config (optional)?
|
|
119
|
+
model = build_model(
|
|
120
|
+
SharedRLModelCfg(
|
|
121
|
+
network= net_cfg,
|
|
122
|
+
device= device,
|
|
123
|
+
policy_output_layer= GaussianLayerCfg(
|
|
124
|
+
output_size=env.action_space,
|
|
125
|
+
min_log_std=-1.2,
|
|
126
|
+
max_log_std=2,
|
|
127
|
+
initial_log_std=0.2,
|
|
128
|
+
),
|
|
129
|
+
value_output_layer= DeterministicLayerCfg(),
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
# RLCfg(
|
|
133
|
+
# Model
|
|
134
|
+
# AgentCfg
|
|
135
|
+
# MemoryCfg
|
|
136
|
+
# )
|
|
137
|
+
|
|
138
|
+
# build_model(cfg.model)
|
|
139
|
+
|
|
140
|
+
# cfg.agent.algoclass(
|
|
141
|
+
# cfg.agent.algo_config
|
|
142
|
+
# cfg.memory
|
|
143
|
+
# )
|
|
144
|
+
|
|
145
|
+
# SharedRLModelCfg(RLModelCfg){
|
|
146
|
+
# class = SharedRLModel
|
|
147
|
+
|
|
148
|
+
# }
|
|
149
|
+
|
|
150
|
+
# net
|
|
151
|
+
# output layers.class()
|
|
152
|
+
|
|
153
|
+
# cfg.model.class( net, output_layers )
|
|
154
|
+
|
|
155
|
+
#configclass -> dataclass - easy (Ron said)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
# variant V:
|
|
159
|
+
# ideas?
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
return {'policy': model, 'value': model}
|
|
163
|
+
|
|
164
|
+
# TODO Update separate model to configclass
|
|
165
|
+
def get_separated_model(env):
|
|
166
|
+
net_cfg = MlpCfg(
|
|
167
|
+
input_size = env.observation_space,
|
|
168
|
+
hidden_units = [64, 64, 64],
|
|
169
|
+
activations = [nn.ReLU, nn.ReLU, nn.ReLU],
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
policy_model = build_model(
|
|
173
|
+
RLModelCfg(
|
|
174
|
+
network= net_cfg,
|
|
175
|
+
device= device,
|
|
176
|
+
output_layer= GaussianLayerCfg(
|
|
177
|
+
output_size=env.action_space,
|
|
178
|
+
min_log_std=-1.2,
|
|
179
|
+
max_log_std=2,
|
|
180
|
+
initial_log_std=0.2,
|
|
181
|
+
),
|
|
182
|
+
)
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
value_model = build_model(
|
|
186
|
+
RLModelCfg(
|
|
187
|
+
network= net_cfg,
|
|
188
|
+
device= device,
|
|
189
|
+
output_layer= DeterministicLayerCfg(),
|
|
190
|
+
)
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
return {'policy': policy_model, 'value': value_model}
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
# load and wrap the gymnasium environment.
|
|
197
|
+
# note: the environment version may change depending on the gymnasium version
|
|
198
|
+
try:
|
|
199
|
+
env = gym.vector.make("Pendulum-v1", num_envs=4, asynchronous=False)
|
|
200
|
+
except (gym.error.DeprecatedEnv, gym.error.VersionNotFound) as e:
|
|
201
|
+
env_id = [spec for spec in gym.envs.registry if spec.startswith("Pendulum-v")][0]
|
|
202
|
+
print("Pendulum-v1 not found. Trying {}".format(env_id))
|
|
203
|
+
env = gym.vector.make(env_id, num_envs=4, asynchronous=False)
|
|
204
|
+
|
|
205
|
+
env = GymnasiumWrapper(env)
|
|
206
|
+
|
|
207
|
+
device = env.device
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
# instantiate a memory as rollout buffer (any memory can be used for this)
|
|
211
|
+
memory = RandomMemory(memory_size=1024, num_envs=env.num_envs, device=device)
|
|
212
|
+
|
|
213
|
+
models = get_shared_model(env)
|
|
214
|
+
#models = get_separated_model(env)
|
|
215
|
+
|
|
216
|
+
print(models)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
# configure and instantiate the agent (visit its documentation to see all the options)
|
|
220
|
+
# https://skrl.readthedocs.io/en/latest/api/agents/ppo.html#configuration-and-hyperparameters
|
|
221
|
+
cfg = PPO_DEFAULT_CONFIG.copy()
|
|
222
|
+
cfg["rollouts"] = 1024 # memory_size
|
|
223
|
+
cfg["learning_epochs"] = 10
|
|
224
|
+
cfg["mini_batches"] = 32
|
|
225
|
+
cfg["discount_factor"] = 0.9
|
|
226
|
+
cfg["lambda"] = 0.95
|
|
227
|
+
cfg["learning_rate"] = 1e-3
|
|
228
|
+
cfg["learning_rate_scheduler"] = KLAdaptiveRL
|
|
229
|
+
cfg["learning_rate_scheduler_kwargs"] = {"kl_threshold": 0.008}
|
|
230
|
+
cfg["grad_norm_clip"] = 0.5
|
|
231
|
+
cfg["ratio_clip"] = 0.2
|
|
232
|
+
cfg["value_clip"] = 0.2
|
|
233
|
+
cfg["clip_predicted_values"] = False
|
|
234
|
+
cfg["entropy_loss_scale"] = 0.0
|
|
235
|
+
cfg["value_loss_scale"] = 0.5
|
|
236
|
+
cfg["kl_threshold"] = 0
|
|
237
|
+
cfg["state_preprocessor"] = RunningStandardScaler
|
|
238
|
+
cfg["state_preprocessor_kwargs"] = {"size": env.observation_space, "device": device}
|
|
239
|
+
cfg["value_preprocessor"] = RunningStandardScaler
|
|
240
|
+
cfg["value_preprocessor_kwargs"] = {"size": 1, "device": device}
|
|
241
|
+
# logging to TensorBoard and write checkpoints (in timesteps)
|
|
242
|
+
cfg["experiment"]["write_interval"] = 500
|
|
243
|
+
cfg["experiment"]["checkpoint_interval"] = 5000
|
|
244
|
+
cfg["experiment"]["directory"] = "runs/torch/Pendulum"
|
|
245
|
+
|
|
246
|
+
# TODO(nicer)
|
|
247
|
+
if models['policy']._rnn:
|
|
248
|
+
agent = PPO_RNN(models=models,
|
|
249
|
+
memory=memory,
|
|
250
|
+
cfg=cfg,
|
|
251
|
+
observation_space=env.observation_space,
|
|
252
|
+
action_space=env.action_space,
|
|
253
|
+
device=device)
|
|
254
|
+
else:
|
|
255
|
+
agent = PPO(models=models,
|
|
256
|
+
memory=memory,
|
|
257
|
+
cfg=cfg,
|
|
258
|
+
observation_space=env.observation_space,
|
|
259
|
+
action_space=env.action_space,
|
|
260
|
+
device=device)
|
|
261
|
+
|
|
262
|
+
# configure and instantiate the RL trainer
|
|
263
|
+
cfg_trainer = {"timesteps": 100000, "headless": True}
|
|
264
|
+
trainer = SequentialTrainer(cfg=cfg_trainer, env=env, agents=[agent])
|
|
265
|
+
|
|
266
|
+
# start training
|
|
267
|
+
trainer.train()
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
# modules:
|
|
3
|
+
"MLP", "RNN", "GRU", "LSTM", "RnnBase", "RnnMlp",
|
|
4
|
+
# configs:
|
|
5
|
+
"NetworkCfg", "MlpCfg", "RnnBaseCfg", "RnnCfg", "GruCfg", "LstmCfg", "RnnMlpCfg"
|
|
6
|
+
]
|
|
7
|
+
from rlmodule.source.network import MLP, RNN, GRU, LSTM, RnnBase, RnnMlp
|
|
8
|
+
from rlmodule.source.network_cfg import NetworkCfg, MlpCfg, RnnBaseCfg, RnnCfg, GruCfg, LstmCfg, RnnMlpCfg
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from rlmodule.source.rlmodel_cfg import BaseRLCfg, RLModelCfg, SharedRLModelCfg
|
|
2
|
+
from rlmodule.source.utils import get_output_size
|
|
3
|
+
|
|
4
|
+
#TODO this, or inside RLModel class that have a config passed
|
|
5
|
+
def build_model(cfg: BaseRLCfg):
|
|
6
|
+
|
|
7
|
+
#1) network = create # for now it is created, todo from config
|
|
8
|
+
|
|
9
|
+
#2) get output size - what type? ..what is Model init doing with action/observation space
|
|
10
|
+
|
|
11
|
+
net = cfg.network.module(cfg.network)
|
|
12
|
+
|
|
13
|
+
network_output_size = get_output_size(net, cfg.network.input_size)
|
|
14
|
+
|
|
15
|
+
def build_output_layer(layer_cfg):
|
|
16
|
+
return layer_cfg.class_type( device = cfg.device, input_size = network_output_size, cfg = layer_cfg)
|
|
17
|
+
|
|
18
|
+
if isinstance(cfg, RLModelCfg):
|
|
19
|
+
rl_model = cfg.class_type( device = cfg.device,
|
|
20
|
+
network = net,
|
|
21
|
+
output_layer = build_output_layer( cfg.output_layer)
|
|
22
|
+
)
|
|
23
|
+
elif isinstance(cfg, SharedRLModelCfg):
|
|
24
|
+
rl_model = cfg.class_type( device = cfg.device,
|
|
25
|
+
network = net,
|
|
26
|
+
policy_output_layer = build_output_layer( cfg.policy_output_layer ),
|
|
27
|
+
value_output_layer = build_output_layer( cfg.value_output_layer ),
|
|
28
|
+
)
|
|
29
|
+
else:
|
|
30
|
+
raise TypeError(
|
|
31
|
+
f" Received unsupported class: '{type(cfg)}'."
|
|
32
|
+
)
|
|
33
|
+
return rl_model
|
|
@@ -72,13 +72,14 @@ class GaussianMixin:
|
|
|
72
72
|
)
|
|
73
73
|
)
|
|
74
74
|
"""
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
# TODO return this functionality
|
|
76
|
+
# self._clip_actions = clip_actions and (issubclass(type(self.action_space), gym.Space) or \
|
|
77
|
+
# issubclass(type(self.action_space), gymnasium.Space))
|
|
77
78
|
|
|
78
|
-
if self._clip_actions:
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
# if self._clip_actions:
|
|
80
|
+
# # TODO(ll) the code assume it will be inherited ... self.device is not set anywhere.
|
|
81
|
+
# self._clip_actions_min = torch.tensor(self.action_space.low, device=self.device, dtype=torch.float32)
|
|
82
|
+
# self._clip_actions_max = torch.tensor(self.action_space.high, device=self.device, dtype=torch.float32)
|
|
82
83
|
|
|
83
84
|
self._clip_log_std = clip_log_std
|
|
84
85
|
self._log_std_min = min_log_std
|
|
@@ -194,8 +195,9 @@ class GaussianMixin:
|
|
|
194
195
|
actions = self._distribution.rsample()
|
|
195
196
|
|
|
196
197
|
# clip actions
|
|
197
|
-
|
|
198
|
-
|
|
198
|
+
# TODO enable
|
|
199
|
+
# if self._clip_actions:
|
|
200
|
+
# actions = torch.clamp(actions, min=self._clip_actions_min, max=self._clip_actions_max)
|
|
199
201
|
|
|
200
202
|
if taken_actions is None:
|
|
201
203
|
taken_actions = actions
|
|
@@ -309,12 +311,14 @@ class DeterministicMixin:
|
|
|
309
311
|
)
|
|
310
312
|
)
|
|
311
313
|
"""
|
|
312
|
-
|
|
313
|
-
|
|
314
|
+
pass
|
|
315
|
+
# TODO return this functionality
|
|
316
|
+
# self._clip_actions = clip_actions and (issubclass(type(self.action_space), gym.Space) or \
|
|
317
|
+
# issubclass(type(self.action_space), gymnasium.Space))
|
|
314
318
|
|
|
315
|
-
if self._clip_actions:
|
|
316
|
-
|
|
317
|
-
|
|
319
|
+
# if self._clip_actions:
|
|
320
|
+
# self._clip_actions_min = torch.tensor(self.action_space.low, device=self.device, dtype=torch.float32)
|
|
321
|
+
# self._clip_actions_max = torch.tensor(self.action_space.high, device=self.device, dtype=torch.float32)
|
|
318
322
|
|
|
319
323
|
def act(self,
|
|
320
324
|
inputs: Mapping[str, Union[torch.Tensor, Any]],
|
|
@@ -344,8 +348,9 @@ class DeterministicMixin:
|
|
|
344
348
|
actions, outputs = self.compute(inputs, role)
|
|
345
349
|
|
|
346
350
|
# clip actions
|
|
347
|
-
|
|
348
|
-
|
|
351
|
+
# TODO enable
|
|
352
|
+
# if self._clip_actions:
|
|
353
|
+
# actions = torch.clamp(actions, min=self._clip_actions_min, max=self._clip_actions_max)
|
|
349
354
|
|
|
350
355
|
return actions, None, outputs
|
|
351
356
|
|
|
@@ -371,8 +376,8 @@ class DeterministicMixin:
|
|
|
371
376
|
>>> print(actions.shape, outputs)
|
|
372
377
|
torch.Size([4096, 1]) {}
|
|
373
378
|
"""
|
|
374
|
-
# clip actions
|
|
375
|
-
if self._clip_actions:
|
|
376
|
-
|
|
379
|
+
# clip actions # TODO enable
|
|
380
|
+
# if self._clip_actions:
|
|
381
|
+
# actions = torch.clamp(actions, min=self._clip_actions_min, max=self._clip_actions_max)
|
|
377
382
|
|
|
378
383
|
return actions, None, outputs
|