python - 将 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)
以上工作正常,但不知道如何在纸浆中设置相同?到目前为止我的尝试:
# 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']))), 0, [i for i in list(df['bounds_max'])])
# the equivalent to 'constant' above - minimize the sum of 'x1, x2, ... 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()
从倒数第二行收到错误消息“TypeError: can't multiply sequence by non-int of type 'list'”。此外,上限的列表推导不像 linprog 那样工作。(TypeError:必须是实数,而不是生成器)
有谁知道正确的设置吗?
解决方案
推荐阅读
- python - pygame.quit()、quit() 或 run = False?
- google-analytics - 如何从 Bigquery 中的 Google 分析导出中查询智能目标?
- javascript - 未捕获的类型错误:sel.add 不是函数
- python - Windows 10 上未解决的导入“MySQLdb”
- php - htaccess https://example.com/dash/something 链接重定向问题
- android - Flutter 应用无法在某些设备上运行
- json - 不使用序列化程序的 crud 操作
- python - 使用 KivyMD 构建接口时如何修复“AttributeError: 'NoneType' object has no attribute 'rgba'”?
- java - 如何在 toString() 方法中返回 2 前导 0?
- xslt - 序列是如何拼接的,为什么我的变量值是文档节点?