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

Use set() instead of [] for var_ids in Solver #28

Open
Prunoideae opened this issue Jul 14, 2024 · 2 comments
Open

Use set() instead of [] for var_ids in Solver #28

Prunoideae opened this issue Jul 14, 2024 · 2 comments

Comments

@Prunoideae
Copy link

def __init__(self, model=None):
self.problem = None
self.var_ids = []
self.constr_ids = []
self.model = model

Will make add_variable to have performance regression if over 40k variables are added into the problem, and adding 200k variables will have 10mins overhead due to the query of a python list is O(n):

image

While replacing it with set() will make it done within a second as the set is O(1) complexity:

image

@cdanielmachado
Copy link
Owner

Thank you, that is very useful feedback!

I'm actually working on refactoring that class (outside the main branch):

https://github.com/cdanielmachado/reframed/compare/solvers

And I just replaced lists with dicts... i wonder how they compare to sets.

Just out of curiosity, which solver are you using ?

@Prunoideae
Copy link
Author

I think dict is the same as set as they both are based on a hash table to query or insert items. Languages like Java don't have a separate implementation of sets, where they are just dict with only keys and empty values.

I was using a fine-tuned Gurobi solver with an academic license. The MILP problem being solved was somehow infeasible due to the presence of a few metabolic models, so I reran the solving for several times to debug and noticed the performance problem.

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

No branches or pull requests

2 participants