簡體   English   中英

路線上的紙漿運輸問題,而不是數量上的問題

[英]Pulp transportation problem on routes and not on quantities

紙漿中的運輸問題適用於運輸的每件物品。 但是,在我的情況下,您使用的每條路線/車道都有成本,而不是運送的每件物品,即目標 function 是盡量減少使用的路線(卡車)的數量。

*即在下面的代碼中,如果優化器將任何 route_var(數量)選擇為 >0,我想對其附加相同的成本,而不管數量如何,否則忽略它(0 成本)。 prob +=lpSum([np.minimum(route_vars[w][b],1) cost[w][b] for (w,b) in Routes]), “總車道”

我嘗試使用 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}

Here, if there is nothing shipped over route r, then r will be zero to minimise the objective function, and if d_r > 0 , then r will be 1, Mr = M , which will work if 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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM