-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
Copy pathenv_w_protobuf_observations.py
78 lines (63 loc) · 3.48 KB
/
env_w_protobuf_observations.py
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
68
69
70
71
72
73
74
75
76
77
78
"""Example of handling an Env that outputs protobuf observations.
This example:
- demonstrates how a custom Env can use protobufs to compress its observation into
a binary format to save space and gain performance.
- shows how to use a very simple ConnectorV2 piece that translates these protobuf
binary observation strings into proper more NN-readable observations (like a 1D
float32 tensor).
To see more details on which env we are building for this example, take a look at the
`CartPoleWithProtobufObservationSpace` class imported below.
To see more details on which ConnectorV2 piece we are plugging into the config
below, take a look at the `ProtobufCartPoleObservationDecoder` class imported below.
How to run this script
----------------------
`python [script file name].py --enable-new-api-stack`
For debugging, use the following additional command line options
`--no-tune --num-env-runners=0`
which should allow you to set breakpoints anywhere in the RLlib code and
have the execution stop there for inspection and debugging.
For logging to your WandB account, use:
`--wandb-key=[your WandB API key] --wandb-project=[some project name]
--wandb-run-name=[optional: WandB run name (within the defined project)]`
Results to expect
-----------------
You should see results similar to the following in your console output:
+------------------------------------------------------+------------+-----------------+
| Trial name | status | loc |
| | | |
|------------------------------------------------------+------------+-----------------+
| PPO_CartPoleWithProtobufObservationSpace_47dd2_00000 | TERMINATED | 127.0.0.1:67325 |
+------------------------------------------------------+------------+-----------------+
+--------+------------------+------------------------+------------------------+
| iter | total time (s) | episode_return_mean | num_episodes_lifetim |
| | | | e |
+--------+------------------+------------------------+------------------------+
| 17 | 39.9011 | 513.29 | 465 |
+--------+------------------+------------------------+------------------------+
"""
from ray.rllib.examples.connectors.classes.protobuf_cartpole_observation_decoder import ( # noqa
ProtobufCartPoleObservationDecoder,
)
from ray.rllib.examples.envs.classes.cartpole_with_protobuf_observation_space import (
CartPoleWithProtobufObservationSpace,
)
from ray.rllib.utils.test_utils import (
add_rllib_example_script_args,
run_rllib_example_script_experiment,
)
from ray.tune.registry import get_trainable_cls
parser = add_rllib_example_script_args(default_timesteps=200000, default_reward=400.0)
parser.set_defaults(enable_new_api_stack=True)
if __name__ == "__main__":
args = parser.parse_args()
base_config = (
get_trainable_cls(args.algo).get_default_config()
# Set up the env to be CartPole-v1, but with protobuf observations.
.environment(CartPoleWithProtobufObservationSpace)
# Plugin our custom ConnectorV2 piece to translate protobuf observations
# (box of dtype uint8) into NN-readible ones (1D tensor of dtype flaot32).
.env_runners(
env_to_module_connector=lambda env: ProtobufCartPoleObservationDecoder(),
)
)
run_rllib_example_script_experiment(base_config, args)