Skip to content
7 changes: 0 additions & 7 deletions src/aff_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -747,13 +747,6 @@ function value(a::GenericAffExpr; result::Int = 1)
end
end

function check_belongs_to_model(a::GenericAffExpr, model::AbstractModel)
for variable in keys(a.terms)
check_belongs_to_model(variable, model)
end
return
end

# Note: No validation is performed that the variables in the AffExpr belong to
# the same model. The verification is done in `check_belongs_to_model` which
# should be called before calling `MOI.ScalarAffineFunction`.
Expand Down
21 changes: 2 additions & 19 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -891,11 +891,6 @@ function constraint_object(
return ScalarConstraint(jump_function(model, f), s)
end

function check_belongs_to_model(con::ScalarConstraint, model)
check_belongs_to_model(con.func, model)
return
end

"""
struct VectorConstraint

Expand Down Expand Up @@ -991,11 +986,6 @@ function constraint_object(
return VectorConstraint(jump_function(model, f), s, con_ref.shape)
end

function check_belongs_to_model(con::VectorConstraint, model)
check_belongs_to_model(con.func, model)
return
end

function _moi_add_constraint(
model::MOI.ModelLike,
f::F,
Expand All @@ -1017,13 +1007,6 @@ function _moi_add_constraint(
return MOI.add_constraint(model, f, s)
end

function check_belongs_to_model(f::AbstractVector, model)
for func in f
check_belongs_to_model(func, model)
end
return
end

"""
add_constraint(
model::GenericModel,
Expand All @@ -1040,8 +1023,8 @@ function add_constraint(
name::String = "",
)
con = model_convert(model, con)
check_belongs_to_model(con, model)
func, set = moi_function(model, con), moi_set(con)
func = moi_function(model, con)
set = moi_set(con)
# The type of backend(model) is unknown so we directly redirect to another
# function.
cindex = _moi_add_constraint(
Expand Down
123 changes: 114 additions & 9 deletions src/jump_moi_function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ function moi_function_type end
moi_function(model::GenericModel, x::AbstractJuMPScalar)
moi_function(model::GenericModel, x::AbstractArray{<:AbstractJuMPScalar})

Given a JuMP object `x`, return the MathOptInterface equivalent.
Check that `x` belongs to `model`, then return the MathOptInterface equivalent
of the function `x`.

This is equivalent to calling `check_belongs_to_model(x, model)` followed by
`moi_function(x)`, but some methods may fuse the ownership check and conversion
into a single function call, and some types do not support the single-argument
[`moi_function`](@ref).

See also: [`jump_function`](@ref).

!!! compat
The `model` argument was added in JuMP v1.31. To maintain backwards
compatibility, there is a default fallback for `moi_function(x)`. New
functions should use the two-argument version.
The `model` argument was added in JuMP v1.31. New functions should use the
two-argument version.

## Example

Expand All @@ -48,9 +53,37 @@ julia> moi_function(model, f)
"""
function moi_function end

# A default fallback for backwards compatibility. The first argument `model` was
# Default fallback for backwards compatibility. The first argument `model` was
# introduced in JuMP@1.31.0.
moi_function(model, f) = moi_function(f)
function moi_function(model, f)
check_belongs_to_model(f, model)
return moi_function(f)
end

"""
check_belongs_to_model(x::AbstractJuMPScalar, model::AbstractModel)

Throw [`VariableNotOwned`](@ref) if the [`owner_model`](@ref) of `x` is not
`model`.

## Example

```jldoctest
julia> model = Model();

julia> @variable(model, x);

julia> check_belongs_to_model(x, model)

julia> model_2 = Model();

julia> check_belongs_to_model(x, model_2)
ERROR: VariableNotOwned{VariableRef}(x): the variable x cannot be used in this model because
it belongs to a different model.
[...]
```
"""
function check_belongs_to_model end

"""
jump_function_type(model::AbstractModel, ::Type{T}) where {T}
Expand Down Expand Up @@ -99,6 +132,13 @@ moi_function_type(::Type{<:AbstractVariableRef}) = MOI.VariableIndex

moi_function(variable::AbstractVariableRef) = index(variable)

function check_belongs_to_model(v::AbstractVariableRef, model::AbstractModel)
if owner_model(v) !== model
throw(VariableNotOwned(v))
end
return
end

function jump_function_type(
::GenericModel{T},
::Type{MOI.VariableIndex},
Expand All @@ -121,6 +161,13 @@ end

moi_function(a::GenericAffExpr) = MOI.ScalarAffineFunction(a)

function check_belongs_to_model(a::GenericAffExpr, model::AbstractModel)
for variable in keys(a.terms)
check_belongs_to_model(variable, model)
end
return
end

function jump_function_type(
::GenericModel{T},
::Type{MOI.ScalarAffineFunction{C}},
Expand All @@ -147,6 +194,15 @@ function moi_function(aff::GenericQuadExpr)
return MOI.ScalarQuadraticFunction(aff)
end

function check_belongs_to_model(q::GenericQuadExpr, model::AbstractModel)
check_belongs_to_model(q.aff, model)
for variable_pair in keys(q.terms)
check_belongs_to_model(variable_pair.a, model)
check_belongs_to_model(variable_pair.b, model)
end
return
end

function jump_function_type(
::GenericModel{T},
::Type{MOI.ScalarQuadraticFunction{C}},
Expand Down Expand Up @@ -211,9 +267,8 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
end
# There are two reasons we might reach here:
# 1. The function `f` has no `AbstractJuMPScalar` terms, like
# `NonlinearExpr(:+, Any[0.0])`. In this case, `model === nothing`. It
# doesn't matter that we pass `(model, ` below, because no method will
# exist and we will fallback to the single argument method.
# `NonlinearExpr(:+, Any[0.0])`. In this case, `model === nothing`, and
# we use the single-argument method below.
# 2. The function `f` contains terms from a JuMP extension, for example
# InfiniteOpt. We call the two-argument version in case they have
# implemented it.
Expand All @@ -222,6 +277,8 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
for i in length(f.args):-1:1
if f.args[i] isa GenericNonlinearExpr{V}
push!(stack, (ret, i, f.args[i]))
elseif model === nothing
ret.args[i] = moi_function(f.args[i])
else
ret.args[i] = moi_function(model, f.args[i])
end
Expand All @@ -233,6 +290,8 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
for j in length(arg.args):-1:1
if arg.args[j] isa GenericNonlinearExpr{V}
push!(stack, (child, j, arg.args[j]))
elseif model === nothing
child.args[j] = moi_function(arg.args[j])
else
child.args[j] = moi_function(model, arg.args[j])
end
Expand All @@ -241,6 +300,31 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
return ret
end

function check_belongs_to_model(
expr::GenericNonlinearExpr,
model::AbstractModel,
)
# TODO: Consider keeping an `IdDict` of visited expressions so that aliases
# are checked only once. This traversal treats the expression as a tree, so
# repeatedly aliased subexpressions can cause the work to grow
# exponentially in the depth of the expression, even though the underlying
# expression is a much smaller DAG. This is not urgent because JuMP's
# internal conversion path checks ownership while converting and caches
# aliases; this method is now used only when a user calls it directly.
stack = Any[expr]
while !isempty(stack)
child = pop!(stack)
if child isa GenericNonlinearExpr
for arg in child.args
push!(stack, arg)
end
elseif child isa AbstractJuMPScalar
check_belongs_to_model(child, model)
end
end
return
end

function jump_function_type(
model::GenericModel,
::Type{<:MOI.ScalarNonlinearFunction},
Expand Down Expand Up @@ -369,6 +453,13 @@ function moi_function(f::AbstractVector{<:GenericNonlinearExpr})
return MOI.VectorNonlinearFunction(f)
end

function moi_function(
model::GenericModel,
f::AbstractVector{<:GenericNonlinearExpr},
)
return MOI.VectorNonlinearFunction([moi_function(model, row) for row in f])
end

function jump_function_type(
::GenericModel{T},
::Type{MOI.VectorNonlinearFunction},
Expand Down Expand Up @@ -444,6 +535,11 @@ function moi_function(model, constraint::AbstractConstraint)
return moi_function(model, jump_function(constraint))
end

function check_belongs_to_model(con::AbstractConstraint, model::AbstractModel)
check_belongs_to_model(jump_function(con), model)
return
end

"""
jump_function(constraint::AbstractConstraint)

Expand All @@ -456,6 +552,8 @@ jump_function(constraint::AbstractConstraint) = constraint.func

moi_function(x::Number) = x

check_belongs_to_model(::Number, ::AbstractModel) = nothing

jump_function(::GenericModel{T}, x::Number) where {T} = convert(T, x)

# Base.AbstractArray
Expand Down Expand Up @@ -488,3 +586,10 @@ function moi_function(x::AbstractArray{AbstractJuMPScalar})
""",
)
end

function check_belongs_to_model(f::AbstractArray, model::AbstractModel)
for func in f
check_belongs_to_model(func, model)
end
return
end
18 changes: 0 additions & 18 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -623,24 +623,6 @@ function owner_model(expr::GenericNonlinearExpr)
return nothing
end

function check_belongs_to_model(
expr::GenericNonlinearExpr,
model::AbstractModel,
)
stack = Any[expr]
while !isempty(stack)
child = pop!(stack)
if child isa GenericNonlinearExpr
for arg in child.args
push!(stack, arg)
end
elseif child isa AbstractJuMPScalar
check_belongs_to_model(child, model)
end
end
return
end

function constraint_object(c::NonlinearConstraintRef)
nlp = nonlinear_model(c.model)::MOI.Nonlinear.Model
data = nlp.constraints[index(c)]
Expand Down
1 change: 0 additions & 1 deletion src/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ 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
end
Expand Down
9 changes: 0 additions & 9 deletions src/quad_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -696,15 +696,6 @@ end

GenericQuadExpr{C,V}() where {C,V} = zero(GenericQuadExpr{C,V})

function check_belongs_to_model(q::GenericQuadExpr, model::AbstractModel)
check_belongs_to_model(q.aff, model)
for variable_pair in keys(q.terms)
check_belongs_to_model(variable_pair.a, model)
check_belongs_to_model(variable_pair.b, model)
end
return
end

"""
_moi_quadratic_term(t::Tuple)

Expand Down
32 changes: 0 additions & 32 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,38 +357,6 @@ function Base.showerror(io::IO, err::VariableNotOwned)
)
end

"""
check_belongs_to_model(x::AbstractJuMPScalar, model::AbstractModel)

Throw [`VariableNotOwned`](@ref) if the [`owner_model`](@ref) of `x` is not
`model`.

## Example

```jldoctest
julia> model = Model();

julia> @variable(model, x);

julia> check_belongs_to_model(x, model)

julia> model_2 = Model();

julia> check_belongs_to_model(x, model_2)
ERROR: VariableNotOwned{VariableRef}(x): the variable x cannot be used in this model because
it belongs to a different model.
[...]
```
"""
function check_belongs_to_model end

function check_belongs_to_model(v::AbstractVariableRef, model::AbstractModel)
if owner_model(v) !== model
throw(VariableNotOwned(v))
end
return
end

Base.iszero(::GenericVariableRef) = false

function Base.copy(v::GenericVariableRef{T}) where {T}
Expand Down
Loading
Loading