我是Python的新手,我正在尝试使用PuLP编写线性程序。
背景信息:
Z_row_col -表示16x16矩阵的矩阵字典的字典 32x32矩阵
C -Z_row_col的相似尺寸。 C包含零,并且包含零 表示Z_row_col的位置等于零,而1表示 Z_row_col的位置大于零。
Z_row_marg_sum -代表所需行的1x32矢量的1x16矢量 Z_row_col的总和
Z_col_marg_sum -32x1矢量的16x1矢量,表示所需的Z_row_col列总和
线性编程
目标是最大化Z_row_col的总和(所有16x16x32x32元素的总和)
约束
代码
# error acceptance
errac = 0.0001
# define LP problem
prob = pulp.LpProblem('IRIO',pulp.LpMaximize)
# decision variables
x_var = pulp.LpVariable.dicts('flows',
[(row,col,m,k) for row in Z_row_col
for col in Z_row_col
for m in range(len(industry))
for k in range(len(industry))],
lowBound=0,upBound=None,cat = pulp.LpContinuous)
# objective function
prob += pulp.lpSum(x_var[(row,col,m,k)] * c[row][col][m,k] for row in Z_row_col
for col in Z_row_col
for m in range(len(industry))
for k in range(len(industry)))
# constraints
# row sum constraint
for row in Z_row_col:
for m in range(len(industry)):
prob += pulp.lpSum(x_var[(row,col,m,k)] for col in Z_row_col
for k in range(len(industry))) == Z_row_marg_sum[row][m]
# column sum constraint
for col in Z_row_col:
for k in range(len(industry)):
prob += pulp.lpSum(x_var[(row,col,m,k)] for row in Z_row_col
for m in range(len(industry))) == Z_col_marg_sum[col][k]
# non-zero constraint
for row in Z_row_col:
for col in Z_row_col:
for m in range(len(industry)):
for k in range(len(industry)):
prob += Z_row_col[row][col][m,k] >= c[row][col][m,k] * errac
prob.solve()
代码为我提供了“ RuntimeError:超出最大递归深度,以cmp为单位” ,从行约束开始。这是一种预期,因为我有(16x16x32x32 = 262,144)变量。
我是否正确编写了此LP问题?我正确编写代码?您对如何使该LP工作有什么建议?关于更好地表示矩阵/向量有什么建议吗?