Skip to content

Commit

Permalink
Fix performance issue in _canonical_quadratic_reduction (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Jul 22, 2024
1 parent 38d473f commit 26e9477
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version = "0.14.2"
[deps]
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
Libdl = "<0.0.1, 1.6"
Expand All @@ -18,7 +17,8 @@ julia = "1.6"

[extras]
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Printf"]
test = ["Printf", "SparseArrays", "Test"]
1 change: 0 additions & 1 deletion src/KNITRO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module KNITRO

import Libdl
import MathOptInterface as MOI
import SparseArrays

const _DEPS_FILE = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
if isfile(_DEPS_FILE)
Expand Down
10 changes: 5 additions & 5 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ const _SETS = Union{
}

function _canonical_quadratic_reduction(f::MOI.ScalarQuadraticFunction)
I = Cint[term.variable_1.value for term in f.quadratic_terms]
J = Cint[term.variable_2.value for term in f.quadratic_terms]
if !MOI.Utilities.is_canonical(f)
f = MOI.Utilities.canonicalize!(copy(f))
end
I = Cint[_c_column(term.variable_1) for term in f.quadratic_terms]
J = Cint[_c_column(term.variable_2) for term in f.quadratic_terms]
V = Cdouble[term.coefficient for term in f.quadratic_terms]
for i in 1:length(V)
if I[i] == J[i]
Expand All @@ -28,9 +31,6 @@ function _canonical_quadratic_reduction(f::MOI.ScalarQuadraticFunction)
I[i], J[i] = J[i], I[i]
end
end
I, J, V = SparseArrays.findnz(SparseArrays.sparse(I, J, V))
I .-= Cint(1)
J .-= Cint(1)
return return length(I), I, J, V
end

Expand Down

0 comments on commit 26e9477

Please sign in to comment.