python 之pulp 線性規劃介紹及舉例

來源:互聯網
上載者:User

標籤: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 線性規劃介紹及舉例

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.