//二次规划 quadprog vs socp
baseDir = "D:/data/quadprog/"
/*quad*/

f = dropna(flatten(float(matrix(select col1, col2, col3, col4, col5, col6, col7, col8, col9, col10 from loadText(baseDir + "C.csv") where rowNo(col1) > 0)).transpose()))

N = f.size()
H = eye(N)
A = matrix(select * from loadText(baseDir + "A_ub.csv"))
b = 
[0.025876723,	0.092515275,	0.035133942,	0.053184884,	0.067410565,	0.009709433,	0.04668745,	0.00636804,	0.022258664,	0.11027537,
0.018488302,	0.027417204,	0.028585,	0.017228214,	0.008055527,	0.015727843,	0.026132369,	0.013646113,	0.066000808,	0.043606587,
0.048325258,	0.033868626,	0.010790603,	0.017737391,	0.03252374,	0.039329965,	0.040665779,	0.010868773,	0.006819891,	0.015879314,
0.008882335,	-0.025876723,	-0.092515275,	-0.035133942,	-0.053184884,	-0.067410565,	-0.009709433,	-0.04668745,	-0.00636804,	-0.022258664,
-0.110275379,	-0.018488302,	-0.027417204,	-0.028585,	-0.017228214,	-0.008055527,	-0.015727843,	-0.026132369,	-0.013646113,	-0.066000808,
-0.043606587,	-0.048325258,	-0.033868626,	-0.010790603,	-0.017737391,	-0.03252374,	-0.039329965,	-0.040665779,	-0.010868773,	-0.006819891,
-0.015879314,	-0.008882335]
Aeq = matrix(take(1, N)).transpose()
beq = array(DOUBLE).append!(1)
timer res1 = quadprog(H, f, A, b, Aeq, beq) 
print(res1)

/*socp*/

// 在本例中H矩阵为单应矩阵，开根结果是其本身，故省去开根过程

f = dropna(flatten(float(matrix(select col1, col2, col3, col4, col5, col6, col7, col8, col9, col10 from loadText(baseDir + "C.csv") where rowNo(col1) > 0)).transpose()))

N = f.size()
H = eye(N).transpose()

A = matrix(select * from loadText(baseDir + "A_ub.csv"))
b = 
[0.025876723,	0.092515275,	0.035133942,	0.053184884,	0.067410565,	0.009709433,	0.04668745,	0.00636804,	0.022258664,	0.11027537,
0.018488302,	0.027417204,	0.028585,	0.017228214,	0.008055527,	0.015727843,	0.026132369,	0.013646113,	0.066000808,	0.043606587,
0.048325258,	0.033868626,	0.010790603,	0.017737391,	0.03252374,	0.039329965,	0.040665779,	0.010868773,	0.006819891,	0.015879314,
0.008882335,	-0.025876723,	-0.092515275,	-0.035133942,	-0.053184884,	-0.067410565,	-0.009709433,	-0.04668745,	-0.00636804,	-0.022258664,
-0.110275379,	-0.018488302,	-0.027417204,	-0.028585,	-0.017228214,	-0.008055527,	-0.015727843,	-0.026132369,	-0.013646113,	-0.066000808,
-0.043606587,	-0.048325258,	-0.033868626,	-0.010790603,	-0.017737391,	-0.03252374,	-0.039329965,	-0.040665779,	-0.010868773,	-0.006819891,
-0.015879314,	-0.008882335]

// c = [f 0.5]
c = f
c.append!(0.5)

// G [A, 1， -2U, 1]
// zeros_cols = matrix(DOUBLE, A.rows(), 1, , 0)  
G = concatMatrix([A, matrix(DOUBLE, A.rows(), 1, , 0)]) // A*x <= b
G = concatMatrix([G, concatMatrix([matrix(DOUBLE, 1, A.cols(), , 0), -eye(1)])], false)  // 1-y
G = concatMatrix([G, concatMatrix([-2 * eye(N), matrix(DOUBLE, N, 1, , 0)])], false)  // 2Ux
G = concatMatrix([G, concatMatrix([matrix(DOUBLE, 1, A.cols(), , 0), eye(1)])], false) // 1+y

// h [b, 1, 0, 1]
h = b // A*x <= b
h.append!(1) // 1-y
h.append!(take(0, N)) // 2Ux
h.append!(1) // 1+y
h;
// l,q
l = b.size()
q = [N+2]

// Aeq * x = beq
Aeq = matrix(take(1, N)).transpose()
Aeq.append!(0)
beq = array(DOUBLE).append!(1)

timer res2 = socp(c, G, h, l, q, Aeq, beq);

print(res2)
