How to modify gurobi model (coefficients in Constraints and Objective Function) after reading .lp file?

2,670 views
Skip to first unread message

Ajay gopinath

unread,
Dec 27, 2016, 3:45:17 AM12/27/16
to Gurobi Optimization
I have a .lp file. I have used gurobi python to read the .lp file and optimize it. However, I am unable to modify the coefficients of the constraint using the  model.chgCoeff(c0 (what should be entered here if .lp file is read?), x, 2.0) function. I am trying to iterate the same model (which is read from the .lp file) using different values of coefficients. Thank you very much! 

Daniel Espinoza

unread,
Dec 28, 2016, 7:42:40 AM12/28/16
to Gurobi Optimization
Dear Ajay,

You can use all getConstrs getConstrByName functions to get your constraints; and getVars getVarByName to get your variables... from that point onward, just use chgCoeff with the appropriate constraint and variable.

Best,
Daniel

PS: LP files may not preserve variable or constraint order, to ensure consistency in order use MPS files

Ajay gopinath

unread,
Dec 30, 2016, 10:57:11 AM12/30/16
to Gurobi Optimization
Dear Daniel,

Thank you very much for your answer.

Best Regards,
Ajay

Ajay gopinath

unread,
Dec 30, 2016, 10:57:11 AM12/30/16
to Gurobi Optimization

Dear Daniel,

I have written the following, and I still do not get the value of coeff (and cannot change the coeff). 

from gurobipy import *
m=Model("testlpread")
m=read("mpsfiletest.lp")
m.optimize()
print((m.getVarByName('x1').x)) #this works. I get the value of the variable.
print((m.getConstrByName('c1'))) #I get the output as "c1" for this. I am not sure if that is what  I should get.

print(m.getCoeff(('c1')),(('x3').x)) #doesnt work.
print(m.getCoeff(('c1'),('x3'))) #doesnt work.

Please let me know if you are able to do it. Thank you very much for your time!

Best Regards,
Ajay
On Wednesday, 28 December 2016 18:12:40 UTC+5:30, Daniel Espinoza wrote:
mpsfiletest.lp

Daniel Espinoza

unread,
Dec 31, 2016, 10:53:45 AM12/31/16
to Gurobi Optimization
Dear Ajay

You should do something like:

vx1 = m.getVarByName('x1')
vx3 = m.getVarByName('x3')
cc1 = m.getConstrByName('c1')

and then use

m.getCoeff(cc1,vx1) or
m.getCoeff(cc1,vx3)

Note that querying by name can be slow, so either try to avoid it, or do it once (and store the result in a dictionary).

Best
Daniel

Ajay gopinath

unread,
Jan 2, 2017, 12:45:39 AM1/2/17
to Gurobi Optimization
Dear Daniel,

Thank you very much!!

Best Regards,
Ajay
Reply all
Reply to author
Forward
0 new messages