Ducted fan

Description of the problem

The ducted fan problem is a classical nonlinear benchmark in optimal control with multiple input and state constraints. It models the planar motion of a ducted fan aircraft, described by its horizontal and vertical positions $(x_1, x_2)$, the angle $\alpha$ with respect to the vertical, and their velocities $(v_1, v_2, v_\alpha)$. The inputs are the body-fixed thrust components $(u_1, u_2)$, generated by moving flaps at the end of the duct.

The objective is to steer the fan from the origin to a horizontal position of $1$ m at altitude $0$, with zero final velocities and attitude, in a free final time $t_f$, while minimising a trade-off between control effort and transition time.

Mathematical formulation

\[\begin{aligned} \min_{x_1, v_1, x_2, v_2, \alpha, v_\alpha, u_1, u_2, t_f} \quad & J = \frac{1}{t_f} \int_0^{t_f} \left( 2 u_1^2(t) + u_2^2(t) \right) \mathrm{d}t + \mu \, t_f \\[1em] \text{s.t.} \quad & \dot{x}_1 = v_1, \quad \dot{v}_1 = \frac{1}{m} \left( u_1 \cos \alpha - u_2 \sin \alpha \right), \\[0.5em] & \dot{x}_2 = v_2, \quad \dot{v}_2 = \frac{1}{m} \left( -mg + u_1 \sin \alpha + u_2 \cos \alpha \right), \\[0.5em] & \dot{\alpha} = v_\alpha, \quad \dot{v}_\alpha = \frac{r}{J} u_1, \\[1em] & (x_1, v_1, x_2, v_2, \alpha, v_\alpha)|_{t=0} = (0, 0, 0, 0, 0, 0), \\[0.5em] & (x_1, v_1, x_2, v_2, \alpha, v_\alpha)|_{t=t_f} = (1, 0, 0, 0, 0, 0), \\[1em] & -30^\circ \le \alpha(t) \le 30^\circ, \quad -5 \le u_1(t) \le 5, \quad 0 \le u_2(t) \le 17. \end{aligned}\]

with $m = 2.2$ kg, $J = 0.05$ kg·m², $r = 0.2$ m, $mg = 4$ N. The weight $\mu > 0$ balances control effort and transition time.

Qualitative behaviour

  • The free end-time formulation yields a time–energy trade-off, governed by $\mu$: large $\mu$ emphasises short transfer time, small $\mu$ emphasises reduced input effort.
  • The thrust constraints induce saturation, often resulting in bang–bang control profiles in $u_1$ and $u_2$.
  • The angular bound $|\alpha| \le 30^\circ$ limits manoeuvrability, shaping feasible trajectories.
  • Flatness-based analysis shows that the system is differentially flat, with outputs that allow explicit trajectory design (Graichen & Petit, 2009).

Characteristics

  • Nonlinear six-dimensional dynamics.
  • Free final time.
  • Multiple input and state inequality constraints.
  • Benchmark for testing constrained nonlinear OCP solvers.

References

  • Graichen, K., & Petit, N. (2009). Incorporating a class of constraints into the dynamics of optimal control problems. Optimal Control Applications and Methods. DOI: 10.1002/oca.880 This work presents a methodology for incorporating constraints into system dynamics, exemplified on ducted fan trajectory optimisation.

  • Yu, J., & Babai, J. A. (2001). Comparison of Nonlinear Control Design Techniques on a Planar Model of a Ducted Fan Engine. Automatica, 37(5), 741–748. Demonstrates practical nonlinear control designs on a planar ducted fan model.

  • Ohanian, O. J. (2011). Ducted Fan Aerodynamics and Modelling, with Applications of Flow Control. Provides detailed modelling of ducted fan aerodynamics and control-relevant effects.

Numerical set-up

In this section, we prepare the numerical environment required to study the problem. We begin by importing the relevant Julia packages and then initialise the data frames that will store the results of our simulations and computations. These structures provide the foundation for solving the problem and for comparing the different solution strategies in a consistent way.

using OptimalControlProblems    # to access the Beam model
using OptimalControl            # to import the OptimalControl model
using NLPModelsIpopt            # to solve the model with Ipopt
import DataFrames: DataFrame    # to store data
using NLPModels                 # to retrieve data from the NLP solution
using Plots                     # to plot the trajectories
using Plots.PlotMeasures        # for leftmargin, bottommargin
using JuMP                      # to import the JuMP model
using Ipopt                     # to solve the JuMP model with Ipopt
using Printf                    # to print

data_pb = DataFrame(            # to store data about the problem
    Problem=Symbol[],
    Grid_Size=Int[],
    Variables=Int[],
    Constraints=Int[],
)

data_re = DataFrame(            # to store data about the resolutions
    Model=Symbol[],
    Flag=Any[],
    Iterations=Int[],
    Objective=Float64[],
)

Metadata

The default number of time steps is:

metadata(:ducted_fan)[:grid_size]
250

The default values of the parameters are:

metadata(:ducted_fan)[:parameters]
Parameter = Value
------------------
    t0 =  0.0000e+00
     r =  2.0000e-01
     J =  5.0000e-02
     m =  2.2000e+00
    mg =  4.0000e+00
     μ =  1.0000e+03
   α_l = -5.2360e-01
   α_u =  5.2360e-01
  u₁_l = -5.0000e+00
  u₁_u =  5.0000e+00
  u₂_l =  0.0000e+00
  u₂_u =  1.7000e+01
  tf_l =  1.0000e-01
 x₁_t0 =  0.0000e+00
 v₁_t0 =  0.0000e+00
 x₂_t0 =  0.0000e+00
 v₂_t0 =  0.0000e+00
  α_t0 =  0.0000e+00
 vα_t0 =  0.0000e+00
 x₁_tf =  1.0000e+00
 v₁_tf =  0.0000e+00
 x₂_tf =  0.0000e+00
 v₂_tf =  0.0000e+00
  α_tf =  0.0000e+00
 vα_tf =  0.0000e+00

Initial guess

Before solving the problem, it is often useful to inspect the initial guess (sometimes called the first iterate). This guess is obtained by running the NLP solver with max_iter = 0, which evaluates the problem formulation without performing any optimisation steps.

We plot the resulting trajectories for both the OptimalControl and JuMP models. Since both backends represent the same mathematical problem, their initial guesses should coincide, providing a useful consistency check before moving on to the optimised solution.

Code to plot the initial guess
Click to unfold and see the code for plotting the initial guess.
function plot_initial_guess(problem)

    # -----------------------------
    # Build OptimalControl problem
    # -----------------------------
    docp = eval(problem)(OptimalControlBackend())
    nlp_oc = nlp_model(docp)
    ocp_oc = ocp_model(docp)

    # Solve NLP with zero iterations (initial guess)
    nlp_oc_sol = NLPModelsIpopt.ipopt(nlp_oc; max_iter=0)

    # Build OptimalControl solution
    ocp_sol = build_ocp_solution(docp, nlp_oc_sol)

    # get dimensions
    n = state_dimension(ocp_oc)
    m = control_dimension(ocp_oc)

    # -----------------------------
    # Plot OptimalControl solution
    # -----------------------------
    plt = plot(
        ocp_sol;
        state_style=(color=1,),
        costate_style=(color=1, legend=:none),
        control_style=(color=1, legend=:none),
        path_style=(color=1, legend=:none),
        dual_style=(color=1, legend=:none),
        size=(816, 220*(n+m)),
        label="OptimalControl",
        leftmargin=20mm,
    )

    # Hide legend for additional state plots
    for i in 2:n
        plot!(plt[i]; legend=:none)
    end

    # -----------------------------
    # Build JuMP model
    # -----------------------------
    nlp_jp = eval(problem)(JuMPBackend())

    # Solve NLP with zero iterations (initial guess)
    set_optimizer(nlp_jp, Ipopt.Optimizer)
    set_optimizer_attribute(nlp_jp, "max_iter", 0)
    optimize!(nlp_jp)

    # Extract trajectories
    t_grid = time_grid(nlp_jp)
    x_fun = state(nlp_jp)
    u_fun = control(nlp_jp)
    p_fun = costate(nlp_jp)

    # -----------------------------
    # Plot JuMP solution on top
    # -----------------------------
    # States
    for i in 1:n
        label = i == 1 ? "JuMP" : :none
        plot!(plt[i], t_grid, t -> x_fun(t)[i]; color=2, linestyle=:dash, label=label)
    end

    # Costates
    for i in 1:n
        plot!(plt[n+i], t_grid, t -> -p_fun(t)[i]; color=2, linestyle=:dash, label=:none)
    end

    # Controls
    for i in 1:m
        plot!(plt[2*n+i], t_grid, t -> u_fun(t)[i]; color=2, linestyle=:dash, label=:none)
    end

    return plt
end
plot_initial_guess(:ducted_fan)
Example block output

Solving the problem

To solve an optimal control problem, we can rely on two complementary formulations: the OptimalControl backend, which works directly with the discretised control problem, and the JuMP backend, which leverages JuMP’s flexible modelling framework.

Both approaches generate equivalent NLPs that can be solved with Ipopt, and comparing them ensures consistency between the two formulations.

Before solving, we can inspect the discretisation details of the problem. The table below reports the number of grid points, decision variables, and constraints associated with the chosen formulation.

push!(data_pb,(
    Problem=:ducted_fan,
    Grid_Size=metadata(:ducted_fan)[:grid_size],
    Variables=get_nvar(nlp_model(ducted_fan(OptimalControlBackend()))),
    Constraints=get_ncon(nlp_model(ducted_fan(OptimalControlBackend()))),
))
1×4 DataFrame
RowProblemGrid_SizeVariablesConstraints
SymbolInt64Int64Int64
1ducted_fan25020091512

OptimalControl model

We first solve the problem using the OptimalControl backend. The process begins by importing the problem definition and constructing the associated nonlinear programming (NLP) model. This NLP is then passed to the Ipopt solver, with standard options for tolerance and barrier parameter strategy.

# import DOCP model
docp = ducted_fan(OptimalControlBackend())

# get NLP model
nlp_oc = nlp_model(docp)

# solve
nlp_oc_sol = NLPModelsIpopt.ipopt(
    nlp_oc;
    print_level=4,
    tol=1e-8,
    mu_strategy="adaptive",
    sb="yes",
)
Total number of variables............................:     2009
                     variables with only lower bounds:        1
                variables with lower and upper bounds:      753
                     variables with only upper bounds:        0
Total number of equality constraints.................:     1512
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0


Number of Iterations....: 104

                                   (scaled)                 (unscaled)
Objective...............:   1.8318277178170149e+02    1.8318277178170149e+03
Dual infeasibility......:   1.4468094361452799e-09    1.4468094361452799e-08
Constraint violation....:   5.7731597280508140e-15    5.7731597280508140e-15
Variable bound violation:   1.6903329225215202e-07    1.6903329225215202e-07
Complementarity.........:   2.0243841582224070e-11    2.0243841582224069e-10
Overall NLP error.......:   1.4468094361452799e-09    1.4468094361452799e-08


Number of objective function evaluations             = 161
Number of objective gradient evaluations             = 105
Number of equality constraint evaluations            = 161
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 105
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 104
Total seconds in IPOPT                               = 2.098

EXIT: Optimal Solution Found.

JuMP model

We now repeat the procedure using the JuMP backend. Here, the problem is reformulated as a JuMP model, which offers a flexible and widely used framework for nonlinear optimisation in Julia. The solver settings are chosen to mirror those used previously, so that the results can be compared on an equal footing.

# import model
nlp_jp = ducted_fan(JuMPBackend())

# solve with Ipopt
set_optimizer(nlp_jp, Ipopt.Optimizer)
set_optimizer_attribute(nlp_jp, "print_level", 4)
set_optimizer_attribute(nlp_jp, "tol", 1e-8)
set_optimizer_attribute(nlp_jp, "mu_strategy", "adaptive")
set_optimizer_attribute(nlp_jp, "linear_solver", "mumps")
set_optimizer_attribute(nlp_jp, "sb", "yes")
optimize!(nlp_jp)
Total number of variables............................:     2009
                     variables with only lower bounds:        1
                variables with lower and upper bounds:      753
                     variables with only upper bounds:        0
Total number of equality constraints.................:     1512
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

In iteration 307, 1 Slack too small, adjusting variable bound
In iteration 336, 1 Slack too small, adjusting variable bound
In iteration 347, 1 Slack too small, adjusting variable bound
In iteration 358, 1 Slack too small, adjusting variable bound
In iteration 362, 1 Slack too small, adjusting variable bound

Number of Iterations....: 365

                                   (scaled)                 (unscaled)
Objective...............:   1.8318277178169586e+02    1.8318277178169585e+03
Dual infeasibility......:   7.9130053305468624e-11    7.9130053305468622e-10
Constraint violation....:   1.4229166507394561e-12    1.4229166507394561e-12
Variable bound violation:   1.6903329225215202e-07    1.6903329225215202e-07
Complementarity.........:   3.2234384412039109e-11    3.2234384412039109e-10
Overall NLP error.......:   7.9130053305468624e-11    7.9130053305468622e-10


Number of objective function evaluations             = 372
Number of objective gradient evaluations             = 366
Number of equality constraint evaluations            = 372
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 366
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 365
Total seconds in IPOPT                               = 25.289

EXIT: Optimal Solution Found.

Numerical comparisons

In this section, we examine the results of the problem resolutions. We extract the solver status (flag), the number of iterations, and the objective value for each model. This provides a first overview of how each approach performs and sets the stage for a more detailed comparison of the solution trajectories.

# from OptimalControl model
push!(data_re,(
    Model=:OptimalControl,
    Flag=nlp_oc_sol.status,
    Iterations=nlp_oc_sol.iter,
    Objective=nlp_oc_sol.objective,
))

# from JuMP model
push!(data_re,(
    Model=:JuMP,
    Flag=termination_status(nlp_jp),
    Iterations=barrier_iterations(nlp_jp),
    Objective=objective_value(nlp_jp),
))
2×4 DataFrame
RowModelFlagIterationsObjective
SymbolAnyInt64Float64
1OptimalControlfirst_order1041831.83
2JuMPLOCALLY_SOLVED3651831.83

We compare the solutions obtained from the OptimalControl and JuMP models by examining the number of iterations required for convergence, the $L^2$-norms of the differences in states, controls, and additional variables, and the corresponding objective values. Both absolute and relative errors are reported, providing a clear quantitative measure of the agreement between the two approaches.

Code to print the numerical comparisons
Click to unfold and get the code of the numerical comparisons.
function L2_norm(T, X)
    # T and X are supposed to be one dimensional
    s = 0.0
    for i in 1:(length(T) - 1)
        s += 0.5 * (X[i]^2 + X[i + 1]^2) * (T[i + 1]-T[i])
    end
    return √(s)
end

function print_numerical_comparisons(problem, docp, nlp_oc_sol, nlp_jp)

    # get relevant data from OptimalControl model
    ocp_sol = build_ocp_solution(docp, nlp_oc_sol)
    t_oc = time_grid(ocp_sol)
    x_oc = state(ocp_sol).(t_oc)
    u_oc = control(ocp_sol).(t_oc)
    v_oc = variable(ocp_sol)
    o_oc = objective(ocp_sol)
    i_oc = iterations(ocp_sol)

    # get relevant data from JuMP model
    t_jp = time_grid(nlp_jp)
    x_jp = state(nlp_jp).(t_jp)
    u_jp = control(nlp_jp).(t_jp)
    o_jp = objective(nlp_jp)
    v_jp = variable(nlp_jp)
    i_jp = iterations(nlp_jp)

    x_vars = state_components(nlp_jp)
    u_vars = control_components(nlp_jp)
    v_vars = variable_components(nlp_jp)

    println("┌─ ", string(problem))
    println("│")
    println("├─  Number of Iterations")
    @printf("│     OptimalControl : %d   JuMP : %d\n", i_oc, i_jp)

    # States
    println("├─  States (L2 Norms)")
    for i in eachindex(x_vars)
        xi_oc = [x_oc[k][i] for k in eachindex(t_oc)]
        xi_jp = [x_jp[k][i] for k in eachindex(t_jp)]
        L2_ae = L2_norm(t_oc, xi_oc - xi_jp)
        L2_re = L2_ae / (0.5 * (L2_norm(t_oc, xi_oc) + L2_norm(t_oc, xi_jp)))
        @printf("│     %-6s Abs: %.3e   Rel: %.3e\n", x_vars[i], L2_ae, L2_re)
    end

    # Controls
    println("├─  Controls (L2 Norms)")
    for i in eachindex(u_vars)
        ui_oc = [u_oc[k][i] for k in eachindex(t_oc)]
        ui_jp = [u_jp[k][i] for k in eachindex(t_jp)]
        L2_ae = L2_norm(t_oc, ui_oc - ui_jp)
        L2_re = L2_ae / (0.5 * (L2_norm(t_oc, ui_oc) + L2_norm(t_oc, ui_jp)))
        @printf("│     %-6s Abs: %.3e   Rel: %.3e\n", u_vars[i], L2_ae, L2_re)
    end

    # Variables
    if !isnothing(v_vars)
        println("├─  Variables")
        for i in eachindex(v_vars)
            vi_oc = v_oc[i]
            vi_jp = v_jp[i]
            vi_ae = abs(vi_oc - vi_jp)
            vi_re = vi_ae / (0.5 * (abs(vi_oc) + abs(vi_jp)))
            @printf("│     %-6s Abs: %.3e   Rel: %.3e\n", v_vars[i], vi_ae, vi_re)
        end
    end

    # Objective
    o_ae = abs(o_oc - o_jp)
    o_re = o_ae / (0.5 * (abs(o_oc) + abs(o_jp)))
    println("├─  Objective")
    @printf("│            Abs: %.3e   Rel: %.3e\n", o_ae, o_re)
    println("└─")
    return nothing
end
print_numerical_comparisons(:ducted_fan, docp, nlp_oc_sol, nlp_jp)
┌─ ducted_fan
│
├─  Number of Iterations
│     OptimalControl : 104   JuMP : 365
├─  States (L2 Norms)
│     x₁     Abs: 3.569e-09   Rel: 4.174e-09
│     v₁     Abs: 2.302e-08   Rel: 2.157e-08
│     x₂     Abs: 1.583e-09   Rel: 9.756e-09
│     v₂     Abs: 9.626e-09   Rel: 1.634e-08
│     α      Abs: 3.055e-08   Rel: 5.558e-08
│     vα     Abs: 2.196e-07   Rel: 9.479e-08
├─  Controls (L2 Norms)
│     u₁     Abs: 5.169e-06   Rel: 1.039e-06
│     u₂     Abs: 1.304e-07   Rel: 1.093e-08
├─  Variables
│     tf     Abs: 6.721e-11   Rel: 3.907e-11
├─  Objective
│            Abs: 5.639e-11   Rel: 3.078e-14
└─

Plotting the solutions

In this section, we visualise the trajectories of the states, costates, and controls obtained from both the OptimalControl and JuMP solutions. The plots provide an intuitive way to compare the two approaches and to observe how the constraints and the optimal control influence the system dynamics.

For each variable, the OptimalControl solution is shown in solid lines, while the JuMP solution is overlaid using dashed lines. Since both models represent the same mathematical problem, their trajectories should closely coincide, highlighting the consistency between the two formulations.

# build an ocp solution to use the plot from OptimalControl package
ocp_sol = build_ocp_solution(docp, nlp_oc_sol)

# dimensions
n = state_dimension(ocp_sol)
m = control_dimension(ocp_sol)

# from OptimalControl solution
plt = plot(
    ocp_sol;
    color=1,
    size=(816, 240*(n+m)),
    label="OptimalControl",
    leftmargin=20mm,
)
for i in 2:length(plt)
    plot!(plt[i]; legend=:none)
end

# from JuMP solution
t = time_grid(nlp_jp)     # t0, ..., tN = tf
x = state(nlp_jp)         # function of time
u = control(nlp_jp)       # function of time
p = costate(nlp_jp)       # function of time

for i in 1:n # state
    label = i == 1 ? "JuMP" : :none
    plot!(plt[i], t, t -> x(t)[i]; color=2, linestyle=:dash, label=label)
end

for i in 1:n # costate
    plot!(plt[n+i], t, t -> -p(t)[i]; color=2, linestyle=:dash, label=:none)
end

for i in 1:m # control
    plot!(plt[2n+i], t, t -> u(t)[i]; color=2, linestyle=:dash, label=:none)
end
Example block output