From 53c7aefd36edb9156a4209044c6aa9893b38ec50 Mon Sep 17 00:00:00 2001 From: Gabriel Vidigal Date: Mon, 22 Jul 2024 19:07:26 -0300 Subject: [PATCH 1/2] Update constraint indexes when adding constraints without parameters --- src/MOI_wrapper.jl | 12 ++++++++++++ test/moi_tests.jl | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index d2fca5f..fdc50e2 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -222,6 +222,18 @@ function _add_to_constraint_map!(model::Optimizer, ci) return end +function _add_to_constraint_map!(model::Optimizer, ci::MOI.ConstraintIndex{F,S}) where {F<:MOI.ScalarAffineFunction, S} + model.last_affine_added += 1 + model.constraint_outer_to_inner[ci] = ci + return +end + +function _add_to_constraint_map!(model::Optimizer, ci::MOI.ConstraintIndex{F,S}) where {F<:MOI.ScalarQuadraticFunction, S} + model.last_quad_add_added += 1 + model.constraint_outer_to_inner[ci] = ci + return +end + function MOI.supports( model::Optimizer, attr::MOI.AbstractVariableAttribute, diff --git a/test/moi_tests.jl b/test/moi_tests.jl index ae537c2..c77a851 100644 --- a/test/moi_tests.jl +++ b/test/moi_tests.jl @@ -1647,3 +1647,43 @@ function test_duals_not_available() ) return end + +function test_duals_without_parameters() + optimizer = POI.Optimizer(GLPK.Optimizer()) + MOI.set(optimizer, MOI.Silent(), true) + x = MOI.add_variables(optimizer, 3) + y, cy = MOI.add_constrained_variable(optimizer, MOI.Parameter(0.0)) + z, cz = MOI.add_constrained_variable(optimizer, MOI.Parameter(0.0)) + cons1 = MOI.ScalarAffineFunction( + MOI.ScalarAffineTerm.([1.0, -1.0], [x[1], y]), + 0.0, + ) + c1 = MOI.add_constraint(optimizer, cons1, MOI.LessThan(0.0)) + cons2 = MOI.ScalarAffineFunction( + MOI.ScalarAffineTerm.([1.0], [x[2]]), + 0.0, + ) + c2 = MOI.add_constraint(optimizer, cons2, MOI.LessThan(1.0)) + cons3 = MOI.ScalarAffineFunction( + MOI.ScalarAffineTerm.([1.0, -1.0], [x[3], z]), + 0.0, + ) + c3 = MOI.add_constraint(optimizer, cons3, MOI.LessThan(0.0)) + obj_func = MOI.ScalarAffineFunction( + MOI.ScalarAffineTerm.([1.0, 2.0, 3.0], [x[1], x[2], x[3]]), + 0.0, + ) + MOI.set( + optimizer, + MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(), + obj_func, + ) + MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE) + MOI.set(optimizer, MOI.ConstraintSet(), cy, MOI.Parameter(1.0)) + MOI.set(optimizer, MOI.ConstraintSet(), cz, MOI.Parameter(1.0)) + MOI.optimize!(optimizer) + @test ≈(MOI.get(optimizer, MOI.ConstraintDual(), c1), -1.0, atol = ATOL) + @test ≈(MOI.get(optimizer, MOI.ConstraintDual(), c2), -2.0, atol = ATOL) + @test ≈(MOI.get(optimizer, MOI.ConstraintDual(), c3), -3.0, atol = ATOL) + return +end From cff4eca5f88baa9806394b199a2aa4b08ed1c765 Mon Sep 17 00:00:00 2001 From: Gabriel Vidigal Date: Mon, 22 Jul 2024 19:16:53 -0300 Subject: [PATCH 2/2] Format --- src/MOI_wrapper.jl | 10 ++++++++-- test/moi_tests.jl | 5 +---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index fdc50e2..aa85572 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -222,13 +222,19 @@ function _add_to_constraint_map!(model::Optimizer, ci) return end -function _add_to_constraint_map!(model::Optimizer, ci::MOI.ConstraintIndex{F,S}) where {F<:MOI.ScalarAffineFunction, S} +function _add_to_constraint_map!( + model::Optimizer, + ci::MOI.ConstraintIndex{F,S}, +) where {F<:MOI.ScalarAffineFunction,S} model.last_affine_added += 1 model.constraint_outer_to_inner[ci] = ci return end -function _add_to_constraint_map!(model::Optimizer, ci::MOI.ConstraintIndex{F,S}) where {F<:MOI.ScalarQuadraticFunction, S} +function _add_to_constraint_map!( + model::Optimizer, + ci::MOI.ConstraintIndex{F,S}, +) where {F<:MOI.ScalarQuadraticFunction,S} model.last_quad_add_added += 1 model.constraint_outer_to_inner[ci] = ci return diff --git a/test/moi_tests.jl b/test/moi_tests.jl index c77a851..9682ddd 100644 --- a/test/moi_tests.jl +++ b/test/moi_tests.jl @@ -1659,10 +1659,7 @@ function test_duals_without_parameters() 0.0, ) c1 = MOI.add_constraint(optimizer, cons1, MOI.LessThan(0.0)) - cons2 = MOI.ScalarAffineFunction( - MOI.ScalarAffineTerm.([1.0], [x[2]]), - 0.0, - ) + cons2 = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x[2])], 0.0) c2 = MOI.add_constraint(optimizer, cons2, MOI.LessThan(1.0)) cons3 = MOI.ScalarAffineFunction( MOI.ScalarAffineTerm.([1.0, -1.0], [x[3], z]),