diff --git a/docs/Project.toml b/docs/Project.toml index 94aa6a73..4533f990 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,8 +1,10 @@ [deps] +BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd" CTDirect = "790bbbee-bee9-49ee-8912-a9de031322d5" CTFlows = "1c39547c-7794-42f7-af83-d98194f657c2" CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" +DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" DocumenterMermaid = "a078cd44-4d9c-4618-b545-3ab9d77f9177" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" diff --git a/docs/make.jl b/docs/make.jl index 74850b8f..4b9f172e 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -29,17 +29,22 @@ makedocs( ), pages = [ "Introduction" => "index.md", + "Basic example" => [ + "Energy min (abstract syntax)" => "tutorial-basic-example.md", + "Energy min (functional syntax)" => "tutorial-basic-example-f.md", + "Time minimisation" => "tutorial-double-integrator.md", + ], + "Manual" => [ + "Abstract syntax" => "tutorial-abstract.md", + "Initial guess" => "tutorial-initial-guess.md", + "Plot a solution" => "tutorial-plot.md", + ], "Tutorials" => [ - "tutorial-basic-example.md", - "tutorial-basic-example-f.md", - "tutorial-double-integrator.md", - "tutorial-initial-guess.md", "tutorial-continuation.md", - "tutorial-nlp.md", - "tutorial-plot.md", - "tutorial-lqr-basic.md", + "Goddard: direct, indirect" => "tutorial-goddard.md", "tutorial-iss.md", - "tutorial-goddard.md", + "Linear–quadratic regulator" => "tutorial-lqr-basic.md", + "tutorial-nlp.md", ], "API" => [ "api-optimalcontrol.md", @@ -50,7 +55,7 @@ makedocs( ], ], "Developers" => [ - #"OptimalControl.jl" => "dev-optimalcontrol.md", + "OptimalControl.jl" => "dev-optimalcontrol.md", "Subpackages" => [ "CTBase.jl" => "dev-ctbase.md", "CTDirect.jl" => "dev-ctdirect.md", diff --git a/docs/src/juliacon2024.md b/docs/src/juliacon2024.md index f8804275..7907aa53 100644 --- a/docs/src/juliacon2024.md +++ b/docs/src/juliacon2024.md @@ -32,8 +32,7 @@ plus boundary, control and state constraints - [Basic example](tutorial-basic-example.html) - [Goddard problem](tutorial-goddard.html) -- [Orbit transfer](application-orbit.html) -- [Solar sailing](application-sail.html) +- [Orbit transfer](http://control-toolbox.org/kepler/stable) ## Wrap up diff --git a/docs/src/tutorial-abstract.md b/docs/src/tutorial-abstract.md new file mode 100644 index 00000000..9cf401d4 --- /dev/null +++ b/docs/src/tutorial-abstract.md @@ -0,0 +1,443 @@ +# [The abstract syntax to define an optimal control problem](@id abstract) + +The full grammar of OptimalControl.jl small *Domain Specific Language* is given below. The idea is to use a syntax that is +- pure Julia (and, as such, effortlessly analysed by the standard Julia parser), +- as close as possible to the mathematical description of an optimal control problem. + +While the syntax will be transparent to those users familiar with Julia expressions (`Expr`'s), we provide examples for every case that should be widely understandable. We rely heavily on [MLStyle.jl](https://github.com/thautwarm/MLStyle.jl) and its pattern matching abilities 👍🏽 for the semantic pass. Abstract definitions use the macro `@def`. + +## [Variable](@id variable) + +```julia +:( $v ∈ R^$q, variable ) +:( $v ∈ R , variable ) +``` + +A variable (only one is allowed) is a finite dimensional vector or reals that will be *optimised* along with state and control values. To define an (almost empty!) optimal control problem, named `ocp`, having a dimension two variable named `v`, do the following: + +```@example main +using OptimalControl #hide +@def ocp begin + v ∈ R², variable +end +``` + +Aliases `v₁` and `v₂` are automatically defined and can be used in subsequent expressions instead of `v[1]` and `v[2]`. The user can also define her own aliases for the components (one alias per dimension): + +```@example main +@def ocp begin + v = (a, b) ∈ R², variable +end +``` + +A one dimensional variable can be declared according to + +```@example main +@def ocp begin + v ∈ R, variable +end +``` + +## Time + +```julia +:( $t ∈ [$t0, $tf], time ) +``` + +The independent variable or *time* is a scalar bound to a given interval. Its name is arbitrary. + +```@example main +t0 = 1 +tf = 5 +@def ocp begin + t ∈ [t0, tf], time +end +``` + +One (or even the two bounds) can be variable, typically for minimum time problems (see [Mayer cost](#mayer) section): + +```@example main +@def ocp begin + v = (T, λ) ∈ R², variable + t ∈ [0, T], time +end +``` + +## [State](@id state) + +```julia +:( $x ∈ R^$n, state ) +:( $x ∈ R , state ) +``` + +The state declaration defines the name and the dimension of the state: + +```@example main +@def ocp begin + x ∈ R⁴, state +end +``` + +As for the variable, there are automatic aliases (`x₁` for `x[1]`, *etc.*) and the user can define her own aliases (one per scalar component of the state): + +```@example main +@def ocp begin + x = (q₁, q₂, v₁, v₂) ∈ R⁴, state +end +``` + +## [Control](@id control) + +```julia +:( $u ∈ R^$m, control ) +:( $u ∈ R , control ) +``` + +The control declaration defines the name and the dimension of the control: + +```@example main +@def ocp begin + u ∈ R², control +end +``` + +As before, there are automatic aliases (`u₁` for `u[1]`, *etc.*) and the user can define her own aliases (one per scalar component of the state): + +```@example main +@def ocp begin + u = (α, β) ∈ R², control +end +``` + +## [Dynamics](@id dynamics) + +```julia +:( ∂($x)($t) == $e1 ) +``` + +The dynamics is given in the standard vectorial ODE form: + +```math + \dot{x}(t) = f(x(t), u(t)) \quad \text{or} \quad \dot{x}(t) = f(t, x(t), u(t)) +``` + +depending on whether it is autonomous or not (the parser will detect dependence time, which entails that time and state must be declared prior to dynamics - an error will be issued otherwise). The symbol `∂` or the dotted state name +(`ẋ` can be used): + +```@example main +@def ocp begin + t ∈ [0, 1], time + x ∈ R², state + u ∈ R, control + ∂(x)(t) == [x₂(t), u(t)] +end +``` + +or + +```@example main +@def ocp begin + t ∈ [0, 1], time + x ∈ R², state + u ∈ R, control + ẋ(t) == [x₂(t), u(t)] +end +``` + +Any Julia code can be used, so the following is also OK: + +```@example main +@def ocp begin + t ∈ [0, 1], time + x ∈ R², state + u ∈ R, control + ẋ(t) == F₀(x(t)) + u(t) * F₁(x(t)) +end + +F₀(x) = [x[2], 0] +F₁(x) = [0, 1] +nothing # hide +``` + +!!! note + The vector fields `F₀` and `F₁` can be defined afterwards, as they only need to be available when the dynamics will be evaluated. + +Currently, it is not possible to declare the dynamics component after component, but a simple workaround is to use *aliases* (check the relevant [aliases](#aliases) section below): + +```@example main +@def damped_integrator begin + tf ∈ R, variable + t ∈ [0, tf], time + x = (q, v) ∈ R², state + u ∈ R, control + q̇ = v(t) + v̇ = u(t) - c(t) + ẋ(t) == [q̇, v̇] +end +``` + +## Constraints + +```julia +:( $e1 == $e2 ) +:( $e1 ≤ $e2 ≤ $e3 ) +:( $e2 ≤ $e3 ) +:( $e3 ≥ $e2 ≥ $e1 ) +:( $e2 ≥ $e1 ) +``` + +Constraints +- can be of five types: boundary, control, state, mixed, variable, +- be linear (ranges) or nonlinear (not ranges), +- equalities or (one or two-sided) inequalities. + +Boundary conditions are detected when the expression contains evaluations of the state at initial and / or final time bounds (*e.g.*, `x(0)`), and may not involve the control. Conversely control, state or mixed constraints will involve control, state or both evaluated at the declared time (*e.g.*, `x(t) + u(t)`). +Other combinations should be detected as incorrect by the parser 🤞🏾. The variable may be involved in any of the four previous constraints. Constraints involving the variable only are variable constraints, either linear or nonlinear. +In the example below, there are +- two linear boundary constraints, +- one linear variable constraint, +- one linear state constraint, +- one (two-sided) nonlinear control constraint. + +```@example main +@def ocp begin + tf ∈ R, variable + t ∈ [0, tf], time + x ∈ R², state + u ∈ R, control + x(0) == [-1, 0] + x(tf) == [0, 0] + ẋ(t) == [x₂(t), u(t)] + tf ≥ 0 + x₂(t) ≤ 1 + u(t)^2 ≤ 1 +end +``` + +!!! caveat + Write either `u(t)^2` or `(u^2)(t)`, not `u^2(t)` since in Julia the latter is means `u^(2t)`. Moreover, + in the case of equalities or of one-sided inequalities, the control and / or the state must belong the *left-hand side*. The following will error: + +```julia +julia> @def ocp begin + t ∈ [0, 2], time + x ∈ R², state + u ∈ R, control + x(0) == [-1, 0] + x(2) == [0, 0] + ẋ(t) == [x₂(t), u(t)] + 1 ≤ x₂(t) + -1 ≤ u(t) ≤ 1 +end +ERROR: ParsingError: +Line 7: 1 ≤ x₂(t) +bad constraint declaration +``` + +## [Mayer cost](@id mayer) + +```julia +:( $e1 → min ) +:( $e1 → max ) +``` + +Mayer costs are defined in a similar way to boundary conditions and follow the same rules. The symbol `→` is used +to denote minimisation of maximisation, the latter being treated by minimising the opposite cost. + +```@example main +@def ocp begin + tf ∈ R, variable + t ∈ [0, tf], time + x = (q, v) ∈ R², state + u ∈ R, control + tf ≥ 0 + -1 ≤ u(t) ≤ 1 + q(0) == 1 + v(0) == 2 + q(tf) == 0 + v(tf) == 0 + 0 ≤ q(t) ≤ 5 + -2 ≤ v(t) ≤ 3 + ẋ(t) == [v(t), u(t)] + tf → min +end +``` + +## Lagrange cost + +```julia +:( ∫($e1) → min ) +:( - ∫($e1) → min ) +:( $e1 * ∫($e2) → min ) +:( ∫($e1) → max ) +:( - ∫($e1) → max ) +:( $e1 * ∫($e2) → max ) +``` + +Lagrange (integral) costs are defined used the symbol `∫`, *with parenthesis: + +```@example main +@def ocp begin + t ∈ [0, 1], time + x = (q, v) ∈ R², state + u ∈ R, control + 0.5∫(q(t) + u(t)^2) → min +end +``` + +The integration range is implicitly equal to the time range, so the cost above is to be understood as +```math +\int_0^1 \left( q(t) + u^2(t) \right) \mathrm{d}t \to \min. +``` + +As for the dynamics, the parser will detect whether the integrand depends or not on time (autonomous / non-autonomous case). + +## Bolza cost + +```julia +:( $e1 + ∫($e2) → min ) +:( $e1 + $e2 * ∫($e3) → min ) +:( $e1 - ∫($e2) → min ) +:( $e1 - $e2 * ∫($e3) → min ) +:( $e1 + ∫($e2) → max ) +:( $e1 + $e2 * ∫($e3) → max ) +:( $e1 - ∫($e2) → max ) +:( $e1 - $e2 * ∫($e3) → max ) +:( ∫($e2) + $e1 → min ) +:( $e2 * ∫($e3) + $e1 → min ) +:( ∫($e2) - $e1 → min ) +:( $e2 * ∫($e3) - $e1 → min ) +:( ∫($e2) + $e1 → max ) +:( $e2 * ∫($e3) + $e1 → max ) +:( ∫($e2) - $e1 → max ) +:( $e2 * ∫($e3) - $e1 → max ) +``` + +Quite readily, Mayer and Lagrange costs can be combined into general Bolza costs. For instance as follows: + +```@example main +@def ocp begin + p = (t0, tf) ∈ R², variable + t ∈ [t0, tf], time + x = (q, v) ∈ R², state + u ∈ R², control + (tf - t0) + 0.5∫(c(t) * u(t)^2) → min +end +``` + +!!! caveat + The expression must be the sum of two terms (plus, possibly, a scalar factor before the integral), not *more*, so mind the parentheses. For instance, the following errors: + +```julia +julia> @def ocp begin + p = (t0, tf) ∈ R², variable + t ∈ [t0, tf], time + x = (q, v) ∈ R², state + u ∈ R², control + (tf - t0) + q(tf) + 0.5∫( c(t) * u(t)^2 ) → min + end +ERROR: ParsingError: +Line 5: (tf - t0) + q(tf) + 0.5 * ∫(c(t) * u(t) ^ 2) → min +bad objective declaration resulting in a Mayer term with trailing ∫ +``` + +The correct syntax is +```@example main +@def ocp begin + p = (t0, tf) ∈ R², variable + t ∈ [t0, tf], time + x = (q, v) ∈ R², state + u ∈ R², control + ( (tf - t0) + q(tf) ) + 0.5∫( c(t) * u(t)^2 ) → min +end +``` + +## [Aliases](@id aliases) + +```julia +:( $a = $e1 ) +``` + +The single `=` symbol is used to define not a constraint but an alias, that is a purely syntactic replacement. There are some automatic aliases, *e.g.* `x₁` for `x[1]` if `x` is the state, and we have also seen that the user can define her own aliases when declaring the [variable](#variable), [state](#state) and [control](#control). Arbitrary aliases can be further defined, as below (compare with previous examples in the [dynamics](#dynamics) section): + +```@example main +@def ocp begin + t ∈ [0, 1], time + x ∈ R², state + u ∈ R, control + F₀ = [x₂(t), 0] + F₁ = [0, 1] + ẋ(t) == F₀ + u(t) * F₁ +end + +``` + +!!! caveat + Such aliases do *not* define any additional function and are just replaced textually by the parser. In particular, they cannot be used outside the `@def` `begin ... end` block. + +!!! hint + You can use a trace mode for the macro `@def` to look at your code after expansions of the aliases adding `true` after your `begin ... end` block: + +```julia +julia> @def damped_integrator begin + tf ∈ R, variable + t ∈ [0, tf], time + x = (q, v) ∈ R², state + u ∈ R, control + q̇ = v(t) + v̇ = u(t) - c(t) + ẋ(t) == [q̇, v̇] + end true + +variable: tf, dim: 1 +time: t, initial time: 0, final time: tf +state: x, dim: 2 +control: u, dim: 1 +alias: q̇ = (x[2])(t) +alias: v̇ = u(t) - c(t) +dynamics: ẋ(t) == [(x[2])(t), u(t) - c(t)] +``` + +!!! caveat + The dynamics of an OCP is indeed a particular constraint, be careful to use `==` and not a single `=` that would try to define an alias: + +```julia +julia> @def double_integrator begin + tf ∈ R, variable + t ∈ [0, tf], time + x = (q, v) ∈ R², state + u ∈ R, control + q̇ = v + v̇ = u + ẋ(t) = [q̇, v̇] + end +ERROR: ParsingError: +Line 7: ẋ(t) = begin + #= REPL[35]:8 =# + [q̇, v̇] + end +forbidden alias name: (∂(x))(t) +``` + +## Misc + +- Declarations (of variable - if any -, time, state and control) must be done first. Then, dynamics, constraints and cost can be introduced in an arbitrary order. +- It is possible to provide numbers / labels (as in math equations) for the constraints to improve readability (this is mostly for future use, typically to retrieve the Lagrange multiplier associated with the discretisation of a given constraint): + +```@example main +@def damped_integrator begin + tf ∈ R, variable + t ∈ [0, tf], time + x = (q, v) ∈ R², state + u ∈ R, control + tf ≥ 0, (1) + q(0) == 2, (♡) + q̇ = v(t) + v̇ = u(t) - c(t) + ẋ(t) == [q̇, v̇] + x(t).^2 ≤ [1, 2], (state_con) +end +``` + +- Parsing errors should be explicit enough (with line number in the `@def` `begin ... end` block indicated) 🤞🏾 +- Check tutorials and applications in the documentation for further use. \ No newline at end of file diff --git a/docs/src/tutorial-basic-example-f.md b/docs/src/tutorial-basic-example-f.md index 43ed0a3c..794daa3a 100644 --- a/docs/src/tutorial-basic-example-f.md +++ b/docs/src/tutorial-basic-example-f.md @@ -1,4 +1,4 @@ -# [Basic example (functional version)](@id basic-f) +# [Double integrator: energy min (functional syntax)](@id basic-f) Let us consider a wagon moving along a rail, whom acceleration can be controlled by a force $u$. We denote by $x = (x_1, x_2)$ the state of the wagon, that is its position $x_1$ and its velocity $x_2$. diff --git a/docs/src/tutorial-basic-example.md b/docs/src/tutorial-basic-example.md index cf7f8d0d..48db765e 100644 --- a/docs/src/tutorial-basic-example.md +++ b/docs/src/tutorial-basic-example.md @@ -1,4 +1,4 @@ -# [Basic example](@id basic) +# [Double integrator: energy min (abstract syntax)](@id basic) Let us consider a wagon moving along a rail, whom acceleration can be controlled by a force $u$. We denote by $x = (x_1, x_2)$ the state of the wagon, that is its position $x_1$ and its velocity $x_2$. @@ -35,7 +35,7 @@ Then, we can define the problem ```@example main @def ocp begin - t ∈ [ 0, 1 ], time + t ∈ [0, 1], time x ∈ R², state u ∈ R, control x(0) == [ -1, 0 ] @@ -59,6 +59,8 @@ And plot the solution plot(sol) ``` +For a comprehensive introduction to the syntax used above to describe the optimal control problem, check [this tutorial](@ref abstract). + We can save the solution in a julia `.jld2` data file and reload it later, and also export a discretised version of the solution in a more portable [JSON](https://en.wikipedia.org/wiki/JSON) format. ```@example main diff --git a/docs/src/tutorial-continuation.md b/docs/src/tutorial-continuation.md index a3b5efd6..230a0981 100644 --- a/docs/src/tutorial-continuation.md +++ b/docs/src/tutorial-continuation.md @@ -24,7 +24,7 @@ and write a function that returns the OCP for a given final time ```@example main function ocp_T(T) @def ocp begin - t ∈ [ 0, T ], time + t ∈ [0, T], time x ∈ R², state u ∈ R, control q = x₁ diff --git a/docs/src/tutorial-double-integrator.md b/docs/src/tutorial-double-integrator.md index e7d74995..8e49755c 100644 --- a/docs/src/tutorial-double-integrator.md +++ b/docs/src/tutorial-double-integrator.md @@ -68,7 +68,7 @@ nothing # hide Solve it ```@example main -sol = solve(ocp; grid_size=200) +sol = solve(ocp; grid_size=100) nothing # hide ``` diff --git a/docs/src/tutorial-goddard.md b/docs/src/tutorial-goddard.md index 9892af35..b829438d 100644 --- a/docs/src/tutorial-goddard.md +++ b/docs/src/tutorial-goddard.md @@ -1,4 +1,4 @@ -# [Goddard problem](@id goddard) +# [Direct and indirect methods for the Goddard problem](@id goddard) ## Introduction @@ -33,25 +33,30 @@ $v(t) \leq v_{\max}$. The initial state is fixed while only the final mass is pr The Hamiltonian is affine with respect to the control, so singular arcs may occur, as well as constrained arcs due to the path constraint on the velocity (see below). -## Direct method - ```@setup main using Suppressor # to suppress warnings ``` -We import the `OptimalControl.jl` package to define the optimal control problem and -`NLPModelsIpopt.jl` to solve it. We import the `Plots.jl` package to plot the solution. The -`OrdinaryDiffEq.jl` package is used to define the shooting function for the indirect method and -the `NonlinearSolve.jl` package permits solve the shooting equation. +We import the `OptimalControl.jl` package to define the optimal control problem and +[`NLPModelsIpopt.jl`](https://github.com/JuliaSmoothOptimizers/NLPModelsIpopt.jl) to solve it. +We import the [`Plots.jl`](https://github.com/JuliaPlots/Plots.jl) package to plot the solution. +The [`OrdinaryDiffEq.jl`](https://github.com/SciML/OrdinaryDiffEq.jl) package is used to +define the shooting function for the indirect method and the +[`NonlinearSolve.jl`](https://github.com/SciML/NonlinearSolve.jl) and +[`MINPACK.jl`](https://github.com/sglyon/MINPACK.jl) packages permit to solve the shooting +equation. ```@example main -using OptimalControl -using NLPModelsIpopt -using OrdinaryDiffEq # to get the Flow function -using NonlinearSolve # NLE solver -using Plots +using OptimalControl # to define the optimal control problem and more +using NLPModelsIpopt # to solve the problem via a direct method +using OrdinaryDiffEq # to get the Flow function from OptimalControl +using NonlinearSolve # interface to NLE solvers +using MINPACK # NLE solver: use to solve the shooting equation +using Plots # to plot the solution ``` +## Optimal control problem + We define the problem ```@example main @@ -65,7 +70,7 @@ mf = 0.6 # final mass to target @def ocp begin # definition of the optimal control problem tf ∈ R, variable - t ∈ [ t0, tf ], time + t ∈ [t0, tf], time x = (r, v, m) ∈ R³, state u ∈ R, control @@ -100,10 +105,12 @@ end nothing # hide ``` +## Direct method + We then solve it ```@example main -direct_sol = solve(ocp; grid_size=100) +direct_sol = solve(ocp; grid_size=100, linear_solver="mumps") nothing # hide ``` @@ -113,7 +120,7 @@ and plot the solution plt = plot(direct_sol, solution_label="(direct)", size=(800, 800)) ``` -## Indirect method +## Structure of the solution We first determine visually the structure of the optimal solution which is composed of a bang arc with maximal control, followed by a singular arc, then by a boundary arc and the final @@ -216,6 +223,8 @@ fb = Flow(ocp, (x, p, tf) -> ub(x), (x, u, tf) -> g(x), (x, p, tf) -> μ(x, p)) nothing # hide ``` +## Shooting function + Then, we define the shooting function according to the optimal structure we have determined, that is a concatenation of four arcs. @@ -240,6 +249,8 @@ end nothing # hide ``` +## Initial guess + To solve the problem by an indirect shooting method, we then need a good initial guess, that is a good approximation of the initial costate, the three switching times and the final time. @@ -266,23 +277,54 @@ s = similar(p0, 7) @suppress_err begin # hide shoot!(s, p0, t1, t2, t3, tf) end # hide -println("Norm of the shooting function: ‖s‖ = ", norm(s), "\n") +println("\nNorm of the shooting function: ‖s‖ = ", norm(s), "\n") +``` + +## Indirect shooting + +We aggregate the data to define the initial guess vector. + +```@example main +ξ = [ p0 ; t1 ; t2 ; t3 ; tf ] # initial guess ``` -Finally, we can solve the shooting equations thanks to the [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl). +### NonlinearSolve.jl + +We first use the [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl) package to solve the shooting +equation. Let us define the problem. ```@example main -nle = (s, ξ, λ) -> shoot!(s, ξ[1:3], ξ[4], ξ[5], ξ[6], ξ[7]) # auxiliary function - # with aggregated inputs -ξ = [ p0 ; t1 ; t2 ; t3 ; tf ] # initial guess +# auxiliary function with aggregated inputs +nle! = (s, ξ, λ) -> shoot!(s, ξ[1:3], ξ[4], ξ[5], ξ[6], ξ[7]) -prob = NonlinearProblem(nle, ξ) -global indirect_sol = # hide -@suppress_err begin # hide -NonlinearSolve.solve(prob) # hide -# resolution of S(p0) = 0 -indirect_sol = NonlinearSolve.solve(prob; abstol=1e-8, reltol=1e-8, show_trace=Val(true)) -end # hide +# NLE problem with initial guess +prob = NonlinearProblem(nle!, ξ) +nothing # hide +``` + +Let us do some benchmarking. + +```@example main +using BenchmarkTools +@benchmark solve(prob; abstol=1e-8, reltol=1e-8, show_trace=Val(false)) +``` + +For small nonlinear systems, it could be faster to use the +[`SimpleNewtonRaphson()` descent algorithm](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/code_optimization/). + +```@example main +@benchmark solve(prob, SimpleNewtonRaphson(); abstol=1e-8, reltol=1e-8, show_trace=Val(false)) +``` + +Now, let us solve the problem and retrieve the initial costate solution. + +```@example main +global indirect_sol = # hide +@suppress_err begin # hide +solve(prob; show_trace=Val(false)) # hide +# resolution of S(ξ) = 0 +indirect_sol = solve(prob; abstol=1e-8, reltol=1e-8, show_trace=Val(true)) +end # hide # we retrieve the costate solution together with the times p0 = indirect_sol.u[1:3] @@ -291,6 +333,7 @@ t2 = indirect_sol.u[5] t3 = indirect_sol.u[6] tf = indirect_sol.u[7] +println("") println("p0 = ", p0) println("t1 = ", t1) println("t2 = ", t2) @@ -302,10 +345,101 @@ s = similar(p0, 7) @suppress_err begin # hide shoot!(s, p0, t1, t2, t3, tf) end # hide -println("Norm of the shooting function: ‖s‖ = ", norm(s), "\n") +println("\nNorm of the shooting function: ‖s‖ = ", norm(s), "\n") ``` -We plot the solution of the indirect solution (in red) over the solution of the direct method (in blue). +### MINPACK.jl + +```@setup main +using MINPACK +function fsolve(f, j, x; kwargs...) + try + MINPACK.fsolve(f, j, x; kwargs...) + catch e + println("Erreur using MINPACK") + println(e) + println("hybrj not supported. Replaced by hybrd even if it is not visible on the doc.") + MINPACK.fsolve(f, x; kwargs...) + end +end +``` + +Instead of the [`NonlinearSolve.jl`](https://github.com/SciML/NonlinearSolve.jl) package we can use the +[`MINPACK.jl`](https://github.com/sglyon/MINPACK.jl) package to solve +the shooting equation. To compute the Jacobian of the shooting function we use the +[`DifferentiationInterface.jl`](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface) package with +[`ForwardDiff`](https://github.com/JuliaDiff/ForwardDiff.jl) backend. + +```@example main +using DifferentiationInterface +import ForwardDiff +backend = AutoForwardDiff() +nothing # hide +``` + +Let us define the problem to solve. + +```@example main +# auxiliary function with aggregated inputs +nle! = ( s, ξ) -> shoot!(s, ξ[1:3], ξ[4], ξ[5], ξ[6], ξ[7]) + + # Jacobian of the (auxiliary) shooting function +jnle! = (js, ξ) -> jacobian!(nle!, similar(ξ), js, backend, ξ) +nothing # hide +``` + +We are now in position to solve the problem with the `hybrj` solver from `MINPACK` through the `fsolve` +function, providing the Jacobian. Let us do some benchmarking. + +```@example main +@benchmark fsolve(nle!, jnle!, ξ; show_trace=false) # initial guess given to the solver +``` + +We can also use the [preparation step](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface/stable/tutorial1/#Preparing-for-multiple-gradients) of `DifferentiationInterface.jl`. + +```@example main +extras = prepare_jacobian(nle!, similar(ξ), backend, ξ) +jnle_prepared!(js, ξ) = jacobian!(nle!, similar(ξ), js, backend, ξ, extras) +@benchmark fsolve(nle!, jnle_prepared!, ξ; show_trace=false) +``` + +Now, let us solve the problem and retrieve the initial costate solution. + +```@example main + +global indirect_sol = # hide +@suppress_err begin # hide +fsolve(nle!, jnle!, ξ; show_trace=false) # hide +# resolution of S(ξ) = 0 +indirect_sol = fsolve(nle!, jnle!, ξ, show_trace=true) +end # hide + +# we retrieve the costate solution together with the times +p0 = indirect_sol.x[1:3] +t1 = indirect_sol.x[4] +t2 = indirect_sol.x[5] +t3 = indirect_sol.x[6] +tf = indirect_sol.x[7] + +println("") +println("p0 = ", p0) +println("t1 = ", t1) +println("t2 = ", t2) +println("t3 = ", t3) +println("tf = ", tf) + +# Norm of the shooting function at solution +s = similar(p0, 7) +@suppress_err begin # hide +shoot!(s, p0, t1, t2, t3, tf) +end # hide +println("\nNorm of the shooting function: ‖s‖ = ", norm(s), "\n") +``` + +## Plot of the solution + +We plot the solution of the indirect solution (in red) over the solution of the direct method +(in blue). ```@example main f = f1 * (t1, fs) * (t2, fb) * (t3, f0) # concatenation of the flows diff --git a/docs/src/tutorial-initial-guess.md b/docs/src/tutorial-initial-guess.md index 4344ac01..e6f473cb 100644 --- a/docs/src/tutorial-initial-guess.md +++ b/docs/src/tutorial-initial-guess.md @@ -1,4 +1,4 @@ -# Initial guess options +# Initial guess (or iterate) for the resolution ```@meta CurrentModule = OptimalControl @@ -24,7 +24,7 @@ tf = 10 α = 5 @def ocp begin - t ∈ [ t0, tf ], time + t ∈ [t0, tf], time v ∈ R, variable x ∈ R², state u ∈ R, control diff --git a/docs/src/tutorial-iss.md b/docs/src/tutorial-iss.md index 2111718b..97789fe2 100644 --- a/docs/src/tutorial-iss.md +++ b/docs/src/tutorial-iss.md @@ -9,12 +9,15 @@ using Suppressor # to suppress warnings Let us start by importing the necessary packages. ```@example main -using OptimalControl -using OrdinaryDiffEq # to get the Flow function from OptimalControl -using NonlinearSolve # NLE solver: we get the fsolve function -using Plots +using OptimalControl # to define the optimal control problem and its flow +using OrdinaryDiffEq # to get the Flow function from OptimalControl +using NonlinearSolve # interface to NLE solvers +using MINPACK # NLE solver: use to solve the shooting equation +using Plots # to plot the solution ``` +## Optimal control problem + Let us consider the following optimal control problem: ```math @@ -38,7 +41,7 @@ xf = 0 α = 1.5 @def ocp begin - t ∈ [ t0, tf ], time + t ∈ [t0, tf], time x ∈ R, state u ∈ R, control @@ -53,6 +56,8 @@ end; nothing # hide ``` +## Boundary value problem + The **pseudo-Hamiltonian** of this problem is ```math @@ -84,6 +89,8 @@ where $[t]~= (x(t),p(t),u(x(t), p(t)))$. Our goal is to solve this (BVP). Solving (BVP) consists in solving the Pontryagin Maximum Principle which provides necessary conditions of optimality. +## Shooting function + To achive our goal, let us first introduce the pseudo-Hamiltonian vector field ```math @@ -135,31 +142,132 @@ Now, to solve the (BVP) we introduce the **shooting function**. \end{array} ``` -At the end, solving (BVP) is equivalent to solve $S(p_0) = 0$. +```@example main +S(p0) = π( φ(t0, x0, p0, tf) ) - xf # shooting function +nothing # hide +``` + +## Resolution of the shooting equation + +At the end, solving (BVP) is equivalent to solve $S(p_0) = 0$. This is what we call the +**indirect simple shooting method**. We define an initial guess. + +```@example main +ξ = [ 0.0 ] # initial guess +nothing # hide +``` + +### NonlinearSolve.jl -This is what we call the **indirect simple shooting method**. +We first use the [NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl) package to solve the shooting +equation. Let us define the problem. ```@example main -S(p0) = π( φ(t0, x0, p0, tf) ) - xf; # shooting function +nle! = (s, ξ, λ) -> s[1] = S(ξ[1]) # auxiliary function +prob = NonlinearProblem(nle!, ξ) # NLE problem with initial guess +nothing # hide +``` -nle = (s, ξ, λ) -> s[1] = S(ξ[1]) # auxiliary function -ξ = [ 0.0 ] # initial guess +Let us do some benchmarking. -prob = NonlinearProblem(nle, ξ) -global indirect_sol = # hide -@suppress_err begin # hide -NonlinearSolve.solve(prob) # hide -indirect_sol = NonlinearSolve.solve(prob) # resolution of S(p0) = 0 -end # hide +```@example main +using BenchmarkTools +@benchmark solve(prob; show_trace=Val(false)) +``` +For small nonlinear systems, it could be faster to use the +[`SimpleNewtonRaphson()` descent algorithm](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/code_optimization/). + +```@example main +@benchmark solve(prob, SimpleNewtonRaphson(); show_trace=Val(false)) +``` + +Now, let us solve the problem and retrieve the initial costate solution. + +```@example main +global indirect_sol = # hide +@suppress_err begin # hide +solve(prob; show_trace=Val(false)) # hide +indirect_sol = solve(prob; show_trace=Val(true)) # resolution of S(p0) = 0 +end # hide p0_sol = indirect_sol.u[1] # costate solution -println("costate: p0 = ", p0_sol) -@suppress_err begin # hide +println("\ncostate: p0 = ", p0_sol) +@suppress_err begin # hide println("shoot: |S(p0)| = ", abs(S(p0_sol)), "\n") -end # hide +end # hide +nothing # hide +``` + +### MINPACK.jl + +```@setup main +using MINPACK +function fsolve(f, j, x; kwargs...) + try + MINPACK.fsolve(f, j, x; kwargs...) + catch e + println("Erreur using MINPACK") + println(e) + println("hybrj not supported. Replaced by hybrd even if it is not visible on the doc.") + MINPACK.fsolve(f, x; kwargs...) + end +end +``` + +Instead of the [`NonlinearSolve.jl`](https://github.com/SciML/NonlinearSolve.jl) package we can use the +[`MINPACK.jl`](https://github.com/sglyon/MINPACK.jl) package to solve +the shooting equation. To compute the Jacobian of the shooting function we use the +[`DifferentiationInterface.jl`](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface) package with +[`ForwardDiff`](https://github.com/JuliaDiff/ForwardDiff.jl) backend. + +```@example main +using DifferentiationInterface +import ForwardDiff +backend = AutoForwardDiff() nothing # hide ``` +Let us define the problem to solve. + +```@example main +nle! = ( s, ξ) -> s[1] = S(ξ[1]) # auxiliary function +jnle! = (js, ξ) -> jacobian!(nle!, similar(ξ), js, backend, ξ) # Jacobian of nle +nothing # hide +``` + +We are now in position to solve the problem with the `hybrj` solver from `MINPACK` through the `fsolve` +function, providing the Jacobian. Let us do some benchmarking. + +```@example main +@benchmark fsolve(nle!, jnle!, ξ; show_trace=false) # initial guess given to the solver +``` + +We can also use the [preparation step](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface/stable/tutorial1/#Preparing-for-multiple-gradients) of `DifferentiationInterface.jl`. + +```@example main +extras = prepare_jacobian(nle!, similar(ξ), backend, ξ) +jnle_prepared!(js, ξ) = jacobian!(nle!, similar(ξ), js, backend, ξ, extras) +@benchmark fsolve(nle!, jnle_prepared!, ξ; show_trace=false) +``` + +Now, let us solve the problem and retrieve the initial costate solution. + +```@example main +global indirect_sol = # hide +@suppress_err begin # hide +fsolve(nle!, jnle!, ξ; show_trace=false) # hide +indirect_sol = fsolve(nle!, jnle!, ξ; show_trace=true) # resolution of S(p0) = 0 +end # hide +p0_sol = indirect_sol.x[1] # costate solution +println("\ncostate: p0 = ", p0_sol) +@suppress_err begin # hide +println("shoot: |S(p0)| = ", abs(S(p0_sol)), "\n") +end # hide +nothing # hide +``` + +## Plot of the solution + The solution can be plot calling first the flow. ```@example main diff --git a/docs/src/tutorial-lqr-basic.md b/docs/src/tutorial-lqr-basic.md index 10258d57..292368fa 100644 --- a/docs/src/tutorial-lqr-basic.md +++ b/docs/src/tutorial-lqr-basic.md @@ -1,4 +1,4 @@ -# LQR example +# A simple Linear–quadratic regulator example We consider the following Linear Quadratic Regulator (LQR) problem which consists in minimising @@ -51,7 +51,7 @@ B = [ 0 function lqr(tf) @def ocp begin - t ∈ [ 0, tf ], time + t ∈ [0, tf], time x ∈ R², state u ∈ R, control x(0) == x0 diff --git a/docs/src/tutorial-nlp.md b/docs/src/tutorial-nlp.md index 612f8df1..21e912d7 100644 --- a/docs/src/tutorial-nlp.md +++ b/docs/src/tutorial-nlp.md @@ -28,7 +28,7 @@ We define a test problem ```@example main @def ocp begin - t ∈ [ 0, 1 ], time + t ∈ [0, 1], time x ∈ R², state u ∈ R, control @@ -97,4 +97,4 @@ using Percival output = percival(nlp) print(output) -``` \ No newline at end of file +``` diff --git a/docs/src/tutorial-plot.md b/docs/src/tutorial-plot.md index 90e0a67c..85527eca 100644 --- a/docs/src/tutorial-plot.md +++ b/docs/src/tutorial-plot.md @@ -1,4 +1,4 @@ -# Plot a solution +# How to plot a solution In this tutorial we explain the different ways to plot a solution of an optimal control problem. @@ -14,14 +14,14 @@ Then, we define a simple optimal control problem and solve it. ```@example main @def ocp begin - t ∈ [ 0, 1 ], time + t ∈ [0, 1], time x ∈ R², state u ∈ R, control - x(0) == [ -1, 0 ] - x(1) == [ 0, 0 ] + x(0) == [-1, 0] + x(1) == [0, 0] - ẋ(t) == [ x₂(t), u(t) ] + ẋ(t) == [x₂(t), u(t)] ∫( 0.5u(t)^2 ) → min @@ -112,14 +112,14 @@ You can plot the solution of a second optimal control problem on the same figure ```@example main @def ocp begin - t ∈ [ 0, 1 ], time + t ∈ [0, 1], time x ∈ R², state u ∈ R, control - x(0) == [ -0.5, -0.5 ] - x(1) == [ 0, 0 ] + x(0) == [-0.5, -0.5] + x(1) == [0, 0] - ẋ(t) == [ x₂(t), u(t) ] + ẋ(t) == [x₂(t), u(t)] ∫( 0.5u(t)^2 ) → min @@ -187,7 +187,7 @@ B = [ 0 function lqr(tf) @def ocp begin - t ∈ [ 0, tf ], time + t ∈ [0, tf], time x ∈ R², state u ∈ R, control x(0) == x0 @@ -220,4 +220,4 @@ pu = plot(plt[5]; legend=false, xlabel="s", ylabel="u") using Plots.PlotMeasures # for leftmargin, bottommargin plot(px1, px2, pu; layout=(1, 3), size=(800, 300), leftmargin=5mm, bottommargin=5mm) -``` \ No newline at end of file +``` diff --git a/src/solve.jl b/src/solve.jl index f84faffd..c7a39e88 100644 --- a/src/solve.jl +++ b/src/solve.jl @@ -1,6 +1,6 @@ # -------------------------------------------------------------------------------------------------- # Resolution -import CommonSolve: solve +import CommonSolve: solve, CommonSolve # by order of preference algorithms = () @@ -66,7 +66,7 @@ julia> sol = solve(ocp, init=(state=t->[-1+t, t*(t-1)], control=t->6-12*t)) ``` """ -function solve(ocp::OptimalControlModel, description::Symbol...; +function CommonSolve.solve(ocp::OptimalControlModel, description::Symbol...; init=__ocp_init(), grid_size::Integer=CTDirect.__grid_size_direct(), display::Bool=CTDirect.__display(),