本文最后更新于:2024年9月25日 下午
在之前记录过 拉格朗日乘数法 求解带约束的优化问题, 本文记录 Python 实现。
示例问题

scipy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| from scipy.optimize import minimize import numpy as np
def func(args): fun = lambda x: 60 - 10*x[0] - 4*x[1] + x[0]**2 + x[1]**2 - x[0]*x[1] return fun
def con(args): cons = ({'type': 'eq', 'fun': lambda x: x[0]+x[1]-8}) return cons if __name__ == "__main__": args = () args1 = () cons = con(args1) x0 = np.array((2.0, 1.0)) res = minimize(func(args), x0, method='SLSQP', constraints=cons) print(res.success) print("x1=",res.x[0],"; x2=",res.x[1]) print("最优解为:",res.fun)
|
-
输出:
1 2
| x1= 4.999999943481969 最优解为: 17.000000000000007
|
sympy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| from sympy import *
x1 = symbols("x1") x2 = symbols("x2") alpha = symbols("alpha")
L = 60 - 10*x1 - 4*x2 + x1*x1 + x2*x2 - x1*x2 - alpha * (x1 + x2 - 8)
difyL_x1 = diff(L, x1) difyL_x2 = diff(L, x2) difyL_alpha = diff(L, alpha)
aa = solve([difyL_x1, difyL_x2, difyL_alpha], [x1, x2, alpha]) print(aa) x1=aa.get(x1) x2=aa.get(x2) alpha=aa.get(alpha) print("最优解为:",60 - 10*x1 - 4*x2 + x1*x1 + x2*x2 - x1*x2 - alpha * (x1 + x2 - 8))
|
-
输出:
1 2
| {alpha: -3, x1: 5, x2: 3} 最优解为: 17
|
参考资料
文章链接:
https://www.zywvvd.com/notes/coding/python/python-lagrange/python-lagrange/