Skip to content

Commit b149b73

Browse files
kalmarekmlubin
authored andcommitted
Julia 0.7 (#100)
* is_apple, is_unix were moved to Sys; Base.Libdl was moved out of Base * Void -> Nothing, IntSet -> BitSet * broadcasting +,= where necessary * kwargs translate to NamedTuple Model.options has become an iterator over pairs: (:kwrd=>val) * fix deprecated constructors Array{T}(n), full(sparse) * Base.@gc_preserve -> GC.@preserve * importall is deprecated import explicitly every function extended in MPBWrapper.jl * dot is in LinearAlgebra; sparse in SparseArrays * using Test, SCS globally in runtests * Pkg is out of Base * Add missing using: LinearAlgebra, SparseArrays, MathProgBase * tidy test_problems * fix: syntax for NamedTuple in setwarmstart! * using Compat where necessary * add VERSION-dependent addoption!(::SCSMathProgModel, option, value) * add VERSION-dependent check for warmstarting * remove superfluous @compat
1 parent c377f2f commit b149b73

File tree

13 files changed

+683
-73
lines changed

13 files changed

+683
-73
lines changed

deps/build.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ using BinDeps
22

33
@BinDeps.setup
44

5-
blasvendor = Base.BLAS.vendor()
5+
using Compat.Libdl
6+
using Compat.LinearAlgebra.BLAS
7+
using Compat.Sys: isapple
8+
9+
blasvendor = BLAS.vendor()
610

711
direct = library_dependency("libscsdir", aliases=["libscsdir64"])
812
indirect = library_dependency("libscsindir", aliases=["libscsindir64"])
913

1014
# TODO: Provide both libs in the "scs" Homebrew package.
11-
# if is_apple()
15+
# if Sys.isapple()
1216
# using Homebrew
1317
# provides(Homebrew.HB, "scs", [direct, indirect], os = :Darwin)
1418
# end
@@ -35,7 +39,7 @@ prefix = joinpath(BinDeps.depsdir(direct), "usr")
3539
srcdir = joinpath(BinDeps.depsdir(direct), "src", "scs-$version/")
3640

3741
ldflags = ""
38-
if is_apple()
42+
if isapple()
3943
ldflags = "$ldflags -undefined suppress -flat_namespace"
4044
end
4145
cflags = "-DCOPYAMATRIX -DDLONG -DUSE_LAPACK -DCTRLC=1"

src/MOIWrapper.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ end
6060
mutable struct SCSOptimizer <: MOI.AbstractOptimizer
6161
cone::ConeData
6262
maxsense::Bool
63-
data::Union{Void, ModelData} # only non-Void between MOI.copy! and MOI.optimize!
63+
data::Union{Nothing, ModelData} # only non-Void between MOI.copy! and MOI.optimize!
6464
sol::MOISolution
6565
function SCSOptimizer()
6666
new(ConeData(), false, nothing, MOISolution())
@@ -169,7 +169,7 @@ function _scalecoef(rows, coef, minus, ::Type{MOI.PositiveSemidefiniteConeTriang
169169
scaling = minus ? -1 : 1
170170
scaling2 = rev ? scaling / 2 : scaling * 2
171171
output = copy(coef)
172-
diagidx = IntSet()
172+
diagidx = BitSet()
173173
for i in 1:d
174174
push!(diagidx, trimap(i, i))
175175
end
@@ -232,11 +232,11 @@ function MOIU.loadconstraint!(optimizer::SCSOptimizer, ci, f::MOI.VectorAffineFu
232232
offset = constroffset(optimizer, ci)
233233
rows = constrrows(s)
234234
optimizer.cone.nrows[offset] = length(rows)
235-
i = offset + rows
235+
i = offset .+ rows
236236
# The SCS format is b - Ax ∈ cone
237237
# so minus=false for b and minus=true for A
238238
optimizer.data.b[i] = scalecoef(rows, orderval(f.constant, s), false, s)
239-
append!(optimizer.data.I, offset + orderidx(I, s))
239+
append!(optimizer.data.I, offset .+ orderidx(I, s))
240240
append!(optimizer.data.J, J)
241241
append!(optimizer.data.V, scalecoef(I, V, true, s))
242242
end
@@ -268,7 +268,7 @@ MOIU.canload(::SCSOptimizer, ::MOI.ObjectiveSense) = true
268268
function MOIU.load!(::SCSOptimizer, ::MOI.ObjectiveSense, ::MOI.OptimizationSense) end
269269
MOIU.canload(::SCSOptimizer, ::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}) = true
270270
function MOIU.load!(optimizer::SCSOptimizer, ::MOI.ObjectiveFunction, f::MOI.ScalarAffineFunction)
271-
c0 = full(sparsevec(_varmap(f), f.coefficients, optimizer.data.n))
271+
c0 = Vector(sparsevec(_varmap(f), f.coefficients, optimizer.data.n))
272272
optimizer.data.objconstant = f.constant
273273
optimizer.data.c = optimizer.maxsense ? -c0 : c0
274274
end
@@ -352,7 +352,7 @@ end
352352
function MOI.get(optimizer::SCSOptimizer, ::MOI.ConstraintPrimal, ci::CI{<:MOI.AbstractFunction, S}) where S <: MOI.AbstractSet
353353
offset = constroffset(optimizer, ci)
354354
rows = constrrows(optimizer, ci)
355-
_unshift(optimizer, offset, unscalecoef(rows, reorderval(optimizer.sol.slack[offset + rows], S), S, length(rows)), S)
355+
_unshift(optimizer, offset, unscalecoef(rows, reorderval(optimizer.sol.slack[offset .+ rows], S), S, length(rows)), S)
356356
end
357357

358358
MOI.canget(optimizer::SCSOptimizer, ::MOI.DualStatus) = true
@@ -372,7 +372,7 @@ end
372372
function MOI.get(optimizer::SCSOptimizer, ::MOI.ConstraintDual, ci::CI{<:MOI.AbstractFunction, S}) where S <: MOI.AbstractSet
373373
offset = constroffset(optimizer, ci)
374374
rows = constrrows(optimizer, ci)
375-
unscalecoef(rows, reorderval(optimizer.sol.dual[offset + rows], S), S, length(rows))
375+
unscalecoef(rows, reorderval(optimizer.sol.dual[offset .+ rows], S), S, length(rows))
376376
end
377377

378378
MOI.canget(optimizer::SCSOptimizer, ::MOI.ResultCount) = true

src/MPBWrapper.jl

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
# MathProgBase.jl interface for the SCS.jl solver wrapper
77
#############################################################################
88

9-
importall MathProgBase.SolverInterface
9+
using Compat.LinearAlgebra: dot
10+
using MathProgBase.SolverInterface
11+
12+
import MathProgBase.SolverInterface: ConicModel, LinearQuadraticModel,
13+
getdual, getobjval, getsolution, getsolvetime, getvardual, loadproblem!,
14+
numconstr, numvar, optimize!, setbvec!, setwarmstart!, status,
15+
supportedcones
16+
1017
import Base.convert
1118

1219
# TODO: don't add to Base.convert!
@@ -145,14 +152,14 @@ function orderconesforscs(A_in, b_in, c_cones, v_cones)
145152
A_t = spzeros(n,0)
146153
b = zeros(0)
147154
row_map_ind = zeros(Int, length(b_in))
148-
row_map_type = Array{Symbol}(length(b_in))
155+
row_map_type = Array{Symbol}(undef, length(b_in))
149156
col_map_ind = zeros(Int, n)
150-
col_map_type = Array{Symbol}(n)
157+
col_map_type = Array{Symbol}(undef, n)
151158

152159
# First, count the total number of variables
153160
num_vars = 0
154161
for (cone, idxs) in v_cones
155-
col_map_type[idxs] = cone
162+
col_map_type[idxs] .= cone
156163
num_vars += length(idxs)
157164
end
158165
@assert num_vars == n
@@ -429,10 +436,23 @@ function getvardual(m::SCSMathProgModel)
429436
return dual
430437
end
431438

439+
if VERSION >= v"0.7-"
440+
function addoption!(m::SCSMathProgModel, option::Symbol, value)
441+
nt = NamedTuple{(option,), Tuple{typeof(value)}}((value,))
442+
m.options = pairs(merge(m.options.data, nt))
443+
return m
444+
end
445+
else
446+
function addoption!(m::SCSMathProgModel, option::Symbol, value)
447+
push!(m.options, (option, value))
448+
return m
449+
end
450+
end
451+
432452
# warmstart
433453
# kwargs can be `primal_sol`, `dual_sol`, and `slack`
434454
function setwarmstart!(m::SCSMathProgModel, primal_sol; kwargs...)
435-
push!(m.options, (:warm_start, true))
455+
addoption!(m, :warm_start, true)
436456
m.primal_sol = primal_sol
437457
for (k,v) in kwargs
438458
setfield!(m, k, v)

src/SCS.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ else
88
error("SCS not properly installed. Please run Pkg.build(\"SCS\") and restart julia")
99
end
1010

11-
import Base.Libdl: RTLD_LAZY, RTLD_DEEPBIND, RTLD_GLOBAL, dlopen
11+
using Compat
12+
using Compat.Libdl
13+
using Compat.Sys: isunix
1214

1315
function __init__()
1416
vnum = VersionNumber(SCS_version())
1517
# default binaries need access to Julia's lapack symbols
16-
if is_unix()
18+
if isunix()
1719
dlopen(Base.liblapack_name, RTLD_LAZY|RTLD_DEEPBIND|RTLD_GLOBAL)
1820
end
1921
depsdir = realpath(joinpath(dirname(@__FILE__),"..","deps"))

src/c_wrapper.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ macro compat_gc_preserve(args...)
44
vars = args[1:end-1]
55
body = args[end]
66
if VERSION > v"0.7.0-"
7-
return esc(Expr(:macrocall, Expr(:., :Base, Base.Meta.quot(Symbol("@gc_preserve"))), __source__, args...))
7+
return esc(Expr(:macrocall, Expr(:., :GC, Base.Meta.quot(Symbol("@preserve"))), __source__, args...))
88
else
99
return esc(body)
1010
end
@@ -52,7 +52,13 @@ function SCS_solve(T::Union{Type{Direct}, Type{Indirect}},
5252
cone = Ref(SCSCone(f, l, q, s, ep, ed, p))
5353
info = Ref(SCSInfo())
5454

55-
if (:warm_start, true) in options && length(primal_sol) == n && length(dual_sol) == m && length(slack) == m
55+
if VERSION >= v"0.7-"
56+
ws = (:warm_start=>true) in options
57+
else
58+
ws = (:warm_start, true) in options
59+
end
60+
61+
if ws && length(primal_sol) == n && length(dual_sol) == m && length(slack) == m
5662
x = primal_sol
5763
y = dual_sol
5864
s = slack
@@ -82,26 +88,26 @@ for (T, lib) in zip([SCS.Direct, SCS.Indirect], [SCS.direct, SCS.indirect])
8288
@eval begin
8389
function SCS_init(::Type{$T}, data::Ref{SCSData}, cone::Ref{SCSCone}, info::Ref{SCSInfo})
8490

85-
p_work = ccall((:scs_init, $lib), Ptr{Void},
91+
p_work = ccall((:scs_init, $lib), Ptr{Nothing},
8692
(Ptr{SCSData}, Ptr{SCSCone}, Ptr{SCSInfo}),
8793
data, cone, info)
8894

8995
return p_work
9096
end
9197

9298
# solution struct is simple enough, we know it won't be modified by SCS so take by value
93-
function SCS_solve(::Type{$T}, p_work::Ptr{Void}, data::Ref{SCSData}, cone::Ref{SCSCone}, solution::SCSSolution, info::Ref{SCSInfo})
99+
function SCS_solve(::Type{$T}, p_work::Ptr{Nothing}, data::Ref{SCSData}, cone::Ref{SCSCone}, solution::SCSSolution, info::Ref{SCSInfo})
94100

95101
status = ccall((:scs_solve, $lib), Int,
96-
(Ptr{Void}, Ptr{SCSData}, Ptr{SCSCone}, Ref{SCSSolution}, Ptr{SCSInfo}),
102+
(Ptr{Nothing}, Ptr{SCSData}, Ptr{SCSCone}, Ref{SCSSolution}, Ptr{SCSInfo}),
97103
p_work, data, cone, solution, info)
98104

99105
return status
100106
end
101107

102-
function SCS_finish(::Type{$T}, p_work::Ptr{Void})
103-
ccall((:scs_finish, $lib), Void,
104-
(Ptr{Void}, ),
108+
function SCS_finish(::Type{$T}, p_work::Ptr{Nothing})
109+
ccall((:scs_finish, $lib), Nothing,
110+
(Ptr{Nothing}, ),
105111
p_work)
106112
end
107113
end

src/types.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
export SCSMatrix, SCSData, SCSSettings, SCSSolution, SCSInfo, SCSCone, SCSVecOrMatOrSparse
1+
using Compat.SparseArrays
22

3+
export SCSMatrix, SCSData, SCSSettings, SCSSolution, SCSInfo, SCSCone, SCSVecOrMatOrSparse
34

45
SCSVecOrMatOrSparse = Union{VecOrMat, SparseMatrixCSC{Float64,Int}}
56

@@ -68,9 +69,9 @@ struct SCSData
6869
end
6970

7071
struct SCSSolution
71-
x::Ptr{Void}
72-
y::Ptr{Void}
73-
s::Ptr{Void}
72+
x::Ptr{Nothing}
73+
y::Ptr{Nothing}
74+
s::Ptr{Nothing}
7475
end
7576

7677

test/MPBWrapper.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
include("mpb_linear.jl")
33
end
44

5-
import SCS
5+
using Compat.Pkg: dir
66
@testset "MathProgBase" begin
7-
include(joinpath(Pkg.dir("MathProgBase"),"test","conicinterface.jl"))
7+
include(joinpath(dir("MathProgBase"),"test","conicinterface.jl"))
88
coniclineartest(SCS.SCSSolver(), duals=true, tol=1e-2)
99
conicSOCtest(SCS.SCSSolver(), duals=true, tol=1e-2)
1010
conicEXPtest(SCS.SCSSolver(), duals=true, tol=1e-2)

test/direct.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using SCS
2-
using Base.Test
3-
41
# Solve a trivial problem
52
A = reshape([1.0],(1,1))
63
solution = SCS_solve(SCS.Direct, 1, 1, A, [1.0], [1.0], 1, 0, Int[], Int[], 0, 0, Float64[]);

test/indirect.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using SCS
2-
using Base.Test
3-
41
# Solve a trivial problem
52
A = reshape([1.0],(1,1))
63
solution = SCS_solve(SCS.Indirect, 1, 1, A, [1.0], [1.0], 1, 0, Int[], Int[], 0, 0, Float64[]);

test/mpb_linear.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
# Test the MathProgBase.jl interface for the SCS.jl solver wrapper
88
#############################################################################
99

10-
using Base.Test
1110
using MathProgBase
12-
using SCS
11+
using Compat.LinearAlgebra
12+
using Compat.SparseArrays
1313

1414
solver = SCSSolver()
1515
objtol = 1e-4

0 commit comments

Comments
 (0)