-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathdemo_random_action.py
More file actions
145 lines (121 loc) · 5.9 KB
/
Copy pathdemo_random_action.py
File metadata and controls
145 lines (121 loc) · 5.9 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import gymnasium as gym
import numpy as np
import sapien
from mani_skill.envs.sapien_env import BaseEnv
from mani_skill.utils import gym_utils
from mani_skill.utils.wrappers import RecordEpisode
import tyro
from dataclasses import dataclass
from typing import List, Optional, Annotated, Union
@dataclass
class Args:
env_id: Annotated[str, tyro.conf.arg(aliases=["-e"])] = "PushCube-v1"
"""The environment ID of the task you want to simulate"""
obs_mode: Annotated[str, tyro.conf.arg(aliases=["-o"])] = "none"
"""Observation mode"""
robot_uids: Annotated[Optional[str], tyro.conf.arg(aliases=["-r"])] = None
"""Robot UID(s) to use. Can be a comma separated list of UIDs or empty string to have no agents. If not given then defaults to the environments default robot"""
sim_backend: Annotated[str, tyro.conf.arg(aliases=["-b"])] = "auto"
"""Which simulation backend to use. Can be 'auto', 'cpu', 'gpu'"""
render_backend: Annotated[str, tyro.conf.arg(aliases=["-rb"])] = "gpu"
"""Which render backend to use. Can be 'gpu', 'cpu', 'none'"""
reward_mode: Optional[str] = None
"""Reward mode"""
num_envs: Annotated[int, tyro.conf.arg(aliases=["-n"])] = 1
"""Number of environments to run."""
control_mode: Annotated[Optional[str], tyro.conf.arg(aliases=["-c"])] = None
"""Control mode"""
render_mode: str = "rgb_array"
"""Render mode"""
shader: str = "default"
"""Change shader used for all cameras in the environment for rendering. Default is 'minimal' which is very fast. Can also be 'rt' for ray tracing and generating photo-realistic renders. Can also be 'rt-fast' for a faster but lower quality ray-traced renderer"""
record_dir: Optional[str] = None
"""Directory to save recordings"""
pause: Annotated[bool, tyro.conf.arg(aliases=["-p"])] = False
"""If using human render mode, auto pauses the simulation upon loading"""
edit_camera_poses: bool = False
"""Enable in-viewer camera pose editing when using the human render mode"""
quiet: bool = False
"""Disable verbose output."""
seed: Annotated[Optional[Union[int, list[int]]], tyro.conf.arg(aliases=["-s"])] = None
"""Seed(s) for random actions and simulator. Can be a single integer or a list of integers. Default is None (no seeds)"""
def main(args: Args):
if args.render_mode == "none":
args.render_mode = None
np.set_printoptions(suppress=True, precision=3)
verbose = not args.quiet
if isinstance(args.seed, int):
args.seed = [args.seed]
if args.seed is not None:
np.random.seed(args.seed[0])
parallel_in_single_scene = args.render_mode == "human"
if args.render_mode == "human" and args.obs_mode in ["sensor_data", "rgb", "rgbd", "depth", "point_cloud"]:
print("Disabling parallel single scene/GUI render as observation mode is a visual one. Change observation mode to state or state_dict to see a parallel env render")
parallel_in_single_scene = False
if args.render_mode == "human" and args.num_envs == 1:
parallel_in_single_scene = False
env_kwargs = dict(
obs_mode=args.obs_mode,
reward_mode=args.reward_mode,
control_mode=args.control_mode,
render_mode=args.render_mode,
sensor_configs=dict(shader_pack=args.shader),
human_render_camera_configs=dict(shader_pack=args.shader),
viewer_camera_configs=dict(shader_pack=args.shader),
num_envs=args.num_envs,
sim_backend=args.sim_backend,
render_backend=args.render_backend,
enable_shadow=True,
parallel_in_single_scene=parallel_in_single_scene,
enable_camera_pose_editing=args.edit_camera_poses,
)
if args.robot_uids is not None:
env_kwargs["robot_uids"] = tuple(args.robot_uids.split(","))
if len(env_kwargs["robot_uids"]) == 1:
env_kwargs["robot_uids"] = env_kwargs["robot_uids"][0]
env: BaseEnv = gym.make(
args.env_id,
**env_kwargs
)
record_dir = args.record_dir
if record_dir:
record_dir = record_dir.format(env_id=args.env_id)
env = RecordEpisode(env, record_dir, info_on_video=False, save_trajectory=False, max_steps_per_video=gym_utils.find_max_episode_steps_value(env))
if verbose:
print("Observation space", env.observation_space)
print("Action space", env.action_space)
if env.unwrapped.agent is not None:
print("Control mode", env.unwrapped.control_mode)
print("Reward mode", env.unwrapped.reward_mode)
obs, _ = env.reset(seed=args.seed, options=dict(reconfigure=True))
if args.seed is not None and env.action_space is not None:
env.action_space.seed(args.seed[0])
pause_after_viewer_boot = False
if args.render_mode == "human":
viewer = env.render()
pause_after_viewer_boot = args.pause and isinstance(viewer, sapien.utils.Viewer)
if args.edit_camera_poses:
print("Camera pose editing enabled: click a camera frustum in the viewer and drag the gizmo in the Camera Editor window.")
while True:
if args.render_mode == "human":
viewer = env.render()
if isinstance(viewer, sapien.utils.Viewer) and pause_after_viewer_boot:
viewer.paused = True
pause_after_viewer_boot = False
continue
action = env.action_space.sample() if env.action_space is not None else None
obs, reward, terminated, truncated, info = env.step(action)
if verbose:
print("reward", reward)
print("terminated", terminated)
print("truncated", truncated)
print("info", info)
if args.render_mode is None or args.render_mode != "human":
if (terminated | truncated).any():
break
env.close()
if record_dir:
print(f"Saving video to {record_dir}")
if __name__ == "__main__":
parsed_args = tyro.cli(Args)
main(parsed_args)