Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 97209a66 authored by CERF Sophie's avatar CERF Sophie
Browse files

removing MPC fmin messages

parent 79ff7712
Branches
No related tags found
No related merge requests found
Pipeline #1033416 passed
%% Cell type:markdown id:5ecd8455-ffcf-4230-adaf-6514c725b40b tags:
# Model-Predictive Control
%% Cell type:code id:cbfec597 tags:
``` python
%pip install ipywidgets==8.0.6
from tuto_control_lib.systems import IntroSystem
from tuto_control_lib.plot import *
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact
from scipy.optimize import fmin
print("Successful loading!")
```
%% Cell type:markdown id:8b9d0e81 tags:
# An Optimal Controller
Optimal control is an alternative to PID controllers. Rather than computing the action as a function of the error, it is the solution of an optimization problem. Optimal control consists in finding the **control actions that optimize a cost function**, often formulated as a performance measuren and relying on the system's model.
For linear systems, their exist an analytical solution. For non-linear systems (e.g. if we have a constraint on the control action) the optimal solution can be approached numerically. **Model-Predictive Control (MPC)** is a form of optimal control that allows a **trade-off between optimality and computation cost**, by considering optimization on a finite horizon.
# Model-Predictive Control
## Principle
A Model-Predictive Controller consists in the following steps:
1. **Predict** the future evolution of the output, given the model and hypothetical control actions, over a finite horizon,
2. **Optimize** a cost function by soundly selecting a series of actions,
3. **Apply the first action**, and
4. **Repeat** each time step.
Applying only the first action and optimizing at each step has the benefits of a **feedback approach**: it can correct uncertainties in the predictions or the model, and compensate for some disturbances.
## Cost function
The usual formulation of the MPC cost function is design to ensure reference tracking (the output signal should converge to the reference value $y - y_{ref}$) with reasonable efforts (reasonable actions $|u|$):
$$
J = \mu \sum_{i=1}^{H} (y(k+i) - y_{ref})^2 + \sum_{i=1}^{H} u(k+i)^2 )
$$
A weighting factor $\mu$ allows specifying the relative important of both objectives.
$H$ is the lenght of the prediction horizon.
Let us define the above cost function, replacing all future value of the output by their prediction using the model (e.g. $a$ and $b$):
%% Cell type:code id:55c4897b tags:
``` python
def cost_function(u, parameters):
y_old, reference_value, a, b, weight = parameters
reference_tracking = 0
action_cost = 0
H = np.size(u)
for i in range(H):
sum_actions = 0
for j in range(i+1):
sum_actions += a**(i-j)*u[j]
reference_tracking += (a**(i+1)*y_old + b*sum_actions - reference_value)**2
action_cost += u[i]**2
cost_value = weight*reference_tracking + action_cost
return cost_value
```
%% Cell type:markdown id:f074aecb tags:
## Implementation
At each time step, the MPC consists in:
1. Computing the action trajectory that minimizes the cost over the horizon $H$:
$$u(1:H) = argmin (J)$$
2. apply $u(1)$
%% Cell type:code id:24a4b359 tags:
``` python
@interact(H=(0, 5, 1), weight=(1,50,1))
@interact(H=(1, 5, 1), weight=(1,20,1))
def draw_p(H=2, weight=10):
system, u_values, y_values, a, b, max_iter = IntroSystem(), [], [], 0.8, 0.5, 100
reference_value = 1
for i in range(max_iter):
y = system.sense()
y_values.append(y)
u = fmin(cost_function,np.ones((H,1)),args=(np.array([y, reference_value, a, b, weight]),))
u = fmin(cost_function,np.ones((H,1)),args=(np.array([y, reference_value, a, b, weight]),), disp=0)
system.apply(u[0])
u_values.append(u[0])
plot_u_y(u_values, y_values, reference_value)
```
%% Cell type:markdown id:3053e74f tags:
<div class="alert alert-info" role="alert">
Try different values of the horizon and the weight factor. How does the controller perform compared to a PID ?
</div>
%% Cell type:markdown id:d421db9d tags:
<div class="alert alert-danger" role="alert">
You need to scroll until the end to see the plots!
</div>
%% Cell type:markdown id:6c4ab817 tags:
### Bibliography
Kouvaritakis, B., & Cannon, M. (2016). Model predictive control. Switzerland: Springer International Publishing, 38, 13-56.
%% Cell type:markdown id:ea0f209d tags:
[Back to menu](./00_Main.ipynb) or [Next chapter](./08_FeedForward.ipynb)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment