@@ -21,41 +21,22 @@ function convertQPData_toCOO(data::QPData{T, S, M1, M2}) where {T, S, M1 <: Abst
21
21
return QPData (data. c0, data. c, HCOO, ACOO)
22
22
end
23
23
24
- function get_QPDataCOO (c0:: T , c:: S , H:: SparseMatrixCSC{T} , A:: AbstractMatrix{T} ) where {T, S}
25
- ncon, nvar = size (A)
26
- tril! (H)
27
- nnzh, Hrows, Hcols, Hvals = nnz (H), findnz (H)...
28
- nnzj, Arows, Acols, Avals = if ncon == 0
29
- 0 , Int[], Int[], S (undef, 0 )
30
- elseif issparse (A)
31
- nnz (A), findnz (A)...
32
- else
33
- I = ((i, j, A[i, j]) for i = 1 : ncon, j = 1 : nvar)
34
- nvar * ncon, getindex .(I, 1 )[:], getindex .(I, 2 )[:], getindex .(I, 3 )[:]
35
- end
36
- data = QPData (
37
- c0,
38
- c,
39
- SparseMatrixCOO (nvar, nvar, Hrows, Hcols, Hvals),
40
- SparseMatrixCOO (ncon, nvar, Arows, Acols, Avals),
41
- )
42
- return data, nnzh, nnzj
43
- end
44
-
45
- get_QPDataCOO (c0:: T , c:: S , H, A:: AbstractMatrix{T} ) where {T, S} =
46
- get_QPDataCOO (c0, c, sparse (H), A)
47
-
48
24
abstract type AbstractQuadraticModel{T, S} <: AbstractNLPModel{T, S} end
49
25
50
26
"""
51
27
qp = QuadraticModel(c, Hrows, Hcols, Hvals; Arows = Arows, Acols = Acols, Avals = Avals,
52
- lcon = lcon, ucon = ucon, lvar = lvar, uvar = uvar)
28
+ lcon = lcon, ucon = ucon, lvar = lvar, uvar = uvar, sortcols = false )
53
29
54
- qp = QuadraticModel(c, H; A = A, lcon = lcon, ucon = ucon, lvar = lvar, uvar = uvar)
30
+ qp = QuadraticModel(c, H; A = A, lcon = lcon, ucon = ucon, lvar = lvar, uvar = uvar, coo_matrices = true )
55
31
56
32
Create a Quadratic model ``min ~\\ tfrac{1}{2} x^T Q x + c^T x + c_0`` with optional bounds
57
33
`lvar ≦ x ≦ uvar` and optional linear constraints `lcon ≦ Ax ≦ ucon`.
58
34
35
+ With the first constructor, if `sortcols = true`, then `Hcols` and `Acols` are sorted in ascending order
36
+ (`Hrows`, `Hvals` and `Arows`, `Avals` are then sorted accordingly).
37
+ With the second constructor, if `coo_matrices = true`, `H` and/or `A` will be converted to SparseMatricesCOO
38
+ (this will be ignored if they already are SparseMatricesCOO).
39
+
59
40
You can also use [`QPSReader.jl`](https://github.com/JuliaSmoothOptimizers/QPSReader.jl) to
60
41
create a Quadratic model from a QPS file:
61
42
@@ -285,20 +266,10 @@ function NLPModels.hess_structure!(
285
266
rows:: AbstractVector{<:Integer} ,
286
267
cols:: AbstractVector{<:Integer} ,
287
268
)
288
- if typeof (qp. data. H) <: SparseMatrixCOO
289
- rows .= qp. data. H. rows
290
- cols .= qp. data. H. cols
291
- else
292
- nvar = qp. meta. nvar
293
- idx = 1
294
- for j = 1 : nvar
295
- for i = j: nvar
296
- rows[idx] = i
297
- cols[idx] = j
298
- idx += 1
299
- end
300
- end
301
- end
269
+ typeof (qp. data. H) <: SparseMatrixCOO ||
270
+ error (" hess_structure! should be used only if H is a SparseMatrixCOO" )
271
+ rows .= qp. data. H. rows
272
+ cols .= qp. data. H. cols
302
273
return rows, cols
303
274
end
304
275
@@ -308,19 +279,10 @@ function NLPModels.hess_coord!(
308
279
vals:: AbstractVector{T} ;
309
280
obj_weight:: Real = one (eltype (x)),
310
281
) where {T}
282
+ typeof (qp. data. H) <: SparseMatrixCOO ||
283
+ error (" hess_coord! should be used only if H is a SparseMatrixCOO" )
311
284
NLPModels. increment! (qp, :neval_hess )
312
- if typeof (qp. data. H) <: SparseMatrixCOO
313
- vals .= obj_weight * qp. data. H. vals
314
- else
315
- nvar = qp. meta. nvar
316
- idx = 1
317
- for j = 1 : nvar
318
- for i = j: nvar
319
- vals[idx] = (i ≥ j) ? obj_weight * qp. data. H[i, j] : zero (T)
320
- idx += 1
321
- end
322
- end
323
- end
285
+ vals .= obj_weight * qp. data. H. vals
324
286
return vals
325
287
end
326
288
@@ -337,28 +299,18 @@ function NLPModels.jac_structure!(
337
299
rows:: AbstractVector{<:Integer} ,
338
300
cols:: AbstractVector{<:Integer} ,
339
301
)
340
- if typeof (qp. data. A) <: SparseMatrixCOO
341
- rows .= qp. data. A. rows
342
- cols .= qp. data. A. cols
343
- else
344
- nvar, ncon = qp. meta. nvar, qp. meta. ncon
345
- for j = 1 : nvar
346
- for i = 1 : ncon
347
- rows[i + (j - 1 ) * ncon] = i
348
- cols[i + (j - 1 ) * ncon] = j
349
- end
350
- end
351
- end
302
+ typeof (qp. data. A) <: SparseMatrixCOO ||
303
+ error (" jac_structure! should be used only if A is a SparseMatrixCOO" )
304
+ rows .= qp. data. A. rows
305
+ cols .= qp. data. A. cols
352
306
return rows, cols
353
307
end
354
308
355
309
function NLPModels. jac_coord! (qp:: QuadraticModel , x:: AbstractVector , vals:: AbstractVector )
310
+ typeof (qp. data. A) <: SparseMatrixCOO ||
311
+ error (" jac_coord! should be used only if A is a SparseMatrixCOO" )
356
312
NLPModels. increment! (qp, :neval_jac )
357
- if typeof (qp. data. A) <: SparseMatrixCOO
358
- vals .= qp. data. A. vals
359
- else
360
- vals .= @views qp. data. A[:]
361
- end
313
+ vals .= qp. data. A. vals
362
314
return vals
363
315
end
364
316
0 commit comments