带界优化

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 优化入门

Passons à la pratique !

Python 优化入门

Preparing Video For Download...