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

Add VariableInSetRef and has_variable_in_set #3955

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
51 changes: 49 additions & 2 deletions docs/src/manual/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,40 @@ julia> x = @variable(model, [1:3], set = SecondOrderCone())
You cannot delete the constraint associated with a variable constrained on
creation.

To check if a variable was constrained on creation, use [`has_variable_in_set`](@ref),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To check if a variable was constrained on creation, use [`has_variable_in_set`](@ref),
To check if a variable was constrained on creation, use [`is_variable_in_set`](@ref),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a general request to change the name of the function? We have is_integer and is_fixed but has_lower_bound and has_upper_bound.

and use [`VariableInSetRef`](@ref) to obtain the associated constraint reference:

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

julia> @variable(model, x[1:2, 1:2], PSD)
2×2 LinearAlgebra.Symmetric{VariableRef, Matrix{VariableRef}}:
x[1,1] x[1,2]
x[1,2] x[2,2]

julia> has_variable_in_set(x)
true

julia> c = VariableInSetRef(x)
[x[1,1] x[1,2]
⋯ x[2,2]] ∈ PSDCone()

julia> @variable(model, y)
y

julia> has_variable_in_set(y)
false

julia> @variable(model, z in Semicontinuous(1, 2))
z

julia> has_variable_in_set(z)
true

julia> c_z = VariableInSetRef(z)
z ∈ MathOptInterface.Semicontinuous{Int64}(1, 2)
```

### Example: positive semidefinite variables

An alternative to the syntax in [Semidefinite variables](@ref), declare a matrix
Expand Down Expand Up @@ -1384,8 +1418,21 @@ julia> @variable(model, H[1:2, 1:2] in HermitianPSDCone())
This adds 4 real variables in the [`MOI.HermitianPositiveSemidefiniteConeTriangle`](@ref):

```jldoctest hermitian_psd
julia> first(all_constraints(model, Vector{VariableRef}, MOI.HermitianPositiveSemidefiniteConeTriangle))
[real(H[1,1]), real(H[1,2]), real(H[2,2]), imag(H[1,2])] ∈ MathOptInterface.HermitianPositiveSemidefiniteConeTriangle(2)
julia> c = VariableInSetRef(H)
[real(H[1,1]) real(H[1,2]) + imag(H[1,2]) im
real(H[1,2]) - imag(H[1,2]) im real(H[2,2])] ∈ HermitianPSDCone()

julia> o = constraint_object(c);

julia> o.func
4-element Vector{VariableRef}:
real(H[1,1])
real(H[1,2])
real(H[2,2])
imag(H[1,2])

julia> o.set
MathOptInterface.HermitianPositiveSemidefiniteConeTriangle(2)
```

### Example: Hermitian variables
Expand Down
3 changes: 3 additions & 0 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ mutable struct GenericModel{T<:Real} <: AbstractModel
# A model-level option that is used as the default for the set_string_name
# keyword to @variable and @constraint.
set_string_names_on_creation::Bool
#
variable_in_set_ref::Dict{Any,MOI.ConstraintIndex}
end

value_type(::Type{GenericModel{T}}) where {T} = T
Expand Down Expand Up @@ -239,6 +241,7 @@ function direct_generic_model(
false,
Dict{Symbol,Any}(),
true,
Dict{Any,MOI.ConstraintIndex}(),
)
end

Expand Down
9 changes: 5 additions & 4 deletions src/sd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ julia> @variable(model, H[1:3, 1:3] in HermitianPSDCone())
real(H[1,2]) - imag(H[1,2]) im real(H[2,3]) + imag(H[2,3]) im
real(H[1,3]) - imag(H[1,3]) im real(H[3,3])

julia> all_variables(model)
julia> c = constraint_object(VariableInSetRef(H));

julia> c.func
9-element Vector{VariableRef}:
real(H[1,1])
real(H[1,2])
Expand All @@ -489,9 +491,8 @@ julia> all_variables(model)
imag(H[1,3])
imag(H[2,3])

julia> all_constraints(model, Vector{VariableRef}, MOI.HermitianPositiveSemidefiniteConeTriangle)
1-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.VectorOfVariables, MathOptInterface.HermitianPositiveSemidefiniteConeTriangle}}}:
[real(H[1,1]), real(H[1,2]), real(H[2,2]), real(H[1,3]), real(H[2,3]), real(H[3,3]), imag(H[1,2]), imag(H[1,3]), imag(H[2,3])] ∈ MathOptInterface.HermitianPositiveSemidefiniteConeTriangle(3)
julia> c.set
MathOptInterface.HermitianPositiveSemidefiniteConeTriangle(3)
```
We see in the output of the last commands that 9 real variables were created.
The matrix `H` constrains affine expressions in terms of these 9 variables that
Expand Down
Loading
Loading