Skip to content

[Bug] Importing solution fails when OCP has exactly one variable #857

Closed
control-toolbox/CTModels.jl
#396

Description

@abavoil

Describe the bug
import_ocp_solution fails if isa(variable(solution), Float64), if there is zero or n >= 2 variables, then the loading works as expected.

To Reproduce

using OptimalControl
using NLPModelsIpopt
using JLD2

filename="sol"

## Fine
tf = 1
ocp = @def begin
    t ∈ [0, tf], time
    x ∈ R, state
    u ∈ R, control
    x(0) == 0
    x(tf) == 1
    ẋ(t) == u(t)
    ∫(0.5u(t)^2) → min
end

s = solve(ocp, grid_size=8)
export_ocp_solution(s; filename)
si = import_ocp_solution(ocp; filename)

## Workaround with dummy parameter p_
ocp = @def begin
    p = (tf, p_) ∈ R², variable
    t ∈ [0, tf], time
    x ∈ R, state
    u ∈ R, control
    p_ == 0
    1 <= tf <= 2
    x(0) == 0
    x(tf) == 1
    ẋ(t) == u(t)
    ∫(0.5u(t)^2) → min
end

s = solve(ocp, grid_size=8)
export_ocp_solution(s; filename)
si = import_ocp_solution(ocp; filename)

## Bug
ocp = @def begin
    tf ∈ R, variable
    t ∈ [0, tf], time
    x ∈ R, state
    u ∈ R, control
    1 <= tf <= 2
    x(0) == 0
    x(tf) == 1
    ẋ(t) == u(t)
    ∫(0.5u(t)^2) → min
end

s = solve(ocp, grid_size=8)
export_ocp_solution(s; filename)
si = import_ocp_solution(ocp; filename)

Expected behavior
variable(solution) returns a vector of length 1, or the loading code can handle Number instead of Vector

Activity

  1. ocots commented on Aug 16, 2026

    @ocots
    Member

    Thanks @abavoil for this bug!

    It will be fixed by control-toolbox/CTModels.jl#396.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions