Skip to content

Commit

Permalink
Merge pull request #63 from oxfordcontrol/bl/single_variable_obj
Browse files Browse the repository at this point in the history
Remove support for SingleVariable objective
  • Loading branch information
blegat authored Sep 23, 2019
2 parents d01f22e + a427571 commit d36d3e0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
BinaryProvider = "≥ 0.3.0"
MathOptInterface = "~0.9.1"
MathOptInterface = "~0.9.3"
MathProgBase = "~0.5.0, ~0.6, ~0.7"
julia = "1"

Expand Down
21 changes: 3 additions & 18 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,7 @@ function processobjective(src::MOI.ModelLike, idxmap)
q = zeros(n)
if sense != MOI.FEASIBILITY_SENSE
function_type = MOI.get(src, MOI.ObjectiveFunctionType())
if function_type == MOI.SingleVariable
fsingle = MOI.get(src, MOI.ObjectiveFunction{MOI.SingleVariable}())
P = spzeros(n, n)
q[idxmap[fsingle.variable].value] = 1
c = 0.
elseif function_type == MOI.ScalarAffineFunction{Float64}
if function_type == MOI.ScalarAffineFunction{Float64}
faffine = MOI.get(src, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}())
P = spzeros(n, n)
processlinearterms!(q, faffine.terms, idxmap)
Expand Down Expand Up @@ -476,25 +471,15 @@ end
MOI.get(optimizer::Optimizer, ::MOI.RawSolver) = optimizer.inner
MOI.get(optimizer::Optimizer, ::MOI.ResultCount) = 1

MOI.supports(::Optimizer, ::MOI.ObjectiveFunction{MOI.SingleVariable}) = true
MOI.supports(::Optimizer, ::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}) = true
MOI.supports(::Optimizer, ::MOI.ObjectiveFunction{Quadratic}) = true
MOI.supports(::Optimizer, ::MOI.ObjectiveSense) = true

function MOI.set(optimizer::Optimizer, a::MOI.ObjectiveFunction{MOI.SingleVariable}, obj::MOI.SingleVariable)
MOI.is_empty(optimizer) && throw(MOI.SetAttributeNotAllowed(a))
optimizer.modcache.P[:] = 0
optimizer.modcache.q[:] = 0
optimizer.modcache.q[obj.variable.value] = 1
optimizer.objconstant = 0
nothing
end

function MOI.set(optimizer::Optimizer, a::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}, obj::MOI.ScalarAffineFunction{Float64})
MOI.is_empty(optimizer) && throw(MOI.SetAttributeNotAllowed(a))
optimizer.modcache.P[:] = 0
processlinearterms!(optimizer.modcache.q, obj.terms)
optimizer.objconstant = obj.constant
optimizer.objconstant = MOI.constant(obj)
nothing
end

Expand All @@ -510,7 +495,7 @@ function MOI.set(optimizer::Optimizer, a::MOI.ObjectiveFunction{Quadratic}, obj:
cache.P[row, col] += coeff
end
processlinearterms!(optimizer.modcache.q, obj.affine_terms)
optimizer.objconstant = obj.constant
optimizer.objconstant = MOI.constant(obj)
nothing
end

Expand Down
8 changes: 5 additions & 3 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const Affine = MOI.ScalarAffineFunction{Float64}
Pmod = sparse(1.0I, n, n)
OSQP.setup!(model4; P = Pmod, q = modcache.q.data, A = Amod, l = l, u = u, verbose = false, eps_abs = 1e-8, eps_rel = 1e-16)
Pmod_setup_results = OSQP.solve!(model4)
@test Pmod_update_results.x Pmod_setup_results.x atol = 1e-8
@test Pmod_update_results.x Pmod_setup_results.x atol = 1e-7

# Modifying the sparsity pattern is not allowed
nzinds = map(CartesianIndex, zip(rows, cols))
Expand Down Expand Up @@ -303,7 +303,7 @@ term(c, x::MOI.VariableIndex, y::MOI.VariableIndex) = MOI.ScalarQuadraticTerm(c,
# flip the feasible set around from what it was originally and minimize +x
test_optimizer_modification(model, optimizer, idxmap, defaultoptimizer(), config) do m
# objective
newobjf = MOI.SingleVariable(mapfrommodel(m, x))
newobjf = convert(MOI.ScalarAffineFunction{Float64}, MOI.SingleVariable(mapfrommodel(m, x)))
F = typeof(newobjf)
MOI.set(m, MOI.ObjectiveFunction{F}(), newobjf)

Expand Down Expand Up @@ -530,8 +530,10 @@ end
MOI.empty!(model)
x = MOI.add_variable(model)
c = MOI.add_constraint(model, 1.0MOI.SingleVariable(x), MOI.GreaterThan(2.0))
fx = MOI.SingleVariable(x)
obj = convert(MOI.ScalarAffineFunction{Float64}, fx)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{MOI.SingleVariable}(), MOI.SingleVariable(x))
MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)

MOI.copy_to(optimizer, model)
inner = MOI.get(optimizer, MOI.RawSolver())
Expand Down

0 comments on commit d36d3e0

Please sign in to comment.