Learning a swing-up policy for an inverted pendulum with no model of the system. Torque is limited, so the pendulum has to rock back and forth to build enough energy to invert.
The Q-learning algorithm took 6000 episodes to learn how to invert the pendulum. In the video above there is one back-and-forth swing before the pendulum lifts up and stays inverted.
Approach

The inverted pendulum has a limit on the maximum torque it can apply, so it must perform a few back-and-forth motions to reach the inverted position (\(\theta=\pi\)) from standing still (\(\theta=0\)).
The state is written as \(x = \begin{pmatrix} \theta \\ \omega \end{pmatrix}\). The dynamics are time-discretised, with \(x_n\) the state at time \(t = n \Delta t\) for a discretisation step \(\Delta t\). The following discounted cost is minimised, \(\sum_{i=0}^{\infty} \alpha^i g(x_i, u_i)\), where:
This cost mostly penalises deviation from the inverted position, but also encourages small velocities and small control effort.
Control of \(u \in \{-4,0,4\}\)
One back-and-forth swing precedes the lift. The graph below shows cost per episode in blue and average cost in orange, falling as learning proceeds. This control scheme took 6000 episodes to converge.



Control of \(u \in \{-5,0,5\}\)
The extra control effort allows quicker learning in the Q-table, visible as a lower final cost of around 200. The policy inverts the pendulum faster than the \(u \in \{-4,0,4\}\) case — a single swing, with no back-and-forth needed. It again took 6000 episodes, learning slightly more in that time.



Acknowledgements
I would like to specially thank Professor Ludovic Righetti for allowing me to showcase this project on my website.