Skip to content

Commit

Permalink
constraint with unnamed arguments now only call the one with named args
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptisteCbl committed Nov 6, 2023
1 parent 8d356e8 commit 27507a6
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,40 +478,10 @@ julia> constraint!(ocp, :variable, 0, 1) # the variable here is of dimension 1
"""
function constraint!(ocp::OptimalControlModel, type::Symbol, lb::Union{ctVector,Nothing}, ub::Union{ctVector,Nothing},
label::Symbol=__constraint_label())
# we check if the dimensions and times have been set
__check_all_set(ocp)
type == :variable && is_variable_independent(ocp) && throw(UnauthorizedCall("the ocp is variable independent" *
", you cannot use constraint! function with type=:variable."))

#
rg = nothing

# dimensions
n = ocp.state_dimension
m = ocp.control_dimension
q = ocp.variable_dimension

#
if type [:initial, :final, :state]
rg = n == 1 ? Index(1) : 1:n
txt = "the lower bound `lb` and the upper bound `ub` must be of dimension $n"
elseif type == :control
rg = m == 1 ? Index(1) : 1:m
txt = "the lower bound `lb` and the upper bound `ub` must be of dimension $m"
elseif type == :variable
rg = q == 1 ? Index(1) : 1:q
txt = "the lower bound `lb` and the upper bound `ub` must be of dimension $q"
else
throw(IncorrectArgument("the following type of constraint is not valid: " * String(type) *
". Please choose in [ :initial, :final, :control, :state, :variable ] or check the arguments of the constraint! method."))
end

#
(length(rg) != length(lb)) && throw(IncorrectArgument(txt))
(length(rg) != length(ub)) && throw(IncorrectArgument(txt))
constraint!(ocp, type, rg=nothing, f=nothing, lb=lb, ub=ub, label=label)

#
constraint!(ocp, type, rg=rg, f=nothing, lb=lb, ub=ub, label=label)
nothing # to force to return nothing

end

Expand Down Expand Up @@ -609,7 +579,29 @@ function constraint!(ocp::OptimalControlModel{T, V}, type::Symbol;

@match (rg,f,lb,ub) begin
(::Nothing,::Nothing,::ctVector,::ctVector) => begin
constraint!(ocp,type,lb,ub,label)
#
rg = nothing

#
if type [:initial, :final, :state]
rg = n == 1 ? Index(1) : 1:n
txt = "the lower bound `lb` and the upper bound `ub` must be of dimension $n"
elseif type == :control
rg = m == 1 ? Index(1) : 1:m
txt = "the lower bound `lb` and the upper bound `ub` must be of dimension $m"
elseif type == :variable
rg = q == 1 ? Index(1) : 1:q
txt = "the lower bound `lb` and the upper bound `ub` must be of dimension $q"
else
throw(IncorrectArgument("the following type of constraint is not valid: " * String(type) *
". Please choose in [ :initial, :final, :control, :state, :variable ] or check the arguments of the constraint! method."))
end

#
(length(rg) != length(lb)) && throw(IncorrectArgument(txt))
(length(rg) != length(ub)) && throw(IncorrectArgument(txt))

constraint!(ocp, type, rg=rg, lb=lb, ub=ub, label=label)
end
(::Nothing,::Function,::ctVector,::ctVector) => begin
# set the constraint
Expand Down

0 comments on commit 27507a6

Please sign in to comment.