【问题标题】:Issue with Integer Linear Programming using PuLP使用 PuLP 的整数线性规划问题
【发布时间】:2021-04-23 23:23:43
【问题描述】:

我正在尝试使用 PuLP 解决此 ILP。 问题指出,我们需要尽量减少建造仓库的成本。我们需要根据最低的成本来决定建哪个仓库。这是LP的样子

min ∑transportcost * X_i, ∑fixedcost * Y_j

[其中 i & j 是仓库# = 1/2/3]

约束应该是: X_i & Y_j 应该属于 0 或 1,因为它们是决策变量 X_i == Y_j

这是我写的代码

import pulp
from pulp import *
import pandas as pd
import numpy as np

n_warehouse = 3

warehouse_fixed_costs = [100, 120, 80]
cost_per_km = [3,3,3]
distances = [20, 30, 45]
transport_costs = [cost_per_km[i]*distances[i] for i in range(len(cost_per_km))]
warehouses = ['1','2','3']

model = LpProblem("Supply-Demand", LpMinimize)

transport_warehouse_dv = LpVariable.matrix("X", warehouses, cat = "Binary", lowBound = 0)
transport_warehouse_allocation = np.array(transport_warehouse_dv)

fixed_cost_warehouse_dv = LpVariable.matrix("Y", warehouses, cat = "Binary", lowBound = 0)
fixed_cost_warehouse_allocation = np.array(fixed_cost_warehouse_dv)

obj_func = lpSum(transport_costs*transport_warehouse_allocation)
obj_func += lpSum(warehouse_fixed_costs*fixed_cost_warehouse_allocation)
model += obj_func

const = lpSum(transport_warehouse_allocation) == fixed_cost_warehouse_allocation, "Warehouse decision"
model += const

model

这是创建的模型:

供需:

最小化 60X_1 + 90X_2 + 135X_3 + 100Y_1 + 120Y_2 + 80Y_3 + 0

受 Warehouse_decision 的影响:X_1 + X_2 + X_3 - Y_1 - Y_2 - Y_3 = 0

变量 0

当我求解模型时,我得到了最优解,但没有一个决策变量保持值为 1。总成本也为 0。理想情况下,答案应该是 160,因为仓库 1 的成本最低。

我不确定我错过了什么。我之前没有使用过 PuLP,所以我无法弄清楚原因。

【问题讨论】:

  • 在我看来,不建造/运输任何东西是最便宜的。您找到了最佳解决方案。
  • 正如@ErwinKalvelagen 所指出的 - 您的模型似乎对仓库需要满足的需求没有任何约束,因此最佳解决方案(零成本)是不建造任何仓库。
  • 如果 X_i == Y_j 那么为什么需要两个变量?您需要添加一个约束,其中 lpSum(X_1 + X_2 + X_3) == 1 假设您只想构建一个新仓库

标签: python linear-programming pulp operations-research


【解决方案1】:

正如 Erwin 指出的那样,最便宜的选择是不建造任何仓库。

您需要添加一个约束来强制构建一个仓库

"""
selects the cheapest warehouse to build

programmer: Michael R. Gibbs
"""

from pulp import LpProblem, LpMinimize, LpVariable, lpSum, PULP_CBC_CMD, value
import pandas as pd
import numpy as np

# we are building only one warehouse
warehouses_to_build = 1

# define the costs for the warehouse choices
warehouse_fixed_costs = [100, 120, 80]
cost_per_km = [3,3,3]
distances = [20, 30, 45]
transport_costs = [cost * dist for cost, dist in zip(cost_per_km,distances)]
warehouses = ['1','2','3']

model = LpProblem("Supply-Demand", LpMinimize)

# to build or not to build flag for each warehouse
build_warehouse_flag = LpVariable.matrix("build", warehouses, cat = "Binary", lowBound = 0)

# no warehouses is the cheepest choice
# this forces warehouses to be built
model += lpSum(build_warehouse_flag) == warehouses_to_build

# cost to be minimized
obj = lpSum([trans_cost * flag for trans_cost, flag in zip(transport_costs, build_warehouse_flag)])
obj += lpSum([fix_cost * flag for fix_cost, flag in zip(warehouse_fixed_costs, build_warehouse_flag)])
model += obj

print(model)

solver = PULP_CBC_CMD()
model.solve(solver)

print("------------------ build results -----------------------")
print([(flag, value(flag)) for flag in build_warehouse_flag])

【讨论】:

  • 这很有帮助。我没有考虑添加标志。非常感谢!
  • 您是否注意到您可以使用此模型选择 2 个仓库而不是仅 1 个?
猜你喜欢
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2022-11-13
  • 2013-09-18
  • 2023-02-22
  • 1970-01-01
相关资源
最近更新 更多