From b99edcd5df49076a403bd453189599d3f818f48a Mon Sep 17 00:00:00 2001 From: Miles Lubin Date: Thu, 26 Apr 2018 21:10:33 -0400 Subject: [PATCH] more 0.7 compat fixes (#209) --- src/SolverInterface/conic_to_lpqp.jl | 10 +-- test/quadprog.jl | 92 ++++++++++++++-------------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/SolverInterface/conic_to_lpqp.jl b/src/SolverInterface/conic_to_lpqp.jl index 686bcc6..441d266 100644 --- a/src/SolverInterface/conic_to_lpqp.jl +++ b/src/SolverInterface/conic_to_lpqp.jl @@ -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 @@ -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 @@ -159,7 +159,7 @@ 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!") @@ -167,12 +167,12 @@ function optimize!(wrap::ConicToLPQPBridge) 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 diff --git a/test/quadprog.jl b/test/quadprog.jl index 79b36fa..cc58c38 100644 --- a/test/quadprog.jl +++ b/test/quadprog.jl @@ -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 @@ -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 @@ -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 @@ -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