You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Double Pendulum: Can we make it stand on its head with Reinforcement Learning?
Imagine a double pendulum with rigid arms connecting weights of masses $m_1$ and $m_2$, with $m_1$ connected by a massless arm of length $l_1$ to a motorized cart of mass $m_m$, and $m_2$ connected to $m_1$ by a massless arm of length $l_2$.
We can express the positions of the masses $(x_1,y_1)$ and $(x_2, y_2)$ in terms of the position of the cart, $x_m$, and the angles of each of the arms relative to vertical, $\theta_1$ and $\theta_2$ as $x_1 = x_m + l_1\sin\theta_1$, $y_1 = -l_1\cos\theta_1$, $x_2 = x_1 + l_2\sin\theta_2$, and $y_2 = y_1 - l_2\cos\theta_2$.
With these definitions, we can express a lagrangian, $L(x_m, \dot{x}_m, \theta_1, \dot{\theta}_1, \theta_2, \dot{\theta}_2)$ as:
where the final term, $x_mF_m$ represents an external force applied to the motorized cart. Applying the Euler-Lagrange equations, we arrive at the equations of motion, first for $x_m$...
All of the physics implementation (with 4th order Runge-Kutta solver) is in DoublePendulum/physics. Next, we need to place this whole system into a reinforcement learning context, where we have the system in a state $s$, and an agent that acts on that state via actions $a$. In this case, our state is easy:
$$\mathbf{s}=(\mathbf{x}^T, F_m)$$
and our actions will be a differential force to apply over the next iteration timestep, $F_{t+1}=F_t + dF$. Using the gymnasium package, we can create a custom environment that can hold the state, apply actions, and, critically, define the reward for that action. The details of this implementation are in DoubplePendulum/envs. Finally, we can start with some machine learning!
The goal is going to be to learn a "policy" function $\pi$ that maps us from a state to an action. That function can be deterministic or stochastic. We've implemented both in DoubplePendulum/nn, using techniques that fall under the broad umbrella called "Q-learning", where we also try to learn a function $Q$ that estimates the reward of applying an action from $pi$ to a state; the deterministic implementation is a Deep Deterministic Policy Gradient model, and the stochastic implementation is a Soft Actor-Critic model. If you want to experiment with using these models, you can find example scripts in scripts. Coming soon: a widget to play with these models on my website!
About
Model a driven double pendulum, and try to get it to stand on its head with reinforcement learning!