2016-12-25 50 views
0

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

例如,

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

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变量和它们的值,但一直没能找到任何关于我在这里提出的问题

回答

1

你可以通过编程语言来做到这一点。以下是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)))