Parse the variables in constraints in gurobi

765 views Asked by At

After creating a LP model, I want to parse the constraints to get some constraint-variable information

For example.

I want to find out what constraints used a specific variable.

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.

Another reason is that I would like to find out what variables a particular constraint is using

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

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

I know I can get the variables and their values in gurobi, but have not been able to find anything regarding the question I have posed here

1

There are 1 answers

0
Greg Glockner On BEST ANSWER

You do this via a programming language. Here is some sample code in 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)))