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

Improve the performance of removing variables from expressions #1319

Merged
merged 1 commit into from
Mar 28, 2023

Conversation

arcondello
Copy link
Member

@arcondello arcondello commented Mar 27, 2023

This provides a pretty significant performance improvement when fixing variables in individual constraints.

import time

import dimod

num_variables = 10_000

cqm = dimod.ConstrainedQuadraticModel()
cqm.add_variables("BINARY", num_variables)
c0 = cqm.add_constraint(((v, 1) for v in range(num_variables)), '==', 1)
expr = cqm.constraints[c0].lhs

t = time.perf_counter()
for v in range(num_variables):
    expr.fix_variable(v, 0)
print(time.perf_counter() - t)

Gives a time of ~0.15s vs 1.37s on 0.12.4 on my system.

The majority of the improvement comes from optimizing remove_variable(). In fact, the C++ fix_variable() is not even called from the Python code. I did test adding a dedicated Cython call rather than using the view, but that doesn't change the performance very much at all since the vast majority of the time is spent on rebuilding the indices.

Additional Context
This is a first step to addressing #1317. But it doesn't actually touch the relevant code (yet).

Copy link
Member

@randomir randomir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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

Successfully merging this pull request may close these issues.

None yet

2 participants