Skip to content

Commit c2973d6

Browse files
merge QPDataLinop and QPDataDense
1 parent 06aee5c commit c2973d6

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/qpmodel.jl

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@ mutable struct QPDataCOO{T, S} <: AbstractQPData{T, S}
1313
Avals::S
1414
end
1515

16-
mutable struct QPDataDense{T, S, M1 <: AbstractMatrix{T}, M2 <: AbstractMatrix{T}} <:
17-
AbstractQPData{T, S}
16+
isdense(data::QPDataCOO) = false
17+
18+
mutable struct QPData{T, S, M1 <: Union{AbstractMatrix{T}, AbstractLinearOperator{T}},
19+
M2 <: Union{AbstractMatrix{T}, AbstractLinearOperator{T}}} <: AbstractQPData{T, S}
1820
c0::T # constant term in objective
1921
c::S # linear term
2022
H::M1
2123
A::M2
2224
end
2325

24-
mutable struct QPDataLinOp{T, S, L1 <: AbstractLinearOperator{T}, L2 <: AbstractLinearOperator{T}} <: AbstractQPData{T, S}
25-
c0::T
26-
c::S
27-
H::L1
28-
A::L2
29-
end
26+
isdense(data::QPData{T, S, M1, M2}) where {T, S, M1, M2} = (M1 <: DenseMatrix|| M2 <: DenseMatrix) ? true : false
3027

3128
function get_QPDataCOO(c0::T, c ::S, H::SparseMatrixCSC{T}, A::AbstractMatrix{T}) where {T, S}
3229
ncon, nvar = size(A)
@@ -163,13 +160,13 @@ function QuadraticModel(
163160
if typeof(H) <: AbstractLinearOperator # convert A to a LinOp if A is a Matrix?
164161
nnzh = 0
165162
nnzj = 0
166-
data = QPDataLinOp(c0, c, H, A)
163+
data = QPData(c0, c, H, A)
167164
elseif issparse(H)
168165
data, nnzh, nnzj = get_QPDataCOO(c0, c, H, A)
169166
else
170167
nnzh = typeof(H) <: DenseMatrix ? nvar * (nvar + 1) / 2 : nnz(H)
171168
nnzj = nnz(A)
172-
data = QPDataDense(c0, c, H, A)
169+
data = QPData(c0, c, H, A)
173170
end
174171
QuadraticModel(
175172
NLPModelMeta(
@@ -243,7 +240,7 @@ function NLPModels.objgrad!(qp::AbstractQuadraticModel, x::AbstractVector, g::Ab
243240
NLPModels.increment!(qp, :neval_grad)
244241
if typeof(qp.data) <: QPDataCOO
245242
coo_sym_prod!(qp.data.Hrows, qp.data.Hcols, qp.data.Hvals, x, g)
246-
elseif typeof(qp.data) <: QPDataLinOp
243+
elseif typeof(qp.data.H) <: AbstractLinearOperator
247244
mul!(g, qp.data.H, x)
248245
else
249246
mul!(g, Symmetric(qp.data.H, :L), x)
@@ -258,7 +255,7 @@ function NLPModels.obj(qp::AbstractQuadraticModel{T, S}, x::AbstractVector) wher
258255
Hx = fill!(S(undef, qp.meta.nvar), zero(T))
259256
if typeof(qp.data) <: QPDataCOO
260257
coo_sym_prod!(qp.data.Hrows, qp.data.Hcols, qp.data.Hvals, x, Hx)
261-
elseif typeof(qp.data) <: QPDataLinOp
258+
elseif typeof(qp.data.H) <: AbstractLinearOperator
262259
mul!(Hx, qp.data.H, x)
263260
else
264261
mul!(Hx, Symmetric(qp.data.H, :L), x)
@@ -270,7 +267,7 @@ function NLPModels.grad!(qp::AbstractQuadraticModel, x::AbstractVector, g::Abstr
270267
NLPModels.increment!(qp, :neval_grad)
271268
if typeof(qp.data) <: QPDataCOO
272269
coo_sym_prod!(qp.data.Hrows, qp.data.Hcols, qp.data.Hvals, x, g)
273-
elseif typeof(qp.data) <: QPDataLinOp
270+
elseif typeof(qp.data.H) <: AbstractLinearOperator
274271
mul!(g, qp.data.H, x)
275272
else
276273
mul!(g, Symmetric(qp.data.H, :L), x)
@@ -383,7 +380,7 @@ function NLPModels.hprod!(
383380
NLPModels.increment!(qp, :neval_hprod)
384381
if typeof(qp.data) <: QPDataCOO
385382
coo_sym_prod!(qp.data.Hrows, qp.data.Hcols, qp.data.Hvals, v, Hv)
386-
elseif typeof(qp.data) <: QPDataLinOp
383+
elseif typeof(qp.data.H) <: AbstractLinearOperator
387384
mul!(Hv, qp.data.H, v)
388385
else
389386
mul!(Hv, Symmetric(qp.data.H, :L), v)
@@ -479,8 +476,8 @@ function opPermutedMinusOnes(T::DataType, ncon::Int, ns::Int, p::Vector{Int})
479476
return LinearOperator(T, ncon, ns, false, false, prod!, tprod!)
480477
end
481478

482-
function slackdata(data::QPDataLinOp{T}, meta::NLPModelMeta{T}, ns::Int) where {T}
483-
return QPDataLinOp(
479+
function slackdata(data::QPData{T}, meta::NLPModelMeta{T}, ns::Int) where {T}
480+
return QPData(
484481
copy(data.c0),
485482
[data.c; fill!(similar(data.c, ns), zero(T))],
486483
BlockDiagonalOperator(data.H, opZeros(T, ns, ns)),
@@ -494,7 +491,7 @@ function NLPModelsModifiers.SlackModel(qp::AbstractQuadraticModel, name = qp.met
494491
ns = qp.meta.ncon - nfix
495492
T = eltype(qp.data.c)
496493

497-
if typeof(qp.data) <: QPDataDense # convert to QPDataCOO first
494+
if isdense(qp.data) # convert to QPDataCOO first
498495
dataCOO, nnzj, nnzh = get_QPDataCOO(qp.data.c0, qp.data.c, qp.data.H, qp.data.A)
499496
data = slackdata(dataCOO, qp.meta, ns)
500497
else

0 commit comments

Comments
 (0)