Is it possible to load .sol files from a buffer-like object (e.g. io.StringIO) instead of a file?
AnsweredHi,
Due to production constraints, my application is limited in terms of I/O operations when working in production. As a result, I want to do something like this (pseudocode).
import io
from gurobipy import Model
buffer = io.StringIO()
buffer.write("my solution contents…") # Write actual solution contents here, in the same format as a .sol file.
model = Model("my_model")
model.read(buffer) # This doesn't work.However, I find it hard to make this setup work. Is there a way to read a solution from a non-file object, but from a buffer(-like) object?
Thanks!
Sam
-
Hi Sam,
There is no direct way for Gurobi to read a buffer. However, there are some workarounds without I/O operations that give you similar behavior.
If your problem is an LP, then you can give Gurobi a solution as a warm start. In your case, that would mean using the PStart variable attribute to assign a (primal) solution to your variables and setting the Method parameter to 0. Gurobi will then use your solution values to find a starting basis for the primal simplex and continue solving the problem from there. If you don't set the Method parameter to 0, then Gurobi also needs matching start values for the constraints, which are assignable via the DStart constraint attribute. (FYI, if you do an LP warm start, you need to set the parameter LPWarmStart to 2 if you want presolve enabled.)
If your problem is a MIP, then you can give Gurobi an initial feasible solution as a MIP Start. This is done by assigning the solution values to the variables via the variable attribute Start. For MIPs, this should give you the same behavior as reading in the solution file.
I hope this helped answer your question.
Best regards,
Martin
0 -
Hi Martin,
Thanks for the prompt reply. Thing is: I want to actually use the .SOL file format (yet in a buffer-like object) to set the warm start directly. From your response, it seems like I'll have to build a workaround parsing the .SOL file with a customer parser and setting the `Start` attribute “manually”.
One more similar question: is it possible to get the Gurobi solution from an optimized model without writing it to a file (but instead to get it as an in-memory buffer/string)?
Thanks again!
0 -
Hi Sam,
Exactly. If you want it based on a buffer, you'd need a custom parser and use the Start attribute.
Pretty much the same is true if you want to write a solution to a buffer/string. Gurobi does not provide its own method to do so directly. Still, you can use the variable attributes VarName and X to extract the name and value for each variable separately and then add them yourself to a buffer/string. You can also extract all variable names/values at once via the following two calls:
- model.getAttr(GRB.Attr.VarName, model.getVars())
- model.getAttr(GRB.Attr.X, model.getVars())
Best regards,
Martin
0 -
Thanks, Martin! That's helpful.
Cheers,
Sam
0
Please sign in to leave a comment.
Comments
4 comments