【发布时间】:2022-11-13 03:20:19
【问题描述】:
我正在研究一种运输/补货模型,我需要在其中以最低成本解决问题。变量是:
- 仓库 - 几个可能的发货起点。
- Items - 在这个例子中我只使用了两个项目。每个 Item-Store 组合都有一个独特的需求值。
- 库存 - 每个“仓库”中每个“项目”的可用库存
- Stores - 每批货物的目的地。在这个例子中,我只使用了两个 Store。
- 成本 - 每个仓库-物品-商店组合的唯一成本,用于解决最低成本。
- 需求 - 每个 'Store' 想要接收的每个 'Item' 的数量;除非没有库存,否则该模型应实现 100%。
我对 Python 不是很有经验。似乎我有点接近,但是,我有一个问题我还没有解决:如果库存太低而无法满足所有需求,模型将中断并返回“不可行”的结果。取而代之的是,我希望模型满足需求,直到库存达到零,然后返回到该点的优化结果。我知道我现在得到的结果是因为我在我的一个约束中将已完成的数量设置为等于需求,但我不确定如何修改/修复它。
这是到目前为止的代码 - 这是大量谷歌搜索的结果,并且像弗兰肯斯坦博士一样将零碎的代码组合在一起 - 如果这里有任何东西看起来很愚蠢,请告诉我。使用当前输入,这将不起作用,因为 Inventory 不能满足 Demand,但如果 Inventory 更高,它似乎可以工作(例如,将 Store1-SKU_B 需求从 250 更改为 50)
from pulp import *
import pandas as pd
# Creates a list of all the supply nodes
warehouses = ["WHS_1","WHS_2","WHS_3"]
# Creates a dictionary for Inventory by Node-SKU
inventory = {"WHS_1": {"SKU_A":50,"SKU_B":100},
"WHS_2": {"SKU_A":50,"SKU_B":75} ,
"WHS_3": {"SKU_A":150,"SKU_B":25} ,
}
# Store list
stores = ["Store1","Store2"]
# SKU list
items = ["SKU_A","SKU_B"]
# Creates a dictionary for the number of units of demand for each Store-SKU
demand = {
"Store1": {"SKU_A":100,"SKU_B":250},
"Store2": {"SKU_A":100,"SKU_B":50},
}
# Creates a dictionary for the lane cost for each Node-Store-SKU
costs = {
"WHS_1": {"Store1": {"SKU_A":10.50,"SKU_B":3.75},
"Store2": {"SKU_A":15.01,"SKU_B":5.15}},
"WHS_2": {"Store1": {"SKU_A":9.69,"SKU_B":3.45},
"Store2": {"SKU_A":17.50,"SKU_B":6.06}},
"WHS_3": {"Store1": {"SKU_A":12.12,"SKU_B":5.15},
"Store2": {"SKU_A":16.16,"SKU_B":7.07}},
}
# Creates the 'prob' variable to contain the problem data
prob = LpProblem("StoreAllocation", LpMinimize)
# Creates a list of tuples containing all the possible routes for transport
routes = [(w, s, i) for w in warehouses for s in stores for i in items]
# A dictionary called 'Vars' is created to contain the referenced variables(the routes)
vars = LpVariable.dicts("Route", (warehouses, stores, items), 0, None, LpInteger)
# The objective function is added to 'prob' first
prob += (
lpSum([vars[w][s][i] * costs[w][s][i] for (w, s, i) in routes]),
"Sum_of_Transporting_Costs",
)
# Supply constraint, must not exceed Node Inventory
for w in warehouses:
for i in items:
prob += (
lpSum([vars[w][s][i] for s in stores]) <= inventory[w][i],
f"Sum_of_Products_out_of_Warehouse_{w}{i}",
)
# Supply constraint, supply to equal demand
for s in stores:
for i in items:
prob += (
lpSum([vars[w][s][i] for w in warehouses]) == demand[s][i],
f"Sum_of_Products_into_Store{s}{i}",
)
# The problem data is written to an .lp file
prob.writeLP("TestProblem.lp")
prob.solve()
# The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])
# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
print(v.name, "=", v.varValue)
# The optimised objective function value is printed to the screen
print("Total Cost of Fulfillment = ", value(prob.objective))
【问题讨论】:
标签: python optimization linear-programming pulp