Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/JuMP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ function Base.getindex(A::ArrayOfVariables{T}, I...) where {T}
return JuMP.GenericVariableRef{T}(A.model, MOI.VariableIndex(index))
end

function JuMP.check_belongs_to_model(
array::ArrayOfVariables,
model::JuMP.AbstractModel,
)
if array.model !== model && !isempty(array)
throw(JuMP.VariableNotOwned(first(array)))
end
return
end

_check_belongs_to_model(::Any, ::JuMP.AbstractModel) = nothing

function _check_belongs_to_model(
expr::Union{JuMP.AbstractJuMPScalar,ArrayOfVariables},
model::JuMP.AbstractModel,
)
return JuMP.check_belongs_to_model(expr, model)
end

function _check_belongs_to_model(
expr::JuMP.GenericNonlinearExpr,
model::JuMP.AbstractModel,
)
for arg in expr.args
_check_belongs_to_model(arg, model)
end
return
end

JuMP._is_real(::ArrayOfVariables) = true
function JuMP.moi_function(array::ArrayOfVariables)
return ContiguousArrayOfVariables(array.offset, array.size)
Expand Down
4 changes: 2 additions & 2 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end
JuMP.variable_ref_type(::Type{ExprTemplate{E,V}}) where {E,V} = V

function JuMP.check_belongs_to_model(f::ExprTemplate, model)
return JuMP.check_belongs_to_model(f.expr, model)
return _check_belongs_to_model(f.expr, model)
end

"""
Expand Down Expand Up @@ -277,7 +277,7 @@ Base.iszero(::_AnyLazySum) = false
JuMP.owner_model(s::_AnyLazySum) = JuMP.owner_model(s.expr)

function JuMP.check_belongs_to_model(s::_AnyLazySum, model::JuMP.AbstractModel)
return JuMP.check_belongs_to_model(s.expr, model)
return _check_belongs_to_model(s.expr, model)
end

function MA.promote_operation(
Expand Down
20 changes: 20 additions & 0 deletions test/JuMP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ function test_container()
@test_broken JuMP.isequal_canonical(con_expr, expr)
end

function test_generator_variable_ownership()
model, other = Model(), Model()
@variable(model, x[1:2])
@variable(other, foreign[1:2])
i = GenOpt.iterator([1, 2])
@test JuMP.check_belongs_to_model(x[i], model) === nothing
@test JuMP.check_belongs_to_model(GenOpt.LazySum(x[i]), model) === nothing
for expr in (foreign[i], x[i] + foreign[i], (x[i] + 1.0) * foreign[i])
@test_throws VariableNotOwned JuMP.check_belongs_to_model(expr, model)
end
@test_throws VariableNotOwned JuMP.check_belongs_to_model(
GenOpt.LazySum(foreign[i]),
model,
)
constraint =
JuMP.build_constraint(error, x[i] + foreign[i], MOI.LessThan(0.0))
@test_throws VariableNotOwned JuMP.check_belongs_to_model(constraint, model)
return
end

function test_ind2sub()
# `bridge.jl` expands a generator with `CartesianIndices` so `_ind2sub`
# must agree with it, otherwise `getindex` and the bridge would disagree
Expand Down
Loading