Mentions légales du service

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

MPC and methodo

parent 88861a10
Branches
Tags
No related merge requests found
Pipeline #1033288 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!")
```
%% Output
%% Cell type:markdown id:8b9d0e81 tags:
Requirement already satisfied: ipywidgets==8.0.6 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (8.0.6)
Requirement already satisfied: ipython>=6.1.0 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipywidgets==8.0.6) (8.13.2)
Requirement already satisfied: traitlets>=4.3.1 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipywidgets==8.0.6) (5.9.0)
Requirement already satisfied: ipykernel>=4.5.1 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipywidgets==8.0.6) (6.23.0)
Requirement already satisfied: widgetsnbextension~=4.0.7 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipywidgets==8.0.6) (4.0.7)
Requirement already satisfied: jupyterlab-widgets~=3.0.7 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipywidgets==8.0.6) (3.0.7)
Requirement already satisfied: psutil in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (5.9.5)
Requirement already satisfied: comm>=0.1.1 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (0.1.3)
Requirement already satisfied: jupyter-client>=6.1.12 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (8.2.0)
Requirement already satisfied: tornado>=6.1 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (6.3.2)
Requirement already satisfied: debugpy>=1.6.5 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (1.6.7)
Requirement already satisfied: packaging in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (23.1)
Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (5.3.0)
Requirement already satisfied: matplotlib-inline>=0.1 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (0.1.6)
Requirement already satisfied: pyzmq>=20 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (25.0.2)
Requirement already satisfied: nest-asyncio in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets==8.0.6) (1.5.6)
Requirement already satisfied: prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (3.0.38)
Requirement already satisfied: pickleshare in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (0.7.5)
Requirement already satisfied: decorator in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (5.1.1)
Requirement already satisfied: jedi>=0.16 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (0.18.2)
Requirement already satisfied: pygments>=2.4.0 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (2.15.1)
Requirement already satisfied: stack-data in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (0.6.2)
Requirement already satisfied: typing-extensions in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (4.5.0)
Requirement already satisfied: backcall in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (0.2.0)
Requirement already satisfied: pexpect>4.3 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.0.6) (4.8.0)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets==8.0.6) (0.8.3)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets==8.0.6) (2.8.2)
Requirement already satisfied: importlib-metadata>=4.8.3 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets==8.0.6) (6.6.0)
Requirement already satisfied: zipp>=0.5 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from importlib-metadata>=4.8.3->jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets==8.0.6) (3.15.0)
Requirement already satisfied: platformdirs>=2.5 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from jupyter-core!=5.0.*,>=4.12->ipykernel>=4.5.1->ipywidgets==8.0.6) (3.5.1)
Requirement already satisfied: ptyprocess>=0.5 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets==8.0.6) (0.7.0)
Requirement already satisfied: wcwidth in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30->ipython>=6.1.0->ipywidgets==8.0.6) (0.2.6)
Requirement already satisfied: six>=1.5 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from python-dateutil>=2.8.2->jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets==8.0.6) (1.16.0)
Requirement already satisfied: executing>=1.2.0 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from stack-data->ipython>=6.1.0->ipywidgets==8.0.6) (1.2.0)
Requirement already satisfied: asttokens>=2.1.0 in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from stack-data->ipython>=6.1.0->ipywidgets==8.0.6) (2.2.1)
Requirement already satisfied: pure-eval in /home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/lib/python3.9/site-packages (from stack-data->ipython>=6.1.0->ipywidgets==8.0.6) (0.2.2)
WARNING: You are using pip version 21.2.3; however, version 24.2 is available.
You should consider upgrading via the '/home/scerf/Documents/enseignements/Tuto/jupyter/tuto.venv/bin/python3 -m pip install --upgrade pip' command.
Note: you may need to restart the kernel to use updated packages.
Successful loading!
# An Optimal Controller
%% Cell type:markdown id:8b9d0e81 tags:
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^2(k+i) ]
$$
$$
u[1:H] = argmin (J)
J = \mu \sum_{i=1}^{H} (y(k+i) - y_{ref})^2 + \sum_{i=1}^{H} u(k+i)^2 )
$$
use model (a and b) to compute future values of y
apply $u[1]$
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))
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]),))
system.apply(u[0])
u_values.append(u[0])
plot_u_y(u_values, y_values, reference_value)
```
%% Output
%% 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:ea0f209d tags:
%% Cell type:markdown id:d421db9d tags:
[Back to menu](./00_Main.ipynb) or [Next chapter](./08_FeedForward.ipynb)
<div class="alert alert-danger" role="alert">
You need to scroll until the end to see the plots!
</div>
%% Cell type:code id:19502aed tags:
%% Cell type:markdown id:6c4ab817 tags:
``` python
```
### 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)
......
%% Cell type:markdown id:c3b876b7-b17f-4264-a7b9-9e11dc34ac18 tags:
# Methodology
# Complete Methodology
%% Cell type:code id:eae61ca7-8af2-46fc-8524-69c39237eb0f tags:
%% Cell type:markdown id:314baaad tags:
``` python
# TODO
```
In the previous chapters, you learned to identify a model, design controllers, and evaluate their performances. To be able to apply those methods on a system, an important step of formulation of the control problem is needed, for instance to define the output and control signals, the reference values, etc.
%% Cell type:code id:e3c6d1c3-e165-477f-9a69-a04ba0356eca tags:
%% Cell type:markdown id:7257aff7 tags:
``` python
```
## Formulate your own control problem !
%% Cell type:markdown id:b489b3e7 tags:
You are now invited to consider your prefered self-adaptive systems to formulate it as a feedback loop and design a controller.
If you have no specific system under study, pair up and join other groups !
Here are the step to follow:
1. **Connect** to the shared note [here](https://notes.inria.fr/MRc7TYzXSg2PLwnwlpXkAg#).
2. Make a **copy** of the slides for your team at the bottom of the note. The slides to copy are: System, Objective, Challenges, Sensor, Action, Objectives in terms of control, Analysis, Control design, Evaluation.
3. Take your time to answer the questions and **fill the slides** with your responses.
4. At the end of the session, we will take few mintues for a **restitution**. Please remove or comment out the questions from your slides, and only let your formulation of your control problem.
%% Cell type:markdown id:8e6b48ee tags:
[Back to menu](./00_Main.ipynb) or [Optional Exercice](./10_CiGri.ipynb)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment