Pass a gurobi model from c++ to python
Awaiting user inputIs it possible to pass the Gurobi model from c++ to python by reference or something like that, instead of writing the model to mps in c++ and reading the file in python?
I have a project of which the code is mostly written in python. It has a complex data preprocessing process and intensive time requirement. So I cannot rewrite all the data preprocessing in c++ due to enomerous work and I need to use the Gurobi c++ api to accelerate the model building process.
I have re-written the model building code in c++ and achieved some speed improvement over python. But I need to write the model into mps and read it back from python, which makes my speedup less significant.
-
I am not aware of a good way to passing Python objects to C++.
I have re-written the model building code in c++ and achieved some speed improvement over python. But I need to write the model into mps and read it back from python, which makes my speedup less significant.
Instead of constructing the model in Python and writing it to an MPS file, you could write only the most necessary information about the model after performing the extensive preprocessing in Python. For example, it would be enough to write a variable file which holds the lower/upper bounds, types, objective coefficients, and names for each variable. Another file would hold the \(A\) matrix entries together with constraint senses and right-hand sides. You could even write all necessary data into only 1 file to save on the reading. Then, you can read the model data into your C++ model construction code.
0 -
Sorry, my work procedure is:
1. Data preprocessing in python
2. Model construction in c++
3. Further process in python
From step 1 to step 2, I use pybind11 to pass necessary data to c++. But after model construction I cannot pass the constructed gurobi model back to python quickly, i.e., from step 2 to step 3. I need to write gurobi model to mps in c++ and read the mps file back in python.
Is there a better way to let python get the gurobi model more quickly?
0 -
Is there a better way to let python get the gurobi model more quickly?
Not that I am aware off.
I think constructing the model directly in Python would be best. Could you try to identify in your code the most consuming parts of model construction in Python and share a minimal reproducible example? Maybe we could figure out a better way to construct the model directly in Python instead of switching to C++. Usually, the speed difference in model construction should not be too dramatic between the two languages.
0 -
I thought the same at first, but in my test using c++ api is twice faster than python api (model construction time down from 100s to 50s).
Sorry, I can't provide the example since the model is confidential.
0 -
Sorry, I can't provide the example since the model is confidential.
This makes the situation difficult.
You could try finding out which parts of the Python model construction code are the slowest. You can use the time package for this task.
import time
[...]
start = time.time()
# execute some model construction code
end = time.time()
print("The above segment took %f seconds"%(end-start))You could then isolate the problematic model construction parts, anonymize them and try to post a small minimal reproducible example here isolating the issue. We could then try to figure out how to improve.
0
Please sign in to leave a comment.
Comments
5 comments