# 供水问题

1问题

 水库 A B C 最大供水量（千吨） 50 60 50

 小区 甲 乙 丙 丁 基本用水量（千吨） 30 70 10 10 额外用水量（千吨） 50 70 20 40

 由水库到小区引水费用：元/千吨 甲 乙 丙 丁 A 160 130 220 170 B 140 130 190 150 C 190 200 230

如何分配水库供水量，公司才能获利最多。

 1 # coding=utf-8
2
3 from pulp import *
4
5
6 def get_re():
7     pass
8
9
10 def getresult(c, con):
11
12 # 设置对象
13     prob = LpProblem('myPro', LpMinimize)
14 # 设置三个变量，并设置变量最小取值
15
16     x11 = LpVariable("x11", lowBound=0)
17     x12 = LpVariable("x12", lowBound=0)
18     x13 = LpVariable("x13", lowBound=0)
19     x14 = LpVariable("x14", lowBound=0)
20     x21 = LpVariable("x21", lowBound=0)
21     x22 = LpVariable("x22", lowBound=0)
22     x23 = LpVariable("x23", lowBound=0)
23     x24 = LpVariable("x24", lowBound=0)
24     x31 = LpVariable("x31", lowBound=0)
25     x32 = LpVariable("x32", lowBound=0)
26     x33 = LpVariable("x33", lowBound=0)
27
28     X = [x11, x12, x13, x14, x21, x22, x23, x24, x31, x32, x33]
29
30     #c = [160, 130, 220, 170, 140, 130, 190, 150, 190, 200, 230]
31
32
33
34
35
36
37 # 目标函数
38     z = 0
39     for i in range(len(X)):
40         z += X[i]*c[i]
41     #print(z)
42     prob += z
43
44 # 载入约束变量
45     prob += x11+x12+x13+x14 == con[0]# 约束条件1
46     prob += x21+x22+x23+x24 == con[1]
47     prob += x31+x32+x33 == con[2]
48
49     prob += x11+x21+x31 <= con[3]
50     prob += x11+x21+x31 >= con[4]
51
52     prob += x12 + x22 + x32 <= con[5]
53     prob += x12 + x22 + x32 >= con[6]
54
55     prob += x13 + x23 + x33 <= con[7]
56     prob += x13 + x23 + x33 >= con[8]
57     prob += x14 + x24 <= con[9]
58     prob += x14 + x24 >= con[10]
59
60 # 求解
61
62     status = prob.solve()
63
64     print(status)
65     print(LpStatus[status])
66     print(value(prob.objective))  # 计算结果
67
68
69 # 显示结果
70 #     for i in prob.variables():
71 #         print(i.name + "=" + str(i.varValue))
72     for i in prob.variables():
73         print(i.varValue)
74
75
76 if __name__ == '__main__':
77     c = [160, 130, 220, 170, 140, 130, 190, 150, 190, 200, 230]
78     con = [50, 60, 50, 80, 30, 140, 70, 30,10, 50, 10]
79     getresult(c, con)

1
Optimal
24400.0
0.0
50.0
0.0
0.0
0.0
50.0
0.0
10.0
40.0
0.0
10.0