#导入sympy包,用于求导,方程组求解等等
from sympy import *
#设置变量
x1 = symbols("x1")
x2 = symbols("x2")
alpha = symbols("alpha")
beta = symbols("beta")
#构造拉格朗日等式
L = 10 - x1*x1 - x2*x2 + alpha * (x1*x1 - x2) + beta * (x1 + x2)
#求导,构造KKT条件
difyL_x1 = diff(L, x1) #对变量x1求导
difyL_x2 = diff(L, x2) #对变量x2求导
difyL_beta = diff(L, beta) #对乘子beta求导
dualCpt = alpha * (x1 * x1 - x2) #对偶互补条件
#求解KKT等式
aa = solve([difyL_x1, difyL_x2, difyL_beta, dualCpt], [x1, x2, alpha, beta])
#打印结果,还需验证alpha>=0和不等式约束<=0
for i in aa:
if i[2] >= 0:
if (i[0]**2 - i[1]) <= 0:
print(i)
转载地址:https://blog.csdn.net/qq_33757398/article/details/82834888