Efficient alternative to model.getCoeff
回答済みI'm using the Gurobi Python interface to analyze an LP file. For this purpose, I need to know which non-zero coefficients a given linear constraint has. The only possibility that I've found is through inserting all variables into the model.getCoeff function and filtering out coefficients that are zero. This has a complexity proportional to the number of variables of the model, independent of the number of nonzeros in the constraint itself, which is orders of magnitude lower.
Is there a more efficient alternative to this approach? This limitation is severely limiting the scalability of my code.
-
正式なコメント
This post is more than three 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 why not try our AI Gurobot?. -
Hi Georg,
You could do something like this:
rows = [model.getRow(c) for c in model.getConstrs()]
for row in rows:
coeffs = {row.getVar(i).varname: row.getCoeff(i) for i in range(row.size())}coeffs
{'x1': -1.0, 'x10': 1.0}You might also find model.getA() interesting.
Cheers,
Matthias0 -
That's about what I was looking for - thanks a ton!
I would suggest some API to get the LinExpr for a constraint on the constraint object itself, though; I expected to be able to access the LHS in the same spot as the RHS and the sense. Gurobi's API generally shines by its object-orientedness, which is somewhat broken in this spot.
0
投稿コメントは受け付けていません。
コメント
3件のコメント