1.拉格朗日乘子法
题目如下:等式约束下的拉格朗日乘子法求解过程
2.python --拉格朗日乘子法
题目如上:
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)
结果如下:
3.python sympy包 --拉格朗日乘子法
题目如上:
#导入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))
结果如下:
三种方法,结果一致!!