From ed448a72c2a0b138555d2a0cf6a2fe81c42f703f Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 22 Jul 2024 13:14:13 +1200 Subject: [PATCH 1/3] Fix performace issue in _canonical_quadratic_reduction --- src/MOI_wrapper.jl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index 1bb4fef..c4e5055 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -18,8 +18,12 @@ 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 = copy(f) + f = MOI.Utilities.canonicalize!(f) + end + I = Cint[term.variable_1.value - 1 for term in f.quadratic_terms] + J = Cint[term.variable_2.value - 1 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] @@ -28,9 +32,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 From 44091b20d20ccdb8b5afe5c86e6ac3036125643a Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 22 Jul 2024 13:56:36 +1200 Subject: [PATCH 2/3] Update --- Project.toml | 4 ++-- src/KNITRO.jl | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index c2cb013..fff14aa 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -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"] diff --git a/src/KNITRO.jl b/src/KNITRO.jl index fa335a1..baa4b35 100644 --- a/src/KNITRO.jl +++ b/src/KNITRO.jl @@ -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) From 6765ee6314c383a79e96952010cc30d0b0f6f3c8 Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 22 Jul 2024 15:16:10 +1200 Subject: [PATCH 3/3] Update --- src/MOI_wrapper.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index c4e5055..4f40317 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -19,11 +19,10 @@ const _SETS = Union{ function _canonical_quadratic_reduction(f::MOI.ScalarQuadraticFunction) if !MOI.Utilities.is_canonical(f) - f = copy(f) - f = MOI.Utilities.canonicalize!(f) + f = MOI.Utilities.canonicalize!(copy(f)) end - I = Cint[term.variable_1.value - 1 for term in f.quadratic_terms] - J = Cint[term.variable_2.value - 1 for term in f.quadratic_terms] + 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]