交通问题与数学优化

问题描述 投票:0回答:1

我必须找到解决交通问题的办法,但有一些限制。

这里我们有基本表,其中A是供应列,B是需求线,矩阵有运输关税。

A(Sp)\B(Dm) 6 15 7 8 8
17 10 8 5 9 16
8 4 3 4 11 12
10 5 10 29 7 6
9 9 2 4 1 3

我已经找到了解决方案并且代码可以正常工作,因为我以多种不同的方式解决了任务,并且答案总是相同的。括号内为运输体积。例如,在元素 (1, 2) 中,我们有 10 吨要运输,价格为每吨 8 美元 = 总共 80 美元。交通总价最低210不能再低了

A\B 6 15 7 8 8
17 10 (10)\8 (7) 9 16
8 (3) (5) 4 11 12
10 (3) 10 29 7 (7)
9 9 2 4 (8) (1)

但是,我的任务有一个限制。我需要使用 6 吨卡车进行运输。例如,在元素 (1, 2) 中找到的解决方案中,我必须以每吨 8 美元的价格交付 10 吨。这意味着总共 80 美元,但由于我的限制,这意味着我需要预订 2 辆卡车并支付 120 美元。

感谢社区的帮助,我收到了解决方案并且它工作正常。


import pandas as pd
import pulp

truck_capacity = 6
suppliers = pd.RangeIndex(name='supplier', stop=4)
consumers = pd.RangeIndex(name='consumer', stop=5)

supply = pd.Series(
    name='supply', 
    index=suppliers, 
    data=(17, 8, 10, 9),
)
demand = pd.Series(
    name='demand', 
    index=consumers, 
    data=(6, 15, 7, 8, 8),
)
price_per_tonne = pd.DataFrame(
    index=suppliers, columns=consumers,
    data=(
        (10,  8,  5,  9, 16),
        ( 4,  3,  4, 11, 12),
        ( 5, 10, 29,  7,  6),
        ( 9,  2,  4,  1,  3),
    ),
).stack()
price_per_tonne.name = 'price'

flow = pd.DataFrame(
    index=suppliers, columns=consumers,
    data=pulp.LpVariable.matrix(
        name='flow_s%d_c%d', cat=pulp.LpContinuous, lowBound=0,
        indices=(suppliers, consumers),
    ),
).stack()
flow.name = 'flow'

trucks = pd.DataFrame(
    index=suppliers, columns=consumers,
    data=pulp.LpVariable.matrix(
        name='trucks_s%d_c%d', cat=pulp.LpInteger, lowBound=0,
        indices=(suppliers, consumers),
    )
).stack()
trucks.name = 'trucks'

price = truck_capacity * pulp.lpDot(price_per_tonne, trucks)
prob = pulp.LpProblem(name='transportation', sense=pulp.LpMinimize)
prob.setObjective(price)

# The flow must not exceed the supply
for supplier, group in flow.groupby('supplier'):
    prob.addConstraint(
        name=f'flow_supply_s{supplier}',
        constraint=pulp.lpSum(group) <= supply[supplier],
    )

# The flow must exactly meet the demand
for consumer, group in flow.groupby('consumer'):
    prob.addConstraint(
        name=f'flow_demand_c{consumer}',
        constraint=pulp.lpSum(group) == demand[consumer],
    )

# The capacity must be able to carry the flow
for (supplier, consumer), truck_flow in flow.items():
    prob.addConstraint(
        name=f'capacity_s{supplier}_c{consumer}',
        constraint=truck_flow <= trucks[(supplier, consumer)] * truck_capacity
    )

print(prob)
prob.solve()
assert prob.status == pulp.LpStatusOptimal

print('Flow:')
flow = flow.apply(pulp.value).unstack(level='consumer')
print(flow)
print()

print('Trucks:')
trucks = trucks.apply(pulp.value).unstack(level='consumer')
print(trucks)
print()

现在我需要使解决方案现代化。在 1/2 平方(表中为粗体)中初步求解后,我必须运输 10 吨。现在我需要找出运输的总价格是多少: a) 6吨 b) 12 吨 1/2 平方?

我不知道该怎么做,我们将感谢您的任何帮助或建议。预先感谢。

python algorithm mathematical-optimization branch-and-bound
1个回答
0
投票

添加此约束:

# For demonstration, add a bad constraint for a specific flow to show non-optimality
prob.addConstraint(
    name='bad_demonstration',
    constraint=flow[(0, 1)] == 12,
)

以及此输出代码:

print(f'Total price: ${price.value():.2f}')
print()

print('Flow:')
flow = flow.apply(pulp.value).unstack(level='consumer')
print(flow)
print()

print('Trucks:')
trucks = trucks.apply(pulp.value).unstack(level='consumer')
print(trucks)
print()

print('Prices:')
print(trucks * truck_capacity * price_per_tonne.unstack(level='consumer'))

它输出:

Total price: $288.00

Flow:
consumer    0     1    2    3    4
supplier                          
0         0.0  12.0  5.0  0.0  0.0
1         6.0   0.0  2.0  0.0  0.0
2         0.0   0.0  0.0  4.0  6.0
3         0.0   3.0  0.0  4.0  2.0

Trucks:
consumer    0    1    2    3    4
supplier                         
0         0.0  2.0  1.0  0.0  0.0
1         1.0  0.0  1.0  0.0  0.0
2         0.0  0.0  0.0  1.0  1.0
3         0.0  1.0  0.0  1.0  1.0

Prices:
consumer     0     1     2     3     4
supplier                              
0          0.0  96.0  30.0   0.0   0.0
1         24.0   0.0  24.0   0.0   0.0
2          0.0   0.0   0.0  42.0  36.0
3          0.0  12.0   0.0   6.0  18.0
© www.soinside.com 2019 - 2024. All rights reserved.