Python學習筆記-PuLP庫(3)線性規劃例項

youcans發表於2021-04-30

本節以一個實際數學建模案例,講解 PuLP 求解線性規劃問題的建模與程式設計。
  

1、問題描述

  某廠生產甲乙兩種飲料,每百箱甲飲料需用原料6千克、工人10名,獲利10萬元;每百箱乙飲料需用原料5千克、工人20名,獲利9萬元。
  今工廠共有原料60千克、工人150名,又由於其他條件所限甲飲料產量不超過8百箱。
  (1)問如何安排生產計劃,即兩種飲料各生產多少使獲利最大?
  (2)若投資0.8萬元可增加原料1千克,是否應作這項投資?投資多少合理?
  (3)若每百箱甲飲料獲利可增加1萬元,是否應否改變生產計劃?
  (4)若每百箱甲飲料獲利可增加1萬元,若投資0.8萬元可增加原料1千克,是否應作這項投資?投資多少合理?
  (5)若不允許散箱(按整百箱生產),如何安排生產計劃,即兩種飲料各生產多少使獲利最大?

2、用PuLP 庫求解線性規劃

2.1 問題 1

(1)數學建模

問題建模:
  決策變數:
    x1:甲飲料產量(單位:百箱)
    x2:乙飲料產量(單位:百箱)
  目標函式:
    max fx = 10*x1 + 9*x2
  約束條件:
    6*x1 + 5*x2 <= 60
    10*x1 + 20*x2 <= 150
  取值範圍:
    給定條件:x1, x2 >= 0,x1 <= 8
    推導條件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
    因此,0 <= x1<=8,0 <= x2<=7.5

(2)Python 程式設計

    ProbLP1 = pulp.LpProblem("ProbLP1", sense=pulp.LpMaximize)    # 定義問題 1,求最大值
    x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous')  # 定義 x1
    x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous')  # 定義 x2
    ProbLP1 += (10*x1 + 9*x2)  # 設定目標函式 f(x)
    ProbLP1 += (6*x1 + 5*x2 <= 60)  # 不等式約束
    ProbLP1 += (10*x1 + 20*x2 <= 150)  # 不等式約束
    ProbLP1.solve()
    print(ProbLP1.name)  # 輸出求解狀態
    print("Status:", pulp.LpStatus[ProbLP1.status])  # 輸出求解狀態
    for v in ProbLP1.variables():
        print(v.name, "=", v.varValue)  # 輸出每個變數的最優值
    print("F1(x)=", pulp.value(ProbLP1.objective))  # 輸出最優解的目標函式值

(3)執行結果

ProbLP1
x1=6.4285714
x2=4.2857143
F1(X)=102.8571427

2.2 問題 2

(1)數學建模

問題建模:
  決策變數:
    x1:甲飲料產量(單位:百箱)
    x2:乙飲料產量(單位:百箱)
    x3:增加投資(單位:萬元)
  目標函式:
    max fx = 10*x1 + 9*x2 - x3
  約束條件:
    6*x1 + 5*x2 <= 60 + x3/0.8
    10*x1 + 20*x2 <= 150
  取值範圍:
    給定條件:x1, x2 >= 0,x1 <= 8
    推導條件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
    因此,0 <= x1<=8,0 <= x2<=7.5

(2)Python 程式設計

    ProbLP2 = pulp.LpProblem("ProbLP2", sense=pulp.LpMaximize)    # 定義問題 2,求最大值
    x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous')  # 定義 x1
    x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous')  # 定義 x2
    x3 = pulp.LpVariable('x3', cat='Continuous')  # 定義 x3
    ProbLP2 += (10*x1 + 9*x2 - x3)  # 設定目標函式 f(x)
    ProbLP2 += (6*x1 + 5*x2 - 1.25*x3 <= 60)  # 不等式約束
    ProbLP2 += (10*x1 + 20*x2 <= 150)  # 不等式約束
    ProbLP2.solve()
    print(ProbLP2.name)  # 輸出求解狀態
    print("Status:", pulp.LpStatus[ProbLP2.status])  # 輸出求解狀態
    for v in ProbLP2.variables():
        print(v.name, "=", v.varValue)  # 輸出每個變數的最優值
    print("F2(x)=", pulp.value(ProbLP2.objective))  # 輸出最優解的目標函式值

(3)執行結果

ProbLP2
x1=8.0
x2=3.5
x3=4.4
F2(X)=107.1

2.3 問題 3

(1)數學建模

問題建模:
  決策變數:
    x1:甲飲料產量(單位:百箱)
    x2:乙飲料產量(單位:百箱)
  目標函式:
    max fx = 11*x1 + 9*x2
  約束條件:
    6*x1 + 5*x2 <= 60
    10*x1 + 20*x2 <= 150
  取值範圍:
    給定條件:x1, x2 >= 0,x1 <= 8
    推導條件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
    因此,0 <= x1<=8,0 <= x2<=7.5

(2)Python 程式設計

    ProbLP3 = pulp.LpProblem("ProbLP3", sense=pulp.LpMaximize)  # 定義問題 3,求最大值
    x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous')  # 定義 x1
    x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous')  # 定義 x2
    ProbLP3 += (11 * x1 + 9 * x2)  # 設定目標函式 f(x)
    ProbLP3 += (6 * x1 + 5 * x2 <= 60)  # 不等式約束
    ProbLP3 += (10 * x1 + 20 * x2 <= 150)  # 不等式約束
    ProbLP3.solve()
    print(ProbLP3.name)  # 輸出求解狀態
    print("Status:", pulp.LpStatus[ProbLP3.status])  # 輸出求解狀態
    for v in ProbLP3.variables():
        print(v.name, "=", v.varValue)  # 輸出每個變數的最優值
    print("F3(x) =", pulp.value(ProbLP3.objective))  # 輸出最優解的目標函式值

(3)執行結果

ProbLP3
x1=8.0
x2=2.4
F3(X) = 109.6

2.4 問題 4

(1)數學建模

問題建模:
  決策變數:
    x1:甲飲料產量(單位:百箱)
    x2:乙飲料產量(單位:百箱)
    x3:增加投資(單位:萬元)
  目標函式:
    max fx = 11*x1 + 9*x2 - x3
  約束條件:
    6*x1 + 5*x2 <= 60 + x3/0.8
    10*x1 + 20*x2 <= 150
  取值範圍:
    給定條件:x1, x2 >= 0,x1 <= 8
    推導條件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
    因此,0 <= x1<=8,0 <= x2<=7.5

(2)Python 程式設計

    ProbLP4 = pulp.LpProblem("ProbLP4", sense=pulp.LpMaximize)  # 定義問題 2,求最大值
    x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Continuous')  # 定義 x1
    x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Continuous')  # 定義 x2
    x3 = pulp.LpVariable('x3', cat='Continuous')  # 定義 x3
    ProbLP4 += (11 * x1 + 9 * x2 - x3)  # 設定目標函式 f(x)
    ProbLP4 += (6 * x1 + 5 * x2 - 1.25 * x3 <= 60)  # 不等式約束
    ProbLP4 += (10 * x1 + 20 * x2 <= 150)  # 不等式約束
    ProbLP4.solve()
    print(ProbLP4.name)  # 輸出求解狀態
    print("Status:", pulp.LpStatus[ProbLP4.status])  # 輸出求解狀態
    for v in ProbLP4.variables():
        print(v.name, "=", v.varValue)  # 輸出每個變數的最優值
    print("F4(x) = ", pulp.value(ProbLP4.objective))  # 輸出最優解的目標函式值

(3)執行結果

ProbLP4
x1=8.0
x2=3.5
x3=4.4
F4(X) = 115.1

2.5 問題 5:整數規劃問題

(1)數學建模

問題建模:
  決策變數:
    x1:甲飲料產量,正整數(單位:百箱)
    x2:乙飲料產量,正整數(單位:百箱)
  目標函式:
    max fx = 10*x1 + 9*x2
  約束條件:
    6*x1 + 5*x2 <= 60
    10*x1 + 20*x2 <= 150
  取值範圍:
    給定條件:x1, x2 >= 0,x1 <= 8,x1, x2 為整數
    推導條件:由 x1,x2>=0 和 10*x1+20*x2<=150 可知:0<=x1<=15;0<=x2<=7.5
    因此,0 <= x1<=8,0 <= x2<=7

說明:本題中要求飲料車輛為整百箱,即決策變數 x1,x2 為整數,因此是整數規劃問題。PuLP提供了整數規劃的

(2)Python 程式設計

    ProbLP5 = pulp.LpProblem("ProbLP5", sense=pulp.LpMaximize)  # 定義問題 1,求最大值
    x1 = pulp.LpVariable('x1', lowBound=0, upBound=8, cat='Integer')  # 定義 x1,變數型別:整數
    x2 = pulp.LpVariable('x2', lowBound=0, upBound=7.5, cat='Integer')  # 定義 x2,變數型別:整數
    ProbLP5 += (10 * x1 + 9 * x2)  # 設定目標函式 f(x)
    ProbLP5 += (6 * x1 + 5 * x2 <= 60)  # 不等式約束
    ProbLP5 += (10 * x1 + 20 * x2 <= 150)  # 不等式約束
    ProbLP5.solve()
    print(ProbLP5.name)  # 輸出求解狀態
    print("Status:", pulp.LpStatus[ProbLP5.status])  # 輸出求解狀態
    for v in ProbLP5.variables():
        print(v.name, "=", v.varValue)  # 輸出每個變數的最優值
    print("F5(x) =", pulp.value(ProbLP5.objective))  # 輸出最優解的目標函式值

(3)執行結果

ProbLP5
x1=8.0
x2=2.0
F5(X) = 98.0

版權說明:
YouCans 原創作品
Copyright 2021 YouCans, XUPT
Crated:2021-04-30

相關文章