Skip to content

Commit

Permalink
Add support for objective to MOI wrapper (#530)
Browse files Browse the repository at this point in the history
* Add support for objective to MOI wrapper

* Fix format

* Fixes

* Add comment

* Update MOI_wrapper.jl

* Update src/MOI_wrapper.jl

* Fix ConstraintDual and ConstraintPrimal

---------

Co-authored-by: Oscar Dowson <odow@users.noreply.github.com>
  • Loading branch information
blegat and odow authored Jan 10, 2024
1 parent 7cf75e0 commit 7086a50
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,24 @@ function MOI.is_valid(model::Optimizer, i::MOI.Index)
return MOI.is_valid(model.context.model, i)
end

function MOI.supports(
::Optimizer,
::MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction},
)
return true
end

function MOI.set(
model::Optimizer,
::MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction},
func::MOI.ScalarNonlinearFunction,
)
cfp = conic_form!(model.context, _expr(model, func))
obj = scalar_fn(cfp)
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
return
end

MOI.optimize!(model::Optimizer) = MOI.optimize!(model.context.model)

function MOI.supports(
Expand Down Expand Up @@ -291,6 +309,21 @@ function MOI.get(
return MOI.get(model.context.model, attr, ci)
end

# See `constraints/constraints.jl`
# `LessThan` constraints are reformulated as `rhs - lhs` unlike MOI while
# `GreaterThan` constraints are reformulated as `lhs - rhs` like in MOI
_flip_dual(x, ::Type{S}) where {S<:MOI.LessThan} = -x
_flip_dual(x, ::Type{S}) where {S<:MOI.AbstractScalarSet} = x

function MOI.get(
model::Optimizer,
attr::Union{MOI.ConstraintDual,MOI.ConstraintPrimal},
ci::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,S},
) where {S<:MOI.AbstractScalarSet}
ret = MOI.get(model.context.model, attr, model.constraint_map[ci.value])
return _flip_dual(ret[], S)
end

function MOI.get(model::Optimizer, I::Type{<:MOI.Index}, name::String)
return MOI.get(model.context.model, I, name)
end
5 changes: 4 additions & 1 deletion test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ function test_runtests()
MOI.ObjectiveBound,
],
)
MOI.Test.runtests(optimizer, config)
MOI.Test.runtests(optimizer, config, exclude = [
# HS071 is not convex
"hs071",
])
return
end

Expand Down

0 comments on commit 7086a50

Please sign in to comment.