【发布时间】:2018-01-26 17:22:08
【问题描述】:
我对下面的代码有疑问。在这个问题中,我们试图找到 x(i) 的最大值。需要三个变量才能找到最佳可行的解决方案,我不会在细节上打扰您。定义目标函数后,我们继续定义约束。参数 d_ajs 包含 a、j 和 s 的所有组合的整数值。现在的问题是,将两个变量相乘会返回以下错误:非常数表达式不能相乘。
谁能帮帮我?是什么导致了这个错误,如何解决?
提前致谢!
from pulp import *
prob = LpProblem("Model2", LpMaximize)
# Variables
x = pd.Series(index=b_i.index)
for i in b_i.index:
x[i] = LpVariable("x"+str(i), cat = 'Binary')
y = pd.DataFrame(index=employees, columns=shifts)
for i in employees:
for j in shifts:
y.loc[i][j] = LpVariable("y"+str(i)+","+str(j), cat='Binary')
p = {}
for a in aeroplanes:
p[a] = pd.DataFrame(index=employees, columns=shifts)
for i in employees:
for j in shifts:
p[a].loc[i][j] = LpVariable("p"+str(a)+","+str(i)+","+str(j), cat='Binary')
# Objective
obj = ''
for i in employees:
obj += x[i]
prob += obj
# Constraints
s1 = time.time()
# Enough for the employees for the jobs
for a in aeroplanes:
for j in shifts:
for s in skills:
nr = ""
for i in employees:
nr += y.loc[i][j]*p[a].loc[i][j]
prob += nr >= d_ajs[a].loc[j][s]
【问题讨论】:
-
问题是,两个变量的乘积通常不是线性的,你正在做线性规划。开始here。
-
您要解决的具体优化问题是什么?目标函数是什么?有什么限制?
标签: python variables optimization constraints pulp