Skip to content

Your first problem ​

We take the clamped beam: a double integrator whose position must stay below a bound, with a quadratic cost. We build it with OptimalControl, solve it, and compare the objective to the reference of the collection.

julia
using OptimalControl, OptimalControlProblems
using NLPModelsIpopt

Get the problem ​

A problem is a function, called with the modeling as argument. With OptimalControl it returns a Problem: the continuous problem and its initial guess.

julia
pb = beam(OptimalControlBackend())
Problem beam (OptimalControl modeling, valid)
  state dimension:    2
  control dimension:  1
  variable dimension: 0
julia
model(pb)
Abstract definition:

    t ∈ [t0, tf], time
    x ∈ R², state
    u ∈ R, control
    x(t0) == [x₁_t0, x₂_t0]
    x(tf) == [x₁_tf, x₂_tf]
    x₁_l ≤ x₁(t) ≤ x₁_u
    ẋ(t) == [x₂(t), u(t)]
    ∫(u(t) ^ 2) → min

The (autonomous) optimal control problem is of the form:

    minimize  J(x, u) = ∫ f⁰(x(t), u(t)) dt, over [0, 1]

    subject to

        ẋ(t) = f(x(t), u(t)), t in [0, 1] a.e.,

        ϕ₋ ≤ ϕ(x(0), x(1)) ≤ ϕ₊, 
        x₋ ≤ x(t) ≤ x₊, 

    where x(t) ∈ R² and u(t) ∈ R.

Solve it ​

The discretization is chosen in solve: here the trapezoidal rule on the default grid of the problem, an ADNLPModels model and Ipopt. The reference objectives of the collection are computed with the trapezoidal rule, which is not the default scheme of solve: give it.

julia
N = grid_size_data(:beam, OptimalControlBackend())
sol = solve(
    model(pb), :collocation, :adnlp, :ipopt;
    init=initial_guess(pb),
    grid_size=N,
    scheme=:trapeze,
    display=false,
)
objective(sol)
8.889281808581273

Check the result ​

The collection stores a reference objective for each problem and modeling, discretized on the default grid of the modeling with the trapezoidal rule: the best objective reached by the configurations of the modeling tested.

julia
reference = OptimalControlProblems.reference_objective(:beam, OptimalControlBackend())
8.889281808581256
julia
abs(objective(sol) - reference) / abs(reference)

The problem page shows the same comparison for every modeling and configuration, and how the JuMP model of the same problem reaches the same objective.

Next ​