Gurobi / gurobipy-pandas

Convenience wrapper for building optimization models from pandas data
https://gurobipy-pandas.readthedocs.io/en/stable/
Apache License 2.0
83 stars 15 forks source link

Potential Issue with gurobipy-pandas constraints with export to MPS #36

Closed rrandall1471 closed 1 year ago

rrandall1471 commented 1 year ago

It looks like there might be an issue in how you're creating the constraint name and how that works with MPS naming conventions. I get the following message when I try to export my model to MPS, so that I can compare it to my original model.

Warning: constraint name "Balance[('y1', 'd1', 'h10', 'AE')]" has a space
Warning: default constraint names used to write mps file

It looks like the code that creates the full constraint name creates spaces after the commas in a multi-index, which is not allowed in MPS format. A second potential issue with exporting to MPS is that the name attribute in pd_add_constrs would probably allow a column name with a space, and while it's fine for the code, no write function of the model will be happy with spaces (or various other symbols) that so at a minimum you'd probably want to provide a warning if you detect spaces in that attribute telling the user that any export of the model is going to have an issue or remove the space internally or throw an error.

simonbowly commented 1 year ago

Yes we need some name cleanup handling or warnings here, similar to what we discussed with datetime formats when we met last. There is a bit to think about but we can have saner defaults for sure.

Can you send a minimal example of where the constraint name occurs? I guess you are creating constraints from a dataframe with a four level index?

rrandall1471 commented 1 year ago

Simon,

I've attached several models that seem to cause problems with exporting:

  1. Constraint Multi-index.ipynb.gz - In an index data value there is a space, this prevents the model from being exported correctly
  2. Space in Data.ipynb.gz - There is a multi-index in the constraint name, this by default has spaces in it in the current gurobipy implementation, whereas vars do not have spaces between the comma and the next index value.
  3. Variable Name with Space.ipynb.gz - This could be a miss in the gurobipy-pandas implementation and something you should probably not allow to begin with. I was playing around with the variable name and gave it one with a space in the name, I tried to use the pandas query notation and surround it with back-ticks but it didn't like that. So you should probably throw an error when someone creates a variable name with a space or determine a method to allow them to use it when creating constraints.
Dr-Irv commented 1 year ago

Another possible solution is that when you auto-create any names, substitute all spaces with an underscore _. We did this with our own internally built naming function.

Dr-Irv commented 1 year ago

I did an example, and if I wrote out an LP file, I get a message about a space in the constraint names, but the resulting LP file has the spaces replaced with underscores. But if I write out an MPS file with the same model, the constraints are renamed to c0, c1, etc.

simonbowly commented 1 year ago

Thank you both for testing this thoroughly!