How to get all constraints from my model as an output?
Awaiting user inputHi, I am just a beginner in using Gurobi so please excuse me if the answer of my question is so obvious.
Imagine that I wrote a Gurobi model for solving the below problem using python.
I used multiple for loops and if conditions while adding constraints to my model.
My question is how to get the constraints that I defined as an inequalities exactly written in "subject to part" in example below, as an output.
minimize x^2 + x*y + y^2 + y*z + z^2 + 2 x
subject to
x + 2 y + 3 z >= 4
x + y >= 1
x, y, z non-negative
Thanks for your help!
-
You can write the complete model using
model.write("model.lp")
in Python API, although the file will not look exactly the way you want.
But I believe it should not be too difficult to come up with a simple Python script that will convert the .lp file. I am not aware of any off-the-shelf scripts which would format the model to the standard you wish to have.
Best regards
Jonasz0 -
Maybe `rlp` format is what you are looking for.
model.write("model.rlp")
but I am not sure how you can convert from the internal Gurobi variable names to the variable names used in your model. Maybe Jonasz Staszek can shed some light on that part (similar to the `dict` file used in GAMS)
0 -
Generally, I would recommend giving all the variables a custom name to quickly distinguish one from the other. Then the .lp file will contain the user-defined variable names.
Just beware not to use spaces in such custom names - there are chances Gurobi will ignore these and use its defaults instead, especially when writing to .lp files.
.lp files have the advantage over .rlp files that they keep the original variable names (if possible), but you can certainly experiment with other extensions. This article should give you guidance.
Best regards
Jonasz0 -
None of my variable names contain spaces (they do contain "_" though) and the LP file is using the internal variable names instead of user-defined variable names. Are "_"s causing problems?
0 -
I have too little information to tell. I used "_" in my codes and it never caused any trouble.
If you like, you can share a minimal reproducible example of your code and we'll see what could cause the trouble.
Best regards
Jonasz0
Please sign in to leave a comment.
Comments
5 comments