Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ext/CTModelsJSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,11 @@ function CTModels.import_ocp_solution(
"control_constraints_ub_dual" => control_constraints_ub_dual,
"variable_constraints_lb_dual" => variable_constraints_lb_dual,
"variable_constraints_ub_dual" => variable_constraints_ub_dual,
"control_interpolation" =>
get(blob, "control_interpolation", string(CTModels.Building.__control_interpolation())),
"control_interpolation" => get(
blob,
"control_interpolation",
string(CTModels.Building.__control_interpolation()),
),
)

# Add time grid data (format detection handled by helper)
Expand Down
9 changes: 5 additions & 4 deletions src/Building/constraint_composers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ struct CompositeConstraint{Sig,CS} <: Function
funs::CS # concrete tuple of the N functions
end

CompositeConstraint{Sig}(n, dims, funs) where {Sig} =
CompositeConstraint{Sig,typeof(funs)}(n, copy(dims), funs)
function CompositeConstraint{Sig}(n, dims, funs) where {Sig}
return CompositeConstraint{Sig,typeof(funs)}(n, copy(dims), funs)
end

"""
$(TYPEDSIGNATURES)
Expand Down Expand Up @@ -102,7 +103,7 @@ $(TYPEDSIGNATURES)
Compact string representation of [`CTModels.Building.CompositeConstraint`](@ref).
"""
function Base.show(io::IO, f::CompositeConstraint{Sig,CS}) where {Sig,CS}
print(io, "CompositeConstraint{:", Sig, "}(n=", f.n, ", dims=", f.dims, ")")
return print(io, "CompositeConstraint{:", Sig, "}(n=", f.n, ", dims=", f.dims, ")")
end

"""
Expand All @@ -116,5 +117,5 @@ function Base.show(
print(io, "CompositeConstraint")
print(io, "\n sig: :", Sig)
print(io, "\n n: ", f.n)
print(io, "\n dims: ", f.dims)
return print(io, "\n dims: ", f.dims)
end
4 changes: 3 additions & 1 deletion src/Components/accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ Return an empty block expression for an [`CTModels.Components.EmptyDefinition`](

See also: [`CTModels.Components.expression`](@ref).
"""
expression(::EmptyDefinition)::Expr = :(begin end)
expression(::EmptyDefinition)::Expr = :(
begin end
)

"""
$(TYPEDSIGNATURES)
Expand Down
10 changes: 5 additions & 5 deletions src/Components/constraints_accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Get the nonlinear path constraints from the model.
See also: [`CTModels.Components.boundary_constraints_nl`](@ref), [`CTModels.Components.dim_path_constraints_nl`](@ref).
"""
function path_constraints_nl(
model::ConstraintsModel{TP,<:Tuple,<:Tuple,<:Tuple,<:Tuple},
model::ConstraintsModel{TP,<:Tuple,<:Tuple,<:Tuple,<:Tuple}
) where {TP}
return model.path_nl
end
Expand All @@ -47,7 +47,7 @@ Get the nonlinear boundary constraints from the model.
See also: [`CTModels.Components.path_constraints_nl`](@ref), [`CTModels.Components.dim_boundary_constraints_nl`](@ref).
"""
function boundary_constraints_nl(
model::ConstraintsModel{<:Tuple,TB,<:Tuple,<:Tuple,<:Tuple},
model::ConstraintsModel{<:Tuple,TB,<:Tuple,<:Tuple,<:Tuple}
) where {TB}
return model.boundary_nl
end
Expand All @@ -63,7 +63,7 @@ Get the state box constraints from the model.
See also: [`CTModels.Components.control_constraints_box`](@ref), [`CTModels.Components.dim_state_constraints_box`](@ref).
"""
function state_constraints_box(
model::ConstraintsModel{<:Tuple,<:Tuple,TS,<:Tuple,<:Tuple},
model::ConstraintsModel{<:Tuple,<:Tuple,TS,<:Tuple,<:Tuple}
) where {TS}
return model.state_box
end
Expand All @@ -79,7 +79,7 @@ Get the control box constraints from the model.
See also: [`CTModels.Components.state_constraints_box`](@ref), [`CTModels.Components.dim_control_constraints_box`](@ref).
"""
function control_constraints_box(
model::ConstraintsModel{<:Tuple,<:Tuple,<:Tuple,TC,<:Tuple},
model::ConstraintsModel{<:Tuple,<:Tuple,<:Tuple,TC,<:Tuple}
) where {TC}
return model.control_box
end
Expand All @@ -95,7 +95,7 @@ Get the variable box constraints from the model.
See also: [`CTModels.Components.state_constraints_box`](@ref), [`CTModels.Components.dim_variable_constraints_box`](@ref).
"""
function variable_constraints_box(
model::ConstraintsModel{<:Tuple,<:Tuple,<:Tuple,<:Tuple,TV},
model::ConstraintsModel{<:Tuple,<:Tuple,<:Tuple,<:Tuple,TV}
) where {TV}
return model.variable_box
end
Expand Down
8 changes: 4 additions & 4 deletions src/Components/functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ end
(f::ConstantInTime)(::Real) = f.value

function Base.show(io::IO, f::ConstantInTime)
print(io, "ConstantInTime(", f.value, ")")
return print(io, "ConstantInTime(", f.value, ")")
end

function Base.show(io::IO, ::MIME"text/plain", f::ConstantInTime{V}) where {V}
print(io, "ConstantInTime")
print(io, "\n value: ", f.value)
print(io, "\n type: ", V)
return print(io, "\n type: ", V)
end

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -75,11 +75,11 @@ end
(f::CoercedTrajectory)(t) = f.coerce(f.inner(t))

function Base.show(io::IO, f::CoercedTrajectory)
print(io, "CoercedTrajectory(", nameof(f.coerce), ")")
return print(io, "CoercedTrajectory(", nameof(f.coerce), ")")
end

function Base.show(io::IO, ::MIME"text/plain", f::CoercedTrajectory{F,C}) where {F,C}
print(io, "CoercedTrajectory")
print(io, "\n inner: ", nameof(typeof(f.inner)))
print(io, "\n coerce: ", nameof(f.coerce))
return print(io, "\n coerce: ", nameof(f.coerce))
end
4 changes: 3 additions & 1 deletion src/Display/Display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ include(joinpath(@__DIR__, "solution.jl"))

# -----------------------------
# RecipesBase.plot stub - to be extended by CTModelsPlots extension
function RecipesBase.plot(sol::Solutions.AbstractSolution, description::Symbol...; kwargs...)
function RecipesBase.plot(
sol::Solutions.AbstractSolution, description::Symbol...; kwargs...
)
throw(Exceptions.ExtensionError(:Plots; message="to plot solutions"))
end

Expand Down
8 changes: 6 additions & 2 deletions src/Display/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ function Base.show(io::IO, ::MIME"text/plain", sol::Solutions.Solution)
end
if Solutions.dim_dual_variable_constraints_box(sol) > 0 &&
Components.dim_variable_constraints_box(Solutions.model(sol)) > 0
println(io, " │ Var dual (lb) : ", Solutions.variable_constraints_lb_dual(sol))
println(io, " └─ Var dual (ub) : ", Solutions.variable_constraints_ub_dual(sol))
println(
io, " │ Var dual (lb) : ", Solutions.variable_constraints_lb_dual(sol)
)
println(
io, " └─ Var dual (ub) : ", Solutions.variable_constraints_ub_dual(sol)
)
end
end

Expand Down
13 changes: 11 additions & 2 deletions src/Init/init_functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,16 @@ $(TYPEDSIGNATURES)
Compact string representation of [`CTModels.Init.MergedTrajectory`](@ref).
"""
function Base.show(io::IO, f::MergedTrajectory{F,C}) where {F,C}
print(io, "MergedTrajectory(dim=", f.dim, ", role=:", f.role, ", ncomps=", length(f.comps), ")")
return print(
io,
"MergedTrajectory(dim=",
f.dim,
", role=:",
f.role,
", ncomps=",
length(f.comps),
")",
)
end

"""
Expand All @@ -189,5 +198,5 @@ function Base.show(io::IO, ::MIME"text/plain", f::MergedTrajectory{F,C}) where {
print(io, "MergedTrajectory")
print(io, "\n role: :", f.role)
print(io, "\n dim: ", f.dim)
print(io, "\n ncomps: ", length(f.comps))
return print(io, "\n ncomps: ", length(f.comps))
end
4 changes: 3 additions & 1 deletion src/Init/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ Build an initial guess from a previous solution (warm start).
# Throws
- `Exceptions.IncorrectArgument`: If dimensions do not match between the problem and solution.
"""
function _initial_guess_from_solution(ocp::Models.AbstractModel, sol::Solutions.AbstractSolution)
function _initial_guess_from_solution(
ocp::Models.AbstractModel, sol::Solutions.AbstractSolution
)
# Basic dimensional consistency checks
if Models.state_dimension(ocp) != Models.state_dimension(sol.model)
throw(
Expand Down
20 changes: 11 additions & 9 deletions src/Models/constraint_functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ function (f::SubPathConstraint)(r, t, x, u, v)
end

function Base.show(io::IO, f::SubPathConstraint)
print(io, "SubPathConstraint(n=", f.n, ", indices=", f.indices, ")")
return print(io, "SubPathConstraint(n=", f.n, ", indices=", f.indices, ")")
end

function Base.show(io::IO, ::MIME"text/plain", f::SubPathConstraint{CP,I}) where {CP,I}
print(io, "SubPathConstraint")
print(io, "\n n: ", f.n)
print(io, "\n indices: ", f.indices)
return print(io, "\n indices: ", f.indices)
end

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -63,13 +63,13 @@ function (f::SubBoundaryConstraint)(r, x0, xf, v)
end

function Base.show(io::IO, f::SubBoundaryConstraint)
print(io, "SubBoundaryConstraint(n=", f.n, ", indices=", f.indices, ")")
return print(io, "SubBoundaryConstraint(n=", f.n, ", indices=", f.indices, ")")
end

function Base.show(io::IO, ::MIME"text/plain", f::SubBoundaryConstraint{CP,I}) where {CP,I}
print(io, "SubBoundaryConstraint")
print(io, "\n n: ", f.n)
print(io, "\n indices: ", f.indices)
return print(io, "\n indices: ", f.indices)
end

# ------------------------------------------------------------------------------
Expand All @@ -95,16 +95,18 @@ end

BoxProjection{Slot}(cidx) where {Slot} = BoxProjection{Slot,typeof(cidx)}(cidx)

(f::BoxProjection{:state})(_, x, _, _) = x[f.cidx]
(f::BoxProjection{:state})(_, x, _, _) = x[f.cidx]
(f::BoxProjection{:control})(_, _, u, _) = u[f.cidx]
(f::BoxProjection{:variable})(_, _, v) = v[f.cidx]
(f::BoxProjection{:variable})(_, _, v) = v[f.cidx]

function Base.show(io::IO, f::BoxProjection{Slot,CIDX}) where {Slot,CIDX}
print(io, "BoxProjection{:", Slot, "}(", f.cidx, ")")
return print(io, "BoxProjection{:", Slot, "}(", f.cidx, ")")
end

function Base.show(io::IO, ::MIME"text/plain", f::BoxProjection{Slot,CIDX}) where {Slot,CIDX}
function Base.show(
io::IO, ::MIME"text/plain", f::BoxProjection{Slot,CIDX}
) where {Slot,CIDX}
print(io, "BoxProjection")
print(io, "\n slot: :", Slot)
print(io, "\n cidx: ", f.cidx)
return print(io, "\n cidx: ", f.cidx)
end
25 changes: 15 additions & 10 deletions src/Models/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1322,8 +1322,9 @@ Return the nonlinear path constraints.

See also: [`CTModels.Models.constraints`](@ref), [`CTModels.Components.boundary_constraints_nl`](@ref).
"""
Components.path_constraints_nl(ocp::Model) =
Components.path_constraints_nl(constraints(ocp))
function Components.path_constraints_nl(ocp::Model)
return Components.path_constraints_nl(constraints(ocp))
end

"""
$(TYPEDSIGNATURES)
Expand All @@ -1338,8 +1339,9 @@ Return the nonlinear boundary constraints.

See also: [`CTModels.Models.constraints`](@ref), [`CTModels.Components.path_constraints_nl`](@ref).
"""
Components.boundary_constraints_nl(ocp::Model) =
Components.boundary_constraints_nl(constraints(ocp))
function Components.boundary_constraints_nl(ocp::Model)
return Components.boundary_constraints_nl(constraints(ocp))
end

"""
$(TYPEDSIGNATURES)
Expand All @@ -1354,8 +1356,9 @@ Return the box constraints on state.

See also: [`CTModels.Models.constraints`](@ref), [`CTModels.Components.control_constraints_box`](@ref).
"""
Components.state_constraints_box(ocp::Model) =
Components.state_constraints_box(constraints(ocp))
function Components.state_constraints_box(ocp::Model)
return Components.state_constraints_box(constraints(ocp))
end

"""
$(TYPEDSIGNATURES)
Expand All @@ -1370,8 +1373,9 @@ Return the box constraints on control.

See also: [`CTModels.Models.constraints`](@ref), [`CTModels.Components.state_constraints_box`](@ref).
"""
Components.control_constraints_box(ocp::Model) =
Components.control_constraints_box(constraints(ocp))
function Components.control_constraints_box(ocp::Model)
return Components.control_constraints_box(constraints(ocp))
end

"""
$(TYPEDSIGNATURES)
Expand All @@ -1386,8 +1390,9 @@ Return the box constraints on variable.

See also: [`CTModels.Models.constraints`](@ref), [`CTModels.Components.state_constraints_box`](@ref).
"""
Components.variable_constraints_box(ocp::Model) =
Components.variable_constraints_box(constraints(ocp))
function Components.variable_constraints_box(ocp::Model)
return Components.variable_constraints_box(constraints(ocp))
end

"""
$(TYPEDSIGNATURES)
Expand Down
17 changes: 13 additions & 4 deletions src/Solutions/dual_functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ end
(f::DualSlice)(t) = f.duals(t)[f.idx]

function Base.show(io::IO, f::DualSlice)
print(io, "DualSlice(", nameof(typeof(f.duals)), ", ", f.idx, ")")
return print(io, "DualSlice(", nameof(typeof(f.duals)), ", ", f.idx, ")")
end

function Base.show(io::IO, ::MIME"text/plain", f::DualSlice{D,I}) where {D,I}
print(io, "DualSlice")
print(io, "\n duals: ", nameof(typeof(f.duals)))
print(io, "\n idx: ", f.idx)
return print(io, "\n idx: ", f.idx)
end

# ------------------------------------------------------------------------------
Expand All @@ -55,12 +55,21 @@ end
(f::BoxDualDiff)(t) = f.lb(t)[f.idx] - f.ub(t)[f.idx]

function Base.show(io::IO, f::BoxDualDiff)
print(io, "BoxDualDiff(", nameof(typeof(f.lb)), ", ", nameof(typeof(f.ub)), ", ", f.idx, ")")
return print(
io,
"BoxDualDiff(",
nameof(typeof(f.lb)),
", ",
nameof(typeof(f.ub)),
", ",
f.idx,
")",
)
end

function Base.show(io::IO, ::MIME"text/plain", f::BoxDualDiff{DL,DU,I}) where {DL,DU,I}
print(io, "BoxDualDiff")
print(io, "\n lb: ", nameof(typeof(f.lb)))
print(io, "\n ub: ", nameof(typeof(f.ub)))
print(io, "\n idx: ", f.idx)
return print(io, "\n idx: ", f.idx)
end
16 changes: 10 additions & 6 deletions test/suite/building/test_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const VERBOSE = isdefined(Main, :TestData) ? Main.TestData.VERBOSE : true
const SHOWTIMING = isdefined(Main, :TestData) ? Main.TestData.SHOWTIMING : true

# Module-level stubs for CompositeConstraint tests (world-age safe)
_path_stub1!(r, _, x, u, _) = (r[1] = x[1] + u[1])
_path_stub2!(r, t, x, _, _) = (r[1] = x[2]; r[2] = t)
_boundary_stub1!(r, x0, _, _) = (r[1] = x0[1])
_boundary_stub2!(r, _, xf, _) = (r[1] = xf[1])
_path_stub1!(r, _, x, u, _) = (r[1] = x[1] + u[1])
_path_stub2!(r, t, x, _, _) = (r[1]=x[2]; r[2]=t)
_boundary_stub1!(r, x0, _, _) = (r[1] = x0[1])
_boundary_stub2!(r, _, xf, _) = (r[1] = xf[1])

# Top-level helper (module-level to avoid world-age issues)
"""
Expand Down Expand Up @@ -456,7 +456,9 @@ function test_constraints()
end

Test.@testset "CompositeConstraint{:boundary} — two functions" begin
f = Building.CompositeConstraint{:boundary}(2, [1, 1], (_boundary_stub1!, _boundary_stub2!))
f = Building.CompositeConstraint{:boundary}(
2, [1, 1], (_boundary_stub1!, _boundary_stub2!)
)
val = zeros(2)
f(val, [2.0], [5.0], nothing)
Test.@test f isa Function
Expand All @@ -469,7 +471,9 @@ function test_constraints()

Test.@testset "CompositeConstraint{:boundary} — concrete fields (no capture-by-ref bug)" begin
dims_orig = [1, 1]
f = Building.CompositeConstraint{:boundary}(2, dims_orig, (_boundary_stub1!, _boundary_stub2!))
f = Building.CompositeConstraint{:boundary}(
2, dims_orig, (_boundary_stub1!, _boundary_stub2!)
)
dims_orig[1] = 99 # mutate the original after construction
val = zeros(2)
f(val, [2.0], [5.0], nothing)
Expand Down
Loading