forked from danijar/dreamerv3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenesis_example.py
160 lines (142 loc) · 4.42 KB
/
genesis_example.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import warnings
from functools import partial as bind
import dreamerv3
import embodied
warnings.filterwarnings('ignore', '.*truncated to dtype int32.*')
def get_cfgs():
env_cfg = {
"num_actions": 12,
# joint/link names
"default_joint_angles": { # [rad]
"FL_hip_joint": 0.0,
"FR_hip_joint": 0.0,
"RL_hip_joint": 0.0,
"RR_hip_joint": 0.0,
"FL_thigh_joint": 0.8,
"FR_thigh_joint": 0.8,
"RL_thigh_joint": 1.0,
"RR_thigh_joint": 1.0,
"FL_calf_joint": -1.5,
"FR_calf_joint": -1.5,
"RL_calf_joint": -1.5,
"RR_calf_joint": -1.5,
},
"dof_names": [
"FR_hip_joint",
"FR_thigh_joint",
"FR_calf_joint",
"FL_hip_joint",
"FL_thigh_joint",
"FL_calf_joint",
"RR_hip_joint",
"RR_thigh_joint",
"RR_calf_joint",
"RL_hip_joint",
"RL_thigh_joint",
"RL_calf_joint",
],
# PD
"kp": 20.0,
"kd": 0.5,
# termination
"termination_if_roll_greater_than": 10, # degree
"termination_if_pitch_greater_than": 10,
# base pose
"base_init_pos": [0.0, 0.0, 0.42],
"base_init_quat": [1.0, 0.0, 0.0, 0.0],
"episode_length_s": 20.0,
"resampling_time_s": 4.0,
"action_scale": 0.25,
"simulate_action_latency": True,
"clip_actions": 100.0,
}
obs_cfg = {
"num_obs": 45,
"obs_scales": {
"lin_vel": 2.0,
"ang_vel": 0.25,
"dof_pos": 1.0,
"dof_vel": 0.05,
},
}
reward_cfg = {
"tracking_sigma": 0.25,
"base_height_target": 0.3,
"feet_height_target": 0.075,
"reward_scales": {
"tracking_lin_vel": 1.0,
"tracking_ang_vel": 0.2,
"lin_vel_z": -1.0,
"base_height": -50.0,
"action_rate": -0.005,
"similar_to_default": -0.1,
},
}
command_cfg = {
"num_commands": 3,
"lin_vel_x_range": [0.5, 0.5],
"lin_vel_y_range": [0, 0],
"ang_vel_range": [0, 0],
}
return env_cfg, obs_cfg, reward_cfg, command_cfg
def main():
config = embodied.Config(dreamerv3.Agent.configs['defaults'])
config = config.update({
**dreamerv3.Agent.configs['size100m'],
'logdir': f'~/logdir/{embodied.timestamp()}-example',
'run.train_ratio': 32,
# 'jax.platform': 'cpu',
'jax.policy_devices':[0],
'jax.train_devices':[0],
})
config = embodied.Flags(config).parse()
print('Logdir:', config.logdir)
logdir = embodied.Path(config.logdir)
logdir.mkdir()
config.save(logdir / 'config.yaml')
def make_agent(config):
env = make_env(config)
agent = dreamerv3.Agent(env.obs_space, env.act_space, config)
env.close()
return agent
def make_logger(config):
logdir = embodied.Path(config.logdir)
return embodied.Logger(embodied.Counter(), [
embodied.logger.TerminalOutput(config.filter),
embodied.logger.JSONLOutput(logdir, 'metrics.jsonl'),
embodied.logger.TensorBoardOutput(logdir),
# embodied.logger.WandbOutput(logdir.name, config=config),
])
def make_replay(config):
return embodied.replay.Replay(
length=config.batch_length,
capacity=config.replay.size,
directory=embodied.Path(config.logdir) / 'replay',
online=config.replay.online)
def make_env(config, env_id=0):
from embodied.envs import mice_go2_env
import genesis as gs
from embodied.envs import from_gym
gs.init(logging_level="warning")
env_cfg, obs_cfg, reward_cfg, command_cfg = get_cfgs()
env = mice_go2_env.Go2Env(
num_envs=args.num_envs, env_cfg=env_cfg, obs_cfg=obs_cfg, reward_cfg=reward_cfg, command_cfg=command_cfg
)
env = from_gym.FromGym(env)
env = dreamerv3.wrap_env(env, config)
return env
args = embodied.Config(
**config.run,
logdir=config.logdir,
batch_size=config.batch_size,
batch_length=config.batch_length,
batch_length_eval=config.batch_length_eval,
replay_context=config.replay_context,
)
embodied.run.train(
bind(make_agent, config),
bind(make_replay, config),
bind(make_env, config),
bind(make_logger, config), args)
if __name__ == '__main__':
main()