Rough investigation: Open-source solver time and memory penalties
#4
PS - my attempt to include an attachment didn't work. In case useful, here are the contents of the Julia script I used, saved as profile_optimize_lp.jl:

using JuMP
using HiGHS
using SparseArrays
using LinearAlgebra
using DelimitedFiles
import MathOptInterface as MOI
using Base.Filesystem: filesize

#### Constants

const MB = 1024 * 1024 # 1 MB in bytes
const lp_path = "TIMES_kea-v2_1_2.lp"

#### Functions

"""
recreate_matrix_model(m0::Model) -> Model

Recreate a linear programming model from its matrix representation.

# Arguments
- `m0::Model`: Original JuMP model to extract matrix data from.

# Returns
- `Model`: New JuMP model built from the extracted matrix data.

This function extracts matrix data such as constraints and objective coefficients from `m0`
and constructs a new model `m1` using the HiGHS optimizer.
"""
function recreate_matrix_model(m0::Model)
lp_data = try
# Extract matrix data
lp_matrix_data(m0);
catch e
error("Failed to extract matrix data: ", e)
end

# Access the matrix data
A = lp_data.A
b_lower = lp_data.b_lower
b_upper = lp_data.b_upper
x_lower = lp_data.x_lower
x_upper = lp_data.x_upper
c = lp_data.c
c_offset = lp_data.c_offset

# Recreate model from the matrix data
m1 = Model(HiGHS.Optimizer)

# Add variables
@variable(m1, x_lower[i] <= x[i=1Confusedize(A, 2)] <= x_upper[i])

# Add all constraints at once using matrix-vector multiplication
@constraint(m1, b_lower .<= A * x .<= b_upper)

# Set the objective
@objective(m1, Min, dot(c, x))

return m1
end

#### Main

function main()
if !isfile(lp_path)
error("LP file does not exist: ", lp_path)
end

# Load and prepare the model
println("Load LP from disk")
@time m0 = read_from_file(lp_path)
set_optimizer(m0, HiGHS.Optimizer)

println("Regenerate model from its matrix representation")
@time m1 = recreate_matrix_model(m0)

println("Size of LP file on disk: ", filesize(lp_path) / MB, " MB")

println("Memory used by original LP model before solving: ", Base.summarysize(m0) / MB, " MB")
println("Memory used by matrix-form version before solving: ", Base.summarysize(m1) / MB, " MB")

# Solve the model
@time optimize!(m0)
@time optimize!(m1)

println("Memory used by original LP model after solving: ", Base.summarysize(m0) / MB, " MB")
println("Memory used by matrix-form version after solving: ", Base.summarysize(m1) / MB, " MB")

@assert termination_status(m0) == MOI.OPTIMAL
@assert termination_status(m1) == MOI.OPTIMAL
end

main()
Reply


Messages In This Thread
RE: Rough investigation: Open-source solver time and memory penalties - by willcattoneeca - 07-08-2024, 10:43 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)