數學最佳化入門

Python 最佳化入門

Jasmin Ludolf

Content Developer

接下來…

 

你將學到…
  • 解決真實世界的最佳化問題
  • 建立工具箱以應對不同問題

 

事前需具備…

 

不需要先懂…
  • 微積分
  • 演算法
Python 最佳化入門

什麼是數學最佳化?

 

  • 為特定問題找出「理想」輸入

 

  • 範例
    • 最大化農作物產量

田間作物生長。

  • 土壤條件、天氣
  • 最佳作物數量與品質
Python 最佳化入門

什麼是數學最佳化?

 

 

廂型車行駛於配送路線。

 

 

  • 最佳配送路線
  • 距離與車流
  • 改善送達時間並降低成本
Python 最佳化入門

目標函式

  • 描述輸入變數與結果之間的關係

 

家具製造

  • 最大化利潤 P
  • 數量 q

 

$P = 40q - 0.5q^2$

  • 哪個 q 值可使 P 最大?
Python 最佳化入門

用 Python 做最佳化

import numpy as np
import matplotlib.pyplot as plt

qs = np.arange(80)


def profit(q): return 40 * q - 0.5 * q**2
plt.plot(qs, profit(qs)) plt.xlabel('Quantity') plt.ylabel('Profit') plt.show()
Python 最佳化入門

製造中的最佳化

 

  • 最大值 = 最佳值

 

  • 可能的成因:
    • 人力或設備不足

 

利潤隨數量變化。

Python 最佳化入門

窮舉搜尋

  • 「暴力法」
  • 計算一系列數量的利潤
  • 選出利潤最大的那一個

 

男子手持望遠鏡,坐在印有美元符號的氣球上尋找利潤。

Python 最佳化入門

在 Python 中進行窮舉搜尋

import numpy as np

qs = np.arange(80)

def profit(q): 
  return 40 * q - 0.5 * q**2


profits = profit(qs) max_profit = profits.max()
max_ind = np.argmax(profits) q_opt = qs[max_ind]
print(f"The optimum is {q_opt} pieces of furniture, which makes ${max_profit} profit.")
The optimum is 40 pieces of furniture, which makes $800 profit.
Python 最佳化入門

窮舉搜尋的優缺點

 

  • 優點
    • (+) 實作簡單
    • (+) 不需昂貴軟體
    • (+) 假設最少
  • 缺點
    • (-) 複雜情境下無法擴充
Python 最佳化入門

一起來練習吧!

Python 最佳化入門

Preparing Video For Download...