Branching priority to MVars
AnsweredHi,
i would like to try setting branching priorities to MVars. The model seems to not complain with both the following codes:
y = m.addMVar(5, vtype=GRB.BINARY)
for v in y:
v.setAttr('BranchPriority', 1)
y = m.addMVar(5, vtype=GRB.BINARY)
y.setAttr('BranchPriority', 1)
for v in y:
v.setAttr('BranchPriority', 1)
print(v.getAttr('BranchPriority'))
GurobiError Traceback (most recent call last)
<ipython-input-16-1b96d765f77f> in <cell line: 1>() 1 for v in y: 2 v.setAttr('BranchPriority', 1) ----> 3 print(v.getAttr('BranchPriority'))
src/gurobipy/mvar.pxi in gurobipy.MVar.getAttr()
src/gurobipy/attrutil.pxi in gurobipy.__gettypedattrlist()
GurobiError: Index 0 out of range for attribute 'BranchPriority'
-
Hi Andrea,
Thank you for reaching us.
Suppose you don't call m.model(), you cannot check your modification on the variables' attributes directly.
Try the below code.for v in y:
v.setAttr('BranchPriority', 1)
m.update()
for v in y:
print(v.getAttr('BranchPriority'))The Gurobi optimizer applies model modification in a lazy fashion, meaning that modifications don't affect the model immediately. The followings are the reasons why the Gurobi optimizer behave like that, and you can find other detail in 'Lazy Updates' section of Python API Overview.
Why does the Gurobi interface behave in this manner? There are a few reasons. The first is that this approach makes it much easier to perform multiple modifications to a model, since the model remains unchanged between modifications. The second is that processing model modifications can be expensive, particularly in a Compute Server environment, where modifications require communication between machines. Thus, it is useful to have visibility into exactly when these modifications are applied. In general, if your program needs to make multiple modifications to the model, you should aim to make them in phases, where you make a set of modifications, then update, then make more modifications, then update again, etc. Updating after each individual modification can be extremely expensive.
If you have further questions, feel free to reach us again.
Best regards,
Chung-Kyun Han0 -
Fantastic!
Thank you very much
Andrea
0
Please sign in to leave a comment.
Comments
2 comments