Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for starting values #111

Merged
merged 2 commits into from
Sep 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions src/MOIWrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MOIU = MOI.Utilities
const SF = Union{MOI.SingleVariable, MOI.ScalarAffineFunction{Float64}, MOI.VectorOfVariables, MOI.VectorAffineFunction{Float64}}
const SS = Union{MOI.EqualTo{Float64}, MOI.GreaterThan{Float64}, MOI.LessThan{Float64}, MOI.Zeros, MOI.Nonnegatives, MOI.Nonpositives, MOI.SecondOrderCone, MOI.ExponentialCone, MOI.PositiveSemidefiniteConeTriangle}

struct MOISolution
mutable struct MOISolution
ret_val::Int
primal::Vector{Float64}
dual::Vector{Float64}
Expand Down Expand Up @@ -77,7 +77,8 @@ MOIU.needs_allocate_load(instance::Optimizer) = true

function MOI.supports(::Optimizer,
::Union{MOI.ObjectiveSense,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}})
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
MOI.VariablePrimalStart})
return true
end

Expand Down Expand Up @@ -259,13 +260,49 @@ function MOIU.load_variables(optimizer::Optimizer, nvars::Integer)
b = zeros(m)
c = zeros(nvars)
optimizer.data = ModelData(m, nvars, I, J, V, b, 0., c)
# `optimizer.sol` contains the result of the previous optimization.
# It is used as a warm start if its length is the same, e.g.
# probably because no variable and/or constraint has been added.
if length(optimizer.sol.primal) != nvars
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What could the length be if not nvars? Would isempty also work as a test? Same below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sol contains the solution of the previous optimization. If the user has added new variables, then it is not nvars

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this interface allow adding variables and/or constraints?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can copy a model with a different number of variables/constraints. Normally, they will arrive in the same order.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this if be replaced with resize! then?

optimizer.sol.primal = zeros(nvars)
end
@assert length(optimizer.sol.dual) == length(optimizer.sol.slack)
if length(optimizer.sol.dual) != m
optimizer.sol.dual = zeros(m)
optimizer.sol.slack = zeros(m)
end
end

function MOIU.allocate(::Optimizer, ::MOI.VariablePrimalStart,
::MOI.VariableIndex, ::Float64)
end
function MOIU.allocate(::Optimizer, ::MOI.ConstraintPrimalStart,
::MOI.ConstraintIndex, ::Float64)
end
function MOIU.allocate(::Optimizer, ::MOI.ConstraintDualStart,
::MOI.ConstraintIndex, ::Float64)
end
function MOIU.allocate(optimizer::Optimizer, ::MOI.ObjectiveSense, sense::MOI.OptimizationSense)
optimizer.maxsense = sense == MOI.MaxSense
end
function MOIU.allocate(::Optimizer, ::MOI.ObjectiveFunction, ::MOI.ScalarAffineFunction) end

function MOIU.load(optimizer::Optimizer, ::MOI.VariablePrimalStart,
vi::MOI.VariableIndex, value::Float64)
optimizer.sol.primal[vi.value] = value
end
function MOIU.load(optimizer::Optimizer, ::MOI.ConstraintPrimalStart,
ci::MOI.ConstraintIndex, value)
offset = constroffset(optimizer, ci)
rows = constrrows(optimizer, ci)
optimizer.sol.primal[offset .+ rows] .= value
end
function MOIU.load(optimizer::Optimizer, ::MOI.ConstraintDualStart,
ci::MOI.ConstraintIndex, value)
offset = constroffset(optimizer, ci)
rows = constrrows(optimizer, ci)
optimizer.sol.primal[offset .+ rows] .= value
end
function MOIU.load(::Optimizer, ::MOI.ObjectiveSense, ::MOI.OptimizationSense) end
function MOIU.load(optimizer::Optimizer, ::MOI.ObjectiveFunction, f::MOI.ScalarAffineFunction)
c0 = Vector(sparsevec(variable_index_value.(f.terms), coefficient.(f.terms), optimizer.data.n))
Expand All @@ -282,7 +319,10 @@ function MOI.optimize!(optimizer::Optimizer)
objconstant = optimizer.data.objconstant
c = optimizer.data.c
optimizer.data = nothing # Allows GC to free optimizer.data before A is loaded to SCS
sol = SCS_solve(SCS.Indirect, m, n, A, b, c, cone.f, cone.l, cone.qa, cone.sa, cone.ep, cone.ed, cone.p)
sol = SCS_solve(SCS.Indirect, m, n, A, b, c,
cone.f, cone.l, cone.qa, cone.sa, cone.ep, cone.ed, cone.p,
optimizer.sol.primal, optimizer.sol.dual,
optimizer.sol.slack)
ret_val = sol.ret_val
primal = sol.x
dual = sol.y
Expand Down