|
| 1 | +import os |
| 2 | + |
| 3 | +import jax |
| 4 | +import jax.numpy as jnp |
| 5 | + |
| 6 | +task_name = "acrobot_swingup" |
| 7 | +benchmark_mode = "direct" |
| 8 | + |
| 9 | +dir_path = os.path.dirname(os.path.realpath(__file__)) |
| 10 | +scene_path = os.path.abspath(os.path.join(dir_path, "..")) + "/data/acrobot/scene.xml" |
| 11 | + |
| 12 | +N = 200 |
| 13 | +n = 4 |
| 14 | +m = 1 |
| 15 | +dt = 0.01 |
| 16 | + |
| 17 | +m1 = 1.0 |
| 18 | +m2 = 1.0 |
| 19 | +l1 = 0.5 |
| 20 | +l2 = 0.5 |
| 21 | +lc1 = 0.5 * l1 |
| 22 | +lc2 = 0.5 * l2 |
| 23 | +g = 9.81 |
| 24 | +I1 = m1 * l1 * l1 / 12.0 |
| 25 | +I2 = m2 * l2 * l2 / 12.0 |
| 26 | + |
| 27 | +parameter = jnp.zeros(N + 1) |
| 28 | +reference = jnp.zeros(N + 1) |
| 29 | + |
| 30 | + |
| 31 | +def dynamics(x, u, t, parameter): |
| 32 | + del t, parameter |
| 33 | + theta1_dot = x[0] |
| 34 | + theta2_dot = x[1] |
| 35 | + theta1 = x[2] |
| 36 | + theta2 = x[3] |
| 37 | + |
| 38 | + d11 = ( |
| 39 | + I1 |
| 40 | + + I2 |
| 41 | + + m1 * lc1 * lc1 |
| 42 | + + m2 * (l1 * l1 + lc2 * lc2 + 2.0 * l1 * lc2 * jnp.cos(theta2)) |
| 43 | + ) |
| 44 | + d12 = I2 + m2 * (lc2 * lc2 + l1 * lc2 * jnp.cos(theta2)) |
| 45 | + d21 = d12 |
| 46 | + d22 = I2 + m2 * lc2 * lc2 |
| 47 | + |
| 48 | + c11 = -2.0 * m2 * l1 * lc2 * jnp.sin(theta2) * theta2_dot |
| 49 | + c12 = -m2 * l1 * lc2 * jnp.sin(theta2) * theta2_dot |
| 50 | + c21 = m2 * l1 * lc2 * jnp.sin(theta2) * theta1_dot |
| 51 | + c22 = 0.0 |
| 52 | + |
| 53 | + g1 = m1 * g * lc1 * jnp.sin(theta1) + m2 * g * (l1 * jnp.sin(theta1) + lc2 * jnp.sin(theta1 + theta2)) |
| 54 | + g2 = m2 * lc2 * g * jnp.sin(theta1 + theta2) |
| 55 | + |
| 56 | + D = jnp.array([[d11, d12], [d21, d22]]) |
| 57 | + C = jnp.array([[c11, c12], [c21, c22]]) |
| 58 | + G = jnp.array([g1, g2]) |
| 59 | + |
| 60 | + theta_dot = jnp.array([theta1_dot, theta2_dot]) |
| 61 | + theta_dot_new = theta_dot + dt * jnp.linalg.inv(D) @ (jnp.array([0.0, u[0]]) - C @ theta_dot - G) |
| 62 | + theta_new = jnp.array([theta1, theta2]) + dt * theta_dot_new |
| 63 | + return jnp.concatenate([theta_dot_new, theta_new]) |
| 64 | + |
| 65 | + |
| 66 | +x0 = jnp.array([0.0, 0.0, 0.0, 0.0]) |
| 67 | +x_ref = jnp.array([0.0, 0.0, jnp.pi, 0.0]) |
| 68 | +u_ref = jnp.array([0.0]) |
| 69 | + |
| 70 | +Q = jnp.diag(jnp.array([1e-5 / dt, 1e-5 / dt, 1e-5 / dt, 1e-5 / dt])) |
| 71 | +R = jnp.diag(jnp.array([1e-4 / dt])) |
| 72 | +Q_f = jnp.diag(jnp.array([10.0, 10.0, 100.0, 100.0])) |
| 73 | +W = jnp.zeros((N, 1)) |
| 74 | + |
| 75 | + |
| 76 | +def cost(W, reference, x, u, t): |
| 77 | + del W, reference |
| 78 | + stage_cost = (x - x_ref).T @ Q @ (x - x_ref) + (u - u_ref).T @ R @ (u - u_ref) |
| 79 | + term_cost = (x - x_ref).T @ Q_f @ (x - x_ref) |
| 80 | + return jnp.where(t == N, 0.5 * term_cost, 0.5 * stage_cost) |
| 81 | + |
| 82 | + |
| 83 | +hessian_approx = None |
| 84 | + |
| 85 | + |
| 86 | +initial_X0 = jnp.tile(x0, (N + 1, 1)) |
| 87 | +initial_U0 = jnp.tile(u_ref, (N, 1)) |
| 88 | +initial_V0 = jnp.zeros((N + 1, n)) |
| 89 | + |
| 90 | +solver_mode = "fddp" |
| 91 | + |
| 92 | + |
| 93 | +def state_to_qpos(x): |
| 94 | + return x[2:] |
| 95 | + |
| 96 | + |
| 97 | +def state_to_qvel(x): |
| 98 | + return x[:2] |
0 commit comments