MCPcopy Create free account
hub / github.com/pytorch/tutorials / _step

Function _step

advanced_source/pendulum.py:242–273  ·  view source on GitHub ↗
(tensordict)

Source from the content-addressed store, hash-verified

240
241
242def _step(tensordict):
243 th, thdot = tensordict["th"], tensordict["thdot"] # th := theta
244
245 g_force = tensordict["params", "g"]
246 mass = tensordict["params", "m"]
247 length = tensordict["params", "l"]
248 dt = tensordict["params", "dt"]
249 u = tensordict["action"].squeeze(-1)
250 u = u.clamp(-tensordict["params", "max_torque"], tensordict["params", "max_torque"])
251 costs = angle_normalize(th) ** 2 + 0.1 * thdot**2 + 0.001 * (u**2)
252
253 new_thdot = (
254 thdot
255 + (3 * g_force / (2 * length) * th.sin() + 3.0 / (mass * length**2) * u) * dt
256 )
257 new_thdot = new_thdot.clamp(
258 -tensordict["params", "max_speed"], tensordict["params", "max_speed"]
259 )
260 new_th = th + new_thdot * dt
261 reward = -costs.view(*tensordict.shape, 1)
262 done = torch.zeros_like(reward, dtype=torch.bool)
263 out = TensorDict(
264 {
265 "th": new_th,
266 "thdot": new_thdot,
267 "params": tensordict["params"],
268 "reward": reward,
269 "done": done,
270 },
271 tensordict.shape,
272 )
273 return out
274
275
276def angle_normalize(x):

Callers

nothing calls this directly

Calls 1

angle_normalizeFunction · 0.85

Tested by

no test coverage detected