Skip to content

Commit 7fae23e

Browse files
geoffroylecontedpo
authored andcommitted
sort cols
1 parent 9c58ae9 commit 7fae23e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/qpmodel.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function QuadraticModel(
4848
lvar::S = fill!(S(undef, length(c)), eltype(c)(-Inf)),
4949
uvar::S = fill!(S(undef, length(c)), eltype(c)(Inf)),
5050
c0::T = zero(eltype(c)),
51+
sortcols::Bool = false,
5152
kwargs...,
5253
) where {T, S}
5354
nnzh = length(Hvals)
@@ -66,6 +67,16 @@ function QuadraticModel(
6667
if !(nvar == length(lvar) == length(uvar))
6768
error("The length of c, lvar and uvar must be the same")
6869
end
70+
if sortcols
71+
pH = sortperm(Hcols)
72+
permute!(Hrows, pH)
73+
permute!(Hcols, pH)
74+
permute!(Hvals, pH)
75+
pA = sortperm(Acols)
76+
permute!(Arows, pA)
77+
permute!(Acols, pA)
78+
permute!(Avals, pA)
79+
end
6980
QuadraticModel(
7081
NLPModelMeta(
7182
length(c),

test/runtests.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,34 @@ end
7272
testSM(qp)
7373
end
7474

75+
@testset "sort cols COO" begin
76+
Hrows = [1; 2; 1; 3]
77+
Hcols = [2; 1; 4; 4]
78+
Hvals = [1.0; 2.0; 3.0; 4.0]
79+
Arows = [2; 2; 1; 4]
80+
Acols = [4; 3; 1; 2]
81+
Avals = [-1.0; -2.0; -3.0; -4.0]
82+
c = [-8.0; -3; -3; 2.0]
83+
l = [0.0; 0; 0; 0]
84+
u = [Inf; Inf; Inf; Inf]
85+
qp = QuadraticModel(
86+
c,
87+
Hrows,
88+
Hcols,
89+
Hvals,
90+
Arows = Arows,
91+
Acols = Acols,
92+
Avals = Avals,
93+
lcon = [-3.0; -4.0; 2.0; 1.0],
94+
ucon = [-2.0; Inf; Inf; Inf],
95+
lvar = l,
96+
uvar = u,
97+
c0 = 0.0,
98+
name = "QM1",
99+
sortcols = true,
100+
)
101+
@test issorted(Hcols)
102+
@test issorted(Acols)
103+
end
104+
75105
include("test_presolve.jl")

0 commit comments

Comments
 (0)