'Model.optimizeAsyn()' method in Gurobi12 is not available for Python API.
AnsweredI recently noticed that Gurobi 12 Python API supports asynchronous optimization via the Model.optimizeAsyn()
method. I am hoping to use this method to solve multiple models in parallel on a single machine.
Based on the example from the reference manual, I wrote a small Python script to test if this method is functional. However, the result indicates that this method does not exist.
import time
import gurobipy as gp
from gurobipy import GRB
# Set up a Gurobi environment and model
with gp.Env() as env, gp.Model(env=env) as model:
# Add variables (for example, two decision variables: x and y)
x = model.addVar(name="x", lb=0) # x >= 0
y = model.addVar(name="y", lb=0) # y >= 0
# Set objective: maximize 2x + 3y
model.setObjective(2 * x + 3 * y, GRB.MAXIMIZE)
# Add constraints: x + y <= 10 and x <= 5
model.addConstr(x + y <= 10, "c1")
model.addConstr(x <= 5, "c2")
# Start optimization asynchronously
model.optimizeAsync()
# Monitor optimization progress
while model.Status == GRB.INPROGRESS:
time.sleep(0.1)
print(f"{model.ObjVal=} {model.ObjBound=}")
# Sync model once optimization is done
model.sync()
# Query solution after optimization is complete
if model.Status == GRB.OPTIMAL:
print(f"Optimal solution found: x = {x.X}, y = {y.X}")
else:
print("No optimal solution found.")
Error information:
Traceback (most recent call last):
File "C:\Users\Yang\Py_Projects\Flood\code\async_.py", line 19, in <module>
model.optimizeAsync()
File "src\gurobipy\model.pxi", line 357, in gurobipy.Model.__getattr__
File "src\gurobipy\model.pxi", line 1901, in gurobipy.Model.getAttr
File "src\gurobipy\attrutil.pxi", line 23, in gurobipy.__getattrinfo
AttributeError: 'gurobipy.Model' object has no attribute 'optimizeAsync'
-
Hi Xuanjing,
There is no "model.pxi" file in our v12 source code, so I believe you are not using v12.
You can confirm the version by running:
print("version:", gp.gurobi.version())
Note that the version of Gurobi installed in your Python environment is independent of the full Gurobi installation you have downloaded from our website. The following article may be useful for you, or others reading this post: How do I install Gurobi for Python?
- Riley
0
Please sign in to leave a comment.
Comments
1 comment