Skip to content

Commit

Permalink
Fix MOI.get for quadratic constraints (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Jun 25, 2024
1 parent cde9813 commit c05544d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
38 changes: 10 additions & 28 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,18 +452,18 @@ end
function MOI.set(
model::Optimizer,
::MOI.ConstraintFunction,
c::MOI.ConstraintIndex{F,S},
c::MOI.ConstraintIndex{F},
f::F,
) where {F,S}
) where {F}
MOI.set(model.optimizer, MOI.ConstraintFunction(), c, f)
return
end

function MOI.get(
model::Optimizer,
attr::MOI.ConstraintFunction,
ci::MOI.ConstraintIndex{F,S},
) where {F,S}
ci::MOI.ConstraintIndex,
)
if haskey(model.quadratic_outer_to_inner, ci)
inner_ci = model.quadratic_outer_to_inner[ci]
return _original_function(model.quadratic_constraint_cache[inner_ci])
Expand Down Expand Up @@ -501,8 +501,8 @@ end
function MOI.get(
model::Optimizer,
attr::MOI.ConstraintSet,
ci::MOI.ConstraintIndex{F,S},
) where {F,S}
ci::MOI.ConstraintIndex,
)
if haskey(model.quadratic_outer_to_inner, ci)
inner_ci = model.quadratic_outer_to_inner[ci]
return model.quadratic_constraint_cache_set[inner_ci]
Expand Down Expand Up @@ -1084,29 +1084,11 @@ end

function MOI.get(
model::Optimizer,
attr::T,
attr::MOI.AbstractConstraintAttribute,
c::MOI.ConstraintIndex,
) where {
T<:Union{MOI.ConstraintPrimal,MOI.ConstraintDual,MOI.ConstraintBasisStatus},
}
return MOI.get(model.optimizer, attr, c)
end

function MOI.get(
model::Optimizer,
attr::AT,
c::MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},S},
) where {
AT<:Union{
MOI.ConstraintPrimal,
MOI.ConstraintDual,
MOI.ConstraintBasisStatus,
},
T,
S<:MOI.AbstractScalarSet,
}
moi_ci = get(model.affine_outer_to_inner, c, c)
return MOI.get(model.optimizer, attr, moi_ci)
)
optimizer_ci = get(model.constraint_outer_to_inner, c, c)
return MOI.get(model.optimizer, attr, optimizer_ci)
end

#
Expand Down
10 changes: 10 additions & 0 deletions test/jump_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1079,3 +1079,13 @@ function test_abstract_optimizer_attributes()
@test get_attribute(model, "tm_lim") 60 * 1000
return
end

function test_get_quadratic_constraint()
model = Model(() -> POI.Optimizer(GLPK.Optimizer()))
@variable(model, x)
@variable(model, p in Parameter(2.0))
@constraint(model, c, p * x <= 10)
optimize!(model)
@test value(c) 2.0 * value(x)
return
end

0 comments on commit c05544d

Please sign in to comment.