python 实现拉格朗日乘子法

2024-09-26 09:33:53 浏览数 (2)

在之前记录过 拉格朗日乘数法 求解带约束的优化问题, 本文记录 Python 实现。

示例问题

scipy

代码语言:txt复制
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]
    #fun = lambda x: 10 - x[0]**2 - x[1]**2
    return fun
 
#约束条件,包括等式约束和不等式约束
def con(args):
    cons = ({'type': 'eq', 'fun': lambda x: x[0] x[1]-8})
    #cons = ({'type': 'ineq', 'fun': lambda x: x[1]-x[0]**2},
    #        {'type': 'eq', 'fun': lambda x: x[0] x[1]})
    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)

输出:

代码语言:txt复制
x1= 4.999999943481969 ;  x2= 3.000000056518032
最优解为: 17.000000000000007

sympy

代码语言:txt复制
#导入sympy包,用于求导,方程组求解等等
from sympy import * 
 
#设置变量
x1 = symbols("x1")
x2 = symbols("x2")
alpha = symbols("alpha")
#beta = symbols("beta")
 
#构造拉格朗日等式
L = 60 - 10*x1 - 4*x2   x1*x1   x2*x2 - x1*x2 - alpha * (x1   x2 - 8)
 
#求导,构造KKT条件
difyL_x1 = diff(L, x1)  #对变量x1求导
difyL_x2 = diff(L, x2)  #对变量x2求导
difyL_alpha = diff(L, alpha) #对alpha求导
 
#求解KKT等式
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))

输出:

代码语言:txt复制
{alpha: -3, x1: 5, x2: 3}
最优解为: 17

参考资料

  • https://blog.csdn.net/qq_34591921/article/details/105637476

文章链接:

https://cloud.tencent.com/developer/article/2453744

0 人点赞