如何在PuLP中找到最大概率

时间:2015-04-14 12:49:34

标签: python linear-programming pulp

我正在尝试解决PuLP中的线性问题,以最小化成本函数。成本函数本身是成本函数的最大值的函数,例如,我有每日成本,并且我试图最小化每月成本,即每日成本加上当月的最大每日成本的总和。我不认为我在最终解决方案中捕获函数的最大值,我不确定如何解决此问题。代码的基本概要如下:

# Initialize the problem to be solved
prob = LpProblem("monthly_cost", LpMinimize)

# The number of time steps
# price is a pre-existing array of variable prices
tmax = len(price)
# Time range
time = list(range(tmax))

# Price reduction at every time step
d = LpVariable.dict("d", (time), 0, 5)
# Price increase at every time step 
c = LpVariable.dict("c", (time), 0, 5)

# Define revenues = price increase - price reduction + initial price
revenue = ([(c[t] - d[t] + price[t]) for t in time])
# Find maximum revenue
max_revenue = max(revenue)

# Initialize the problem
prob += sum([revenue[t]*0.0245 for t in time]) + max_revenue

# Solve the problem
prob.solve()

变量max_revenue总是等于c_0 - d_0 + price [0],即使price [0]不是价格的最大值而且c_0和d_0都等于0.有谁知道如何确保动态最大值被插入到问题?谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您无法在PuLP或任何其他标准LP求解器中执行以下操作:

max_revenue = max(revenue)

这是因为确定最大值需要求解器来评估revenue方程;所以在这种情况下,我认为你不能提取标准的LP模型。事实上,这些模型是non-smooth

在这种情况下,您可以轻松地将问题重新表述如下:

max_revenue >= revenue = ([(c[t] - d[t] + price[t]) for t in time])

这适用于revenuemax_revenue >= revenue的任何值。这反过来有助于从方程中提取标准LP模型。因此,原始问题公式随着额外的不等式约束而扩展(等式约束和目标函数应该与之前相同)。所以它看起来像这样(谨慎之处:我没有测试过这个):

# Define variable
max_revenue = LpVariable("Max Revenue", 0)

# Define other variables, revenues, etc.

# Add the inequality constraints
for item in revenue:
    prob += max_revenue >= item

我还建议您查看scipy.optimize.linprog。 PuLP将模型写入中间文件,然后调用已安装的求解器来解决模型。另一方面,在scipy.optimize.linprog中,它们都在python中完成,应该更快。但是,如果使用单纯形算法无法解决您的问题,或者您需要其他专业解算器(例如CPlex,Gurobi等),那么PuLP是一个不错的选择。

另请参阅Introduction to Linear Optimisation by Bertsimas中有关数据拟合(第19页)的讨论。

希望这会有所帮助。欢呼声。