如何将图中gurobi解的结果赋值到一个字典dict中

小千儿 2015-04-08 05:14:27


如何将图中gurobi解的结果赋值到一个字典dict中,形式如下:
dict = {(0,18):{ 50: [0,11,13,18],
100:[0,18],
100:[0,9,18]
}
}

用python写,其实存储的就是路径。字典dict的key值对应的是(s,d);
里面的嵌套dict对应的是每条路径及其上的流的大小
...全文
623 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
李察德-泰森 2015-04-10
  • 打赏
  • 举报
回复
引用 9 楼 zhangjie19930729 的回复:
[quote=引用 8 楼 zhangjie19930729 的回复:] [quote=引用 7 楼 selecthis 的回复:] [quote=引用 5 楼 zhangjie19930729 的回复:] [quote=引用 4 楼 zhangjie19930729 的回复:] [quote=引用 3 楼 selecthis 的回复:]

#-*- coding: utf-8 -*-

import re

VALUE1 = 'source 0 -> destination 18 : <gurobi.Var Flow_0_18 (value 250.0)>'
TEXTS = ['<gurobi.Var Flow_0_18_9_18 (value 100.0)>', '<gurobi.Var Flow_0_18_0_11 (value 50.0)>',
         '<gurobi.Var Flow_0_18_11_13 (value 50.0)>', '<gurobi.Var Flow_0_18_0_18 (value 100.0)>',
         '<gurobi.Var Flow_0_18_13_18 (value 50.0)>', '<gurobi.Var Flow_0_18_0_9 (value 100.0)>']

vPattern = re.compile(r'source\s+(\d+)\s+->\s+destination\s+(\d+)')
lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')

def funcs():
    dVal = {}
    cVal = vPattern.findall(VALUE1)
    dVal[int(cVal[0][0]), int(cVal[0][1])] = {}
    currVal = dVal[(int(cVal[0][0]), int(cVal[0][1]))]
    for line in TEXTS:
        lVal = lPattern.findall(line)
        x, y, z = lVal[0]
        bFlg = True
        if not currVal.has_key(int(z)):
            currVal[int(z)] = [[int(x), int(y)]]
        else:
            for item in currVal[int(z)]:
                if int(x) == item[-1]:
                    item.append(int(y))
                    bFlg = False
                elif int(x) == item[0]:
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
                elif int(x) > item[0] and int(x) < item[-1]:
                    item.append(int(x))
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
            else:
                if bFlg:
                    currVal[int(z)].append([int(x), int(y)])
        for item in currVal[int(z)]:
            item.sort()
                
    print dVal

if __name__ == '__main__':
    funcs()
非常感谢!但我还有个问题: lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 当这句里面的Flow_0_18_中的0,18是变量时,该如何改呢,re.compile好像不能加参数的吧 [/quote] lPattern = re.compile(r'<gurobi.Var\s+Flow_' + str(s) + '_' + str(d) + '_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 这样写对不对呢,为什么会出现: dVal[int(cVal[0][0]), int(cVal[0][1])] = {} IndexError: list index out of range[/quote] 试试这样写:

lPattern = re.compile(r'<gurobi.Var\s+Flow_%s_%s_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>'%(s, d))
[/quote] 非常感谢,问题已解决,其实两种写法都对,只是在源代码中加了两行判断语句: if len(cVal) > 0: .... if len(lVal) > 0: ....[/quote] 现在又出现一个问题 : 当gurobi解得结果如上图所示的时候,用这个funcs()解得的结果为: [/quote] 不会啊,我试过了,出来的结果是:
{(1, 18): {25: [[1, 11, 13, 18]]}}
小千儿 2015-04-09
  • 打赏
  • 举报
回复
引用 1 楼 selecthis 的回复:
你不是都已经知道写到字典中是啥样了吗?
不知道具体怎么用for循环赋值,而且 dict = {(0,18):{ 50: [0,11,13,18], 100:[0,18], 100:[0,9,18] } } 这种形式是不可能存在的,因为key值唯一。所以想写成如下形式,但不知道怎么赋值? dict = {(0,18):{ 50: [0,11,13,18], 100:[[0,18],[0,9,18]] } }
李察德-泰森 2015-04-09
  • 打赏
  • 举报
回复
你不是都已经知道写到字典中是啥样了吗?
小千儿 2015-04-09
  • 打赏
  • 举报
回复
引用 8 楼 zhangjie19930729 的回复:
[quote=引用 7 楼 selecthis 的回复:]
[quote=引用 5 楼 zhangjie19930729 的回复:]
[quote=引用 4 楼 zhangjie19930729 的回复:]
[quote=引用 3 楼 selecthis 的回复:]

#-*- coding: utf-8 -*-

import re

VALUE1 = 'source 0 -> destination 18 : <gurobi.Var Flow_0_18 (value 250.0)>'
TEXTS = ['<gurobi.Var Flow_0_18_9_18 (value 100.0)>', '<gurobi.Var Flow_0_18_0_11 (value 50.0)>',
'<gurobi.Var Flow_0_18_11_13 (value 50.0)>', '<gurobi.Var Flow_0_18_0_18 (value 100.0)>',
'<gurobi.Var Flow_0_18_13_18 (value 50.0)>', '<gurobi.Var Flow_0_18_0_9 (value 100.0)>']

vPattern = re.compile(r'source\s+(\d+)\s+->\s+destination\s+(\d+)')
lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')

def funcs():
dVal = {}
cVal = vPattern.findall(VALUE1)
dVal[int(cVal[0][0]), int(cVal[0][1])] = {}
currVal = dVal[(int(cVal[0][0]), int(cVal[0][1]))]
for line in TEXTS:
lVal = lPattern.findall(line)
x, y, z = lVal[0]
bFlg = True
if not currVal.has_key(int(z)):
currVal[int(z)] = [[int(x), int(y)]]
else:
for item in currVal[int(z)]:
if int(x) == item[-1]:
item.append(int(y))
bFlg = False
elif int(x) == item[0]:
if int(y) not in item:
item.append(int(y))
bFlg = False
elif int(x) > item[0] and int(x) < item[-1]:
item.append(int(x))
if int(y) not in item:
item.append(int(y))
bFlg = False
else:
if bFlg:
currVal[int(z)].append([int(x), int(y)])
for item in currVal[int(z)]:
item.sort()

print dVal

if __name__ == '__main__':
funcs()


非常感谢!但我还有个问题:
lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')
当这句里面的Flow_0_18_中的0,18是变量时,该如何改呢,re.compile好像不能加参数的吧
[/quote]


lPattern = re.compile(r'<gurobi.Var\s+Flow_' + str(s) + '_' + str(d) + '_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')
这样写对不对呢,为什么会出现:
dVal[int(cVal[0][0]), int(cVal[0][1])] = {}
IndexError: list index out of range[/quote]
试试这样写:

lPattern = re.compile(r'<gurobi.Var\s+Flow_%s_%s_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>'%(s, d))
[/quote]

非常感谢,问题已解决,其实两种写法都对,只是在源代码中加了两行判断语句:
if len(cVal) > 0:
....
if len(lVal) > 0:
....[/quote]

现在又出现一个问题 :


当gurobi解得结果如上图所示的时候,用这个funcs()解得的结果为:
小千儿 2015-04-09
  • 打赏
  • 举报
回复
引用 7 楼 selecthis 的回复:
[quote=引用 5 楼 zhangjie19930729 的回复:] [quote=引用 4 楼 zhangjie19930729 的回复:] [quote=引用 3 楼 selecthis 的回复:]

#-*- coding: utf-8 -*-

import re

VALUE1 = 'source 0 -> destination 18 : <gurobi.Var Flow_0_18 (value 250.0)>'
TEXTS = ['<gurobi.Var Flow_0_18_9_18 (value 100.0)>', '<gurobi.Var Flow_0_18_0_11 (value 50.0)>',
         '<gurobi.Var Flow_0_18_11_13 (value 50.0)>', '<gurobi.Var Flow_0_18_0_18 (value 100.0)>',
         '<gurobi.Var Flow_0_18_13_18 (value 50.0)>', '<gurobi.Var Flow_0_18_0_9 (value 100.0)>']

vPattern = re.compile(r'source\s+(\d+)\s+->\s+destination\s+(\d+)')
lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')

def funcs():
    dVal = {}
    cVal = vPattern.findall(VALUE1)
    dVal[int(cVal[0][0]), int(cVal[0][1])] = {}
    currVal = dVal[(int(cVal[0][0]), int(cVal[0][1]))]
    for line in TEXTS:
        lVal = lPattern.findall(line)
        x, y, z = lVal[0]
        bFlg = True
        if not currVal.has_key(int(z)):
            currVal[int(z)] = [[int(x), int(y)]]
        else:
            for item in currVal[int(z)]:
                if int(x) == item[-1]:
                    item.append(int(y))
                    bFlg = False
                elif int(x) == item[0]:
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
                elif int(x) > item[0] and int(x) < item[-1]:
                    item.append(int(x))
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
            else:
                if bFlg:
                    currVal[int(z)].append([int(x), int(y)])
        for item in currVal[int(z)]:
            item.sort()
                
    print dVal

if __name__ == '__main__':
    funcs()
非常感谢!但我还有个问题: lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 当这句里面的Flow_0_18_中的0,18是变量时,该如何改呢,re.compile好像不能加参数的吧 [/quote] lPattern = re.compile(r'<gurobi.Var\s+Flow_' + str(s) + '_' + str(d) + '_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 这样写对不对呢,为什么会出现: dVal[int(cVal[0][0]), int(cVal[0][1])] = {} IndexError: list index out of range[/quote] 试试这样写:

lPattern = re.compile(r'<gurobi.Var\s+Flow_%s_%s_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>'%(s, d))
[/quote] 非常感谢,问题已解决,其实两种写法都对,只是在源代码中加了两行判断语句: if len(cVal) > 0: .... if len(lVal) > 0: ....
李察德-泰森 2015-04-09
  • 打赏
  • 举报
回复
引用 5 楼 zhangjie19930729 的回复:
[quote=引用 4 楼 zhangjie19930729 的回复:] [quote=引用 3 楼 selecthis 的回复:]

#-*- coding: utf-8 -*-

import re

VALUE1 = 'source 0 -> destination 18 : <gurobi.Var Flow_0_18 (value 250.0)>'
TEXTS = ['<gurobi.Var Flow_0_18_9_18 (value 100.0)>', '<gurobi.Var Flow_0_18_0_11 (value 50.0)>',
         '<gurobi.Var Flow_0_18_11_13 (value 50.0)>', '<gurobi.Var Flow_0_18_0_18 (value 100.0)>',
         '<gurobi.Var Flow_0_18_13_18 (value 50.0)>', '<gurobi.Var Flow_0_18_0_9 (value 100.0)>']

vPattern = re.compile(r'source\s+(\d+)\s+->\s+destination\s+(\d+)')
lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')

def funcs():
    dVal = {}
    cVal = vPattern.findall(VALUE1)
    dVal[int(cVal[0][0]), int(cVal[0][1])] = {}
    currVal = dVal[(int(cVal[0][0]), int(cVal[0][1]))]
    for line in TEXTS:
        lVal = lPattern.findall(line)
        x, y, z = lVal[0]
        bFlg = True
        if not currVal.has_key(int(z)):
            currVal[int(z)] = [[int(x), int(y)]]
        else:
            for item in currVal[int(z)]:
                if int(x) == item[-1]:
                    item.append(int(y))
                    bFlg = False
                elif int(x) == item[0]:
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
                elif int(x) > item[0] and int(x) < item[-1]:
                    item.append(int(x))
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
            else:
                if bFlg:
                    currVal[int(z)].append([int(x), int(y)])
        for item in currVal[int(z)]:
            item.sort()
                
    print dVal

if __name__ == '__main__':
    funcs()
非常感谢!但我还有个问题: lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 当这句里面的Flow_0_18_中的0,18是变量时,该如何改呢,re.compile好像不能加参数的吧 [/quote] lPattern = re.compile(r'<gurobi.Var\s+Flow_' + str(s) + '_' + str(d) + '_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 这样写对不对呢,为什么会出现: dVal[int(cVal[0][0]), int(cVal[0][1])] = {} IndexError: list index out of range[/quote] 试试这样写:

lPattern = re.compile(r'<gurobi.Var\s+Flow_%s_%s_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>'%(s, d))
ban_gun 2015-04-09
  • 打赏
  • 举报
回复
应该差不多1
小千儿 2015-04-09
  • 打赏
  • 举报
回复
引用 4 楼 zhangjie19930729 的回复:
[quote=引用 3 楼 selecthis 的回复:]

#-*- coding: utf-8 -*-

import re

VALUE1 = 'source 0 -> destination 18 : <gurobi.Var Flow_0_18 (value 250.0)>'
TEXTS = ['<gurobi.Var Flow_0_18_9_18 (value 100.0)>', '<gurobi.Var Flow_0_18_0_11 (value 50.0)>',
         '<gurobi.Var Flow_0_18_11_13 (value 50.0)>', '<gurobi.Var Flow_0_18_0_18 (value 100.0)>',
         '<gurobi.Var Flow_0_18_13_18 (value 50.0)>', '<gurobi.Var Flow_0_18_0_9 (value 100.0)>']

vPattern = re.compile(r'source\s+(\d+)\s+->\s+destination\s+(\d+)')
lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')

def funcs():
    dVal = {}
    cVal = vPattern.findall(VALUE1)
    dVal[int(cVal[0][0]), int(cVal[0][1])] = {}
    currVal = dVal[(int(cVal[0][0]), int(cVal[0][1]))]
    for line in TEXTS:
        lVal = lPattern.findall(line)
        x, y, z = lVal[0]
        bFlg = True
        if not currVal.has_key(int(z)):
            currVal[int(z)] = [[int(x), int(y)]]
        else:
            for item in currVal[int(z)]:
                if int(x) == item[-1]:
                    item.append(int(y))
                    bFlg = False
                elif int(x) == item[0]:
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
                elif int(x) > item[0] and int(x) < item[-1]:
                    item.append(int(x))
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
            else:
                if bFlg:
                    currVal[int(z)].append([int(x), int(y)])
        for item in currVal[int(z)]:
            item.sort()
                
    print dVal

if __name__ == '__main__':
    funcs()
非常感谢!但我还有个问题: lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 当这句里面的Flow_0_18_中的0,18是变量时,该如何改呢,re.compile好像不能加参数的吧 [/quote] lPattern = re.compile(r'<gurobi.Var\s+Flow_' + str(s) + '_' + str(d) + '_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 这样写对不对呢,为什么会出现: dVal[int(cVal[0][0]), int(cVal[0][1])] = {} IndexError: list index out of range
小千儿 2015-04-09
  • 打赏
  • 举报
回复
引用 3 楼 selecthis 的回复:

#-*- coding: utf-8 -*-

import re

VALUE1 = 'source 0 -> destination 18 : <gurobi.Var Flow_0_18 (value 250.0)>'
TEXTS = ['<gurobi.Var Flow_0_18_9_18 (value 100.0)>', '<gurobi.Var Flow_0_18_0_11 (value 50.0)>',
         '<gurobi.Var Flow_0_18_11_13 (value 50.0)>', '<gurobi.Var Flow_0_18_0_18 (value 100.0)>',
         '<gurobi.Var Flow_0_18_13_18 (value 50.0)>', '<gurobi.Var Flow_0_18_0_9 (value 100.0)>']

vPattern = re.compile(r'source\s+(\d+)\s+->\s+destination\s+(\d+)')
lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')

def funcs():
    dVal = {}
    cVal = vPattern.findall(VALUE1)
    dVal[int(cVal[0][0]), int(cVal[0][1])] = {}
    currVal = dVal[(int(cVal[0][0]), int(cVal[0][1]))]
    for line in TEXTS:
        lVal = lPattern.findall(line)
        x, y, z = lVal[0]
        bFlg = True
        if not currVal.has_key(int(z)):
            currVal[int(z)] = [[int(x), int(y)]]
        else:
            for item in currVal[int(z)]:
                if int(x) == item[-1]:
                    item.append(int(y))
                    bFlg = False
                elif int(x) == item[0]:
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
                elif int(x) > item[0] and int(x) < item[-1]:
                    item.append(int(x))
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
            else:
                if bFlg:
                    currVal[int(z)].append([int(x), int(y)])
        for item in currVal[int(z)]:
            item.sort()
                
    print dVal

if __name__ == '__main__':
    funcs()
非常感谢!但我还有个问题: lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>') 当这句里面的Flow_0_18_中的0,18是变量时,该如何改呢,re.compile好像不能加参数的吧
李察德-泰森 2015-04-09
  • 打赏
  • 举报
回复

#-*- coding: utf-8 -*-

import re

VALUE1 = 'source 0 -> destination 18 : <gurobi.Var Flow_0_18 (value 250.0)>'
TEXTS = ['<gurobi.Var Flow_0_18_9_18 (value 100.0)>', '<gurobi.Var Flow_0_18_0_11 (value 50.0)>',
         '<gurobi.Var Flow_0_18_11_13 (value 50.0)>', '<gurobi.Var Flow_0_18_0_18 (value 100.0)>',
         '<gurobi.Var Flow_0_18_13_18 (value 50.0)>', '<gurobi.Var Flow_0_18_0_9 (value 100.0)>']

vPattern = re.compile(r'source\s+(\d+)\s+->\s+destination\s+(\d+)')
lPattern = re.compile(r'<gurobi.Var\s+Flow_0_18_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>')

def funcs():
    dVal = {}
    cVal = vPattern.findall(VALUE1)
    dVal[int(cVal[0][0]), int(cVal[0][1])] = {}
    currVal = dVal[(int(cVal[0][0]), int(cVal[0][1]))]
    for line in TEXTS:
        lVal = lPattern.findall(line)
        x, y, z = lVal[0]
        bFlg = True
        if not currVal.has_key(int(z)):
            currVal[int(z)] = [[int(x), int(y)]]
        else:
            for item in currVal[int(z)]:
                if int(x) == item[-1]:
                    item.append(int(y))
                    bFlg = False
                elif int(x) == item[0]:
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
                elif int(x) > item[0] and int(x) < item[-1]:
                    item.append(int(x))
                    if int(y) not in item:
                        item.append(int(y))
                    bFlg = False
            else:
                if bFlg:
                    currVal[int(z)].append([int(x), int(y)])
        for item in currVal[int(z)]:
            item.sort()
                
    print dVal

if __name__ == '__main__':
    funcs()

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧