37,719
社区成员
发帖
与我相关
我的任务
分享
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]]}}
lPattern = re.compile(r'<gurobi.Var\s+Flow_%s_%s_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>'%(s, d))
[/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:
....
lPattern = re.compile(r'<gurobi.Var\s+Flow_%s_%s_(\d+)_(\d+)\s+\(value\s+(\d+)\.0\)>'%(s, d))
#-*- 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()