I'm attempting to reformulate an Objective Q matrix in an optimization problem (with quadratic constraints and a quadratic objective function) that I am solving with Gurobi and Python. Gurobi has the option of adding in constraints and objective functions as linear expressions instead of fiddling with matrices so I don't have the original matrix, Gurobi creates it for me via my objective equations and coefficients.
To perform analysis on convexity psd properties of the Objective Q matrix, I need to have the Q (objective matrix) and A (constraint matrix). Does anyone know if there is aa command in the gurobi.py shell that allows me to access/view the Objective Q matrix?
Thank you in advance and feel free to ask for clarification if necessary!
Gurobi Optimizer does not have a simple function to retrieve matrices from a Model
object. However, you can get that data iteratively via LinExpr
and QuadExpr
objects. Here is a simple program that iterates through a linear constraint matrix (A) and prints the coefficients:
from gurobipy import *
m = read('afiro.mps')
for ct in m.getConstrs():
row = m.getRow(ct)
for i in range(row.size()):
print("%s %s %f" % (ct.ConstrName, row.getVar(i).VarName, row.getCoeff(i)))
Adapt this to a QuadExpr
for a quadratic objective or quadratic constraint.
With gurobipy 9.x, you can use the (undocumented) Model.getQ()
method to get the objective Q matrix as scipy.sparse.coo_matrix
:
from gurobipy import read
m = read('mwe_example.mps')
Q = m.getQ()
Qdense = Q.todense()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.