@@ -62,14 +62,14 @@ function __constraint!(
6262)
6363
6464 # checkings: the constraint must not be set before
65- label ∈ keys (ocp_constraints) && throw (
65+ @ensure ( ! ( label ∈ keys (ocp_constraints)),
6666 CTBase. UnauthorizedCall (
6767 " the constraint named " * String (label) * " already exists."
6868 ),
6969 )
7070
7171 # checkings: lb and ub cannot be both nothing
72- ( isnothing (lb) && isnothing (ub)) && throw (
72+ @ensure ( ! ( isnothing (lb) && isnothing (ub)),
7373 CTBase. UnauthorizedCall (
7474 " The lower bound `lb` and the upper bound `ub` cannot be both nothing."
7575 ),
@@ -80,7 +80,7 @@ function __constraint!(
8080 isnothing (ub) && (ub = Inf * ones (eltype (lb), length (lb)))
8181
8282 # lb and ub must have the same length
83- length (lb) != length (ub) && throw (
83+ @ensure ( length (lb) == length (ub),
8484 CTBase. IncorrectArgument (
8585 " the lower bound `lb` and the upper bound `ub` must have the same length."
8686 ),
@@ -107,28 +107,28 @@ function __constraint!(
107107 ),
108108 )
109109 end
110- (length (rg) != length (lb)) && throw ( CTBase. IncorrectArgument (txt))
110+ @ensure (length (rg) == length (lb), CTBase. IncorrectArgument (txt))
111111 __constraint! (ocp_constraints, type, n, m, q; rg= rg, lb= lb, ub= ub, label= label)
112112 end
113113
114114 (:: OrdinalRange{<:Int} , :: Nothing , :: ctVector , :: ctVector ) => begin
115115 txt = " the range `rg`, the lower bound `lb` and the upper bound `ub` must have the same dimension"
116- (length (rg) != length (lb)) && throw ( CTBase. IncorrectArgument (txt))
116+ @ensure (length (rg) == length (lb), CTBase. IncorrectArgument (txt))
117117 # check if the range is valid
118118 if type == :state
119- ! all (1 .≤ rg .≤ n) && throw (
119+ @ensure ( all (1 .≤ rg .≤ n),
120120 CTBase. IncorrectArgument (
121121 " the range of the state constraint must be contained in 1:$n " ,
122122 ),
123123 )
124124 elseif type == :control
125- ! all (1 .≤ rg .≤ m) && throw (
125+ @ensure ( all (1 .≤ rg .≤ m),
126126 CTBase. IncorrectArgument (
127127 " the range of the control constraint must be contained in 1:$m " ,
128128 ),
129129 )
130130 elseif type == :variable
131- ! all (1 .≤ rg .≤ q) && throw (
131+ @ensure ( all (1 .≤ rg .≤ q),
132132 CTBase. IncorrectArgument (
133133 " the range of the variable constraint must be contained in 1:$q " ,
134134 ),
@@ -192,50 +192,44 @@ julia> constraint!(ocp, :control, rg=1:2, lb=[0.0], ub=[1.0], label=:control_con
192192function constraint! (
193193 ocp:: PreModel ,
194194 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 (),
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 (),
200200)
201201
202202 # checkings: times, state and control must be set before adding constraints
203- ! __is_state_set (ocp) &&
204- throw (CTBase. UnauthorizedCall (" the state must be set before adding constraints." ))
205- ! __is_control_set (ocp) &&
206- throw (CTBase. UnauthorizedCall (" the control must be set before adding constraints." ))
207- ! __is_times_set (ocp) &&
208- throw (CTBase. UnauthorizedCall (" the times must be set before adding constraints." ))
209-
210- # checkings: if the ocp has no variable, then the constraint! function cannot be used with type=:variable
211- ! __is_variable_set (ocp) &&
212- type == :variable &&
213- throw (
214- CTBase. UnauthorizedCall (
215- " the ocp has no variable" *
216- " , you cannot use constraint! function with type=:variable. If it is a mistake, please set the variable first." ,
217- ),
218- )
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." )
206+
207+ # checkings: variable must be set if using type=:variable
208+ @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."
210+ )
219211
220212 # dimensions
221213 n = dimension (ocp. state)
222214 m = dimension (ocp. control)
223215 q = dimension (ocp. variable)
216+
224217 # add the constraint
225218 return __constraint! (
226219 ocp. constraints,
227220 type,
228221 n,
229222 m,
230223 q;
231- rg= as_range (rg),
232- f= f,
233- lb= as_vector (lb),
234- ub= as_vector (ub),
235- label= label,
224+ rg = as_range (rg),
225+ f = f,
226+ lb = as_vector (lb),
227+ ub = as_vector (ub),
228+ label = label,
236229 )
237230end
238231
232+
239233as_vector (:: Nothing ) = nothing
240234(as_vector (x:: T ):: Vector{T} ) where {T<: ctNumber } = [x]
241235as_vector (x:: Vector{T} ) where {T<: ctNumber } = x
0 commit comments