This project's purpose is to implement a genetic algorithm in order to determine parameters of an inverted pendulum full-state feedback controller
The main.py is the main file which you can execute in order to start a simulation and which contains a basic system configuration where you can choose some inverted pendulum parameters as well as the genetic algorithm ones. You can also choose LQR controller in order to compare it with the genetic algorithm based controller.
Let's compare how the genetic algorithm does its job in comparison to based on mathematical derivations LQR controller - for this purpose, let's define the inverted pendulum's physical properties, create some simulation scenario with the system's initial conditions vector
- Pendulum:
- length:
$0.8 \text{ } m$ , - mass:
$0.3 \text{ } kg$ , - friction:
$0.05$ .
- length:
- Cart:
- mass:
$0.6 \text{ } kg$ , - friction:
$0.1$ .
- mass:
- Gravitational acceleration:
$9.81 \text{ } m/s^2$ .
Vector
The point of the inverted pendulum control is to make it maintain a predefined angle
This strategy allows to compute the controller's
The state-space matrices have been determined as follows:
The weighting matrices have been defined as follows:
Animation below shows how the LQR controller manages to stabilize the system:
In order to proceed with the genetic algorithm approach, its parameters have to be firstly determined. After several simulations, values leading to the best solutions with respect to computational effort, have been determined as follows:
- population size: 20,
- chromosome size: 4,
- gene bounds: (0, 100.01),
- mutation probability: 0.2,
- crossover probability: 0.4,
- crossover rate: 0.2.
An objective function, being the most crucial genetic algorithm's parameter directly responsible for the evolution curve shape has been determined as follows:
Why integral of a pendulum's angle to ensure staying in upright postion hasn't been included?
Because simulation is terminated every time when pendulum's angle exceeds 90 degrees. The quicker it does so, the less the simulation time. The less the simulation time, the less the value of objective function defined above. The less the value of objective function, the less likely to survive is the given individual causing pendulum to fall. The less likely the given ok let's finally check out how does this stuff work.
The fittest individual of 100 generations:
The plot below shows comparison between the mathematically derived LQR and the genetic algorithm approach.
The LQR advantage over the genetic algorithm based controller can be clearly seen. But is it so in every scenario? How about varying friction? Varying masses? The LQR controller concept is about linearization of the inverted pendulum mathematical model. If we add to the model more non-linearities (by changing assumed as constant physical properties mentioned earlier to varying), we will be able to observe how linearized model differs from the "realistic" one.
Let's start with the pendulum friction. We defined its value before as 0.05. But in the reality it is not constant over the whole angular range within which pendulum can rotate. From the realistic example we could expect the friction to be greater at some points and less at the others. Let's arbitrary define its change with respect to the angular position of the pendulum as on the graph showed below:
Comparison of both approaches in this scenario:
Looks like this time it was easier for the genetic algorithm based controller to catch up with the LQR.
Again, we arbitrary define friction change over the cart's position
All together put on one graph:
The plot above shows that for shorter distances between
Finally, let's investigate an impact of varying in time pendulum's mass on control conditions.
Graph showing simulation outocomes for both approaches:
Here the advantage of genetic algorithm based controller is less visible. The reason for such case is the probabilistic nature of this strategy - the solutions does not necessarily always have to converge to an actual objective function's minimum.
The genetic algorithm can provide more reliable results when the model consists of additional non-linearities. However, provided more computational power to easily handle population size of 50 and gene range extended to 1000.0 performed for 1000 generations, the outcome of the genetic algorithm computations (GA) for a model WITHOUT extra non-linearities, in comparison to the LQR, looks like in the below graph:
It means that even weak non-linearities, derived from the inverted pendulum physical model using only the II Newton's principle, are enough for the genetic algorithm to prove its advantage over linearized LQR approach.