Skip to content

Commit

Permalink
Add support for constraint attributes (#122)
Browse files Browse the repository at this point in the history
* Add support for constraint attributes

* Fix format
  • Loading branch information
blegat authored Mar 30, 2024
1 parent 35ece1a commit dff7ec9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
26 changes: 3 additions & 23 deletions src/QCQP/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down
15 changes: 9 additions & 6 deletions test/qcqp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -349,10 +344,18 @@ 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)
Expand Down

0 comments on commit dff7ec9

Please sign in to comment.