Skip to content

Commit 3829fb2

Browse files
authored
Add VariableInSetRef and is_variable_in_set (#3955)
1 parent 7db04da commit 3829fb2

File tree

5 files changed

+365
-30
lines changed

5 files changed

+365
-30
lines changed

docs/src/manual/variables.md

+49-2
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,40 @@ julia> x = @variable(model, [1:3], set = SecondOrderCone())
13081308
You cannot delete the constraint associated with a variable constrained on
13091309
creation.
13101310

1311+
To check if a variable was constrained on creation, use [`is_variable_in_set`](@ref),
1312+
and use [`VariableInSetRef`](@ref) to obtain the associated constraint reference:
1313+
1314+
```jldoctest
1315+
julia> model = Model();
1316+
1317+
julia> @variable(model, x[1:2, 1:2], PSD)
1318+
2×2 LinearAlgebra.Symmetric{VariableRef, Matrix{VariableRef}}:
1319+
x[1,1] x[1,2]
1320+
x[1,2] x[2,2]
1321+
1322+
julia> is_variable_in_set(x)
1323+
true
1324+
1325+
julia> c = VariableInSetRef(x)
1326+
[x[1,1] x[1,2]
1327+
⋯ x[2,2]] ∈ PSDCone()
1328+
1329+
julia> @variable(model, y)
1330+
y
1331+
1332+
julia> is_variable_in_set(y)
1333+
false
1334+
1335+
julia> @variable(model, z in Semicontinuous(1, 2))
1336+
z
1337+
1338+
julia> is_variable_in_set(z)
1339+
true
1340+
1341+
julia> c_z = VariableInSetRef(z)
1342+
z ∈ MathOptInterface.Semicontinuous{Int64}(1, 2)
1343+
```
1344+
13111345
### Example: positive semidefinite variables
13121346

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

13861420
```jldoctest hermitian_psd
1387-
julia> first(all_constraints(model, Vector{VariableRef}, MOI.HermitianPositiveSemidefiniteConeTriangle))
1388-
[real(H[1,1]), real(H[1,2]), real(H[2,2]), imag(H[1,2])] ∈ MathOptInterface.HermitianPositiveSemidefiniteConeTriangle(2)
1421+
julia> c = VariableInSetRef(H)
1422+
[real(H[1,1]) real(H[1,2]) + imag(H[1,2]) im
1423+
real(H[1,2]) - imag(H[1,2]) im real(H[2,2])] ∈ HermitianPSDCone()
1424+
1425+
julia> o = constraint_object(c);
1426+
1427+
julia> o.func
1428+
4-element Vector{VariableRef}:
1429+
real(H[1,1])
1430+
real(H[1,2])
1431+
real(H[2,2])
1432+
imag(H[1,2])
1433+
1434+
julia> o.set
1435+
MathOptInterface.HermitianPositiveSemidefiniteConeTriangle(2)
13891436
```
13901437

13911438
### Example: Hermitian variables

src/JuMP.jl

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ mutable struct GenericModel{T<:Real} <: AbstractModel
124124
# A model-level option that is used as the default for the set_string_name
125125
# keyword to @variable and @constraint.
126126
set_string_names_on_creation::Bool
127+
#
128+
variable_in_set_ref::Dict{Any,MOI.ConstraintIndex}
127129
end
128130

129131
value_type(::Type{GenericModel{T}}) where {T} = T
@@ -239,6 +241,7 @@ function direct_generic_model(
239241
false,
240242
Dict{Symbol,Any}(),
241243
true,
244+
Dict{Any,MOI.ConstraintIndex}(),
242245
)
243246
end
244247

src/sd.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ julia> @variable(model, H[1:3, 1:3] in HermitianPSDCone())
477477
real(H[1,2]) - imag(H[1,2]) im real(H[2,3]) + imag(H[2,3]) im
478478
real(H[1,3]) - imag(H[1,3]) im real(H[3,3])
479479
480-
julia> all_variables(model)
480+
julia> c = constraint_object(VariableInSetRef(H));
481+
482+
julia> c.func
481483
9-element Vector{VariableRef}:
482484
real(H[1,1])
483485
real(H[1,2])
@@ -489,9 +491,8 @@ julia> all_variables(model)
489491
imag(H[1,3])
490492
imag(H[2,3])
491493
492-
julia> all_constraints(model, Vector{VariableRef}, MOI.HermitianPositiveSemidefiniteConeTriangle)
493-
1-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.VectorOfVariables, MathOptInterface.HermitianPositiveSemidefiniteConeTriangle}}}:
494-
[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)
494+
julia> c.set
495+
MathOptInterface.HermitianPositiveSemidefiniteConeTriangle(3)
495496
```
496497
We see in the output of the last commands that 9 real variables were created.
497498
The matrix `H` constrains affine expressions in terms of these 9 variables that

0 commit comments

Comments
 (0)