Running a loaded Model with New Data
AnsweredHello,
I have been trying to figure out how to search from this question in the forums and I have not been able to phrase it correctly to find an already posted solution so I apologize if this has been answered.
We are building a very small model ~100kB. The goal is the build this model once and store it on disk and when it is needed call up the stored model and update the data values that are used to build the objective functions and the constraint functions.
What I have seen is the if I load the model and attempt to run it the result is that it solves for the values that were present when the model was first built. As a result, I have been trying to call the variables once the model is loaded using getVars() in Python to rebuild the objective function but the data type is “None”. I have then tried removing the variables from the model and re-adding them so that they variables can be used in the objective function but this results in removing and re-adding nearly half of the variables.
Then the attempt to load the new dataset into the constraint function I use the chgCoeff function to update the data values in all of the various coefficients.
The end result is that I still have not gotten this approach to work and solve for the new data but the code I have written so far to modify the model has ended up being longer than the code used the buidl the model in the first place.
Is there an easier to modify the input data for a prebuilt model or would I simply be better served by building a new model with the current dataset every time I need to run the solver?
-
Hi Ryan,
I assume that the time required to build a model for a given dataset is relatively small. Therefore, it would be better to re-write your script so that it takes the data as input and returns a Gurobi model object as output. This way, you can easily run the script with different inputs.
I'm not entirely sure what you mean by “storing the model to disk.” If you're referring to saving the model, you can write it to a file using the MPS format. Later, you can load the model back and use various API methods—such as
getVars(),getConstrs(), andchgCoeff()—to inspect or modify the model as needed.Hope this answers your question.
Best regards,
Maliheh
0 -
Hello Maliheh
Thank you for the quick response in support.
Yes, the time to build the model is less than a second.
We are using the model in a cloud architecture. We had planned on building the model and writing it to a .mps file using the write() command. This is then stored in the cloud and called in our python script using read(). This imports the prebuilt model. My attempts to modify it using those API methods is about 50 lines of code and so far have been unsuccessful and I imagine it will take more code to get it to work.
That said the code used to build it is also about 50 lines.
Are there any speed or other operational benefits to modifying a prebuilt model vs building a new one? This model will have its structure tailored to different applications and so we will likely have many active models/model building scripts that we will need to maintain. These will be called hundreds to thousands of times a day.
Thank you for the support.
0 -
Are there any speed or other operational benefits to modifying a prebuilt model vs building a new one?
This depends on how many changes are needed to derive the modified model from the existing one. If only a small number of coefficients need to be updated, it may make sense to reload the pre-built model and apply the changes. However, if most or all of the coefficients must be replaced, it’s more efficient to rebuild the model from scratch—especially since the build time is typically less than a second.
This model will have its structure tailored to different applications and so we will likely have many active models/model building scripts that we will need to maintain. These will be called hundreds to thousands of times a day.
I'm not sure why you would need to write separate scripts for models that differ only in the values of their coefficients. If the underlying mathematical formulation changes, then yes, a new function to build the model would be necessary. Otherwise, you can use the same function and simply supply different input data.
Best regards,
Maliheh
0
Please sign in to leave a comment.
Comments
3 comments