【问题标题】:How to multiply two variables in constraints in Python (pulp package, optimization)?如何在 Python 中将约束中的两个变量相乘(纸浆包,优化)?
【发布时间】: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


【解决方案1】:

嗨,Pulp 不支持非线性编程,并且通过将两个变量相乘,您会使问题变成非线性的。

我建议你考虑一下你的问题,看看有没有什么办法可以把它转化为线性问题,或者使用其他支持非线性问题的工具。

【讨论】:

    猜你喜欢
    • 2022-11-17
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多