forked from rll/deeprlhw2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mjcmdp.py
166 lines (121 loc) · 4.95 KB
/
mjcmdp.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
161
162
163
164
165
166
from rl import MDP, Serializable
import os
from mjpy import MjModel, MjViewer
import numpy as np
class MjcMDP(MDP):
def __init__(self):
self.model = MjModel(self.model_path())
self.data = self.model.data
self.viewer = None
self.init_qpos = self.model.data.qpos
self.init_qvel = self.model.data.qvel
self.ctrl_dim = self.model.data.ctrl.size
o,r,_ = self.step(np.zeros(self.ctrl_dim))
self.obs_dim = o.size
self.rew_dim = r.size
def model_path(self):
raise NotImplementedError
def step(self, s, a):
raise NotImplementedError
def get_viewer(self):
if self.viewer is None:
self.viewer = MjViewer()
self.viewer.start()
self.viewer.set_model(self.model)
return self.viewer
def action_spec(self):
return (np.float64,(self.model.nu,))
def observation_spec(self):
return (np.float64, (self.obs_dim,))
def plot(self):
viewer = self.get_viewer()
viewer.loop_once()
class HopperMDP(MjcMDP, Serializable):
def __init__(self):
self.frame_skip = 5
self.ctrl_scaling = 100.0
self.timestep = .02
MjcMDP.__init__(self)
Serializable.__init__(self)
def model_path(self):
return os.path.join(os.path.dirname(__file__),'vendor/mujoco_models/hopper.xml')
def reset(self):
self.x0 = np.concatenate([self.model.data.qpos, self.model.data.qvel])
self.model.data.qpos = self.init_qpos
self.model.data.qvel = self.init_qvel
# self.model.data.qvel = np.random.randn(len(self.model.data.qvel))*.2
# self.model.data.qfrc_constraint[:] = 0
self.model.forward()
return self._get_obs()
def _get_obs(self):
qpos = self.model.data.qpos
return np.concatenate([qpos[0:1], qpos[2:], np.clip(self.model.data.qvel,-10,10), np.clip(self.model.data.qfrc_constraint,-10,10)]).reshape(1,-1)
def step(self, a):
posbefore = self.model.data.qpos[1]
self.model.data.ctrl = a * self.ctrl_scaling
for _ in range(self.frame_skip):
self.model.step()
posafter = self.model.data.qpos[1]
reward = (posafter - posbefore) / self.timestep
s = np.concatenate([self.model.data.qpos, self.model.data.qvel])
notdone = np.isfinite(s).all() and (np.abs(s[3:])<100).all() and (s[0] > .7) and (abs(s[2]) < .2)
done = not notdone
ob = self._get_obs()
return ob, reward, done
class WalkerMDP(MjcMDP,Serializable):
def __init__(self):
self.frame_skip = 4
self.ctrl_scaling = 20.0
self.timestep = .02
MjcMDP.__init__(self)
Serializable.__init__(self)
def model_path(self):
return os.path.join(os.path.dirname(__file__),'vendor/mujoco_models/walker2d.xml')
def reset(self):
self.x0 = np.concatenate([self.model.data.qpos, self.model.data.qvel])
self.model.data.qpos = self.init_qpos
self.model.data.qvel = self.init_qvel
# self.model.data.qvel = np.random.randn(len(self.model.data.qvel))*.2
# self.model.data.qfrc_constraint[:] = 0
self.model.forward()
return self._get_obs()
def _get_obs(self):
return np.concatenate([self.model.data.qpos, np.sign(self.model.data.qvel), np.sign(self.model.data.qfrc_constraint)]).reshape(1,-1)
def step(self, a):
posbefore = self.model.data.xpos[:,0].min()
self.model.data.ctrl = a * self.ctrl_scaling
for _ in range(self.frame_skip):
self.model.step()
posafter = self.model.data.xpos[:,0].min()
reward = (posafter - posbefore) / self.timestep + 1.0
s = np.concatenate([self.model.data.qpos, self.model.data.qvel])
notdone = np.isfinite(s).all() and (np.abs(s[3:])<100).all() and (s[0] > 0.7) and (abs(s[2]) < .5)
done = not notdone
ob = self._get_obs()
return ob, reward, done
# class WalkerMDP(MjcMDP):
# def __init__(self):
# super(WalkerMDP, self).__init__()
# def model_path(self):
# return os.path.join(os.path.dirname(__file__),'vendor/mujoco_models/walker2d.xml')
# class CartpoleMDP(MjcMDP):
# def __init__(self):
# super(CartpoleMDP, self).__init__()
# def model_path(self):
# return os.path.join(os.path.dirname(__file__),
# 'vendor/mujoco_models/cartpole.xml')
# def reward(self, s0, a, s1):
# assert s0.ndim == 1
# return (abs(s0[1]) < .1).astype('float64')
# def done(self, s1):
# assert s1.ndim == 1
# return (np.abs(s1) > 5).any()
# class HumanoidMDP(MjcMDP):
# def __init__(self):
# super(HumanoidMDP, self).__init__()
# def model_path(self):
# return os.path.join(os.path.dirname(__file__),
# 'vendor/mujoco_models/humanoid.xml')
# @property
# def frame_skip(self):
# return 8