In some situations, especially when dealing with very large models, we can observe a significant increase in memory consumption when retrieving solution attributes after optimization in Python. The memory increase can be much larger than the size of the corresponding attribute array.
This effect can be more visible if the model is not solved locally but on a remote server (Gurobi Instant Cloud or Compute Server), when much less memory is consumed on the client machine.
The reason for this memory increase is that some Python objects for handling the model must first be created to store the queried attributes. Those Python objects might not have existed before, for example, if the model is read from a file and solved without any further modifications:
import gurobipy as gp
model = gp.read("model.mps")
model.optimize()
# Python objects are created before solution values can be retrieved
values = model.getAttr("X", model.getVars())
Third-party optimization libraries often work this way: They create the model in their own data structures, write it to a file in a standardized modeling format, and read it in again with solver-specific APIs.
If one is interested only in the solution information, there are workarounds to avoid the delayed creation of the Python objects, as discussed in the following.
Write solution to a file
Instead of storing the solution in Python objects, the solution values can be written directly to a file with:
model.write("solution.sol")
Retrieve solution information in JSON format
Alternatively, solution information can be queried in JSON format with:
json_string = model.getJSONSolution()
The resulting text string needs to be parsed, e.g., with Python package json. The benefit of this method is that the string contains much more information than just the solution values, see the description of the JSON format.
Comments
0 comments
Article is closed for comments.