@@ -62,14 +62,16 @@ function __constraint!(
6262)
6363
6464 # checkings: the constraint must not be set before
65- @ensure (! (label ∈ keys (ocp_constraints)),
65+ @ensure (
66+ ! (label ∈ keys (ocp_constraints)),
6667 CTBase. UnauthorizedCall (
6768 " the constraint named " * String (label) * " already exists."
6869 ),
6970 )
7071
7172 # checkings: lb and ub cannot be both nothing
72- @ensure (! (isnothing (lb) && isnothing (ub)),
73+ @ensure (
74+ ! (isnothing (lb) && isnothing (ub)),
7375 CTBase. UnauthorizedCall (
7476 " The lower bound `lb` and the upper bound `ub` cannot be both nothing."
7577 ),
@@ -80,7 +82,8 @@ function __constraint!(
8082 isnothing (ub) && (ub = Inf * ones (eltype (lb), length (lb)))
8183
8284 # lb and ub must have the same length
83- @ensure (length (lb) == length (ub),
85+ @ensure (
86+ length (lb) == length (ub),
8487 CTBase. IncorrectArgument (
8588 " the lower bound `lb` and the upper bound `ub` must have the same length."
8689 ),
@@ -116,21 +119,24 @@ function __constraint!(
116119 @ensure (length (rg) == length (lb), CTBase. IncorrectArgument (txt))
117120 # check if the range is valid
118121 if type == :state
119- @ensure (all (1 .≤ rg .≤ n),
122+ @ensure (
123+ all (1 .≤ rg .≤ n),
120124 CTBase. IncorrectArgument (
121- " the range of the state constraint must be contained in 1:$n " ,
125+ " the range of the state constraint must be contained in 1:$n "
122126 ),
123127 )
124128 elseif type == :control
125- @ensure (all (1 .≤ rg .≤ m),
129+ @ensure (
130+ all (1 .≤ rg .≤ m),
126131 CTBase. IncorrectArgument (
127- " the range of the control constraint must be contained in 1:$m " ,
132+ " the range of the control constraint must be contained in 1:$m "
128133 ),
129134 )
130135 elseif type == :variable
131- @ensure (all (1 .≤ rg .≤ q),
136+ @ensure (
137+ all (1 .≤ rg .≤ q),
132138 CTBase. IncorrectArgument (
133- " the range of the variable constraint must be contained in 1:$q " ,
139+ " the range of the variable constraint must be contained in 1:$q "
134140 ),
135141 )
136142 else
@@ -192,21 +198,27 @@ julia> constraint!(ocp, :control, rg=1:2, lb=[0.0], ub=[1.0], label=:control_con
192198function constraint! (
193199 ocp:: PreModel ,
194200 type:: Symbol ;
195- rg:: Union{Int, OrdinalRange{Int}, Nothing} = nothing ,
196- f:: Union{Function, Nothing} = nothing ,
197- lb:: Union{ctNumber, ctVector, Nothing} = nothing ,
198- ub:: Union{ctNumber, ctVector, Nothing} = nothing ,
199- label:: Symbol = __constraint_label (),
201+ rg:: Union{Int,OrdinalRange{Int},Nothing} = nothing ,
202+ f:: Union{Function,Nothing} = nothing ,
203+ lb:: Union{ctNumber,ctVector,Nothing} = nothing ,
204+ ub:: Union{ctNumber,ctVector,Nothing} = nothing ,
205+ label:: Symbol = __constraint_label (),
200206)
201207
202208 # checkings: times, state and control must be set before adding constraints
203- @ensure __is_state_set (ocp) CTBase. UnauthorizedCall (" the state must be set before adding constraints." )
204- @ensure __is_control_set (ocp) CTBase. UnauthorizedCall (" the control must be set before adding constraints." )
205- @ensure __is_times_set (ocp) CTBase. UnauthorizedCall (" the times must be set before adding constraints." )
209+ @ensure __is_state_set (ocp) CTBase. UnauthorizedCall (
210+ " the state must be set before adding constraints."
211+ )
212+ @ensure __is_control_set (ocp) CTBase. UnauthorizedCall (
213+ " the control must be set before adding constraints."
214+ )
215+ @ensure __is_times_set (ocp) CTBase. UnauthorizedCall (
216+ " the times must be set before adding constraints."
217+ )
206218
207219 # checkings: variable must be set if using type=:variable
208220 @ensure (type != :variable || __is_variable_set (ocp)) CTBase. UnauthorizedCall (
209- " the ocp has no variable, you cannot use constraint! function with type=:variable. If it is a mistake, please set the variable first."
221+ " the ocp has no variable, you cannot use constraint! function with type=:variable. If it is a mistake, please set the variable first." ,
210222 )
211223
212224 # dimensions
@@ -221,15 +233,14 @@ function constraint!(
221233 n,
222234 m,
223235 q;
224- rg = as_range (rg),
225- f = f,
226- lb = as_vector (lb),
227- ub = as_vector (ub),
228- label = label,
236+ rg= as_range (rg),
237+ f= f,
238+ lb= as_vector (lb),
239+ ub= as_vector (ub),
240+ label= label,
229241 )
230242end
231243
232-
233244as_vector (:: Nothing ) = nothing
234245(as_vector (x:: T ):: Vector{T} ) where {T<: ctNumber } = [x]
235246as_vector (x:: Vector{T} ) where {T<: ctNumber } = x
0 commit comments