Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkoolen committed Feb 6, 2018
1 parent de19e17 commit d849131
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ end



function warm_start!(model::OSQP.Model; x::Vector{Float64} = nothing, y::Vector{Float64} = nothing)
function warm_start!(model::OSQP.Model; x::Union{Vector{Float64}, Nothing} = nothing, y::Union{Vector{Float64}, Nothing} = nothing)
# Get problem dimensions
(n, m) = OSQP.dimensions(model)

Expand Down
70 changes: 59 additions & 11 deletions test/mpbinterface.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using MathProgBase
import MathProgBase: LinearQuadraticModel, loadproblem!, setquadobj!, optimize!, status, getobjval, getsolution
import MathProgBase: LinearQuadraticModel, loadproblem!, setquadobj!, optimize!, status, numvar, numconstr, setwarmstart!, getsense,
getobjval, getsolution, setsense!, getvarLB, setvarLB!, getvarUB, setvarUB!, getconstrLB, setconstrLB!, getconstrUB, setconstrUB!,
getconstrmatrix, numlinconstr, getsolvetime, getrawsolver, getvartype, setvartype!

@testset "MathProgBase" begin
solver = OSQPMathProgBaseInterface.OSQPSolver(eps_abs = 1e-7, polish = true)
solver = OSQPMathProgBaseInterface.OSQPSolver(eps_abs = 1e-7, eps_rel = 1e-16)
MathProgBase.setparameters!(solver, Silent=true)

# from joinpath(Pkg.dir("MathProgBase"), "test", "quadprog.jl"):
# modified version of joinpath(Pkg.dir("MathProgBase"), "test", "quadprog.jl"):
sol = quadprog([0., 0., 0.],[2. 1. 0.; 1. 2. 1.; 0. 1. 2.],[1. 2. 3.; 1. 1. 0.],'>',[4., 1.],-Inf,Inf,solver)
@test sol.status == :Optimal
@test isapprox(sol.objval, 130/70, atol=1e-6)
Expand All @@ -20,14 +22,60 @@ import MathProgBase: LinearQuadraticModel, loadproblem!, setquadobj!, optimize!,
cols = [1, 1, 1, 2, 2, 3, 3]
vals = Float64[2, 0.5, 0.5, 2, 1, 1, 1]
setquadobj!(m,rows,cols,vals)
m2 = copy(m)

verify_solution = function (m)
stat = status(m)
@test stat == :Optimal
@test isapprox(getobjval(m), 130/70, atol=1e-6)
@test isapprox(norm(getsolution(m) - [0.5714285714285715,0.4285714285714285,0.8571428571428572]), 0.0, atol=1e-6)
@test getsolvetime(m) > 0
end

optimize!(m)
stat = status(m)
@test stat == :Optimal
@test isapprox(getobjval(m), 130/70, atol=1e-6)
@test isapprox(norm(getsolution(m) - [0.5714285714285715,0.4285714285714285,0.8571428571428572]), 0.0, atol=1e-6)
verify_solution(m)

# TODO: enable warmstart:
# setwarmstart!(m2, getsolution(m) .+ 0.1) # currently causes a segfault
optimize!(m2)
verify_solution(m2)
end

@testset "Unsupported behavior" begin
m = LinearQuadraticModel(solver)
@test_throws ErrorException setsense!(m, :Max)
@test_throws ErrorException loadproblem!(m, spzeros(0, 2), [-Inf, -Inf], [Inf, Inf], [1.; 1.], Float64[], Float64[], :Max) # maximization not supported
@test_throws ErrorException loadproblem!(m, spzeros(0, 2), [-1., -Inf], [Inf, Inf], [1.; 1.], Float64[], Float64[], :Min) # variable bounds not supported
@test_throws ErrorException loadproblem!(m, spzeros(0, 2), [-Inf, -Inf], [10., Inf], [1.; 1.], Float64[], Float64[], :Min) # variable bounds not supported

loadproblem!(m, [1. 2. 3.; 1. 1. 0.],[-Inf,-Inf,-Inf],[Inf,Inf,Inf],[0.,0.,0.],[4., 1.],[Inf,Inf], :Min)
@test_throws ErrorException setvarLB!(m, rand(numvar(m)))
@test_throws ErrorException setvarUB!(m, rand(numvar(m)))

@test_throws ErrorException setvartype!(m, [:Cont, :Bin, :Cont])
end
end

# include(joinpath(Pkg.dir("MathProgBase"), "test", "quadprog.jl"))
# quadprogtest(solver)
# qpdualtest(solver)
@testset "Getters/setters" begin
m = LinearQuadraticModel(solver)
loadproblem!(m, [1. 2. 3.; 1. 1. 0.],[-Inf,-Inf,-Inf],[Inf,Inf,Inf],[0.,0.,0.],[4., 1.],[Inf,Inf], :Min)

@test getrawsolver(m) isa OSQP.Model

@test getsense(m) == :Min

@test getvarLB(m) == [-Inf, -Inf, -Inf]
@test getvarUB(m) == [Inf, Inf, Inf]

@test getconstrLB(m) == [4., 1.]
setconstrLB!(m, [-4., Inf])
@test getconstrLB(m) == [-4., Inf]
@test getconstrUB(m) == [Inf, Inf]
setconstrUB!(m, [5., 8.])
@test getconstrUB(m) == [5., 8.]

@test getconstrmatrix(m) == [1. 2. 3.; 1. 1. 0.]
@test numlinconstr(m) == 2

@test getvartype(m) == [:Cont, :Cont, :Cont]
end
end

0 comments on commit d849131

Please sign in to comment.