2016-12-25 53 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)))