-
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
Build function to update constraints #79
Comments
Hi @alexlusak, thanks for the feedback! Just to confirm, is the requirement here to modify only the right hand side of existing constraints? This can be done using the attribute accessors. Say you have built a constraint: df_constr = df.gppd.add_constrs(model, "lhs <= rhs", name="c1") you can modify the right hand side of the set of constraints by assigning to the 'RHS' attribute of the constraints as follows: df_constr["c1"].gppd.RHS = new_rhs where |
@simonbowly that was exactly what I was looking for, thanks! I couldn't seem to find anything in the docs on this - if there isn't, perhaps it's something worth adding a small section on? |
Agreed, thanks for pointing that out, there are no concrete example of this in the docs. I'll add some in #80. |
That would be great. In a similar vein, is there similar syntax for updating a constraint coefficient? |
This is a bit tricker. Currently there is no syntax in gurobipy-pandas for this, it has to be done directly in gurobipy using chgCoeff with a handle to the variable and constraint you want to change the coefficient for. So you would need to loop manually over the dataframe to make the changes. I'm happy to consider adding some functionality to do this, but I'm having trouble thinking of how it might look as an API in gurobipy-pandas. The attribute accessor updates just one value per constraint, and the attribute name (in the above case, 'RHS') specifies what data is being updated. For coefficients, you could have an arbitrary number of them in each constraint, so I'm not sure the same API style make sense. Do you have a concrete example? |
Sure, I can provide one example. I have a dataframe with a float, df['c1'].gppd.update_coef('v1', 'price') whereas right now it needs to be something like: for row in df.itertuples():
# access constraint location and update coefficient with price and v1 in the same row so getting some vectorized version of this could be super useful on top of being more legible. To your point though I suppose this only works if there's one coefficient? Though I could imagine a function as such just takes the variable name as its first argument and any successive arguments are all considered to be possible coefficients? And if another variable in that constraint needs updating, can be done as a separate call? |
Got it, so say we have a simple dataframe built like this:
result:
and looking up
To change the coefficients on each 'x' to the corresponding value in 'price2' would just require an itertuples loop as you said:
We could add a function like Extending the idea a little to multiple coefficients per constraint, I think the possibilities would be:
For (2), you could use the same syntax (one update per row) if you construct the right dataframe, e.g.
(A separate call might be cleaner if the code to build the update dataframe looks anything like the one above!) |
Got it, that all makes sense. Probably not the biggest priority given the simplicity of writing an itertuples call (and especially given there's no way to have it vectorized), and I know this is documented in the Advanced Patterns section. |
It would be very useful to add in functionality to have similar syntax to
.gppd.add_constrs(model, 'lhs <= rhs', name='c1')
but instead for re-assignment of a lhs or rhs of an expression.
For example, if models are being built across each day of a year, but the model size is too complex to construct the entire year in one go, constraints can be built on the first day of the year. They would need to be updated as values change across days. Something like the following would be very useful:
.gppd.update_constrs_rhs(model, [CONSTRAINT NAME], [NEW RHS COLUMN NAME])
As it stands right now I believe there would need to be some for loop construction across
itertuples
and manually update each constraint entry.The text was updated successfully, but these errors were encountered: