从gurobipy获取矩阵格式的约束 [英] Get constraints in matrix format from gurobipy

查看:323
本文介绍了从gurobipy获取矩阵格式的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用gurobipy编码了我的模型,我想获得约束矩阵和成本向量. 有什么方法可以访问那些?

I coded my model in gurobipy and I want to get the matrix of constraints and vector of cost. Is there any way to access those?

推荐答案

在python API中,没有从Gurobi模型中获取矩阵系数的单个函数,但是您自己编写也不难.

From the python API, there's no single function to get the matrix coefficients from a Gurobi model, but it's not to hard to write one yourself.

拥有变量和约束的列表很方便.如果您在变量m

It is convenient to have lists of your variables and constraints. If you have a gurobi model in variable m

dvars = m.getVars()
constrs = m.getConstrs()

将为您提供变量和约束的列表.然后,您可以使用m.getAttr检索与变量相关的属性.要获取目标函数系数,请查询"Obj"属性

will give you the list of variables and constraints. You can then use m.getAttr to retrieve attributes related to the variables. To obtain the objective function coefficients, you query the 'Obj' attribute

obj_coeffs = m.getAttr('Obj', dvars)

这将为您提供模型中每个变量的目标系数列表.对于约束矩阵,您可能只想存储非零值.我将它们存储在 COOrdinate 格式

This will give you a list of the objective coefficient for each variable in the model. For the constraint matrix, you likely want to store just the nonzeros. I'll just store them in the COOrdinate format

  • 行索引
  • 列索引
  • 系数

在此示例中,具有每个变量和约束对象的索引很方便.我将创建将对象映射到索引的字典

In this example, it is convenient to have the index of each variable and constraint object. I'll just create dictionaries that map the objects to the indices

var_index = {v: i for i, v in enumerate(dvars)}
constr_index= {c: i for i, c in enumerate(constrs)}

constrs列表中的每个约束对象都对应于模型中的约束.每个约束都有一个

Each constraint object in the constrs list corresponds to a constraint in the model. Each constraint has a

  • 左侧表情
  • 感官(< =,==,> =)
  • 右侧常量

对于约束矩阵,只需要左侧.它由 LinExpr 对象表示,您可以通过<模型上的href ="http://www.gurobi.com/documentation/8.1/refman/py_model_getrow.html" rel ="nofollow noreferrer"> getRow 方法.从Gurobi 6.x开始,要获取列索引列表,系数元组,需要以下函数

For the constraint matrix, you need only the left hand side. It is represented by a LinExpr object which you can obtain with the getRow method on the model. As of Gurobi 6.x, obtaining a list of column index, coefficient tuples requires a function like the following

def get_expr_coos(expr, var_indices):
    for i in range(expr.size()):
        dvar = expr.getVar(i)
        yield expr.getCoeff(i), var_indices[dvar]

要获取矩阵,您需要对每个约束应用此函数.

To get the matrix, you need to apply this function for every constraint.

def get_matrix_coos(m):
    dvars = m.getVars()
    constrs = m.getConstrs()
    var_indices = {v: i for i, v in enumerate(dvars)}
    for row_idx, constr in enumerate(constrs):
        for coeff, col_idx in get_expr_coos(m.getRow(constr), var_indices):
            yield row_idx, col_idx, coeff

使用此功能,您可以将矩阵存储到类似熊猫的结构中

Using this function, you can store the matrix into a structure like a pandas dataframe

 nzs = pd.DataFrame(get_matrix_coos(m), 
                    columns=['row_idx', 'col_idx', 'coeff'])

通过这种结构,您可以对非零模式进行基本绘图.使用 miplib aflow40b基准测试问题.

From this structure, you can do a basic plot of the nonzero pattern. Using a problem from the miplib aflow40b benchmark problem.

 import matplotlib.pyplot as plt
 import pandas as pd
 import gurobipy as grb
 m = grb.read("miplib/instances/miplib2010/aflow40b.mps.gz")
 nzs = pd.DataFrame(get_matrix_coos(m), 
                    columns=['row_idx', 'col_idx', 'coeff'])
 plt.scatter(nzs.col_idx, nzs.row_idx, 
        marker='.', lw=0)

这篇关于从gurobipy获取矩阵格式的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆