pythongurobi-将双重问题写入文件

pythongurobi-将双重问题写入文件,python,linear-programming,gurobi,Python,Linear Programming,Gurobi,使用gurobipy,我能够使用model.write()编写我的原始问题。我还知道我能够用m.getAttr(“Pi”,m.getConstrs())计算对偶。现在我很好奇,如果不在代码中手动写出对偶模型,是否可以轻松生成LP的对偶并将其写入文件,就像我们将原始问题写入.LP文件一样。以下是我尝试使用的一些玩具代码,请随意测试: import gurobipy as gp import os from gurobipy import GRB def write_to_file(model,

使用
gurobipy
,我能够使用
model.write()
编写我的原始问题。我还知道我能够用
m.getAttr(“Pi”,m.getConstrs())
计算对偶。现在我很好奇,如果不在代码中手动写出对偶模型,是否可以轻松生成LP的对偶并将其写入文件,就像我们将原始问题写入
.LP
文件一样。以下是我尝试使用的一些玩具代码,请随意测试:

import gurobipy as gp
import os
from gurobipy import GRB

def write_to_file(model, filestring):
    directory = os.getcwd()
    filename = directory+filestring
    model.write(filename)


nodes = ['s', 'a', 'b', 'c', 'd', 'e','t']
orig = 's'
dest = 't'

arcs, capacity = gp.multidict({
    ('s', 'a') : 1,
    ('a', 'b') : 3,
    ('b', 'a') : 2,
    ('c', 'e') : 4,
    ('s', 'b') : 4,
    ('a', 'd') : 4,
    ('b', 'd') : 3,
    ('d', 't') : 9,
    ('s', 'c') : 6,
    ('b', 'e') : 1,
    ('e', 't') : 4
})

m = gp.Model("Rec4")

# add our variables
flow = m.addVars(arcs, name = "flow")

# cannot go over capacity
m.addConstrs(
    (flow.sum(i,j) <= capacity[i,j] for i,j in arcs), "cap")

# flow conservation
m.addConstrs(
    (flow.sum(i,'*') - flow.sum('*',i) == 0 for i in nodes if i != orig and i != dest), "conservation")

# maximize flow
obj = (flow.sum('*', dest) - flow.sum(dest, '*'))    
m.setObjective(obj, GRB.MAXIMIZE)

m.optimize()

# print solutions
if m.status == GRB.OPTIMAL:
    solution = m.getAttr('x', flow)
    for i, j in arcs:
        if solution[i, j] > 0:
            print('%s -> %s: %g' % (i, j, solution[i, j]))

write_to_file(m, "//test.lp")

########################################################################
# Dual problem
duals = m.getAttr("Pi", m.getConstrs())
print(duals)

将gurobipy导入为gp
导入操作系统
从古罗比进口GRB
def将_写入_文件(模型、文件字符串):
directory=os.getcwd()
filename=目录+文件字符串
model.write(文件名)
节点=['s','a','b','c','d','e','t']
orig='s'
dest='t'
弧,容量=gp.multidict({
('s','a'):1,
('a','b'):3,
('b','a'):2,
('c','e'):4,
('s','b'):4,
('a','d'):4,
('b','d'):3,
('d','t'):9,
('s','c'):6,
('b','e'):1,
('e','t'):4
})
m=总成模型(“Rec4”)
#添加我们的变量
flow=m.addVars(弧,name=“flow”)
#不能超过容量
m、 addConstrs(
(流量和(i,j)0:
打印(“%s->%s:%g%”(i,j,解决方案[i,j]))
将_写入_文件(m,“//test.lp”)
########################################################################
#对偶问题
duals=m.getAttr(“Pi”,m.getConstrs())
印刷品(双联)

此功能目前在Gurobi中不可用。在将来的版本中可能会写出双重问题