邊界限制最佳化

Python 最佳化入門

Jasmin Ludolf

Content Developer

邊界限制最佳化

 

  • 預設求解器:Broyden-Fletcher-Goldfarb-Shanno(BFGS)
  • 限制是一個範圍
  • 每天生產 50 到 100 單位

 

from scipy.optimize import Bounds

bounds = Bounds(50, 100)
Python 最佳化入門

L-BFGS-B

from scipy.optimize import minimize, Bounds

def objective_function(b):
  return (2*b[0] - 1.5*b[1])

bounds = Bounds([5, 10], [25, 30])
x0 = [10, 5] result = minimize(objective_function, x0, method='L-BFGS-B', bounds=bounds) print(result.x)
[ 5. 30.]
Python 最佳化入門

線性限制最佳化

目標函式 或

  • 線性
  • $p = 2x + 1.5y$

目標函式的線性圖

限制條件

  • 線性
  • $3x + 2y <= 100$

限制條件的線性圖

Python 最佳化入門

硬性不等式

  • 一個條件
  • 麵粉用完就不能再做餅乾!

一袋麵粉

Python 最佳化入門

解線性限制問題

from scipy.optimize import minimize


def objective_function(b): return (b[0]) ** 2 + (b[1]) ** 0.5
def constraint_function(x): return 2*x[0] + 3*x[1] - 6
constraint = {'type': 'ineq', 'fun': constraint_function}
x0 = [20, 20]
result = minimize(objective_function, x0, constraints=[constraint])
print(result)
     fun: 1.4000405652399073
     jac: array([0.24057153, 0.36086182])
 message: 'Optimization terminated successfully'
    nfev: 25
     nit: 8
    njev: 8
  status: 0
 success: True
       x: array([0.12028576, 1.9198095 ])
  • 限制類型:
    • 'eq':相等式
    • 'ineq':不等式
Python 最佳化入門

一起來練習吧!

Python 最佳化入門

Preparing Video For Download...