LP: How to get a variable's coefficients in each constraint and store it in an array?

172 views
Skip to first unread message

b Yang4869

unread,
Jul 15, 2017, 4:42:00 PM7/15/17
to Gurobi Optimization
Dear Gurobi Community,

My problem is a LP. I want to get one variable's coefficients from each constraint and store them in an array, namely I just want one column from the coefficient matrix of the LP. Is there any efficient way to do that? I'm using C++ and the version of Gurobi is 7.0.2.

BTW, I don't want skip any coefficient even if it's 0 in that constraint.

Best,

Bo

Tobias Achterberg

unread,
Jul 15, 2017, 4:50:04 PM7/15/17
to gur...@googlegroups.com
You have to get the column using

GRBColumn c = model.getCol(var),

see http://www.gurobi.com/documentation/7.5/refman/cpp_grbmodel_getcol.html

This yields a GRBColumn object. With this you can query the number of non-zero
entries using iterate through the non-zero entries in the column by calling

c.size()

and then you can query the individual non-zero entries with c.getConstr(k) and
c.getCoef(k) with k going from 0 to c.size()-1.

If you want to get a dense representation (i.e., including the zeros) of the
column, then you would just start with a vector of all zeros that has a size
equal to the number of rows in the model, and then you iterate over the non-zero
entries of the column vector as described above, modifying the corresponding
entries in your dense vector while doing so.


Regards,

Tobias
Reply all
Reply to author
Forward
0 new messages