diff --git a/src/constraints.jl b/src/constraints.jl index 90b00951e4b..e4645f0d190 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -95,6 +95,7 @@ function check_belongs_to_model(con_ref::ConstraintRef, model::AbstractModel) if owner_model(con_ref) !== model throw(ConstraintNotOwned(con_ref)) end + return end Base.broadcastable(con_ref::ConstraintRef) = Ref(con_ref) @@ -891,7 +892,8 @@ function constraint_object( end function check_belongs_to_model(con::ScalarConstraint, model) - return check_belongs_to_model(con.func, model) + check_belongs_to_model(con.func, model) + return end """ @@ -990,9 +992,8 @@ function constraint_object( end function check_belongs_to_model(con::VectorConstraint, model) - for func in con.func - check_belongs_to_model(func, model) - end + check_belongs_to_model(con.func, model) + return end function _moi_add_constraint( @@ -1016,7 +1017,7 @@ function _moi_add_constraint( return MOI.add_constraint(model, f, s) end -function check_belongs_to_model(f::Vector, model) +function check_belongs_to_model(f::AbstractVector, model) for func in f check_belongs_to_model(func, model) end diff --git a/src/nlp.jl b/src/nlp.jl index 172ba5aa274..f9be0271eb7 100644 --- a/src/nlp.jl +++ b/src/nlp.jl @@ -218,9 +218,10 @@ struct NonlinearParameter <: AbstractJuMPScalar index::Int end -function check_belongs_to_model(arg::NonlinearParameter, model::AbstractModel) - return arg.model === model -end +# A pass-through. This is needed to catch wben a parameter is used with the new +# nonlinear API. It doesn't matter what the model is, because we're going to +# throw an error regardless. +check_belongs_to_model(::NonlinearParameter, ::AbstractModel) = nothing variable_ref_type(arg::NonlinearParameter) = variable_ref_type(arg.model) @@ -344,9 +345,10 @@ struct NonlinearExpression <: AbstractJuMPScalar index::Int end -function check_belongs_to_model(arg::NonlinearExpression, model::AbstractModel) - return arg.model === model -end +# A pass-through. This is needed to catch wben an expression is used with the +# new nonlinear API. It doesn't matter what the model is, because we're going to +# throw an error regardless. +check_belongs_to_model(::NonlinearExpression, ::AbstractModel) = nothing variable_ref_type(arg::NonlinearExpression) = variable_ref_type(arg.model) diff --git a/src/objective.jl b/src/objective.jl index 6feb9a608ea..56718c94bc8 100644 --- a/src/objective.jl +++ b/src/objective.jl @@ -278,7 +278,10 @@ function set_objective_function(model::GenericModel, func::MOI.AbstractFunction) return end -function set_objective_function(model::GenericModel, func::AbstractJuMPScalar) +function set_objective_function( + model::GenericModel, + func::Union{AbstractJuMPScalar,AbstractVector{<:AbstractJuMPScalar}}, +) check_belongs_to_model(func, model) set_objective_function(model, moi_function(model, func)) return @@ -292,17 +295,6 @@ function set_objective_function(model::GenericModel{T}, func::Real) where {T} return end -function set_objective_function( - model::GenericModel, - func::AbstractVector{<:AbstractJuMPScalar}, -) - for f in func - check_belongs_to_model(f, model) - end - set_objective_function(model, moi_function(model, func)) - return -end - function set_objective_function(model::AbstractModel, func) return error( """ diff --git a/src/quad_expr.jl b/src/quad_expr.jl index 6d9eb3aef10..3668f191292 100644 --- a/src/quad_expr.jl +++ b/src/quad_expr.jl @@ -702,6 +702,7 @@ function check_belongs_to_model(q::GenericQuadExpr, model::AbstractModel) check_belongs_to_model(variable_pair.a, model) check_belongs_to_model(variable_pair.b, model) end + return end """ diff --git a/src/variables.jl b/src/variables.jl index ed0f0191606..658daba65ed 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -367,7 +367,6 @@ end """ check_belongs_to_model(x::AbstractJuMPScalar, model::AbstractModel) - check_belongs_to_model(x::AbstractConstraint, model::AbstractModel) Throw [`VariableNotOwned`](@ref) if the [`owner_model`](@ref) of `x` is not `model`.