Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9ae281e

Browse files
committedMar 21, 2023
Merge branch 'main' of github.com:control-toolbox/CTBase.jl
2 parents 0b45b2f + c47e505 commit 9ae281e

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed
 

‎src/CTBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ export AbstractOptimalControlModel, OptimalControlModel
8282
export Model, time!, constraint!, objective!, state!, control!, remove_constraint!, constraint
8383
export ismin, dynamics, lagrange, mayer, criterion, initial_time, final_time
8484
export control_dimension, state_dimension, constraints
85-
export initial_condition, final_condition, initial_constraint, final_constraint
8685
export isautonomous, isnonautonomous
8786
export control_labels, state_labels
87+
export nlp_constraints
8888

8989
# solution
9090
export AbstractOptimalControlSolution, OptimalControlSolution

‎src/model.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ julia> constraint!(ocp, :final, 1, 0)
152152
```
153153
"""
154154
function constraint!(ocp::OptimalControlModel, type::Symbol, rg::Union{Integer, UnitRange{<:Integer}}, val, label::Symbol=gensym(:anonymous))
155-
if type [ :initial, :final ] # not allowed for :control or :state
155+
if type [ :initial, :final ] # not allowed for :control or :state (does not make sense)
156156
ocp.constraints[label] = (type, :eq, x -> x[rg], val, val)
157157
else
158158
throw(IncorrectArgument("the following type of constraint is not valid: " * String(type) *
@@ -175,11 +175,11 @@ julia> constraint!(ocp, :state, [ 0, 0, 0 ], [ 1, 2, 1 ])
175175
"""
176176
function constraint!(ocp::OptimalControlModel, type::Symbol, lb, ub, label::Symbol=gensym(:anonymous))
177177
if type [ :initial, :final ]
178-
ocp.constraints[label] = (type, :ineq, x -> x, ub, lb)
178+
ocp.constraints[label] = (type, :ineq, x -> x, lb, ub)
179179
elseif type == :control
180-
ocp.constraints[label] = (type, :ineq, lb isa MyNumber ? 1 : 1:control_dimension(ocp), ub, lb) # debug
180+
ocp.constraints[label] = (type, :ineq, lb isa MyNumber ? 1 : 1:control_dimension(ocp), lb, ub) # debug
181181
elseif type == :state
182-
ocp.constraints[label] = (type, :ineq, lb isa MyNumber ? 1 : 1:state_dimension(ocp), ub, lb) # debug
182+
ocp.constraints[label] = (type, :ineq, lb isa MyNumber ? 1 : 1:state_dimension(ocp), lb, ub) # debug
183183
else
184184
throw(IncorrectArgument("the following type of constraint is not valid: " * String(type) *
185185
". Please choose in [ :initial, :final, :control, :state ] or check the arguments of the constraint! method."))
@@ -199,11 +199,11 @@ julia> constraint!(ocp, :control, 1, 0, 2)
199199
julia> constraint!(ocp, :state, 2:3, [ 0, 0 ], [1, 2])
200200
```
201201
"""
202-
function constraint!(ocp::OptimalControlModel, type::Symbol, rg, lb, ub, label::Symbol=gensym(:anonymous))
202+
function constraint!(ocp::OptimalControlModel, type::Symbol, rg::Union{Integer, UnitRange{<:Integer}}, lb, ub, label::Symbol=gensym(:anonymous))
203203
if type [ :initial, :final ]
204-
ocp.constraints[label] = (type, :ineq, x -> x[rg], ub, lb)
204+
ocp.constraints[label] = (type, :ineq, x -> x[rg], lb, ub)
205205
elseif type [ :control, :state ]
206-
ocp.constraints[label] = (type, :ineq, rg, ub, lb)
206+
ocp.constraints[label] = (type, :ineq, rg, lb, ub)
207207
else
208208
throw(IncorrectArgument("the following type of constraint is not valid: " * String(type) *
209209
". Please choose in [ :initial, :final, :contol, :state ] or check the arguments of the constraint! method."))
@@ -319,7 +319,7 @@ end
319319

320320
#
321321
"""
322-
nlp_constraint(ocp)
322+
nlp_constraints(ocp)
323323
324324
Return a 6-tuple of tuples:
325325
- `(ξl, ξ, ξu)` are control constraints
@@ -331,7 +331,7 @@ Return a 6-tuple of tuples:
331331
332332
# Examples
333333
```jldoctest
334-
julia> (ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (ulb, uind, uub), (xlb, xind, xub) = nlp_constraint(ocp)
334+
julia> (ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (ulb, uind, uub), (xlb, xind, xub) = nlp_constraints(ocp)
335335
```
336336
"""
337337
function nlp_constraints(ocp::OptimalControlModel{time_dependence, dimension_usage}) where {time_dependence, dimension_usage}

‎src/print.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,6 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::OptimalControlModel{time_dep
6666
end
6767
nb_fixed += 1
6868
end
69-
if initial_condition(ocp) !== nothing
70-
if s == ""
71-
s = s * "x0"
72-
else
73-
s = s * " and x0"
74-
end
75-
nb_fixed += 1
76-
end
7769
if nb_fixed > 1
7870
s = s * " are fixed."
7971
elseif nb_fixed == 1

‎test/test_model.jl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,37 +78,50 @@ constraint!(ocp, :dynamics, f)
7878
#
7979
# goddard (version 2, ranges, vectorial constraints)
8080
ocp = Model()
81-
#
8281
Cd = 310.; Tmax = 3.5; β = 500.; b = 2.; t0 = 0.; r0 = 1.; v0 = 0.
8382
vmax = 0.1; m0 = 1.; mf = 0.6; x0 = [r0, v0, m0]
84-
#
8583
m = 1
8684
n = 3
87-
#
85+
8886
time!(ocp, :initial, t0) # if not provided, final time is free
8987
state!(ocp, n) # state dim
9088
control!(ocp, m) # control dim
9189
constraint!(ocp, :initial, x0, :initial_con1)
9290
constraint!(ocp, :control, 0., 1., :control_con1)
9391
constraint!(ocp, :state, 1:2, [ r0, 0 ], [ Inf, vmax ], :state_con1)
9492
constraint!(ocp, :state, 3, m0, mf, :state_con2)
95-
#
93+
9694
objective!(ocp, :mayer, (t0, x0, tf, xf) -> xf[1], :max)
97-
#
95+
9896
constraint!(ocp, :dynamics, f) # see previous defs
9997

100-
#
10198
@test isautonomous(ocp)
10299
@test lagrange(ocp) === nothing
103100
@test mayer(ocp)(t0, x0, tf, x0) x0[1] atol=1e-8
104101
@test !ismin(ocp)
102+
105103
@test initial_time(ocp) == t0
106104
@test final_time(ocp) === nothing
107105
@test control_dimension(ocp) == m
108106
@test state_dimension(ocp) == n
107+
109108
@test constraint(ocp, :initial_con1)(x0) == x0
110109
@test constraint(ocp, :control_con1)(1) == 1
111110
@test constraint(ocp, :state_con1)(x0) == x0[1:2]
112111
@test constraint(ocp, :state_con2)(x0) == x0[3]
113112

113+
(ξl, ξ, ξu), (ηl, η, ηu), (ψl, ψ, ψu), (ϕl, ϕ, ϕu), (ulb, uind, uub), (xlb, xind, xub) = nlp_constraints(ocp)
114+
@test ξl == [ ]
115+
@test ξu == [ ]
116+
@test ηl == [ ]
117+
@test ηu == [ ]
118+
@test ψl == [ ]
119+
@test ψu == [ ]
120+
@test ϕl == x0
121+
@test ϕu == x0
122+
@test ulb == [ 0 ][uind]
123+
@test uub == [ 1 ][uind]
124+
@test xlb == [ r0, 0, m0 ][xind]
125+
@test xub == [ Inf, vmax, mf ][xind]
126+
114127
end

0 commit comments

Comments
 (0)
Please sign in to comment.