Repository navigation
Expand file tree
/
Copy pathtransportation_problem.py
More file actions
74 lines (63 loc) · 2.64 KB
/
Copy pathtransportation_problem.py
File metadata and controls
74 lines (63 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# !/usr/local/bin/python3
#
# transportation_problem.py
#
# 用单纯形算法求解运输问题
# 参考 [PulP Docs -- A Transportation Problem](https://coin-or.github.io/pulp/CaseStudies/a_transportation_problem.html)
import numpy as np
from linprog.problem import LpProblem
from linprog.simplex_vectorized import SimplexVectorized
from linprog.simplex_method import SimplexMethod
class TransportationProblem(object):
class TransportationResult(object):
def __init__(self, transportation_dict, total_cost):
self.transportation_dict = transportation_dict
self.total_cost = total_cost
def __init__(self, supply: list, demand: list, costs: list):
super().__init__()
self.supply = supply
self.demand = demand
self.costs = costs
self.result = None
def solve(self, print_result=True) -> TransportationResult:
# 将运输问题转化为线性规划问题
lp_c = [-i for v in self.costs for i in v] # 注意 `-i`,因为 linprog.problem 中 LpProblem 表示的是 max,而这里运输问题是 min。
lp_a = []
lp_b = []
rows, cols = len(self.costs), len(self.costs[0])
for i in range(len(self.supply)):
a = [0] * len(lp_c)
a[cols * i: cols * (i + 1)] = [1] * cols
lp_a.append(a)
lp_b.append(self.supply[i][1])
for i in range(len(self.demand)):
a = [1 if j % cols == i else 0 for j in range(len(lp_c))]
lp_a.append(a)
lp_b.append(self.demand[i][1])
lpPb = LpProblem(lp_c, lp_a, lp_b)
# 用单纯形法求解线性规划问题
s = lpPb.solve(SimplexVectorized)
if s.success:
self.result = TransportationProblem.TransportationResult(np.reshape(s.solve, (rows, cols)), s.target * -1)
# 输出解
if print_result:
if s.success:
transportation = np.reshape(s.solve, (rows, cols))
echo = [[' '] + [i[0] for i in self.demand]]
for i in range(rows):
echo.append([self.supply[i][0]] + [j for j in transportation[i]])
es = ''
for r in echo:
for c in r:
es += f'{c}\t'
es += '\n'
print(es)
else:
print("求解失败")
return self.result
if __name__ == "__main__":
supply = [('A1', 14), ('A2', 27), ('A3', 19)]
demand = [('B1', 22), ('B2', 13), ('B3', 12), ('B4', 13)]
costs = [[6, 7, 5, 3], [8, 4, 2, 7], [5, 9, 10, 6]]
p = TransportationProblem(supply, demand, costs)
p.solve()