微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 scipy linprog 中的设置与 PuLP 匹配

如何解决将 scipy linprog 中的设置与 PuLP 匹配

我在 SciPy linprog 中设置了一个相当简单的 LP 模型,但在 PuLP 中设置时遇到问题。

我打算增加 linprog 无法处理的更多复杂性,所以想改为设置它。虽然看起来不像 linprog 那样用户友好!

linprog 设置如下:

import pandas as pd
df = pd.read_excel('df.xlsx')

from scipy.optimize import linprog

#1d n-element array of ones,*-1 to minimize the sum of the variables to solve for: x1,x2 ... xn
constants=np.array(df['Ones'])*(-1)

# equations for the inequality constraints,simply a M x n array
A_ub=df.loc[:,'col_a':'col_n'].to_numpy().transpose()

# array of M zeros as bounds for above inequality constraint
b_ub = np.array((A_ub.shape[0])*[0])

# lower and upper bounds set for the elements
bounds=[(0,x) for x in list(df['bounds_max'])]

# the set up of the equation
main_result=linprog(constants,A_ub=A_ub,b_ub=b_ub,A_eq=None,b_eq=None,bounds=bounds,method='interior-point',callback=None,options=None,x0=None)

以上一切正常,但不确定如何在 PuLP 中设置相同的内容?我到目前为止的尝试:

# importing the package
import pulp as pl

# defining the problem
prob = pl.LpProblem("df",pl.LpMinimize)

# defining the set of variables to solve for like above (x1,x2,... xn) with the same constraints like in 'bounds' above. different 'x's have different upper bounds
x = pl.LpVariable.matrix('x',list(range(len(df['Helper']))),[i for i in list(df['bounds_max'])])

# the equivalent to 'constant' above - minimize the sum of 'x1,... xn'
prob += pl.lpSum(x for j in range(len(list(df['Ones']))))

# the same equations for the inequality constraints
A_ub=df.loc[:,'col_a':'col_a'].to_numpy().transpose()

# here is where it breaks down,the multiplying each 'x' element be each element in each row of the array
prob += pl.lpSum(x*[a for a in A_ub]) <= 0

# Solve the problem
prob.solve()

从倒数第二行收到错误消息“类型错误:不能将序列乘以非整数的‘列表’类型”。此外,上限的列表推导式不像 linprog 那样工作。(类型错误:必须是实数,而不是生成器)

有人知道正确的设置吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。