Skip to content

Commit 61cd5a7

Browse files
Merge pull request #3676 from vyudu/pyomo
feat: PyomoDynamicOptProblem, docs
2 parents fdbf149 + 639ed6a commit 61cd5a7

File tree

11 files changed

+1078
-621
lines changed

11 files changed

+1078
-621
lines changed

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6"
7373
FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac"
7474
InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57"
7575
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
76+
Pyomo = "0e8e1daf-01b5-4eba-a626-3897743a3816"
7677

7778
[extensions]
7879
MTKBifurcationKitExt = "BifurcationKit"
@@ -81,6 +82,7 @@ MTKDeepDiffsExt = "DeepDiffs"
8182
MTKFMIExt = "FMI"
8283
MTKInfiniteOptExt = "InfiniteOpt"
8384
MTKLabelledArraysExt = "LabelledArrays"
85+
MTKPyomoDynamicOptExt = "Pyomo"
8486

8587
[compat]
8688
ADTypes = "1.14.0"
@@ -140,6 +142,7 @@ OrdinaryDiffEqCore = "1.15.0"
140142
OrdinaryDiffEqDefault = "1.2"
141143
OrdinaryDiffEqNonlinearSolve = "1.5.0"
142144
PrecompileTools = "1"
145+
Pyomo = "0.1.0"
143146
REPL = "1"
144147
RecursiveArrayTools = "3.26"
145148
Reexport = "0.2, 1"

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
55
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
66
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"
77
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
8+
DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d"
89
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
910
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
1011
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
1112
FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac"
1213
FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195"
14+
InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57"
15+
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
1316
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1417
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1518
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"

docs/src/API/dynamic_opt.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
### Solvers
2+
3+
Currently 4 backends are exposed for solving dynamic optimization problems using collocation: JuMP, InfiniteOpt, CasADi, and Pyomo.
4+
5+
Please note that there are differences in how to construct the collocation solver for the different cases. For example, the Python based ones, CasADi and Pyomo, expect the solver to be passed in as a string (CasADi and Pyomo come pre-loaded with Ipopt, but other solvers may need to be manually installed using `pip` or `conda`), while JuMP/InfiniteOpt expect the optimizer object to be passed in directly:
6+
7+
```
8+
JuMPCollocation(Ipopt.Optimizer, constructRK4())
9+
CasADiCollocation("ipopt", constructRK4())
10+
```
11+
12+
**JuMP** and **CasADi** collocation require an ODE tableau to be passed in. These can be constructed by calling the `constructX()` functions from DiffEqDevTools. The list of tableaus can be found [here](https://docs.sciml.ai/DiffEqDevDocs/dev/internals/tableaus/). If none is passed in, both solvers will default to using Radau second-order with five collocation points.
13+
14+
**Pyomo** and **InfiniteOpt** each have their own built-in collocation methods.
15+
16+
1. **InfiniteOpt**: The list of InfiniteOpt collocation methods can be found [in the table on this page](https://infiniteopt.github.io/InfiniteOpt.jl/stable/guide/derivative/). If none is passed in, the solver defaults to `FiniteDifference(Backward())`, which is effectively implicit Euler.
17+
2. **Pyomo**: The list of Pyomo collocation methods can be found [at the bottom of this page](https://github.com/SciML/Pyomo.jl). If none is passed in, the solver defaults to a `LagrangeRadau(3)`.
18+
19+
Some examples of the latter two collocations:
20+
21+
```julia
22+
PyomoCollocation("ipopt", LagrangeRadau(2))
23+
InfiniteOptCollocation(Ipopt.Optimizer, OrthogonalCollocation(3))
24+
```
25+
26+
```@docs; canonical = false
27+
JuMPCollocation
28+
InfiniteOptCollocation
29+
CasADiCollocation
30+
PyomoCollocation
31+
solve(::AbstractDynamicOptProblem)
32+
```
33+
34+
### Problem constructors
35+
36+
```@docs; canonical = false
37+
JuMPDynamicOptProblem
38+
InfiniteOptDynamicOptProblem
39+
CasADiDynamicOptProblem
40+
PyomoDynamicOptProblem
41+
```
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Solving Dynamic Optimization Problems
2+
3+
Systems in ModelingToolkit.jl can be directly converted to dynamic optimization or optimal control problems. In such systems, one has one or more input variables that are externally controlled to control the dynamics of the system. A dynamic optimization solves for the optimal time trajectory of the input variables in order to maximize or minimize a desired objective function. For example, a car driver might like to know how to step on the accelerator if the goal is to finish a race while using the least gas.
4+
5+
To begin, let us take a rocket launch example. The input variable here is the thrust exerted by the engine. The rocket state is described by its current height, mass, and velocity. The mass decreases as the rocket loses fuel while thrusting.
6+
7+
```@example dynamic_opt
8+
using ModelingToolkit
9+
t = ModelingToolkit.t_nounits
10+
D = ModelingToolkit.D_nounits
11+
12+
@parameters h_c m₀ h₀ g₀ D_c c Tₘ m_c
13+
@variables begin
14+
h(..)
15+
v(..)
16+
m(..), [bounds = (m_c, 1)]
17+
T(..), [input = true, bounds = (0, Tₘ)]
18+
end
19+
20+
drag(h, v) = D_c * v^2 * exp(-h_c * (h - h₀) / h₀)
21+
gravity(h) = g₀ * (h₀ / h)
22+
23+
eqs = [D(h(t)) ~ v(t),
24+
D(v(t)) ~ (T(t) - drag(h(t), v(t))) / m(t) - gravity(h(t)),
25+
D(m(t)) ~ -T(t) / c]
26+
27+
(ts, te) = (0.0, 0.2)
28+
costs = [-h(te)]
29+
cons = [T(te) ~ 0, m(te) ~ m_c]
30+
31+
@named rocket = System(eqs, t; costs, constraints = cons)
32+
rocket = mtkcompile(rocket, inputs = [T(t)])
33+
34+
u0map = [h(t) => h₀, m(t) => m₀, v(t) => 0]
35+
pmap = [
36+
g₀ => 1, m₀ => 1.0, h_c => 500, c => 0.5 * √(g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀,
37+
Tₘ => 3.5 * g₀ * m₀, T(t) => 0.0, h₀ => 1, m_c => 0.6]
38+
```
39+
40+
What we would like to optimize here is the final height of the rocket. We do this by providing a vector of expressions corresponding to the costs. By default, the sense of the optimization is to minimize the provided cost. So to maximize the rocket height at the final time, we write `-h(te)` as the cost.
41+
42+
Now we can construct a problem and solve it. Let us use JuMP as our backend here. Note that the package trigger is actually [InfiniteOpt](https://infiniteopt.github.io/InfiniteOpt.jl/stable/), and not JuMP - this package includes JuMP but is designed for optimization on function spaces. Additionally we need to load the solver package - we will use [Ipopt](https://github.com/jump-dev/Ipopt.jl) here (a good choice in general).
43+
44+
Here we have also loaded DiffEqDevTools because we will need to construct the ODE tableau. This is only needed if one desires a custom ODE tableau for the collocation - by default the solver will use RadauIIA5.
45+
46+
```@example dynamic_opt
47+
using InfiniteOpt, Ipopt, DiffEqDevTools
48+
jprob = JuMPDynamicOptProblem(rocket, [u0map; pmap], (ts, te); dt = 0.001)
49+
jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5()));
50+
```
51+
52+
The solution has three fields: `jsol.sol` is the ODE solution for the states, `jsol.input_sol` is the ODE solution for the inputs, and `jsol.model` is the wrapped model that we can use to query things like objective and constraint residuals.
53+
54+
Let's plot the final solution and the controller here:
55+
56+
```@example dynamic_opt
57+
using CairoMakie
58+
fig = Figure(resolution = (800, 400))
59+
ax1 = Axis(fig[1, 1], title = "Rocket trajectory", xlabel = "Time")
60+
ax2 = Axis(fig[1, 2], title = "Control trajectory", xlabel = "Time")
61+
62+
for u in unknowns(rocket)
63+
lines!(ax1, jsol.sol.t, jsol.sol[u], label = string(u))
64+
end
65+
lines!(ax2, jsol.input_sol, label = "Thrust")
66+
axislegend(ax1)
67+
axislegend(ax2)
68+
fig
69+
```
70+
71+
### Free final time problems
72+
73+
There are additionally a class of dynamic optimization problems where we would like to know how to control our system to achieve something in the least time. Such problems are called free final time problems, since the final time is unknown. To model these problems in ModelingToolkit, we declare the final time as a parameter.
74+
75+
Below we have a model system called the double integrator. We control the acceleration of a block in order to reach a desired destination in the least time.
76+
77+
```@example dynamic_opt
78+
@variables begin
79+
x(..)
80+
v(..)
81+
u(..), [bounds = (-1.0, 1.0), input = true]
82+
end
83+
84+
@parameters tf
85+
86+
constr = [v(tf) ~ 0, x(tf) ~ 0]
87+
cost = [tf] # Minimize time
88+
89+
@named block = System(
90+
[D(x(t)) ~ v(t), D(v(t)) ~ u(t)], t; costs = cost, constraints = constr)
91+
92+
block = mtkcompile(block; inputs = [u(t)])
93+
94+
u0map = [x(t) => 1.0, v(t) => 0.0]
95+
tspan = (0.0, tf)
96+
parammap = [u(t) => 0.0, tf => 1.0]
97+
```
98+
99+
The `tf` mapping in the parameter map is treated as an initial guess.
100+
101+
Please note that, at the moment, free final time problems cannot support constraints defined at definite time values, like `x(3) ~ 2`.
102+
103+
!!! warning
104+
105+
The Pyomo collocation methods (LagrangeRadau, LagrangeLegendre) currently are bugged for free final time problems. Strongly suggest using BackwardEuler() for such problems when using Pyomo as the backend.
106+
107+
When declaring the problem in this case we need to provide the number of steps, since dt can't be known in advanced. Let's solve plot our final solution and the controller for the block, using InfiniteOpt as the backend:
108+
109+
```@example dynamic_opt
110+
iprob = InfiniteOptDynamicOptProblem(block, [u0map; parammap], tspan; steps = 100)
111+
isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer));
112+
```
113+
114+
Let's plot the final solution and the controller here:
115+
116+
```@example dynamic_opt
117+
fig = Figure(resolution = (800, 400))
118+
ax1 = Axis(fig[1, 1], title = "Block trajectory", xlabel = "Time")
119+
ax2 = Axis(fig[1, 2], title = "Control trajectory", xlabel = "Time")
120+
121+
for u in unknowns(block)
122+
lines!(ax1, isol.sol.t, isol.sol[u], label = string(u))
123+
end
124+
lines!(ax2, isol.input_sol, label = "Acceleration")
125+
axislegend(ax1)
126+
axislegend(ax2)
127+
fig
128+
```

0 commit comments

Comments
 (0)