Pulp中的运输问题适用于所运输的每个物品。但是,就我而言,您使用的每条路线/车道都需要付费,而不是每件物品都要付费,也就是说,目标功能是最大程度地减少使用的路线(卡车)数量。
* 即在下面的代码中,如果优化程序将任何route_var(数量)选择为> 0,我想将相同的成本附加到其上,而与数量无关,否则将其忽略(0成本)。 prob + = lpSum([np.minimum(route_vars [w] [b],1)成本[w] [b] for(w,b)in Routes]),“ Total Lanes” < / em>
我尝试使用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]
答案 0 :(得分:0)
您的代码不是很清楚也不完整;我们不知道费用是多少。例如,请通过解释或使用更清晰的变量名称来改进它。
要添加一个参数,以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
,如果{ {1}}。因此,这完全取决于您为M选择的值。
用python术语:
d_r <= M