纸浆运输的路线问题,而不是数量问题



PuLP中的运输问题适用于运输的每个项目。然而,在我的情况下,你使用的每一条路线/车道都有成本,而不是每一件运输的物品,即目标函数是最大限度地减少使用的路线(卡车(的数量。

*,即在下面的代码中,如果任何route_var(数量(被选择为>0,我想将相同的成本附加到它上,而不考虑数量,否则忽略它(0成本(。prob+=lpSum([np.minimum(route_vars[w][b],1(对于Routes]中的(w,b(costs[w][b](,";Total Lanes">

我试着使用np.minimum,但解决方案似乎没有考虑到它。替代方案是什么?

supply=pd.DataFrame.from_dict({38893: {'location_code': '2025', 'excess_cases': 18.0},
43872: {'location_code': '1580', 'excess_cases': 16.0},
43929: {'location_code': '1036', 'excess_cases': 16.0},
62403: {'location_code': '1607', 'excess_cases': 10.0},
67220: {'location_code': '1983', 'excess_cases': 9.0}}).T
demand=pd.DataFrame.from_dict({12223: {'location_code': '3321', 'deficit_cases': 12.0},
15682: {'location_code': '3077', 'deficit_cases': 9.0},
16147: {'location_code': '1264', 'deficit_cases': 9.0},
18964: {'location_code': '3208', 'deficit_cases': 7.0},
19389: {'location_code': '1031', 'deficit_cases': 7.0}}).T

VendorStores = supply['location_code']
excess = supply.set_index(['location_code'])['excess_cases'].to_dict()
deficitStores = demand['location_code']
deficit = demand.set_index(['location_code'])['deficit_cases'].to_dict()
costs = makeDict((VendorStores, deficitStores),[[1]*len(deficitStores)]*len(VendorStores))
prob = LpProblem("LP Problem",LpMinimize)
Routes = [(w,b) for w in VendorStores for b in deficitStores]
route_vars = LpVariable.dicts("Route",(VendorStores,deficitStores),0,None,LpInteger)
prob += lpSum([np.minimum(route_vars[w][b],1)*costs[w][b] for (w,b) in Routes]), "Total Lanes"
for w in VendorStores:
prob += lpSum([route_vars[w][b] for b in deficitStores]) <= excess[w], "Sum of Cases out of VendorStore {0}".format(str(w))
for b in deficitStores:
prob += lpSum([route_vars[w][b] for w in VendorStores]) >= deficit[b]

您的代码不是很清晰和不完整;我们不知道成本是什么样子。例如,请通过解释或使用更清晰的变量名来改进它。

要添加一个参数来表示您使用路线的位置,用LP术语来说,以下条件应该有效:

Let c_r = cost of using route r
r   = whether route r is being used
d_r = total quantity shipped over route r
M   = a very big number (at least the sum of all quantities or the capacity of a truck)
min  sum(c_r * r)
s.t. Mr >= d_r
d_r >= 0
r in {0, 1}

这里,如果在路线r上没有运输任何东西,那么r将为零以最小化目标函数,如果是d_r > 0,则r将为1,Mr = M,这将在d_r <= M的情况下起作用。因此,这完全取决于您为M.选择的值

在python术语中:

prob = LpProblem("LP Problem", LpMinimize)
route_vars = LpVariable.dicts("Route",(VendorStores, deficitStores), 0, None, LpInteger)  # d_r in the example
route_used = LpVariable.dicts("Route",(VendorStores, deficitStores), 0, 1, LpInteger)  # d_r in the example
a_very_large_number = 10000 # replace or calculate
# Objective function
prob += lpSum([(route_used[w][b],1)*costs[w][b] for (w,b) in Routes])
for w in VendorStores:
prob += lpSum([route_vars[w][b] for b in deficitStores]) <= excess[w], "Sum of Cases out of VendorStore {0}".format(str(w))
for b in deficitStores:
prob += lpSum([route_vars[w][b] for w in VendorStores]) >= deficit[b]
for (w, b) in routes:
prob += a_very_large_number * route_used[w][b] >= route_vars[w][b], "Route used"

最新更新