Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[breaking] throw error if Containers.SparseAxisArray used in constraint function #3680

Closed
wants to merge 3 commits into from
Closed
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
63 changes: 63 additions & 0 deletions src/macros/@constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,53 @@ function build_constraint(
return VectorConstraint(x, set)
end

function _build_sparse_axis_array_error_msg()
return """
Building a constraint in which the function is a `Containers.SparseAxisArray`.

## Work-around

Convert `x` into a vector by explicitly providing an ordered list of
indices. For example, instead of:

```julia
A = [[1, 2, 10], [2, 3, 30]]
model = Model()
@variable(model, x[i in 1:2, j in A[i]])
@constraint(model, x[1, :] in SecondOrderCone())
```

do:

```julia
A = [[1, 2, 10], [2, 3, 30]]
model = Model()
@variable(model, x[i in 1:2, j in A[i]])
y = [x[1, j] for j in A[1]]::Vector{VariableRef}
@constraint(model, y in SecondOrderCone())
```

## Explanation for breaking change in JuMP v1.21

Prior to JuMP v1.20, this constraint would have been allowed. It is now
an error because of the high risk of incorrect usage.

The backend data structure of a `Containers.SparseAxisArray` is a
`Dict`. JuMP used to create the constraint with
`[x[k] for k in eachindex(x)]`, however, the iteration order of
`eachindex` is undefined. This can silently cause incorrect models to be
built if the set of the constraint depends on the order of the elements.
"""
end

function build_constraint(
error_fn::Function,
::Containers.SparseAxisArray{<:Union{Number,AbstractJuMPScalar},1},
::MOI.AbstractVectorSet,
)
return error_fn(_build_sparse_axis_array_error_msg())
end

function build_constraint(
error_fn::Function,
x::AbstractArray,
Expand Down Expand Up @@ -963,3 +1010,19 @@ function build_constraint(
MOI.SOS2{value_type(variable_ref_type(T))}(set.weights),
)
end

function build_constraint(
error_fn::Function,
::Containers.SparseAxisArray{<:Union{Number,AbstractJuMPScalar},1},
::MOI.SOS1,
)
return error_fn(_build_sparse_axis_array_error_msg())
end

function build_constraint(
error_fn::Function,
::Containers.SparseAxisArray{<:Union{Number,AbstractJuMPScalar},1},
::MOI.SOS2,
)
return error_fn(_build_sparse_axis_array_error_msg())
end
26 changes: 26 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2358,4 +2358,30 @@ function test_op_or_short_circuit()
return
end

function test_sparseaxisarray_constraint_zeros()
A = [[1, 2, 10], [2, 3, 30]]
model = Model()
@variable(model, x[i in 1:2, j in A[i]])
msg = JuMP._build_sparse_axis_array_error_msg()
@test_throws_runtime(
ErrorException("In `@constraint(model, x[1, :] == 0)`: $msg"),
@constraint(model, x[1, :] == 0),
)
@test_throws_runtime(
ErrorException(
"In `@constraint(model, x[1, :] in SecondOrderCone())`: $msg",
),
@constraint(model, x[1, :] in SecondOrderCone()),
)
@test_throws_runtime(
ErrorException("In `@constraint(model, x[1, :] in SOS1())`: $msg"),
@constraint(model, x[1, :] in SOS1()),
)
@test_throws_runtime(
ErrorException("In `@constraint(model, x[1, :] in SOS2())`: $msg"),
@constraint(model, x[1, :] in SOS2()),
)
return
end

end # module
Loading