Skip to content

Commit 8b42658

Browse files
authored
[Bridges] add tests and functionality for IndicatorActiveOnFalseBridge (#1897)
1 parent 2449ea5 commit 8b42658

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/Bridges/Constraint/bridges/indicator_activate_on_zero.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,80 @@ function concrete_bridge_type(
112112
) where {F<:MOI.VectorAffineFunction,S<:MOI.AbstractScalarSet}
113113
return IndicatorActiveOnFalseBridge{Float64,F,S}
114114
end
115+
116+
MOI.get(::IndicatorActiveOnFalseBridge, ::MOI.NumberOfVariables)::Int64 = 1
117+
118+
function MOI.get(b::IndicatorActiveOnFalseBridge, ::MOI.ListOfVariableIndices)
119+
return [b.variable]
120+
end
121+
122+
function MOI.get(
123+
::IndicatorActiveOnFalseBridge{T},
124+
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}},
125+
)::Int64 where {T}
126+
return 1
127+
end
128+
129+
function MOI.get(
130+
b::IndicatorActiveOnFalseBridge{T},
131+
::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}},
132+
) where {T}
133+
return [b.disjunction_cons]
134+
end
135+
136+
function MOI.get(
137+
::IndicatorActiveOnFalseBridge{T,F,S},
138+
::MOI.NumberOfConstraints{F,MOI.Indicator{MOI.ACTIVATE_ON_ONE,S}},
139+
)::Int64 where {T,F,S}
140+
return 1
141+
end
142+
143+
function MOI.get(
144+
b::IndicatorActiveOnFalseBridge{T,F,S},
145+
::MOI.ListOfConstraintIndices{F,MOI.Indicator{MOI.ACTIVATE_ON_ONE,S}},
146+
) where {T,F,S}
147+
return [b.indicator_cons_index]
148+
end
149+
150+
function MOI.get(
151+
::IndicatorActiveOnFalseBridge{T},
152+
::MOI.NumberOfConstraints{MOI.VariableIndex,MOI.ZeroOne},
153+
)::Int64 where {T}
154+
return 1
155+
end
156+
157+
function MOI.get(
158+
b::IndicatorActiveOnFalseBridge{T},
159+
::MOI.ListOfConstraintIndices{MOI.VariableIndex,MOI.ZeroOne},
160+
) where {T}
161+
return [b.zero_one_cons]
162+
end
163+
164+
function MOI.get(
165+
model::MOI.ModelLike,
166+
attr::MOI.ConstraintSet,
167+
b::IndicatorActiveOnFalseBridge,
168+
)
169+
set = MOI.get(model, attr, b.indicator_cons_index)
170+
return MOI.Indicator{MOI.ACTIVATE_ON_ZERO}(set.set)
171+
end
172+
173+
function MOI.get(
174+
model::MOI.ModelLike,
175+
attr::MOI.ConstraintFunction,
176+
b::IndicatorActiveOnFalseBridge{T},
177+
) where {T}
178+
f = MOI.get(model, attr, b.indicator_cons_index)
179+
y, fz = MOI.Utilities.eachscalar(f)
180+
z_plus_y = MOI.get(model, MOI.ConstraintFunction(), b.disjunction_cons)
181+
z = MOI.Utilities.operate(-, T, z_plus_y, y)
182+
MOI.Utilities.canonicalize!(z)
183+
return MOI.Utilities.operate(vcat, T, z, fz)
184+
end
185+
186+
function MOI.delete(model::MOI.ModelLike, bridge::IndicatorActiveOnFalseBridge)
187+
MOI.delete(model, bridge.disjunction_cons)
188+
MOI.delete(model, bridge.indicator_cons_index)
189+
MOI.delete(model, bridge.variable)
190+
return
191+
end

test/Bridges/Constraint/indicator_activate_on_zero.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ function test_indicator_activate_on_zero()
8181
end
8282
end
8383

84+
function test_runtests()
85+
MOI.Bridges.runtests(
86+
MOI.Bridges.Constraint.IndicatorActiveOnFalseBridge,
87+
"""
88+
variables: x, z
89+
[z, 2.0 * x] in Indicator{ACTIVATE_ON_ZERO}(LessThan(2.0))
90+
z in ZeroOne()
91+
""",
92+
"""
93+
variables: x, z, y
94+
[y, 2.0 * x] in Indicator{ACTIVATE_ON_ONE}(LessThan(2.0))
95+
z + y == 1.0
96+
z in ZeroOne()
97+
y in ZeroOne()
98+
""",
99+
)
100+
return
101+
end
102+
84103
end # module
85104

86105
TestConstraintIndicatorActiveOnFalse.runtests()

0 commit comments

Comments
 (0)