Skip to content

Commit 3f7299b

Browse files
authored
Fix constructors of Indicator set and tidy docstring (#2712)
1 parent 39e3549 commit 3f7299b

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

src/sets.jl

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,31 +1891,37 @@ Base.:(==)(a::SOS2{T}, b::SOS2{T}) where {T} = a.weights == b.weights
18911891
""",
18921892
ActivationCondition,
18931893
"""
1894-
The indicator constraint holds whhen the binary variable is zero.
1894+
The indicator constraint holds when the binary variable is zero.
18951895
""",
18961896
ACTIVATE_ON_ZERO,
18971897
"""
1898-
The indicator constraint holds whhen the binary variable is one.
1898+
The indicator constraint holds when the binary variable is one.
18991899
""",
19001900
ACTIVATE_ON_ONE,
19011901
)
19021902

19031903
"""
1904-
Indicator{A<:ActivationCondition,S<:AbstractScalarSet}(set::S)
1904+
Indicator{ACTIVATE_ON_ZERO}(set::AbstractScalarSet)
1905+
Indicator{ACTIVATE_ON_ONE}(set::AbstractScalarSet)
19051906
19061907
The set corresponding to an indicator constraint.
19071908
1908-
When `A` is [`ACTIVATE_ON_ZERO`](@ref), this means:
1909-
``\\{(y, x) \\in \\{0, 1\\} \\times \\mathbb{R}^n : y = 0 \\implies x \\in set\\}``
1909+
The type parameter must be an [`ActivationCondition`](@ref).
19101910
1911-
When `A` is [`ACTIVATE_ON_ONE`](@ref), this means:
1912-
``\\{(y, x) \\in \\{0, 1\\} \\times \\mathbb{R}^n : y = 1 \\implies x \\in set\\}``
1911+
When the type parameter is [`ACTIVATE_ON_ZERO`](@ref), this means:
1912+
1913+
``\\{(y, x) \\in \\{0, 1\\} \\times \\mathbb{R} : y = 0 \\implies x \\in set\\}``
1914+
1915+
When the type parameter is [`ACTIVATE_ON_ONE`](@ref), this means:
1916+
1917+
``\\{(y, x) \\in \\{0, 1\\} \\times \\mathbb{R} : y = 1 \\implies x \\in set\\}``
19131918
19141919
## Notes
19151920
19161921
Most solvers expect that the first row of the function is interpretable as a
1917-
variable index `x_i` (for example, `1.0 * x + 0.0`). An error will be thrown if this is
1918-
not the case.
1922+
[`VariableIndex`](@ref) (for example, `1.0 * x + 0.0`), and that the variable is
1923+
constrained to the [`ZeroOne`](@ref) set. An error will be thrown if this is not
1924+
the case.
19191925
19201926
## Example
19211927
@@ -1927,22 +1933,11 @@ julia> import MathOptInterface as MOI
19271933
19281934
julia> model = MOI.Utilities.Model{Float64}();
19291935
1930-
julia> x = MOI.add_variables(model, 2)
1931-
2-element Vector{MathOptInterface.VariableIndex}:
1932-
MOI.VariableIndex(1)
1933-
MOI.VariableIndex(2)
1936+
julia> x = MOI.add_variables(model, 2);
19341937
1935-
julia> y, _ = MOI.add_constrained_variable(model, MOI.ZeroOne())
1936-
(MOI.VariableIndex(3), MathOptInterface.ConstraintIndex{MathOptInterface.VariableIndex, MathOptInterface.ZeroOne}(3))
1938+
julia> y, _ = MOI.add_constrained_variable(model, MOI.ZeroOne());
19371939
1938-
julia> f = MOI.VectorAffineFunction(
1939-
[
1940-
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, y)),
1941-
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x[1])),
1942-
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x[2])),
1943-
],
1944-
[0.0, 0.0],
1945-
)
1940+
julia> f = MOI.Utilities.vectorize([y, 1.0 * x[1] + 1.0 * x[2]])
19461941
┌ ┐
19471942
│0.0 + 1.0 MOI.VariableIndex(3) │
19481943
│0.0 + 1.0 MOI.VariableIndex(1) + 1.0 MOI.VariableIndex(2)│
@@ -1954,10 +1949,37 @@ MathOptInterface.Indicator{MathOptInterface.ACTIVATE_ON_ONE, MathOptInterface.Le
19541949
julia> MOI.add_constraint(model, f, s)
19551950
MathOptInterface.ConstraintIndex{MathOptInterface.VectorAffineFunction{Float64}, MathOptInterface.Indicator{MathOptInterface.ACTIVATE_ON_ONE, MathOptInterface.LessThan{Float64}}}(1)
19561951
```
1952+
1953+
The constraint
1954+
``\\{(y, x) \\in \\{0, 1\\} \\times \\mathbb{R} : y = 0 \\implies x = 0 \\}``
1955+
is defined as
1956+
```jldoctest
1957+
julia> import MathOptInterface as MOI
1958+
1959+
julia> model = MOI.Utilities.Model{Float64}();
1960+
1961+
julia> x = MOI.add_variable(model);
1962+
1963+
julia> y, _ = MOI.add_constrained_variable(model, MOI.ZeroOne());
1964+
1965+
julia> f = MOI.VectorOfVariables([y, x]);
1966+
1967+
julia> s = MOI.Indicator{MOI.ACTIVATE_ON_ZERO}(MOI.EqualTo(0.0))
1968+
MathOptInterface.Indicator{MathOptInterface.ACTIVATE_ON_ZERO, MathOptInterface.EqualTo{Float64}}(MathOptInterface.EqualTo{Float64}(0.0))
1969+
1970+
julia> MOI.add_constraint(model, f, s)
1971+
MathOptInterface.ConstraintIndex{MathOptInterface.VectorOfVariables, MathOptInterface.Indicator{MathOptInterface.ACTIVATE_ON_ZERO, MathOptInterface.EqualTo{Float64}}}(1)
1972+
```
19571973
"""
19581974
struct Indicator{A,S<:AbstractScalarSet} <: AbstractVectorSet
19591975
set::S
1960-
Indicator{A}(set::S) where {A,S<:AbstractScalarSet} = new{A,S}(set)
1976+
1977+
function Indicator{ACTIVATE_ON_ONE}(set::S) where {S<:AbstractScalarSet}
1978+
return new{ACTIVATE_ON_ONE,S}(set)
1979+
end
1980+
function Indicator{ACTIVATE_ON_ZERO}(set::S) where {S<:AbstractScalarSet}
1981+
return new{ACTIVATE_ON_ZERO,S}(set)
1982+
end
19611983
end
19621984

19631985
dimension(::Indicator) = 2

0 commit comments

Comments
 (0)