Skip to content

Commit 38f45a7

Browse files
authored
Clean up check_belongs_to_model methods (#4200)
1 parent b2b5127 commit 38f45a7

5 files changed

Lines changed: 19 additions & 24 deletions

File tree

src/constraints.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function check_belongs_to_model(con_ref::ConstraintRef, model::AbstractModel)
9595
if owner_model(con_ref) !== model
9696
throw(ConstraintNotOwned(con_ref))
9797
end
98+
return
9899
end
99100

100101
Base.broadcastable(con_ref::ConstraintRef) = Ref(con_ref)
@@ -891,7 +892,8 @@ function constraint_object(
891892
end
892893

893894
function check_belongs_to_model(con::ScalarConstraint, model)
894-
return check_belongs_to_model(con.func, model)
895+
check_belongs_to_model(con.func, model)
896+
return
895897
end
896898

897899
"""
@@ -990,9 +992,8 @@ function constraint_object(
990992
end
991993

992994
function check_belongs_to_model(con::VectorConstraint, model)
993-
for func in con.func
994-
check_belongs_to_model(func, model)
995-
end
995+
check_belongs_to_model(con.func, model)
996+
return
996997
end
997998

998999
function _moi_add_constraint(
@@ -1016,7 +1017,7 @@ function _moi_add_constraint(
10161017
return MOI.add_constraint(model, f, s)
10171018
end
10181019

1019-
function check_belongs_to_model(f::Vector, model)
1020+
function check_belongs_to_model(f::AbstractVector, model)
10201021
for func in f
10211022
check_belongs_to_model(func, model)
10221023
end

src/nlp.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,10 @@ struct NonlinearParameter <: AbstractJuMPScalar
218218
index::Int
219219
end
220220

221-
function check_belongs_to_model(arg::NonlinearParameter, model::AbstractModel)
222-
return arg.model === model
223-
end
221+
# A pass-through. This is needed to catch wben a parameter is used with the new
222+
# nonlinear API. It doesn't matter what the model is, because we're going to
223+
# throw an error regardless.
224+
check_belongs_to_model(::NonlinearParameter, ::AbstractModel) = nothing
224225

225226
variable_ref_type(arg::NonlinearParameter) = variable_ref_type(arg.model)
226227

@@ -344,9 +345,10 @@ struct NonlinearExpression <: AbstractJuMPScalar
344345
index::Int
345346
end
346347

347-
function check_belongs_to_model(arg::NonlinearExpression, model::AbstractModel)
348-
return arg.model === model
349-
end
348+
# A pass-through. This is needed to catch wben an expression is used with the
349+
# new nonlinear API. It doesn't matter what the model is, because we're going to
350+
# throw an error regardless.
351+
check_belongs_to_model(::NonlinearExpression, ::AbstractModel) = nothing
350352

351353
variable_ref_type(arg::NonlinearExpression) = variable_ref_type(arg.model)
352354

src/objective.jl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ function set_objective_function(model::GenericModel, func::MOI.AbstractFunction)
278278
return
279279
end
280280

281-
function set_objective_function(model::GenericModel, func::AbstractJuMPScalar)
281+
function set_objective_function(
282+
model::GenericModel,
283+
func::Union{AbstractJuMPScalar,AbstractVector{<:AbstractJuMPScalar}},
284+
)
282285
check_belongs_to_model(func, model)
283286
set_objective_function(model, moi_function(model, func))
284287
return
@@ -292,17 +295,6 @@ function set_objective_function(model::GenericModel{T}, func::Real) where {T}
292295
return
293296
end
294297

295-
function set_objective_function(
296-
model::GenericModel,
297-
func::AbstractVector{<:AbstractJuMPScalar},
298-
)
299-
for f in func
300-
check_belongs_to_model(f, model)
301-
end
302-
set_objective_function(model, moi_function(model, func))
303-
return
304-
end
305-
306298
function set_objective_function(model::AbstractModel, func)
307299
return error(
308300
"""

src/quad_expr.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ function check_belongs_to_model(q::GenericQuadExpr, model::AbstractModel)
702702
check_belongs_to_model(variable_pair.a, model)
703703
check_belongs_to_model(variable_pair.b, model)
704704
end
705+
return
705706
end
706707

707708
"""

src/variables.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ end
367367

368368
"""
369369
check_belongs_to_model(x::AbstractJuMPScalar, model::AbstractModel)
370-
check_belongs_to_model(x::AbstractConstraint, model::AbstractModel)
371370
372371
Throw [`VariableNotOwned`](@ref) if the [`owner_model`](@ref) of `x` is not
373372
`model`.

0 commit comments

Comments
 (0)