Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numerical Bounds Check is slow #207

Open
jd-lara opened this issue Mar 18, 2024 · 3 comments
Open

Numerical Bounds Check is slow #207

jd-lara opened this issue Mar 18, 2024 · 3 comments

Comments

@jd-lara
Copy link
Contributor

jd-lara commented Mar 18, 2024

Recently I noticed that the numerical checks in PowerSimulations.jl since it takes a long time to run with HiGHS w.r.t to Gurobi or Xpress.

This is the loop we use is something like this

function get_constraint_numerical_bounds(model::OperationModel)
    if !is_built(model)
        error("Model not built, can't calculate constraint numerical bounds")
    end
    bounds = ConstraintBounds()
    for (const_key, constraint_array) in get_constraints(get_optimization_container(model))
        # TODO: handle this at compile and not at run time
        if isa(constraint_array, SparseAxisArray)
            for idx in eachindex(constraint_array)
                constraint_array[idx] == 0.0 && continue
                con_obj = JuMP.constraint_object(constraint_array[idx])
                update_coefficient_bounds(bounds, con_obj, (const_key, idx))
                update_rhs_bounds(bounds, con_obj, (const_key, idx))
            end
        else
            for idx in Iterators.product(constraint_array.axes...)
                !isassigned(constraint_array, idx...) && continue
                con_obj = JuMP.constraint_object(constraint_array[idx...])
                update_coefficient_bounds(bounds, con_obj, (const_key, idx))
                update_rhs_bounds(bounds, con_obj, (const_key, idx))
            end
        end
    end
    return bounds
end
@odow
Copy link
Member

odow commented Mar 19, 2024

To clarify, what type of constraints is this iterating over?

@jd-lara
Copy link
Contributor Author

jd-lara commented Mar 19, 2024

To clarify, what type of constraints is this iterating over?

All supported ones in direct mode. Nothing esoteric

@odow
Copy link
Member

odow commented Apr 18, 2024

I think we decided that this was because it queries the constraint matrix:

HiGHS.jl/src/MOI_wrapper.jl

Lines 1662 to 1708 in 1d192e2

function MOI.get(
model::Optimizer,
::MOI.ConstraintFunction,
c::MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S},
) where {S<:_SCALAR_SETS}
r = row(model, c)
num_row = Ref{HighsInt}(0)
num_nz = Ref{HighsInt}(0)
matrix_start = Ref{HighsInt}(0)
lower, upper = Ref{Cdouble}(), Ref{Cdouble}()
_ = Highs_getRowsByRange(
model,
r,
r,
num_row,
lower,
upper,
num_nz,
matrix_start,
C_NULL,
C_NULL,
)
matrix_index = Vector{HighsInt}(undef, num_nz[])
matrix_value = Vector{Cdouble}(undef, num_nz[])
ret = Highs_getRowsByRange(
model,
r,
r,
num_row,
lower,
upper,
num_nz,
matrix_start,
matrix_index,
matrix_value,
)
_check_ret(ret)
return MOI.ScalarAffineFunction(
MOI.ScalarAffineTerm{Float64}[
MOI.ScalarAffineTerm(
val,
model.variable_info[CleverDicts.LinearIndex(col + 1)].index,
) for (col, val) in zip(matrix_index, matrix_value) if !iszero(val)
],
0.0,
)
end

One option would be to use a cache instead of direct_model, so something like model = Model(HiGHS.Optimizer; add_bridges = false).

I don't think there's an easy way to speed this up in HiGHS.jl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants