Export standard model matrices (Python)

186 views
Skip to first unread message

arkha

unread,
Aug 7, 2015, 7:49:37 AM8/7/15
to Gurobi Optimization
Hello,

What is the best way to export the matrices (A,b,c) of a MILP standard model
 
min  c'x
s.t. Ax <= b
     x_i \in {0,1} for some i

(or any of its equivalent variants) once this has been created using the Gurobi Python interface?

Thank you in advance.

Renan Garcia

unread,
Aug 7, 2015, 9:31:25 AM8/7/15
to gur...@googlegroups.com
You can use the Model.write() method (see http://www.gurobi.com/documentation/6.0/refman/py_model_write.html) to export to the MPS file format (see http://www.gurobi.com/documentation/6.0/refman/mps_format.html).

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

arkha

unread,
Aug 9, 2015, 6:20:48 AM8/9/15
to Gurobi Optimization
Thank you Renan,

What I meant is to export those matrices as, e.g., lists or numpy arrays so I can directly do computations on them. 

In cvxpy, the equivalent of what I am asking is the get_problem_data() method (see right at the very bottom of http://www.cvxpy.org/en/latest/tutorial/advanced/index.html). 

If I export as mps, I would then have to somehow parse those files to get the matrices/vectors that I need. But I couldn't find a python utility to do that.

Renan Garcia

unread,
Aug 9, 2015, 6:26:14 AM8/9/15
to gur...@googlegroups.com
It's not clear to me what the format of data["A"] is from those docs, but if you want a dense representation, you could use something like:

  A = [[model.getCoeff(constr, var) for var in model.getVars()] for constr in model.getConstrs()]
  b = [constr.rhs for constr in model.getConstrs()]
  c = [var.obj for var in model.getVars()]

If you want a sparse representation of A, take a look at the Model.getRow method (http://www.gurobi.com/documentation/6.0/refman/py_model_getrow.html).
Reply all
Reply to author
Forward
0 new messages