-
Notifications
You must be signed in to change notification settings - Fork 15
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
Support for index broadcasting in add_constrs #89
Comments
Hi @taka110811, thanks for raising this. Multi-indexes are supported. You can create a set of constraints with any arbitrary left- and right- hand side series, as long as the indices are aligned. What's missing for your case is broadcasting, i.e. what pandas is doing to align the single-indexed
Does that achieve what you're looking for? Feature request
Side noteIf is really equivalent to which is expressed over a single level index in
This is a better approach since it will create a smaller model that still expresses the same requirement. Even better would be to do this kind of reduction of the input data in pre-processing so that the right-hand side data is already aligned on the correct index. Of course, your request still would be a good addition in the case that the right-hand side involves variables. |
@simonbowly Thank you for the prompt reply. Also, I would like to mention that both implementation methods you provided worked correctly.This might get a bit long, but for my own reference as well, I will document the implementation log of the sample case. Sample Case sample case
Constraints using pandas broadcasting
Constraints using min
Feature requestThank you for suggesting the enhancement regarding the broadcasting functionality.
|
Thanks for adding the complete example! The last example is not what I intended though, sorry for the confusion. I'm proposing that this: # Case 1
# Proposed new feature: broadcast between the left- and right- sides in add_constrs
# (In the current version, this is an error)
constraints = gppd.add_constrs(
model,
(c * y).groupby("i").sum(),
GRB.LESS_EQUAL,
b_multi_index,
name="constr",
) would give the same result you can already get from this, where the broadcasting happens in the subtraction operation: # Case 2
constraints = gppd.add_constrs(
model,
(c * y).groupby("i").sum() - b_multi_index,
GRB.LESS_EQUAL,
0.0,
name="constr",
) i.e. these both create this set of constraints: By contrast, this: # Case 3
constraints = gppd.add_constrs(
model,
(c * y) - b_multi_index,
GRB.LESS_EQUAL,
0.0,
name="constr",
) and this: # Case 4
constraints = gppd.add_constrs(
model,
(c * y),
GRB.LESS_EQUAL,
b_multi_index,
name="constr",
) should already work, but they both model the following: which I don't think is what you are after. We won't change the behaviour for (2), (3) or (4), we would only implement the necessary changes to make (1) give the same result as (2). Note that broadcasting is a pandas concept. In this case it refers to aligning a single-level index with a multi-index, based on the names of the series levels. The left-side entries of I hope that's clear? |
There is a more fundamental issue here is that the original constraint, as mathematically written, has an unclear meaning: The reason this is unclear is that you are stating the constraint for each element When I write mathematical constraints, anything after the "forall" ( Fix your math, and then we can decide if |
Description:
I am working on a problem where I need to create constraints with a right-hand side that has two indices, as shown in the following formulation:
In the current implementation of gurobipy-pandas, I can implement a constraint with a single-indexed right-hand side using the following syntax:
This works for constraints of the form:
However, I am now facing a situation where the right-hand side has two indices, b_{ij} , as shown in the first equation above.
Question:
Is there a way to implement constraints with a two-indexed right-hand side b_{ij} using gurobipy-pandas? If so, could you please provide an example?
If not, would this feature be worth considering as an enhancement to gurobipy-pandas? I believe the ability to handle more complex constraints with two indices on both sides would be a valuable addition for users working on more advanced optimization problems.
Thank you for your consideration.
The text was updated successfully, but these errors were encountered: