A bug of setting warm-start basis using gurobipy
回答済みDear Gurobi Support Team,
I am reaching out to you today because I have encountered a bug that I am confused about. I was wondering if you could kindly assist me in finding a solution or provide some guidance on how to approach the issue.
I have the following function for adding warm-start basis for LP solving:
def add_warm_start_basis(self,
basis: Basis) -> None:
for i, var in enumerate(self.model.getVars()):
var.setAttr('VBasis', basis.vbasis[i])
for j, constr in enumerate(self.model.getConstrs()):
constr.setAttr('CBasis', basis.cbasis[j])
self.model.setParam("LPWarmStart", 2) # Make warm-start basis work when conduct presolve.
And Basis class is defined as:
@dataclass
class Basis:
"""Information of: vbasis and cbasis."""
vbasis: np.ndarray
cbasis: np.ndarray
def __post_init__(self):
self.vbasis = self.vbasis.astype(int)
self.cbasis = self.cbasis.astype(int)
So it seems I have made sure vbasis and cbasis in ``basis'' are int (I have also checked using the debug function). However, I got the following error report:
in add_warm_start_basis
var.setAttr('VBasis', basis.vbasis[i])
File "src/gurobipy/var.pxi", line 187, in gurobipy.Var.setAttr
File "src/gurobipy/attrutil.pxi", line 241, in gurobipy.__setattr
AttributeError: Attribute 'VBasis' takes integer value
-
正式なコメント
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Cheng,
Numpy uses its own types, eg
>>> type(np.array([1])[0])
numpy.int64Numpy is not a dependency of gurobipy (unless matrix API is used) and so we cannot check for this type. You can however convert individual numpy ints to standard ints using .item()
>>> type(np.array([1])[0].item())
intbut the best course of action in your example is to convert the numpy arrays to lists and set all vbasis and cbasis information in one step, rather than using the enumeration approach which is slow
model.setAttr("VBasis", model.getVars(), basis.vbasis.tolist())
model.setAttr("CBasis", model.getConstrs(), basis.cbasis.tolist())Depending on your application, it may make sense to convert to lists within your dataclass.
- Riley
1 -
Hi Riley,
Thank you very much for your detailed explanation. It works now!
Best regards,
Cheng
0
投稿コメントは受け付けていません。
コメント
3件のコメント