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

2024-09-28 03:19:34 发布

您现在位置:Python中文网/ 问答频道 /正文

纸浆厂的运输问题涉及到运输的每一个项目。然而,在我的情况下,您使用的每一条路线/车道都有成本,而不是每一个装运的项目,即目标函数是最小化使用的路线(卡车)数量

*即,如果任何路线变量(数量)选择为>;通过优化器0,我希望将相同的成本附加到它,而不考虑数量,否则忽略它(0成本)。 概率+=lpSum([np.最小值(路线变量[w][b],1)路线中(w,b)的成本[w][b]),“总车道”

我尝试使用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]

Tags: inforcodelocation路线routedict成本
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:34

你的代码不是很清楚,也不完整;我们不知道成本是多少。例如,请通过解释或使用更清晰的变量名来改进它

要添加一个参数,表示您在何处使用路由,在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"

相关问题 更多 >

    热门问题