Skip to content

Commit

Permalink
Add support for SingleVariable objective
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 29, 2018
1 parent 70bb13d commit d39043e
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/load.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@ function MOIU.allocate!(optimizer::SOItoMOIBridge, ::MOI.ObjectiveSense, sense::
# To be sure that it is done before load!(optimizer, ::ObjectiveFunction, ...), we do it in allocate!
optimizer.objsign = sense == MOI.MinSense ? -1 : 1
end
MOIU.canallocate(::SOItoMOIBridge{T}, ::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}) where T = true
function MOIU.allocate!(::SOItoMOIBridge, ::MOI.ObjectiveFunction, ::MOI.ScalarAffineFunction) end
MOIU.canallocate(::SOItoMOIBridge{T}, ::MOI.ObjectiveFunction{<:Union{MOI.SingleVariable, MOI.ScalarAffineFunction{T}}}) where T = true
function MOIU.allocate!(::SOItoMOIBridge, ::MOI.ObjectiveFunction, ::Union{MOI.SingleVariable, MOI.ScalarAffineFunction}) end

MOIU.canload(m::SOItoMOIBridge, ::MOI.ObjectiveSense) = true
function MOIU.load!(::SOItoMOIBridge, ::MOI.ObjectiveSense, ::MOI.OptimizationSense) end
MOIU.canload(m::SOItoMOIBridge{T}, ::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}) where T = true
MOIU.canload(m::SOItoMOIBridge{T}, ::MOI.ObjectiveFunction{<:Union{MOI.SingleVariable, MOI.ScalarAffineFunction{T}}}) where T = true
# Loads objective coefficient α * vi
function load_objective_term!(optimizer::SOItoMOIBridge, α, vi::MOI.VariableIndex)
for (blk, i, j, coef, shift) in varmap(optimizer, vi)
if !iszero(blk)
# in SDP format, it is max and in MPB Conic format it is min
setobjectivecoefficient!(optimizer.sdoptimizer, optimizer.objsign * coef * α, blk, i, j)
end
optimizer.objshift += α * shift
end
end
function MOIU.load!(optimizer::SOItoMOIBridge, ::MOI.ObjectiveFunction, f::MOI.ScalarAffineFunction)
obj = MOIU.canonical(f)
optimizer.objconstant = f.constant
for t in obj.terms
if !iszero(t.coefficient)
for (blk, i, j, coef, shift) in varmap(optimizer, t.variable_index)
if !iszero(blk)
# in SDP format, it is max and in MPB Conic format it is min
setobjectivecoefficient!(optimizer.sdoptimizer, optimizer.objsign * coef * t.coefficient, blk, i, j)
end
optimizer.objshift += t.coefficient * shift
end
load_objective_term!(optimizer, t.coefficient, t.variable_index)
end
end
end
function MOIU.load!(optimizer::SOItoMOIBridge{T}, ::MOI.ObjectiveFunction, f::MOI.SingleVariable) where T
load_objective_term!(optimizer, one(T), f.variable)
end

function MOIU.allocatevariables!(optimizer::SOItoMOIBridge{T}, nvars) where T
optimizer.free = IntSet(1:nvars)
Expand Down

0 comments on commit d39043e

Please sign in to comment.