Skip to content

Commit

Permalink
more 0.7 compat fixes (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Apr 27, 2018
1 parent f2d9c55 commit b99edcd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
10 changes: 5 additions & 5 deletions src/SolverInterface/conic_to_lpqp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mutable struct ConicToLPQPBridge <: AbstractLinearQuadraticModel
vartypes::Vector{Symbol}
end

ConicToLPQPBridge(s::AbstractConicModel) = ConicToLPQPBridge(s, sparse(Int[],Int[],Float64[]), Float64[], Float64[], Float64[], Float64[], Float64[], :Min, Int[], Array{Vector{Int}}(0),Array{Vector{Int}}(0), Symbol[])
ConicToLPQPBridge(s::AbstractConicModel) = ConicToLPQPBridge(s, sparse(Int[],Int[],Float64[]), Float64[], Float64[], Float64[], Float64[], Float64[], :Min, Int[], Array{Vector{Int}}(undef, 0),Array{Vector{Int}}(undef, 0), Symbol[])

export ConicToLPQPBridge

Expand Down Expand Up @@ -113,7 +113,7 @@ function optimize!(wrap::ConicToLPQPBridge)
(nvar = length(collb)) == length(colub) || error("Unequal lengths for column bounds")
(nrow = length(rowlb)) == length(rowub) || error("Unequal lengths for row bounds")

constr_cones = Array{Any}(0)
constr_cones = []
var_cones = [(:Free,1:nvar)]

# for each variable bound, create a new constraint
Expand Down Expand Up @@ -159,20 +159,20 @@ function optimize!(wrap::ConicToLPQPBridge)
if rowlb[it] == rowub[it]
# a'x = b ==> b - a'x = 0
push!(b, rowlb[it])
push!(constr_cones,(:Zero,it+(extrarows:extrarows)))
push!(constr_cones,(:Zero,it.+(extrarows:extrarows)))
# Range constraint - not supported
elseif rowlb[it] != -Inf && rowub[it] != Inf
error("Ranged constraints unsupported!")
# Less-than constraint
elseif rowlb[it] == -Inf
# a'x <= b ==> b - a'x >= 0
push!(b, rowub[it])
push!(constr_cones,(:NonNeg,it+(extrarows:extrarows)))
push!(constr_cones,(:NonNeg,it.+(extrarows:extrarows)))
# Greater-than constraint
else
# a'x >= b ==> b - a'x <= 0
push!(b, rowlb[it])
push!(constr_cones,(:NonPos,it+(extrarows:extrarows)))
push!(constr_cones,(:NonPos,it.+(extrarows:extrarows)))
end
end

Expand Down
92 changes: 46 additions & 46 deletions test/quadprog.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Base.Test
using Compat.Test
using Compat.LinearAlgebra
using MathProgBase
using MathProgBase.SolverInterface

function quadprogtest(solver)
@testset "Testing quadprog with $solver" begin
Expand All @@ -10,30 +10,30 @@ function quadprogtest(solver)
@test isapprox(norm(sol.sol[1:3] - [0.5714285714285715,0.4285714285714285,0.8571428571428572]), 0.0, atol=1e-6)

@testset "QP1" 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)
m = MathProgBase.LinearQuadraticModel(solver)
MathProgBase.loadproblem!(m, [1. 2. 3.; 1. 1. 0.],[-Inf,-Inf,-Inf],[Inf,Inf,Inf],[0.,0.,0.],[4., 1.],[Inf,Inf], :Min)

setquadobj!(m,diagm([10.0,10.0,10.0]))
MathProgBase.setquadobj!(m,diagm([10.0,10.0,10.0]))
rows = [1, 2, 2, 2, 3, 3, 3]
cols = [1, 1, 1, 2, 2, 3, 3]
vals = Float64[2, 0.5, 0.5, 2, 1, 1, 1]
setquadobj!(m,rows,cols,vals)
optimize!(m)
stat = status(m)
MathProgBase.setquadobj!(m,rows,cols,vals)
MathProgBase.optimize!(m)
stat = MathProgBase.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 isapprox(MathProgBase.getobjval(m), 130/70, atol=1e-6)
@test isapprox(norm(MathProgBase.getsolution(m) - [0.5714285714285715,0.4285714285714285,0.8571428571428572]), 0.0, atol=1e-6)
end

@testset "QP2" begin
m = LinearQuadraticModel(solver)
loadproblem!(m, [-1. 1.; 1. 1.], [0.,0.], [Inf,Inf], [1.,1.], [0.,0.], [Inf,Inf], :Max)
addquadconstr!(m, [2], [1.], [1], [1], [1.], '<', 2)
optimize!(m)
stat = status(m)
m = MathProgBase.LinearQuadraticModel(solver)
MathProgBase.loadproblem!(m, [-1. 1.; 1. 1.], [0.,0.], [Inf,Inf], [1.,1.], [0.,0.], [Inf,Inf], :Max)
MathProgBase.addquadconstr!(m, [2], [1.], [1], [1], [1.], '<', 2)
MathProgBase.optimize!(m)
stat = MathProgBase.status(m)
@test stat == :Optimal
@test isapprox(getobjval(m), 2.25, atol=1e-6)
@test isapprox(norm(getsolution(m) - [0.5,1.75]), 0.0, atol=1e-3)
@test isapprox(MathProgBase.getobjval(m), 2.25, atol=1e-6)
@test isapprox(norm(MathProgBase.getsolution(m) - [0.5,1.75]), 0.0, atol=1e-3)
end
end
end
Expand All @@ -43,37 +43,37 @@ function qpdualtest(solver)
# max x
# s.t. x^2 <= 2
@testset "QP1" begin
m = LinearQuadraticModel(solver)
loadproblem!(m, Array{Float64}(0,1), [-Inf], [Inf], [1.0], Float64[], Float64[], :Max)
addquadconstr!(m, [], [], [1], [1], [1.0], '<', 2.0)
optimize!(m)
stat = status(m)
m = MathProgBase.LinearQuadraticModel(solver)
MathProgBase.loadproblem!(m, Array{Float64}(0,1), [-Inf], [Inf], [1.0], Float64[], Float64[], :Max)
MathProgBase.addquadconstr!(m, [], [], [1], [1], [1.0], '<', 2.0)
MathProgBase.optimize!(m)
stat = MathProgBase.status(m)

@test numlinconstr(m) == 0
@test numquadconstr(m) == 1
@test numconstr(m) == 1
@test MathProgBase.numlinconstr(m) == 0
@test MathProgBase.numquadconstr(m) == 1
@test MathProgBase.numconstr(m) == 1
@test stat == :Optimal
@test isapprox(getobjval(m), sqrt(2), atol=1e-6)
@test isapprox(getsolution(m)[1], sqrt(2), atol=1e-6)
@test isapprox(getquadconstrduals(m)[1], 0.5/sqrt(2), atol=1e-6)
@test isapprox(MathProgBase.getobjval(m), sqrt(2), atol=1e-6)
@test isapprox(MathProgBase.getsolution(m)[1], sqrt(2), atol=1e-6)
@test isapprox(MathProgBase.getquadconstrduals(m)[1], 0.5/sqrt(2), atol=1e-6)
end

# min -x
# s.t. x^2 <= 2
@testset "QP2" begin
m = LinearQuadraticModel(solver)
loadproblem!(m, Array{Float64}(0,1), [-Inf], [Inf], [-1.0], Float64[], Float64[], :Min)
addquadconstr!(m, [], [], [1], [1], [1.0], '<', 2.0)
optimize!(m)
stat = status(m)
m = MathProgBase.LinearQuadraticModel(solver)
MathProgBase.loadproblem!(m, Array{Float64}(0,1), [-Inf], [Inf], [-1.0], Float64[], Float64[], :Min)
MathProgBase.addquadconstr!(m, [], [], [1], [1], [1.0], '<', 2.0)
MathProgBase.optimize!(m)
stat = MathProgBase.status(m)

@test numlinconstr(m) == 0
@test numquadconstr(m) == 1
@test numconstr(m) == 1
@test MathProgBase.numlinconstr(m) == 0
@test MathProgBase.numquadconstr(m) == 1
@test MathProgBase.numconstr(m) == 1
@test stat == :Optimal
@test isapprox(getobjval(m), -sqrt(2), atol=1e-6)
@test isapprox(getsolution(m)[1], sqrt(2), atol=1e-6)
@test isapprox(getquadconstrduals(m)[1], -0.5/sqrt(2), atol=1e-6)
@test isapprox(MathProgBase.getobjval(m), -sqrt(2), atol=1e-6)
@test isapprox(MathProgBase.getsolution(m)[1], sqrt(2), atol=1e-6)
@test isapprox(MathProgBase.getquadconstrduals(m)[1], -0.5/sqrt(2), atol=1e-6)
end
end
end
Expand All @@ -84,14 +84,14 @@ function socptest(solver)
# s.t. x + y >= 1
# x^2 + y^2 <= t^2
# t >= 0
m = LinearQuadraticModel(solver)
loadproblem!(m, [ 1.0 1.0 0.0 ], [-Inf,-Inf,0.0], [Inf,Inf,Inf], [0.0,0.0,1.0], [1.0],[Inf], :Min)
addquadconstr!(m, [], [], [1,2,3], [1,2,3], [1.0,1.0,-1.0],'<',0.0)
optimize!(m)
stat = status(m)
m = MathProgBase.LinearQuadraticModel(solver)
MathProgBase.loadproblem!(m, [ 1.0 1.0 0.0 ], [-Inf,-Inf,0.0], [Inf,Inf,Inf], [0.0,0.0,1.0], [1.0],[Inf], :Min)
MathProgBase.addquadconstr!(m, [], [], [1,2,3], [1,2,3], [1.0,1.0,-1.0],'<',0.0)
MathProgBase.optimize!(m)
stat = MathProgBase.status(m)

@test stat == :Optimal
@test isapprox(getobjval(m), sqrt(1/2), atol=1e-6)
@test isapprox(norm(getsolution(m) - [0.5,0.5,sqrt(1/2)]), 0.0, atol=1e-3)
@test isapprox(MathProgBase.getobjval(m), sqrt(1/2), atol=1e-6)
@test isapprox(norm(MathProgBase.getsolution(m) - [0.5,0.5,sqrt(1/2)]), 0.0, atol=1e-3)
end
end

0 comments on commit b99edcd

Please sign in to comment.