首页 > 解决方案 > 使用 Pandas 将 Excel Solver 转换为 Python

问题描述

我在 Excel Solver 中构建了一个示例,只是为了演示我希望从 Python 开始的内容。我尝试在 Python 中使用 PuLP,但一直出现错误。我试图最小化 df['AP'] 和输入变量的差异,或者只是最大化利润。

Excel 求解器

idx = df.index

#Define Inclusion Variable as Boolean
x1 = LpVariable.dicts("idx", idx, lowBound=-1, upBound = 1)

#Objective
prob += sum([x1[l] for l in idx]), 'Max of Profit'
#prob += lpSum([costs[i]*ingredient_vars[i] for i in Ingredients]), "Total Cost of Ingredients per can"

#Constraints
prob += lpSum([df['CalcVar'][l] for l in idx]) <= curDy + 0.01, "CalcVar"
prob += lpSum([df['CalcVar'][l] for l in idx]) >= curDy - 0.01, "CalcVar"

#Wrap up & Solve
LpSolverDefault.msg = 1
prob.writeLP('LaneOpt.lp')
prob.solve()

错误

Traceback (most recent call last):
  File "solver.py", line 84, in <module>
    prob.solve()
  File "PATH", line 1671, in solve
    status = solver.actualSolve(self, **kwargs)
  File "PATH", line 1362, in actualSolve
    return self.solve_CBC(lp, **kwargs)
  File "PATH", line 1427, in solve_CBC
    raise PulpSolverError("Pulp: Error while executing "+self.path)
pulp.solvers.PulpSolverError: Pulp: Error while executing ...\cbc.exe

标签: pythonpandaspulp

解决方案


推荐阅读