Skip to content

Commit f7d246f

Browse files
committed
docs: make examples dynamic
1 parent 5f33573 commit f7d246f

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

docs/src/tutorials/dynamic_optimization.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
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.
33

44
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 and velocity.
5-
```julia
5+
6+
```@example dynamic_opt
67
using ModelingToolkit
78
t = ModelingToolkit.t_nounits
89
D = ModelingToolkit.D_nounits
@@ -37,19 +38,25 @@ pmap = [
3738
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.
3839

3940
Now we can construct a problem and solve it. Let us use JuMP as our backend here.
40-
```julia
41+
```@example dynamic_opt
42+
using InfiniteOpt
4143
jprob = JuMPDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false)
4244
jsol = solve(jprob, JuMPCollocation(Ipopt.Optimizer, constructRadauIIA5()))
4345
```
4446

4547
Let's plot our final solution and the controller here:
46-
```julia
48+
```@example dynamic_opt
49+
using CairoMakie
50+
plot(
51+
plot(jsol.sol, title = "Rocket trajectory"),
52+
plot(jsol.input_sol, title = "Control trajectory")
53+
)
4754
```
4855

4956
###### Free final time problems
5057
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.
5158

52-
```julia
59+
```@example dynamic_opt
5360
@variables x(..) v(..)
5461
@variables u(..) [bounds = (-1.0, 1.0), input = true]
5562
@parameters tf
@@ -69,8 +76,14 @@ parammap = [u(t) => 0.0, tf => 1.0]
6976

7077
Please note that, at the moment, free final time problems cannot support constraints defined at definite time values, like `x(3) ~ 2`.
7178

72-
Let's plot our final solution and the controller for the block:
73-
```julia
79+
!!! warning
80+
81+
The Pyomo collocation methods (LagrangeRadau, LagrangeLegendre) currently are bugged for free final time problems. Strongly suggest using BackwardEuler() for such problems.
82+
83+
Let's solve plot our final solution and the controller for the block, using InfiniteOpt as the backend:
84+
```@example dynamic_opt
85+
iprob = InfiniteOptDynamicOptProblem(rocket, u0map, (ts, te), pmap; dt = 0.001, cse = false)
86+
isol = solve(iprob, InfiniteOptCollocation(Ipopt.Optimizer))
7487
```
7588

7689
### Solvers
@@ -86,7 +99,7 @@ CasADiCollocation("ipopt", constructRK4())
8699

87100
**Pyomo** and **InfiniteOpt** each have their own built-in collocation methods.
88101
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.
89-
2. **Pyomo**: The list of Pyomo collocation methods can be found [here](). If none is passed in, the solver defaults to a `LagrangeRadau(3)`.
102+
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)`.
90103

91104
```@docs; canonical = false
92105
JuMPCollocation

0 commit comments

Comments
 (0)