標籤:print style html load sof mini 如何 .so mic
pulp http://pythonhosted.org/PuLP/main/basic_python_coding.html供水
問題
1問題
供水公司有三個水庫分別為A,B,C向四個小區甲乙丙丁供水,A和B向所有小區供水,C僅向甲乙丙供水,水庫最大供水量(千噸)
水庫 |
A |
B |
C |
最大供水量(千噸) |
50 |
60 |
50 |
小區用水情況為
小區 |
甲 |
乙 |
丙 |
丁 |
基本用水量(千噸) |
30 |
70 |
10 |
10 |
額外用水量(千噸) |
50 |
70 |
20 |
40 |
水庫供水收入900元/千噸,支出費用為:其他費用450/千噸,引水管理費:
由水庫到小區引水費用: 元/千噸 |
甲 |
乙 |
丙 |
丁 |
A |
160 |
130 |
220 |
170 |
B |
140 |
130 |
190 |
150 |
C |
190 |
200 |
230 |
|
問 如何分配水庫供水量,公司才能獲利最多。
2 解題思路
使引水管理費最小,則利潤最高。
設:水庫i向小區j的供水量為,總引水管理費為Z
目標函數為:z = 160.0*x11 + 130.0*x12 + 220.0*x13 + 170.0*x14 + 140.0*x21 + 130.0*x22 + 190.0*x23 + 150.0*x24 + 190.0*x31 + 200.0*x32 + 230.0*x33
求的值,使Z最小。
約束條件為:
1 # coding=utf-8 2 # coding=utf-8 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 = 039 for i in range(len(X)):40 z += X[i]*c[i]41 #print(z)42 prob += z43 44 # 載入約束變數45 prob += x11+x12+x13+x14 == con[0]# 約束條件146 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 += x14 + x24 <= con[7]56 prob += x14 + x24 >= con[8]57 58 # 求解59 60 status = prob.solve()61 62 #print(LpStatus[status])63 print(value(prob.objective)) # 計算結果64 65 66 # 顯示結果67 for i in prob.variables():68 print(i.name + "=" + str(i.varValue))69 for i in prob.variables():70 71 72 73 if __name__ == ‘__main__‘:74 c = [160, 130, 220, 170, 140, 130, 190, 150, 190, 200, 230]75 con = [50, 60, 50, 80, 30, 140, 70, 50, 10]76 getresult(c, con)
輸出結果:
Optimal24000.0x11=0.0x12=50.0x13=0.0x14=0.0x21=0.0x22=50.0x23=0.0x24=10.0x31=50.0x32=0.0x33=0.0
python 之pulp 線性規劃介紹及舉例