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

Pulp transportation problem on routes and not on quantities

PuLP 中的运输问题适用于运输的每件物品。但是,在我的例子中,您使用的每个 route/lane 都有成本,而不是每件运输的物品,即 objective 功能是尽量减少使用的路线(卡车)数量。

*即在下面的代码中,如果任何 route_var (数量)被优化器选择为>0,我想附加相同的成本而不考虑数量,否则忽略它(0成本)。 prob +=lpSum([np.minimum(route_vars[w][b],1)costs[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}

这里,如果没有任何东西通过路由 r 运送,那么 r 将为零以最小化 objective 函数,如果 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"