Skip to content
Draft
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
114 changes: 114 additions & 0 deletions src/direct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,117 @@ function JuMP.build_constraint(
)
return JuMP.VectorConstraint(collect(promote(func...)), set)
end

################################################################################
# DISJUNCTION SET RAISING
################################################################################
# Inverse of the lowering above, for models copied in from MOI: each
# `DisjunctionSet` becomes a disjunction whose logical variables reuse its bin

function _affine_part(vref::JuMP.AbstractVariableRef)
T = JuMP.value_type(typeof(JuMP.owner_model(vref)))
return convert(JuMP.GenericAffExpr{T, typeof(vref)}, vref)
end
_affine_part(expr::JuMP.GenericAffExpr) = expr
# A quadratic row makes every entry a `QuadExpr`; one without quadratic terms
# is still affine
function _affine_part(expr::JuMP.GenericQuadExpr)
isempty(expr.terms) && return expr.aff
return error("`DisjunctionSet` indicator `$expr` is not affine.")
end
# Any other expression is not affine
function _affine_part(expr)
return error("`DisjunctionSet` indicator `$expr` is not affine.")
end

# Binary `y` of an indicator
function _indicator_binary(indicator)
aff = _affine_part(indicator)
# y -> (y, false), 1 - y -> (y, true)
if length(aff.terms) == 1
bvref, coef = only(aff.terms)
constant = JuMP.constant(aff)
isone(coef) && iszero(constant) && return bvref, false
isone(-coef) && isone(constant) && return bvref, true
end
return error("`DisjunctionSet` indicator `$indicator` must be a binary " *
"variable `y` or its complement `1 - y`.")
end

# Logical variable for an indicator, created on first use and reusing `y`
# as its binary
function _indicator_logical_variable(model, indicator_map, indicator)
key = _indicator_binary(indicator)
haskey(indicator_map, key) && return indicator_map[key]
bvref, is_complement = key
if is_complement
complement_lvref = _indicator_logical_variable(model, indicator_map,
bvref)
lvar = LogicalVariable(nothing, nothing, complement_lvref)
lvref = _add_logical_variable(model, lvar,
"not_" * JuMP.name(complement_lvref))
else
JuMP.is_binary(bvref) || error(
"`DisjunctionSet` indicator `$bvref` must be binary.")
lvar = LogicalVariable(nothing, nothing, nothing)
lvref = _add_logical_variable(model, lvar, JuMP.name(bvref), bvref)
end
indicator_map[key] = lvref
return lvref
end

# Replace each `DisjunctionSet` by a disjunction (permanently). Parents and
# children share indicators through `indicator_map`, so order does not matter.
function _raise_disjunction_sets(model::JuMP.AbstractModel)
return
end
function _raise_disjunction_sets(model::JuMP.GenericModel)
indicator_map = Dict{Tuple{Any, Bool}, LogicalVariableRef}()
parent_lvrefs = LogicalVariableRef[]
for (F, S) in JuMP.list_of_constraint_types(model)
S == DisjunctionSet || continue
for cref in JuMP.all_constraints(model, F, S)
con = JuMP.constraint_object(cref)
func, set = con.func, con.set
activation = _affine_part(func[activation_index(set)])
is_nested = !(isempty(activation.terms) &&
isone(activation.constant))
if is_nested
parent_lvref = _indicator_logical_variable(
model, indicator_map, activation
)
push!(parent_lvrefs, parent_lvref)
end
indicators = [_indicator_logical_variable(model, indicator_map,
indicator) for indicator in func[indicator_indices(set)]]
for (disjunct_index, lvref) in enumerate(indicators)
disjunct_rows = func[row_indices(set, disjunct_index)]
for (row_func, row_set) in
zip(disjunct_rows, set.inner_sets[disjunct_index])
# affine form: reformulations reject a bare variable here
if row_func isa JuMP.AbstractVariableRef
row_func = _affine_part(row_func)
end
JuMP.@constraint(model, row_func in row_set, Disjunct(lvref))
end
end
name = JuMP.name(cref)
if is_nested
disjunction(model, indicators, Disjunct(parent_lvref), name)
else
disjunction(model, indicators, name)
end
JuMP.delete(model, cref)
end
end
# a nested disjunction under a non-disjunct indicator would be dropped
disjunct_indicators = Set(lvref
for disjunction_data in values(_disjunctions(model))
for lvref in disjunction_data.constraint.indicators)
for parent_lvref in parent_lvrefs
parent_lvref in disjunct_indicators || error(
"`DisjunctionSet` activation `$parent_lvref` is not the " *
"indicator of any disjunct.")
end
return
end
2 changes: 2 additions & 0 deletions src/reformulate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ all previous reformulation variables and constraints are deleted.
function reformulate_model(model::JuMP.AbstractModel, method::AbstractSolutionMethod = BigM())
#clear all previous reformulations
_clear_reformulations(model)
#turn DisjunctionSet constraints into disjunctions (permanent)
_raise_disjunction_sets(model)
#reformulate
_reformulate_disjunctions(model, method)
_reformulate_logical_constraints(model)
Expand Down
14 changes: 13 additions & 1 deletion src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ function JuMP.add_variable(
v::Union{LogicalVariable, _TaggedLogicalVariable},
name::String = ""
)
return _add_logical_variable(model, v, name)
end

# Add a logical variable whose binary is `bvref` if given, or a new binary
function _add_logical_variable(
model::JuMP.AbstractModel,
v::Union{LogicalVariable, _TaggedLogicalVariable},
name::String,
bvref::Union{Nothing, JuMP.AbstractVariableRef} = nothing
)
is_gdp_model(model) || error("Can only add logical variables to `GDPModel`s.")
# add the logical variable
data = LogicalVariableData(_get_variable(v), name)
Expand All @@ -117,7 +127,9 @@ function JuMP.add_variable(
# add the associated binary variables
extracted_var = _get_variable(v)
if isnothing(extracted_var.logical_complement)
bvref = _make_binary_variable(model, v, name)
if isnothing(bvref)
bvref = _make_binary_variable(model, v, name)
end
_add_logical_info(bvref, v)
jump_expr = bvref
else
Expand Down
137 changes: 137 additions & 0 deletions test/constraints/disjunction_set.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using HiGHS

# Two disjuncts on x: [x <= 2] ∨ [x >= 8]
const _RAISE_SETS = [[MOI.LessThan(2.0)], [MOI.GreaterThan(8.0)]]

_raised_disjunctions(model) = DisjunctionRef.(model,
keys(DP._disjunctions(model)))

function test_raise_two_binaries()
model = GDPModel()
@variable(model, 0 <= x <= 10)
@variable(model, y[1:2], Bin)
@constraint(model, d, [1, y[1], y[2], x, x] in DisjunctionSet(_RAISE_SETS))
num_vars = num_variables(model)
reformulate_model(model, BigM())
@test isempty(all_constraints(model, Vector{AffExpr}, DisjunctionSet))
dref = only(_raised_disjunctions(model))
@test name(dref) == "d"
indicators = constraint_object(dref).indicators
@test binary_variable.(indicators) == y
@test name.(indicators) == name.(y)
@test num_variables(model) == num_vars
sets = [constraint_object(only(DP._indicator_to_constraints(model)[lv])).set
for lv in indicators]
@test sets == [MOI.LessThan(2.0), MOI.GreaterThan(8.0)]
end

function test_raise_complement()
model = GDPModel()
@variable(model, 0 <= x <= 10)
@variable(model, y, Bin)
@constraint(model, [1, y, 1 - y, x, x] in DisjunctionSet(_RAISE_SETS))
num_vars = num_variables(model)
reformulate_model(model, Hull())
indicators = constraint_object(only(_raised_disjunctions(model))).indicators
@test binary_variable(indicators[1]) == y
@test has_logical_complement(indicators[2])
@test binary_variable(indicators[2]) == 1 - y
@test length(DP._logical_variables(model)) == 2
end

function test_raise_nested()
model = GDPModel()
@variable(model, 0 <= x <= 10)
@variable(model, y[1:2], Bin)
@variable(model, w[1:2], Bin)
inner_sets = [[MOI.LessThan(1.0)], [MOI.GreaterThan(1.5)]]
# child listed first: raising must not depend on order
@constraint(model, [y[1], w[1], w[2], x, x] in DisjunctionSet(inner_sets))
@constraint(model, [1, y[1], y[2], x, x] in DisjunctionSet(_RAISE_SETS))
reformulate_model(model, BigM())
drefs = _raised_disjunctions(model)
@test length(drefs) == 2
nested = filter(dref -> constraint_object(dref).nested, drefs)
@test binary_variable.(constraint_object(only(nested)).indicators) == w
parent = DP._indicator_to_binary(model)
parent_lvref = only(lv for (lv, b) in parent if b == y[1])
@test any(cref -> constraint_object(cref) isa Disjunction,
DP._indicator_to_constraints(model)[parent_lvref])
end

function test_raise_quadratic()
# one quadratic row makes every entry, indicators included, a QuadExpr
model = GDPModel()
@variable(model, 0 <= x <= 10)
@variable(model, y[1:2], Bin)
@constraint(model, [1, y[1], y[2], x^2, x] in DisjunctionSet(_RAISE_SETS))
reformulate_model(model, BigM(100, false))
indicators = constraint_object(only(_raised_disjunctions(model))).indicators
@test binary_variable.(indicators) == y
end

function test_raise_errors()
model = GDPModel()
@variable(model, x)
@variable(model, y[1:3], Bin)
@constraint(model, [1, 2y[1], y[2], x, x] in DisjunctionSet(_RAISE_SETS))
@test_throws ErrorException reformulate_model(model, BigM())

model = GDPModel()
@variable(model, x)
@variable(model, y[1:2], Bin)
sets = [[MOI.LessThan(1.0)], [MOI.LessThan(2.0)], [MOI.LessThan(3.0)]]
@constraint(model, [1, y[1], 1 - y[1], y[2], x, x, x] in
DisjunctionSet(sets))
@test_throws ErrorException reformulate_model(model, BigM())

model = GDPModel()
@variable(model, x)
@variable(model, y[1:3], Bin)
@constraint(model, [y[3], y[1], y[2], x, x] in DisjunctionSet(_RAISE_SETS))
@test_throws ErrorException reformulate_model(model, BigM())

model = GDPModel()
@variable(model, x)
@variable(model, y[1:2])
@constraint(model, [1, y[1], y[2], x, x] in DisjunctionSet(_RAISE_SETS))
@test_throws ErrorException reformulate_model(model, BigM())

for indicator in (y -> y^2, sin)
model = GDPModel()
@variable(model, x)
@variable(model, y[1:2], Bin)
@constraint(model, [1, indicator(y[1]), y[2], x, x] in
DisjunctionSet(_RAISE_SETS))
@test_throws ErrorException reformulate_model(model, BigM())
end
end

# The receiver path: an MOI model holding `DisjunctionSet` is copied into a
# `GDPModel`, raised, and solved with existing reformulations.
function test_raise_from_moi_copy()
for method in (BigM(), Hull(), MBM(HiGHS.Optimizer), Indicator())
method isa Indicator && continue # HiGHS has no indicator support
source = Model()
@variable(source, 0 <= x <= 9)
@variable(source, y, Bin)
@objective(source, Max, x)
@constraint(source, [1, y, 1 - y, x, x] in DisjunctionSet(_RAISE_SETS))
model = GDPModel()
MOI.copy_to(backend(model), backend(source))
set_optimizer(model, HiGHS.Optimizer)
set_silent(model)
optimize!(model, gdp_method = method)
@test termination_status(model) == MOI.OPTIMAL
@test objective_value(model) ≈ 9 atol = 1e-6
end
end

@testset "DisjunctionSet raising" begin
test_raise_two_binaries()
test_raise_complement()
test_raise_nested()
test_raise_quadratic()
test_raise_errors()
test_raise_from_moi_copy()
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ include("constraints/hull.jl")
include("constraints/fallback.jl")
include("constraints/disjunction.jl")
include("constraints/basicsteps.jl")
include("constraints/disjunction_set.jl")
include("print.jl")
include("solve.jl")
include("extensions/InfiniteDisjunctiveProgramming.jl")
Expand Down
Loading