From 7a160dc2daf83344f1d7cf16ad3f9423aa565be1 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 26 Jun 2018 01:01:11 +1200 Subject: [PATCH] Update LQOI to reflect refactoring of modifications in MOI (#29) * Update LQOI to reflect refactoring of modifications in MOI * Add multirowchange and get Travis to checkout MOI PR * Update .travis.yml * Bump MOI version --- REQUIRE | 2 +- src/constraints/scalaraffine.jl | 22 +++++++++++----------- src/constraints/singlevariable.jl | 4 ++-- src/constraints/vectoraffine.jl | 12 ++++++++++-- src/objective.jl | 19 ++++++++++++++++--- test/runtests.jl | 15 ++++++--------- 6 files changed, 46 insertions(+), 28 deletions(-) diff --git a/REQUIRE b/REQUIRE index 165ec0f..8a6cefc 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,2 @@ julia 0.6 -MathOptInterface 0.3 0.4 +MathOptInterface 0.4 0.5 diff --git a/src/constraints/scalaraffine.jl b/src/constraints/scalaraffine.jl index e8e0653..b52832e 100644 --- a/src/constraints/scalaraffine.jl +++ b/src/constraints/scalaraffine.jl @@ -163,8 +163,8 @@ end Scalar Coefficient Change of Linear Constraint =# -MOI.canmodifyconstraint(m::LinQuadOptimizer, c::LCI{<: LinSets}, ::Type{MOI.ScalarCoefficientChange{Float64}}) = true -function MOI.modifyconstraint!(m::LinQuadOptimizer, c::LCI{<: LinSets}, chg::MOI.ScalarCoefficientChange{Float64}) +MOI.canmodify(m::LinQuadOptimizer, ::Type{LCI{S}}, ::Type{MOI.ScalarCoefficientChange{Float64}}) where S <: LinSets = true +function MOI.modify!(m::LinQuadOptimizer, c::LCI{S}, chg::MOI.ScalarCoefficientChange{Float64}) where S <: LinSets col = m.variable_mapping[chg.variable] change_matrix_coefficient!(m, m[c], col, chg.new_coefficient) end @@ -173,13 +173,13 @@ end Change RHS of linear constraint without modifying sense =# -MOI.canmodifyconstraint(m::LinQuadOptimizer, c::LCI{S}, ::Type{S}) where S <: Union{LE, GE, EQ} = true -function MOI.modifyconstraint!(m::LinQuadOptimizer, c::LCI{S}, newset::S) where S <: Union{LE, GE, EQ} +MOI.canset(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{LCI{S}}) where S <: Union{LE, GE, EQ} = true +function MOI.set!(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::LCI{S}, newset::S) where S <: Union{LE, GE, EQ} change_rhs_coefficient!(m, m[c], _getrhs(newset)) end -MOI.canmodifyconstraint(m::LinQuadOptimizer, c::LCI{IV}, ::Type{IV}) = true -function MOI.modifyconstraint!(m::LinQuadOptimizer, c::LCI{IV}, set::IV) +MOI.canset(m::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{LCI{IV}}) = true +function MOI.set!(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::LCI{IV}, set::IV) modify_ranged_constraints!(m, [m[c]], [set.lower], [set.upper]) end @@ -205,17 +205,17 @@ end Transform scalar constraint =# -function MOI.cantransformconstraint(m::LinQuadOptimizer, ref::LCI{S}, newset::S) where S +function MOI.cantransform(m::LinQuadOptimizer, ref::LCI{S}, newset::S) where S false end -function MOI.transformconstraint!(m::LinQuadOptimizer, ref::LCI{S}, newset::S) where S - error("Cannot transform constraint of same set. use `modifyconstraint!` instead.") +function MOI.transform!(m::LinQuadOptimizer, ::LCI{S}, newset::S) where S + error("Cannot transform constraint of same set. use `set!` instead.") end -function MOI.cantransformconstraint(m::LinQuadOptimizer, ref::LCI{S1}, newset::S2) where S1 where S2 <: Union{LE, GE, EQ} +function MOI.cantransform(m::LinQuadOptimizer, ref::LCI{S1}, newset::S2) where S1 where S2 <: Union{LE, GE, EQ} true end -function MOI.transformconstraint!(m::LinQuadOptimizer, ref::LCI{S1}, newset::S2) where S1 where S2 <: Union{LE, GE, EQ} +function MOI.transform!(m::LinQuadOptimizer, ref::LCI{S1}, newset::S2) where S1 where S2 <: Union{LE, GE, EQ} dict = constrdict(m, ref) row = dict[ref] change_linear_constraint_sense!(m, [row], [backend_type(m,newset)]) diff --git a/src/constraints/singlevariable.jl b/src/constraints/singlevariable.jl index ba025c2..a861ac5 100644 --- a/src/constraints/singlevariable.jl +++ b/src/constraints/singlevariable.jl @@ -90,8 +90,8 @@ function MOI.get(m::LinQuadOptimizer, ::MOI.ConstraintFunction, c::SVCI{<: LinSe end # modify -MOI.canmodifyconstraint(::LinQuadOptimizer, ::SVCI{S}, ::Type{S}) where S <: LinSets = true -function MOI.modifyconstraint!(m::LinQuadOptimizer, c::SVCI{S}, newset::S) where S <: LinSets +MOI.canset(::LinQuadOptimizer, ::MOI.ConstraintSet, ::Type{SVCI{S}}) where S <: LinSets = true +function MOI.set!(m::LinQuadOptimizer, ::MOI.ConstraintSet, c::SVCI{S}, newset::S) where S <: LinSets setvariablebound!(m, SinVar(m[c]), newset) end diff --git a/src/constraints/vectoraffine.jl b/src/constraints/vectoraffine.jl index 00e5213..2fd609e 100644 --- a/src/constraints/vectoraffine.jl +++ b/src/constraints/vectoraffine.jl @@ -50,8 +50,8 @@ function addlinearconstraint!(m::LinQuadOptimizer, func::VecLin, sense::Cchar) add_linear_constraints!(m, A, fill(sense, length(func.constants)), -func.constants) end -MOI.canmodifyconstraint(m::LinQuadOptimizer, ::VLCI{<: VecLinSets}, ::Type{MOI.VectorConstantChange{Float64}}) = true -function MOI.modifyconstraint!(m::LinQuadOptimizer, ref::VLCI{<: VecLinSets}, chg::MOI.VectorConstantChange{Float64}) +MOI.canmodify(m::LinQuadOptimizer, ::Type{VLCI{S}}, ::Type{MOI.VectorConstantChange{Float64}}) where S <: VecLinSets = true +function MOI.modify!(m::LinQuadOptimizer, ref::VLCI{<: VecLinSets}, chg::MOI.VectorConstantChange{Float64}) @assert length(chg.new_constant) == length(m[ref]) for (r, v) in zip(m[ref], chg.new_constant) change_rhs_coefficient!(m, r, -v) @@ -59,6 +59,14 @@ function MOI.modifyconstraint!(m::LinQuadOptimizer, ref::VLCI{<: VecLinSets}, ch end end +MOI.canmodify(m::LinQuadOptimizer, ::Type{VLCI{S}}, ::Type{MOI.MultirowChange{Float64}}) where S <: VecLinSets = true +function MOI.modify!(m::LinQuadOptimizer, ref::VLCI{<: VecLinSets}, chg::MOI.MultirowChange{Float64}) + col = m.variable_mapping[chg.variable] + for (row, coef) in chg.new_coefficients + change_matrix_coefficient!(m, row, col, coef) + end +end + MOI.candelete(m::LinQuadOptimizer, c::VLCI{<:VecLinSets}) = MOI.isvalid(m, c) function MOI.delete!(m::LinQuadOptimizer, c::VLCI{<:VecLinSets}) deleteconstraintname!(m, c) diff --git a/src/objective.jl b/src/objective.jl index e49889a..4f26039 100644 --- a/src/objective.jl +++ b/src/objective.jl @@ -141,13 +141,26 @@ end Modify objective function =# -MOI.canmodifyobjective(m::LinQuadOptimizer, ::Type{MOI.ScalarCoefficientChange{Float64}}) = true -function MOI.modifyobjective!(m::LinQuadOptimizer, chg::MOI.ScalarCoefficientChange{Float64}) +function MOI.canmodify(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}, ::Type{MOI.ScalarCoefficientChange{Float64}}) where F + if F <: MOI.ScalarQuadraticFunction + return m.obj_type == QuadraticObjective + elseif F <: MOI.ScalarAffineFunction + return m.obj_type == AffineObjective + end + return false +end +function MOI.modify!(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}, chg::MOI.ScalarCoefficientChange{Float64}) where F if m.obj_type == SingleVariableObjective m.obj_type = AffineObjective m.single_obj_var = nothing end col = m.variable_mapping[chg.variable] - # 0 row is the objective change_objective_coefficient!(m, col, chg.new_coefficient) end + +function MOI.canmodify(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}, ::Type{MOI.ScalarConstantChange{Float64}}) where F + return !(F == MOI.SingleVariable) +end +function MOI.modify!(m::LinQuadOptimizer, ::MOI.ObjectiveFunction{F}, chg::MOI.ScalarConstantChange{Float64}) where F + m.objective_constant = chg.new_constant +end diff --git a/test/runtests.jl b/test/runtests.jl index 6823405..1c44118 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,24 +1,22 @@ -#push!(Base.LOAD_PATH,joinpath(dirname(@__FILE__),"..","..")) - using Base.Test, MathOptInterface using LinQuadOptInterface const MOIT = MathOptInterface.Test const LQOI = LinQuadOptInterface - @testset "LinQuadOptInterface" begin @testset "Unit Tests" begin config = MOIT.TestConfig(solve = false) solver = LQOI.MockLinQuadOptimizer() - MOIT.basic_constraint_tests(solver, config; - exclude = [ - ] - ) + MOIT.basic_constraint_tests(solver, config) MOIT.unittest(solver, config, [ - "solve_affine_interval" + "solve_affine_interval", + "solve_qp_edge_cases", + "solve_qcp_edge_cases", + "solve_affine_deletion_edge_cases" ]) end + @testset "Linear tests" begin linconfig = MOIT.TestConfig(solve = false) solver = LQOI.MockLinQuadOptimizer() @@ -65,4 +63,3 @@ const LQOI = LinQuadOptInterface end end end -; \ No newline at end of file