From bb59362571a6ba49873a8b4062b6ea6af78ef075 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Fri, 8 Aug 2025 15:30:01 -0500 Subject: [PATCH] compute_tableau: Compute directly in compressed row format --- src/Cuts/tableau/tableau.jl | 83 ++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/src/Cuts/tableau/tableau.jl b/src/Cuts/tableau/tableau.jl index 4e23721..b86cb79 100644 --- a/src/Cuts/tableau/tableau.jl +++ b/src/Cuts/tableau/tableau.jl @@ -74,15 +74,15 @@ function compute_tableau( factor = klu(sparse(lhs_b')) end - @timeit "Initialize sparse arrays" begin - tableau_rhs::Array{Float64} = zeros(length(rows)) - tableau_lhs_I::Array{Int} = Int[] - tableau_lhs_J::Array{Int} = Int[] - tableau_lhs_V::Array{Float64} = Float64[] - estimated_nnz::Int = round(length(rows) * ncols * estimated_density) - sizehint!(tableau_lhs_I, estimated_nnz) - sizehint!(tableau_lhs_J, estimated_nnz) - sizehint!(tableau_lhs_V, estimated_nnz) + @timeit "Initialize arrays" begin + num_rows = length(rows) + tableau_rhs::Array{Float64} = zeros(num_rows) + tableau_rowptr::Array{Int} = zeros(Int, num_rows + 1) + tableau_colval::Array{Int} = Int[] + tableau_nzval::Array{Float64} = Float64[] + estimated_nnz::Int = round(num_rows * ncols * estimated_density) + sizehint!(tableau_colval, estimated_nnz) + sizehint!(tableau_nzval, estimated_nnz) e::Array{Float64} = zeros(nrows) sol::Array{Float64} = zeros(nrows) tableau_row::Array{Float64} = zeros(ncols) @@ -90,49 +90,48 @@ function compute_tableau( A = data.constr_lhs' b = data.constr_ub - - for k in eachindex(rows) - @timeit "Solve" begin - fill!(e, 0.0) - e[rows[k]] = 1.0 - ldiv!(sol, factor, e) - end - - @timeit "Compute row" begin - mul!(tableau_row, A, sol) - tableau_rhs[k] = dot(sol, b) - end - - needed_space = length(tableau_lhs_I) + ncols - if needed_space > estimated_nnz - @timeit "Grow arrays" begin - estimated_nnz *= 2 - sizehint!(tableau_lhs_I, estimated_nnz) - sizehint!(tableau_lhs_J, estimated_nnz) - sizehint!(tableau_lhs_V, estimated_nnz) + tableau_rowptr[1] = 1 + + @timeit "Process rows" begin + for k in eachindex(rows) + @timeit "Solve" begin + fill!(e, 0.0) + e[rows[k]] = 1.0 + ldiv!(sol, factor, e) end - end - - @timeit "Collect nonzeros" begin - for j in 1:ncols - val = tableau_row[j] - if abs(val) > tol - push!(tableau_lhs_I, k) - push!(tableau_lhs_J, j) - push!(tableau_lhs_V, val) + @timeit "Compute row" begin + mul!(tableau_row, A, sol) + tableau_rhs[k] = dot(sol, b) + end + needed_space = length(tableau_colval) + ncols + if needed_space > estimated_nnz + @timeit "Grow arrays" begin + estimated_nnz *= 2 + sizehint!(tableau_colval, estimated_nnz) + sizehint!(tableau_nzval, estimated_nnz) + end + end + @timeit "Collect nonzeros for row" begin + for j in 1:ncols + val = tableau_row[j] + if abs(val) > tol + push!(tableau_colval, j) + push!(tableau_nzval, val) + end end end + tableau_rowptr[k + 1] = length(tableau_colval) + 1 end end @timeit "Shrink arrays" begin - sizehint!(tableau_lhs_I, 0) - sizehint!(tableau_lhs_J, 0) - sizehint!(tableau_lhs_V, 0) + sizehint!(tableau_colval, length(tableau_colval)) + sizehint!(tableau_nzval, length(tableau_nzval)) end @timeit "Build sparse matrix" begin - tableau_lhs = sparse(tableau_lhs_I, tableau_lhs_J, tableau_lhs_V, length(rows), ncols) + tableau_lhs_transposed = SparseMatrixCSC(ncols, num_rows, tableau_rowptr, tableau_colval, tableau_nzval) + tableau_lhs = transpose(tableau_lhs_transposed) end @timeit "Compute tableau objective row" begin