解析gurobi约束中的变量

Parse the variables in constraints in gurobi

创建 LP 模型后,我想解析约束以获取一些约束变量信息

例如

我想找出哪些约束使用了特定变量。

if I want to search for variable 'x' and the constraints used in lp are the following
c0: x + y <= 2
c1: x + z <= 5
c2: y + z <= 10

I should get c0 and c1 as the constraints that use x.

另一个原因是我想找出特定约束使用的变量

if constraint is  c0: x + y + z <= 2

I want to return variables x, y and z as the variables used in this constraint

我知道我可以在 gurobi 中获取变量及其值,但无法找到与我在此处提出的问题相关的任何信息

您可以通过编程语言来完成此操作。这是 Python 中的一些示例代码:

m = read('mymodel.lp') # or use the model object you created

x = m.getVarByName('x')
col = m.getCol(x)
for i in range(col.size()):
  print("constraint %s, coefficient=%f" % (col.getConstr(i).ConstrName, col.getCoeff(i)))

c0 = m.getConstrByName('c0')
row = m.getRow(c0)
for i in range(row.size()):
  print("variable %s, coefficient=%f" % (row.getVar(i).VarName, row.getCoeff(i)))