Skip to content

Commit 3422b87

Browse files
committed
Add support for Julia v0.7 and fix #17
1 parent d640956 commit 3422b87

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
julia 0.6
22
MathOptInterface 0.5 0.6
3-
Compat 0.69
3+
Compat 1.0.1

src/SemidefiniteOptInterface.jl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ using MathOptInterface
44
const MOI = MathOptInterface
55
const MOIU = MOI.Utilities
66

7+
using Compat
8+
using Compat.LinearAlgebra # for diag
9+
710
abstract type AbstractSDOptimizer <: MOI.AbstractOptimizer end
811

912
include("interface.jl")
@@ -36,8 +39,8 @@ mutable struct SOItoMOIBridge{T, SIT <: AbstractSDOptimizer} <: MOI.AbstractOpti
3639
nconstrs::Int
3740
nblocks::Int
3841
blockdims::Vector{Int}
39-
free::IntSet
40-
varmap::Vector{Vector{Tuple{Int, Int, Int, T, T}}} # Variable Index vi -> blk, i, j, coef, shift # x = sum coef * X[blk][i, j] + shift
42+
free::BitSet
43+
varmap::Vector{Vector{Tuple{Int, Int, Int, T, T}}} # Variable Index vi -> blk, i, j, coef, shift # x = sum coef * block(X, blk)[i, j] + shift
4144
zeroblock::Dict{CI, Int}
4245
constrmap::Dict{CI, UnitRange{Int}} # Constraint Index ci -> cs
4346
slackmap::Vector{Tuple{Int, Int, Int, T}} # c -> blk, i, j, coef
@@ -46,7 +49,7 @@ mutable struct SOItoMOIBridge{T, SIT <: AbstractSDOptimizer} <: MOI.AbstractOpti
4649
new{T, SIT}(sdoptimizer, Dict{Int64, T}(), Dict{Int, T}(),
4750
zero(T), 1, zero(T), 0, 0,
4851
Int[],
49-
IntSet(),
52+
BitSet(),
5053
Vector{Tuple{Int, Int, Int, T}}[],
5154
Dict{CI, Int}(),
5255
Dict{CI, UnitRange{Int}}(),
@@ -95,7 +98,7 @@ function MOI.empty!(optimizer::SOItoMOIBridge{T}) where T
9598
optimizer.nconstrs = 0
9699
optimizer.nblocks = 0
97100
optimizer.blockdims = Int[]
98-
optimizer.free = IntSet()
101+
optimizer.free = BitSet()
99102
optimizer.varmap = Vector{Tuple{Int, Int, Int, T}}[]
100103
optimizer.zeroblock = Dict{CI, Int}()
101104
optimizer.constrmap = Dict{CI, UnitRange{Int}}()
@@ -114,7 +117,7 @@ function addsetconstant(optimizer::SOItoMOIBridge, ci::CI, x)
114117
end
115118
function addblkconstant(optimizer::SOItoMOIBridge, ci::CI{<:Any, <:Union{NS, PS}}, x)
116119
blk = -ci.value
117-
x + optimizer.blkconstant[blk]
120+
x .+ optimizer.blkconstant[blk]
118121
end
119122
function addblkconstant(optimizer::SOItoMOIBridge, ci::CI, x)
120123
x
@@ -186,18 +189,18 @@ MOI.canget(m::SOItoMOIBridge, ::Union{MOI.VariablePrimal,
186189
MOI.ConstraintDual}, ::Type{<:MOI.Index}) = true
187190

188191
function _getblock(M, blk::Int, s::Type{<:Union{NS, ZS}})
189-
diag(M[blk])
192+
diag(block(M, blk))
190193
end
191194
function _getblock(M, blk::Int, s::Type{<:PS})
192-
-diag(M[blk])
195+
-diag(block(M, blk))
193196
end
194197
# Vectorized length for matrix dimension d
195198
sympackedlen(d::Integer) = (d*(d+1)) >> 1
196199
function _getblock(M::AbstractMatrix{T}, blk::Int, s::Type{<:DS}) where T
197-
B = M[blk]
198-
d = Base.LinAlg.checksquare(B)
200+
B = block(M, blk)
201+
d = Compat.LinearAlgebra.checksquare(B)
199202
n = sympackedlen(d)
200-
v = Vector{T}(n)
203+
v = Vector{T}(undef, n)
201204
k = 0
202205
for j in 1:d
203206
for i in 1:j
@@ -228,7 +231,7 @@ function MOI.get(m::SOItoMOIBridge{T}, ::MOI.VariablePrimal, vi::VI) where T
228231
for (blk, i, j, coef, shift) in varmap(m, vi)
229232
x += shift
230233
if blk != 0
231-
x += X[blk][i, j] * sign(coef)
234+
x += block(X, blk)[i, j] * sign(coef)
232235
end
233236
end
234237
x
@@ -253,9 +256,9 @@ function getslack(m::SOItoMOIBridge{T}, c::Int) where T
253256
zero(T)
254257
else
255258
if i != j
256-
coef *= 2 # We should take X[blk][i, j] + X[blk][j, i] but they are equal
259+
coef *= 2 # We should take block(X, blk)[i, j] + block(X, blk)[j, i] but they are equal
257260
end
258-
coef * X[blk][i, j]
261+
coef * block(X, blk)[i, j]
259262
end
260263
end
261264

@@ -274,7 +277,7 @@ function getvardual(m::SOItoMOIBridge{T}, vi::VI) where T
274277
z = zero(T)
275278
for (blk, i, j, coef) in varmap(m, vi)
276279
if blk != 0
277-
z += Z[blk][i, j] * sign(coef)
280+
z += block(Z, blk)[i, j] * sign(coef)
278281
end
279282
end
280283
z
@@ -319,7 +322,8 @@ function scalevec!(v, c)
319322
end
320323
v
321324
end
322-
function MOI.get(m::SOItoMOIBridge{T}, ::MOI.ConstraintDual, ci::CI{F, DS}) where {T,F}
325+
function MOI.get(m::SOItoMOIBridge{T}, ::MOI.ConstraintDual,
326+
ci::CI{<:AF{T}, DS}) where T
323327
scalevec!(_getattribute(m, ci, getdual), one(T)/2)
324328
end
325329

src/constraint.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function createslack!(m::SOItoMOIBridge{T}, cs, ::ZS) where T
2-
m.slackmap[cs] = (0, 0, 0, zero(T))
2+
m.slackmap[cs] .= ((0, 0, 0, zero(T)),)
33
end
44
function createslack!(m::SOItoMOIBridge, cs, ::S) where S <: Union{NS, PS}
55
blk = newblock(m, -length(cs))
@@ -31,7 +31,7 @@ nconstraints(f::VAF, s) = MOI.output_dimension(f)
3131
function _allocateconstraint!(m::SOItoMOIBridge, f, s)
3232
ci = CI{typeof(f), typeof(s)}(m.nconstrs)
3333
n = nconstraints(f, s)
34-
m.constrmap[ci] = m.nconstrs + (1:n)
34+
m.constrmap[ci] = m.nconstrs .+ (1:n)
3535
m.nconstrs += n
3636
resize!(m.slackmap, m.nconstrs)
3737
createslack!(m, ci, f, s)
@@ -65,7 +65,7 @@ _getconstant(m::SOItoMOIBridge{T}, s::MOI.AbstractSet) where T = zero(T)
6565
function loadcoefficients!(m::SOItoMOIBridge, cs::UnitRange, f::AF, s)
6666
f = MOIU.canonical(f) # sum terms with same variables and same outputindex
6767
if !isempty(cs)
68-
rhs = _getconstant(m, s) - MOIU._constant(f)
68+
rhs = _getconstant(m, s) .- MOIU._constant(f)
6969
for t in f.terms
7070
st = scalar_term(t)
7171
if !iszero(st.coefficient)

src/load.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function MOIU.load!(optimizer::SOItoMOIBridge{T}, ::MOI.ObjectiveFunction, f::MO
3232
end
3333

3434
function MOIU.allocatevariables!(optimizer::SOItoMOIBridge{T}, nvars) where T
35-
optimizer.free = IntSet(1:nvars)
36-
optimizer.varmap = Vector{Vector{Tuple{Int, Int, Int, T, T}}}(nvars)
35+
optimizer.free = BitSet(1:nvars)
36+
optimizer.varmap = Vector{Vector{Tuple{Int, Int, Int, T, T}}}(undef, nvars)
3737
VI.(1:nvars)
3838
end
3939

src/mock.jl

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
1-
struct BlockMatrix{T} <: AbstractMatrix{T}
1+
abstract type AbstractBlockMatrix{T} <: AbstractMatrix{T} end
2+
struct BlockMatrix{T} <: AbstractBlockMatrix{T}
23
blocks::Vector{Matrix{T}}
34
end
4-
Base.getindex(BM::BlockMatrix, i::Integer) = BM.blocks[i]
5+
nblocks(bm::BlockMatrix) = length(bm.blocks)
6+
block(bm::BlockMatrix, i::Integer) = bm.blocks[i]
7+
8+
function Base.size(bm::AbstractBlockMatrix)
9+
n = sum(blk -> Compat.LinearAlgebra.checksquare(block(bm, blk)),
10+
1:nblocks(bm))
11+
return (n, n)
12+
end
13+
function Base.getindex(bm::AbstractBlockMatrix, i::Integer, j::Integer)
14+
(i < 0 || j < 0) && throw(BoundsError(i, j))
15+
for k in 1:nblocks(bm)
16+
blk = block(bm, k)
17+
n = size(blk, 1)
18+
if i <= n && j <= n
19+
return blk[i, j]
20+
elseif i <= n || j <= n
21+
return 0
22+
else
23+
i -= n
24+
j -= n
25+
end
26+
end
27+
i, j = (i, j) .+ size(bm)
28+
throw(BoundsError(i, j))
29+
end
30+
Base.getindex(A::AbstractBlockMatrix, I::Tuple) = getindex(A, I...)
31+
532
mutable struct MockSDOptimizer{T} <: AbstractSDOptimizer
633
nconstrs::Int
734
blkdims::Vector{Int}
@@ -76,9 +103,9 @@ gety(mock::MockSDOptimizer) = mock.y
76103
function getprimalobjectivevalue(mock::MockSDOptimizer{T}) where T
77104
v = zero(T)
78105
for (α, blk, i, j) in mock.objective_coefficients
79-
v += α * mock.X[blk][i, j]
106+
v += α * block(mock.X, blk)[i, j]
80107
if i != j
81-
v += α * mock.X[blk][j, i]
108+
v += α * block(mock.X, blk)[j, i]
82109
end
83110
end
84111
v
@@ -174,17 +201,17 @@ function MOIU.mock_condual!(mock::MockSDOptimizer{T}, y::Vector{T}) where T
174201
# Infeasibility certificate is a ray so we only take the homogeneous part
175202
# FIXME:check that this is also done IN MOIU.MockOptimizer
176203
for (α, blk, i, j) in mock.objective_coefficients
177-
mock.Z[blk][i, j] -= α
204+
block(mock.Z, blk)[i, j] -= α
178205
if i != j
179-
mock.Z[blk][j, i] -= α
206+
block(mock.Z, blk)[j, i] -= α
180207
end
181208
end
182209
end
183210
for (c, constraint_coefficients) in enumerate(mock.constraint_coefficients)
184211
for (α, blk, i, j) in constraint_coefficients
185-
mock.Z[blk][i, j] += α * y[c]
212+
block(mock.Z, blk)[i, j] += α * y[c]
186213
if i != j
187-
mock.Z[blk][j, i] += α * y[c]
214+
block(mock.Z, blk)[j, i] += α * y[c]
188215
end
189216
end
190217
end

0 commit comments

Comments
 (0)