From 7a02b21ee05c248645beb0bacf1e67efd40f9f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 28 Mar 2024 11:54:21 +0100 Subject: [PATCH 1/2] Add support for constraint attributes --- src/QCQP/MOI_wrapper.jl | 26 +++----------------------- test/qcqp.jl | 11 +++++------ 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/src/QCQP/MOI_wrapper.jl b/src/QCQP/MOI_wrapper.jl index 6597fa9..463c93a 100644 --- a/src/QCQP/MOI_wrapper.jl +++ b/src/QCQP/MOI_wrapper.jl @@ -79,7 +79,7 @@ function MOI.set( ci::MOI.ConstraintIndex{<:PolyJuMP.ScalarPolynomialFunction{T},S}, value, ) where {T,S<:MOI.AbstractScalarSet} - return MOI.get(model.constraints[S][2], attr, model.index_map[ci], value) + return MOI.set(model.constraints[S][2], attr, ci, value) end function MOI.get( @@ -425,7 +425,7 @@ function _add_constraints( dest, src, index_map, - cis_src::Vector{MOI.ConstraintIndex{F,S}}, + cis_src::AbstractVector{MOI.ConstraintIndex{F,S}}, index_to_var, d, div, @@ -438,27 +438,7 @@ function _add_constraints( dest_ci = MOI.Utilities.normalize_and_add_constraint(dest, quad, set) index_map[ci] = dest_ci end - # `Utilities.pass_attributes` needs `index_map` to be an `IndexMap` :( - #MOI.Utilities.pass_attributes(dest, src, index_map, cis_src) - # `ListOfConstraintAttributesSet` not defined for `VectorOfConstraints` - # for attr in MOI.get(src, MOI.ListOfConstraintAttributesSet{F,S}()) - # if !MOI.supports(dest, attr) - # if attr == MOI.Name() - # continue # Skipping names is okay. - # end - # end - # for ci in cis_src - # value = MOI.get(src, attr, ci) - # if value !== nothing - # MOI.set( - # dest, - # attr, - # index_map[ci], - # MOI.Utilities.map_indices(index_map, attr, value), - # ) - # end - # end - # end + MOI.Utilities.pass_attributes(dest, src, index_map, cis_src) return end diff --git a/test/qcqp.jl b/test/qcqp.jl index 7bb1183..dbad759 100644 --- a/test/qcqp.jl +++ b/test/qcqp.jl @@ -315,7 +315,7 @@ function test_variable_primal(x, y, T) end # We test names as it's supported by `MOI.Utilities.Model` -function test_name(x, y, T) +function test_name(_, _, T) model = JuMP.GenericModel{T}() JuMP.@variable(model, 1 <= a <= 3) JuMP.@variable(model, 1 <= b <= 3) @@ -333,11 +333,6 @@ function test_name(x, y, T) attr = MOI.ConstraintName() @test MOI.get(qcqp, attr, idxmap[JuMP.index(aff)]) == "aff" @test MOI.get(qcqp, attr, idxmap[JuMP.index(con_ref)]) == "con_ref" - inner = qcqp.model.model - F = MOI.ScalarQuadraticFunction{T} - S = MOI.GreaterThan{T} - ci = first(MOI.get(inner, MOI.ListOfConstraintIndices{F,S}())) - @test_broken MOI.get(inner, attr, ci) == "con_ref" end function test_start(x, y, T) @@ -349,10 +344,14 @@ function test_start(x, y, T) MOI.set(model, MOI.VariablePrimalStart(), b, 3one(T)) p = PolyJuMP.ScalarPolynomialFunction(one(T) * x^3 - x * y^2, [a, b]) ci = MOI.add_constraint(model, p, MOI.LessThan(zero(T))) + MOI.set(model, MOI.ConstraintPrimalStart(), ci, 5one(T)) + @test MOI.get(model, MOI.ConstraintPrimalStart(), ci) == 5 @test MOI.is_valid(model, ci) MOI.Utilities.final_touch(model, nothing) vis = MOI.get(inner, MOI.ListOfVariableIndices()) @test sort(MOI.get(inner, MOI.VariablePrimalStart(), vis)) == T[2, 3, 4, 9] + @test MOI.get(model, MOI.ConstraintPrimalStart(), ci) == 5 + @test MOI.get(model.model, MOI.ConstraintPrimalStart(), model.index_map[ci]) == 5 end function test_inner_bridge(x, y, T) From 3428c2aaf7ad9312b8bd751966bb1faf0e045379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 28 Mar 2024 12:03:04 +0100 Subject: [PATCH 2/2] Fix format --- test/qcqp.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/qcqp.jl b/test/qcqp.jl index dbad759..3a517e7 100644 --- a/test/qcqp.jl +++ b/test/qcqp.jl @@ -351,7 +351,11 @@ function test_start(x, y, T) vis = MOI.get(inner, MOI.ListOfVariableIndices()) @test sort(MOI.get(inner, MOI.VariablePrimalStart(), vis)) == T[2, 3, 4, 9] @test MOI.get(model, MOI.ConstraintPrimalStart(), ci) == 5 - @test MOI.get(model.model, MOI.ConstraintPrimalStart(), model.index_map[ci]) == 5 + @test MOI.get( + model.model, + MOI.ConstraintPrimalStart(), + model.index_map[ci], + ) == 5 end function test_inner_bridge(x, y, T)