1

我正在尝试解决 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] 不是 price 的最大值并且 c_0 和 d_0 都等于 0。有谁知道如何确保将动态最大值插入到问题中?谢谢!

4

1 回答 1

3

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

max_revenue = max(revenue)

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

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

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

revenue这适用于:的任何值max_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 是一个不错的选择。

此外,请参阅Bertsimas 的线性优化简介中关于数据拟合(第 19 页)的讨论。

希望这可以帮助。干杯。

于 2015-04-21T12:50:44.260 回答