-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_ALM.m
121 lines (90 loc) · 2.97 KB
/
Test_ALM.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
clc; clear; close all
yalmip('clear')
%ALM for Maxcut
%Package requirement: Yalmip and Mosek
addpath("Packages\");
addpath("Examples\")
datapath = '';
savepath = 'results/';
name = {'n20r5MC'}; %G1_n20 %BQP_22
idx = 1;
load([datapath,name{idx},'.mat']);
[info,mosektime]=SolveMosek(At,b,c,K);
Optimal.Cost = info.sol.itr.pobjval;
m = height(At);
n = sqrt(width(At));
r = 1; %augmetned term
x = sdpvar(n); %Yalmip variable
u = sdpvar(n);
%Extract the true solution from Mosek
Xtrue = zeros(n);
[IndSym,IndDiag,IndOffDiag,ShrinkIndDiag,ShrinkIndOffDiag,IndOffDiagCounter] = SymmetricIndices(n,false);
Xtrue(IndSym) = info.sol.itr.barx;
Xtrue(IndOffDiagCounter) = Xtrue(IndOffDiag);
ytrue = info.sol.itr.y;
Ztrue = reshape(c -At.'*ytrue,n,n);
%initialization for ALM
yk = zeros(m,1);
z = c - At.'*yk;
zk = reshape(z,n,n);
C = reshape(z,n,n);
Max_iter = 10;
cost = [];
Dcost = [];
xstar = [];
Affinefeasi = [];
DAffinefeasi = [];
Conefeasi = [];
DualGap = [];
Dist = [];
DDist = [];
normb = norm(b);
normC = norm(C,'fro');
normXtrue = norm(Xtrue,'fro');
normytrue = norm(ytrue,'fro');
normZtrue = norm(Ztrue,'fro');
%main algorithm here
for k = 1:Max_iter
%solve the subproblem
bAx = b-At*vec(x);
xu = x-u; %trace(C*x)
lk = c(:).'*x(:) + 1/(2*r)*norm(r*x-zk-u,'fro')^2 + 1/(2*r)*norm(yk+r*bAx)^2;
cons = [u>=0];
ops = sdpsettings('solver','mosek');
optimize(cons,lk);
xk = value(x);
cost(k) = c.'*x(:);
xstar{k} = xk;
%dual update
tempzk = zk - r*xk;
[V,D] = eig(tempzk);
D(D<=0) = 0;
zk = V*D*V';
zk = (zk+zk')/2;
yk = yk+r*value(bAx);
Ztest = reshape(c - At.'*yk,n,n);
%residual
Dcost = [Dcost,b.'*yk];
Affinefeasi = [Affinefeasi,norm(b-At*vec(xk))/(1+normb)];
DAffinefeasi = [DAffinefeasi,norm(Ztest-zk)/(1+normC)];
%DualGap = [DualGap,abs(cost(k) - b'*yk)];
[V,D] = eig(xk);
Conefeasi = [Conefeasi,norm(D(D<0))/(1+norm(xk))];
Dist = [Dist,norm(xk-Xtrue,'fro')/(1+normXtrue)];
DDist = [DDist,sqrt(norm(yk-ytrue)^2+norm(zk-Ztrue,'fro')^2)/(1+normytrue+normZtrue)];
end
Out.PCostgap = abs(cost-Optimal.Cost)/abs(Optimal.Cost);
Out.DCostgap = abs(Dcost-Optimal.Cost)/abs(Optimal.Cost);
%
Out.Conefeasi = Conefeasi;
Out.PCost = cost;
Out.DCost = Dcost;
Out.Affinefeasi = Affinefeasi;
Out.DAffinefeasi = DAffinefeasi;
Out.Dist = Dist;
Out.DDist = DDist;
Out.OptimalCost = info.sol.itr.pobjval;
semilogy(Out.PCostgap);
hold on
semilogy(Out.Conefeasi);
%save([savepath,name{idx},'_result'],'Out');