一、算法公式
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、python程序
# 创作者:眰恦
# 地 点:桂林
# 时 间:2021/11/14 16:48
import math
import numpy as np
# 函数表达式
# fun = lambda x:x[1]^2+2*x[2]^2+x[3]^2-2*x[1]*x[2]+x[3]
def Quadratic_programming(H,A,b,c):
IH = np.linalg.inv(H)
AHA = np.dot(np.dot(A,IH),A.T)
# print(AHA)
IAHA = np.linalg.inv(AHA)
AIH = np.dot(A,IH)
G = IH-np.dot(np.dot(AIH.T,IAHA),AIH)
B = np.dot(IAHA,AIH)
C = -IAHA
x = np.dot(B.T,b)-np.dot(G,c)
lam = np.dot(B,c)-np.dot(C,b)
# fun = 0.5*x.T*H*x+c.T*x
return x,lam
H = np.array([[2,-2,0],[-2,4,0],[0,0,2]])
# print(H)
c = np.array([[0],[0],[1]])
# print(c)
A = np.array([[1,1,1],[2,-1,1]])
# print(A.T)
b = np.array([[4],[2]])
# print(b)
x,lam = Quadratic_programming(H,A,b,c)
print(x,lam)
总结
x=[[1.90909091] [1.95454545] [0.13636364]] lam= [[ 2.63636364] [-1.36363636]]
|