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

Refactor constraint deletions and GRBupdatemodel #516

Closed
odow opened this issue Aug 16, 2023 · 2 comments
Closed

Refactor constraint deletions and GRBupdatemodel #516

odow opened this issue Aug 16, 2023 · 2 comments
Labels

Comments

@odow
Copy link
Member

odow commented Aug 16, 2023

The MOI wrapper eagerly updates the row indices of constraints in a model upon deletion:

ret = GRBdelconstrs(model, length(rows_to_delete), rows_to_delete)
_require_update(model)
for (_, info) in model.affine_constraint_info
# The trick here is: searchsortedlast returns, in O(log n), the
# last index with a row smaller than info.row, over rows_to_delete
# this is the same as the number of rows deleted before it, and
# how much its value need to be shifted.
info.row -= searchsortedlast(rows_to_delete, info.row - 1)
end
cs_values = sort!(getfield.(cs, :value))
# If the key of an model.affine_constraint_info entry is in cs_values,
# then that entry is deleted.
filter!(model.affine_constraint_info) do pair
return isempty(searchsorted(cs_values, pair.first))
end

We should instead do what we do for variables and lazily delay the update until the next call to GRBupdatemodel:

ret = GRBdelvars(model, length(del_cols), del_cols)
_check_ret(model, ret)
for var_idx in indices
delete!(model.variable_info, var_idx)
end
append!(model.columns_deleted_since_last_update, del_cols .+ 1)

sort!(model.columns_deleted_since_last_update)
for var_info in values(model.variable_info)
# The trick here is: searchsortedlast returns, in O(log n), the
# last index with a column smaller than var_info.column, over
# columns_deleted_since_last_update this is the same as the number
# of columns deleted before it, and how much its value need to be
# shifted.
var_info.column -= searchsortedlast(
model.columns_deleted_since_last_update,
var_info.column,
)
end
model.next_column -= length(model.columns_deleted_since_last_update)
empty!(model.columns_deleted_since_last_update)
ret = GRBupdatemodel(model)

This also explains why we didn't trigger #515 for variable deletion: #515 (comment)

@odow

This comment was marked as off-topic.

@odow
Copy link
Member Author

odow commented Mar 26, 2024

Closed by #552

@odow odow closed this as completed Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

1 participant