-
Notifications
You must be signed in to change notification settings - Fork 51
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
New class LinearConstraintsMatrix for better model building performance #239
Comments
I think this is already handled by I think the issue is more that those strategies are not really documented anywhere. |
In addition to what Christian said, please note that it can be very slow to iteratively build sympy expressions by adding terms, for example. That's why we use the aforementioned |
Thanks for pointing this out, I wasn't aware of this feature. I now did some benchmarking with a random sparse matrix
Integrating the whole matrix at once
takes about 0.4 seconds while doing it constraint-wise
takes about 16 seconds. |
Hi, if you look at the profile of the second case:
you see that what makes the difference is not |
When using larger models it is becoming increasingly apparent that rerpresenting each constraint as a SymPy expression incurs a significant performance penalty when setting up such models. Although SymPy expressions are very flexible and can also be used to represent non-linear constraints the vast majority of constraints are typically linear and can often be pooled as matrices.
In order to pass such matrices efficiently to the solvers I suggest to introduce a new class LinearConstraintsMatrix which can be added to models in a similar fashion as Constraint objects but is really just a wrapper for a collection of constraints in the underlying solver object. In particular, a LinearConstraintsMatrix object would implement methods to directly get/set coefficients in the underlying solver object.
A simpler, but less flexible solution, would be to add an abstract method add_lp_matrix to the Model class which can be implemented by the solver-specific interfaces. For CPLEX, this could look like this:
However, it would then not be possible to transparently display, modify or remove these constraints. Nonetheless, it would be a simple method for efficienlty adding a large number of constraints in matrix format to a model.
The text was updated successfully, but these errors were encountered: