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

Fix indicator #137

Merged
merged 3 commits into from
Oct 25, 2019
Merged
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
4 changes: 3 additions & 1 deletion src/MOI_wrapper/indicator_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ function MOI.add_constraint(o::Optimizer, func::MOI.VectorAffineFunction{T}, set
y = VarRef(first_index_terms[1].variable_index.value)
x = [VarRef(vi.variable_index.value) for vi in scalar_index_terms]
a = [vi.coefficient for vi in scalar_index_terms]
b = func.constants[2]
# a^T x + b <= c ===> a^T <= c - b

cr = add_indicator_constraint(o.mscip, y, x, a, MOI.constant(set.set))
cr = add_indicator_constraint(o.mscip, y, x, a, MOI.constant(set.set) - b)
ci = CI{MOI.VectorAffineFunction{T}, MOI.IndicatorSet{MOI.ACTIVATE_ON_ONE, LT}}(cr.val)
register!(o, ci)
register!(o, cons(o, ci), cr)
Expand Down
1 change: 1 addition & 0 deletions test/MOI_wrapper_bridged.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ end
@testset "MOI Integer Linear" begin
excluded = String[]
MOIT.intlineartest(BRIDGED, CONFIG, excluded)
MOIT.indicator3_test(BRIDGED, CONFIG)
end

@testset "MOI Integer Conic" begin
Expand Down
81 changes: 81 additions & 0 deletions test/MOI_wrapper_direct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,85 @@ end
MOIT.ncqcp2test(OPTIMIZER, CONFIG)
end

function indicator4_test(model::MOI.ModelLike, config)
atol = config.atol
rtol = config.rtol
# equivalent to indicator1_test with left-hand-side partially in LHS constant
# linear problem with indicator constraint and
# max 2x1 + 3x2
# s.t. x1 + x2 <= 10
# z1 ==> x2 - 1 <= 7
# z2 ==> x2 + x1/5 + 1 <= 10
# z1 + z2 >= 1


MOI.empty!(model)
@test MOI.is_empty(model)

@test MOI.supports(model, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}())
@test MOI.supports(model, MOI.ObjectiveSense())
@test MOI.supports_constraint(model, MOI.SingleVariable, MOI.ZeroOne)
@test MOI.supports_constraint(model, MOI.SingleVariable, MOI.Interval{Float64})
@test MOI.supports_constraint(model, MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64})
@test MOI.supports_constraint(model, MOI.VectorAffineFunction{Float64}, MOI.IndicatorSet{MOI.ACTIVATE_ON_ONE, MOI.LessThan{Float64}})
x1 = MOI.add_variable(model)
x2 = MOI.add_variable(model)
z1 = MOI.add_variable(model)
z2 = MOI.add_variable(model)
MOI.add_constraint(model, z1, MOI.ZeroOne())
MOI.add_constraint(model, z2, MOI.ZeroOne())
f1 = MOI.VectorAffineFunction(
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, z1)),
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x2)),
],
[0.0, -1.0]
)
iset1 = MOI.IndicatorSet{MOI.ACTIVATE_ON_ONE}(MOI.LessThan(7.0))
MOI.add_constraint(model, f1, iset1)

f2 = MOI.VectorAffineFunction(
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, z2)),
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(0.2, x1)),
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x2)),
],
[0.0, 1.0],
)
iset2 = MOI.IndicatorSet{MOI.ACTIVATE_ON_ONE}(MOI.LessThan(10.0))

MOI.add_constraint(model, f2, iset2)

# Additional regular constraint.
MOI.add_constraint(model,
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x1), MOI.ScalarAffineTerm(1.0, x2)], 0.0),
MOI.LessThan(10.0),
)

# Disjunction z1 ⋁ z2
MOI.add_constraint(model,
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, z1), MOI.ScalarAffineTerm(1.0, z2)], 0.0),
MOI.GreaterThan(1.0),
)

MOI.set(model, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2.0, 3.0], [x1, x2]), 0.0)
)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)

if config.solve
@test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMIZE_NOT_CALLED

MOI.optimize!(model)

@test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMAL
@test MOI.get(model, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT
@test MOI.get(model, MOI.ObjectiveValue()) ≈ 28.75 atol=atol rtol=rtol
@test MOI.get(model, MOI.VariablePrimal(), x1) ≈ 1.25 atol=atol rtol=rtol
@test MOI.get(model, MOI.VariablePrimal(), x2) ≈ 8.75 atol=atol rtol=rtol
@test MOI.get(model, MOI.VariablePrimal(), z1) ≈ 0.0 atol=atol rtol=rtol
@test MOI.get(model, MOI.VariablePrimal(), z2) ≈ 1.0 atol=atol rtol=rtol
end
end

@testset "MOI Integer Linear" begin
# MOIT.intlineartest(OPTIMIZER, CONFIG)

Expand All @@ -60,5 +139,7 @@ end
MOIT.int3test(OPTIMIZER, CONFIG)
MOIT.indicator1_test(OPTIMIZER, CONFIG)
MOIT.indicator2_test(OPTIMIZER, CONFIG)
# replace with MOIT when MathOptInterface.jl#929 merged
indicator4_test(OPTIMIZER, CONFIG)
# MOIT.indicator3_test(OPTIMIZER, CONFIG) # no support for ACTIVATE_ON_ZERO
end