Differential Equations Solution
Differential Equations Solution
uses DifferentialEquations.jl
to solve the Problems. Build the problem and call generateDEcodes
, work done!
OptControl.generateDEcodes
— FunctiongenerateDEcodes(L, F, state, u, tspan, t0, tf) -> Any
generateDEcodes(L, F, state, u, tspan, t0, tf, Φ) -> Any
generateDEcodes(
L,
F,
state,
u,
tspan,
t0,
tf,
Φ,
dt;
guess,
writeFilePath
) -> Any
generateDEcodes
will generate the solution code that using DifferentialEquations.jl
to solve problem
A general form of optimal control problem:
\[min (\Phi(\boldsymbol{x}(t_f),t_f)+\int_{t_0}^{t_f} L[\boldsymbol{x}(t),\boldsymbol{u}(t),t]dt) \\ s.t. \hspace{0.4cm} \dot{\boldsymbol{x}} =f[\boldsymbol{x}(t),\boldsymbol{u}(t),t]\]
args:
L
: above L
\[L[\boldsymbol{x}(t),\boldsymbol{u}(t),t]\]
F
: above f
\[f[\boldsymbol{x}(t),\boldsymbol{u}(t),t]\]
state
: states(variable) inF
u
: control variable u inF
tspan
: time fieldt0
: initial value attspan[1]
, length oft0
must be equal to length of statetf
: final(end) value attspan[2]
, length oftf
must be equal to length of stateΦ
: above Φ, default: nothing
\[\Phi(\boldsymbol{x}(t_f),t_f)\]
dt
: dt of BVProblem, default: 0.1guess
: initial value of BVProblem, default: 1.0. length of guess must be equal to (2*length(state)+length(u))writeFilePath
: path of generated code, default: nothing
Example:
using OptControl
@variables t u x[1:2]
f = [0 1; 0 0] * x + [0, 1] * u
L = 0.5 * u^2
t0 = [1.0, 1.0]
tf = [0.0, 0.0]
tspan = (0.0, 2.0)
sol = generateDEcodes(L, f, x, u, tspan, t0, tf)
Overview of generateDEcodes
and generateJuMPcodes
generateDEcodes
and generateJuMPcodes
using the same way to build problem, and they both can solve Fixed Value(Example1) and Free End(Example2). But generateDEcodes
cannot deal with End constraint and Add variable limit. Meanwhile, generateDEcodes
have lower accuracy than generateJuMPcodes
. Maybe accuracy can be improved by pass different solver parameters. You can have a try.
Example | generateJuMPcodes | generateDEcodes |
---|---|---|
1. Fixed Value | ✅ | ✅ |
2. Free Value | ✅ | ✅ |
3. Constraint | ✅ | ❌ |
4. Multiple x and u | ✅ | ✅ |
5. variable limit | ✅ | ❌ |
✅ = supported
❌ = not supported
Example 1: Fixed value
To solve:
\[min \int_{0}^{2} u^2dt \newline s.t. ~~~~~ \dot{\boldsymbol{x}} =\begin{bmatrix}0&1 \newline 0&0\end{bmatrix}\boldsymbol{x}+ \begin{bmatrix}0 \newline 1 \end{bmatrix}u \newline \boldsymbol{x}(0) = \begin{bmatrix} 1 \newline 1 \end{bmatrix}, \boldsymbol{x}(2)=\begin{bmatrix} 0 \newline 0 \end{bmatrix}\]
Just define variables and build functions. Call generateDEcodes
and get the results.
The analytical solution of $x_1$ is
\[x_1(t) = 0.5*t^3-1.75*t^2+t+1\]
and we can campare the difference between them by using Mean Square Error(MSE).
using OptControl, Statistics, ModelingToolkit
@variables t u x[1:2]
f = [0 1; 0 0] * x + [0, 1] * u
L = 0.5 * u^2
t0 = [1.0, 1.0]
tf = [0.0, 0.0]
tspan = (0.0, 2.0)
sol = generateDEcodes(L, f, x, u, tspan, t0, tf)
nu = [sol.u[i][1] for i in 1:length(sol.u)]
xs = collect(range(tspan[1], tspan[2], length=length(sol.u)))
an = @.(0.5 * xs^3 - 1.75 * xs^2 + xs + 1)
mean((an - nu).^2)
7.279902475761595e13
Example 2: Free End
To solve:
\[min \int_{0}^{2} u^2dt \newline s.t. ~~~~~ \dot{\boldsymbol{x}} =\begin{bmatrix}0&1 \newline 0&0\end{bmatrix}\boldsymbol{x}+ \begin{bmatrix}0 \newline 1 \end{bmatrix}u \newline \boldsymbol{x}(0) = \begin{bmatrix} 1 \newline 1 \end{bmatrix}, \boldsymbol{x}(1)=\begin{bmatrix} 0 \newline free \end{bmatrix}\]
If variable is free, use nothing
.
The analytical solution of $x_1$ is
\[x_1(t) = t^3-3.0*t^2+t+1\]
and get MSE.
using OptControl, Statistics, ModelingToolkit
@variables t u x[1:2]
f = [0 1; 0 0] * x + [0, 1] * u
L = 0.5 * u^2
t0 = [1.0, 1.0]
tf = [0.0, nothing]
tspan = (0.0, 1.0)
N = 100
sol = generateDEcodes(L, f, x, u, tspan, t0, tf)
nu = [sol.u[i][1] for i in 1:length(sol.u)]
xs = collect(range(tspan[1], tspan[2], length=length(sol.u)))
an = @.(xs^3 - 3.0 * xs^2 + xs + 1)
mean((an - nu).^2)
0.020118421612599108
Example 3: Multiple x
and u
To solve:
\[min \int_{0}^{2} u^2dt \newline s.t. ~~~~~ \dot{\boldsymbol{x}} =\begin{bmatrix}0&1 \newline 0&0\end{bmatrix}\boldsymbol{x}+ \begin{bmatrix}1&0 \newline 0&1 \end{bmatrix}\boldsymbol{u} \newline \boldsymbol{x}(0) = \begin{bmatrix} 1 \newline 1 \end{bmatrix}, \boldsymbol{x}(2)=\begin{bmatrix} 0.0 \newline free \end{bmatrix} \]
The analytical solution of $u_1,u_2$ is
\[\begin{matrix} u_1=-\frac{9}{14}\\u_2=\frac{9}{14}*t-\frac{9}{7} \end{matrix} \]
and get MSE.
using OptControl, Statistics, ModelingToolkit
@variables t u[1:2] x[1:2]
f = [0 1; 0 0] * x + [1 0; 0 1] * u
L = 0.5 * (u[1]^2 + u[2]^2)
t0 = [1.0, 1.0]
tf = [0.0, nothing]
tspan = (0.0, 2.0)
sol = generateDEcodes(L, f, x, u, tspan, t0, tf)
nu1 = [sol.u[i][5] for i in 1:length(sol.u)]
nu2 = [sol.u[i][6] for i in 1:length(sol.u)]
xs = collect(range(tspan[1], tspan[2], length=length(sol.u)))
an_u2 = @.(9 / 14 * xs - 9 / 7)
res1 = mean((-9 / 14 .- nu1).^2)
res2 = mean((an_u2 - nu2).^2)
(res1,res2)
(1.4141364774585914e13, 1.3718426377342753e14)