-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopg1_eqp_comparison_driver.m
267 lines (224 loc) · 6.34 KB
/
opg1_eqp_comparison_driver.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
%{
This is the driver for exercise 1.
This file contains:
- A test for correctness using the problem given in exercise 1.4.
- A test for efficiency under growing problem using the recycling
problem given in week 5.
- A benchmark test for matrix factorizations.
- We vary the number of constraints instead of the number of variables
%}
%% Given problem
names = ["LDLdense", "LDLsparse", "LUdense", "LUsparse", "rangespace", "nullspace", "quadprog"];
tests = 20;
times = zeros(7,tests);
answers = zeros(5,tests,7);
bs = [];
i=1;
for b1 = linspace(8.5, 18.68, tests)
bs = [bs;b1];
n = 5;
H = [5.0 1.86 1.24 1.48 -0.46; ...
1.86 3.0 0.44 1.12 0.52; ...
1.24 0.44 3.8 1.56 -0.54; ...
1.48 1.12 1.56 7.2 -1.12; ...
-0.46 0.52 -0.54 -1.12 7.8];
g = [-16.1; -8.5; -15.7; -10.02; -18.68];
A = [16.1 8.5 15.7 10.02 18.68; 1.0 1.0 1.0 1.0 1.0]';
b = [b1;1];
%We test all solvers one by one and save the answers
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "LDLdense");
answers(:, i,1) = x;
times(1, i) = cputime-start;
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "LDLsparse");
answers(:, i,2) = x;
times(2, i) = cputime-start;
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "LUdense");
answers(:, i,3) = x;
times(3, i) = cputime-start;
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "LUsparse");
answers(:, i,4) = x;
times(4, i) = cputime-start;
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "rangespace");
answers(:, i,5) = x;
times(5, i) = cputime-start;
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "nullspace");
answers(:, i,6) = x;
times(6, i) = cputime-start;
options = optimset('Display', 'off');
start = cputime;
[x, lambda] = quadprog(H,g,[],[],A',b,[],[],[],options);
answers(:, i,7) = x;
times(7, i) = cputime-start;
i = i+1;
end
%We compare answers to quadprog
answer_diff = zeros(6,20);
for i=1:6
answer_diff(i,:) = mean(sqrt((answers(:,:,i)-answers(:,:,7)).^2));
end
hold off
for i=1:size(times,1)
plot(bs, times(i,:))
hold on
end
legend(names)
xlabel("b(1)")
ylabel("time [log s]")
%and plot the answers
figure
for i=1:6
plot(bs, (answer_diff(i,:)))
hold on
end
legend(names(1:6))
xlabel("b(1)")
ylabel("Error relative to quadprog")
%% Recycling problem
names = ["LDLdense", "LDLsparse", "LUdense", "LUsparse", "rangespace", "nullspace", "quadprog"];
tests = 20;
times = ones(7,tests)*tests;
ns = zeros(tests,1);
%We test every solver using the Recycling problem of different sizes n.
for i = 1:tests
disp(i)
n = i*(2000/tests);
ns(i) = n;
[H, g, A, b] = ProblemEQPRecycling(n, 0.2, 1);
for k =1:1
j=1;
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "LDLdense");
times(j, i) = cputime-start;
j = j+1;
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "LDLsparse");
times(j, i) = cputime-start;
j = j+1;
%
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "LUdense");
times(j, i) = cputime-start;
j = j+1;
%
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "LUsparse");
times(j, i) = min(cputime-start, times(j,i));
j = j+1;
%
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "rangespace");
times(j, i) = cputime-start;
j = j+1;
start = cputime;
[x, lambda] = EqualityQPSolver(H,g,A,b, "nullspace");
times(j, i) = cputime-start;
j = j+1;
options = optimset('Display', 'off');
start = cputime;
[x, lambda] = quadprog(H,g,[],[],A',b,[],[],[],options);
times(j, i) = cputime-start;
j = j+1;
%
end
disp("n="+n)
end
%We plot the time for each method.
figure
hold off
for i=1:size(times,1)
plot(ns, times(i,:))
hold on
end
legend(names, 'Location','northwest')
xlabel("n")
ylabel("time [s]")
title("Growing size problem")
%% Factorization benchmark
tests = 10;
times = ones(5,tests)*100;
ns = zeros(tests,1);
%We bench LU vs LDL versus Cholesky of varying sizes
for i = 1:tests
n = i*(5000/tests);
ns(i) = n;
[H,g,A,b,x,lambda] = generateRandomEQP(n,n);
KKT = [H -A;-A', zeros(size(A,2), size(A,2))];
for k =1:1
j=1;
start = cputime;
x = lu(KKT,'vector');
times(j, i) = min(cputime-start, times(j,i));
j = j+1;
start = cputime;
x = lu(H,'vector');
times(j, i) = min(cputime-start, times(j,i));
j = j+1;
start = cputime;
x = ldl(KKT,'vector');
times(j, i) = min(cputime-start, times(j,i));
j = j+1;
start = cputime;
x = ldl(H,'vector');
times(j, i) = min(cputime-start, times(j,i));
j = j+1;
start = cputime;
x = chol(H);
times(j, i) = min(cputime-start, times(j,i));
j = j+1;
end
disp("n="+n)
end
%We then plot the time for each method and target.
hold off
for i=1:5%size(times,1)
semilogy(ns, times(i,:))
hold on
end
legend(["LU, KKT", "LU, H", "LDL, KKT", "LDL, H", "Cholesky, H"])
xlabel("n")
ylabel("time [s]")
title("Benchmark of factorizations")
%% m dependent
names = ["rangespace", "nullspace"];
tests = 10;
times = ones(2,tests)*100;
ms = zeros(tests,1);
top = 3000;
%To compare range space and null space, we solve random EQPs with varying
%number of constraints.
for i = 1:tests
m = i*(top/tests);
ms(i) = m;
[H, g, A, b] = generateRandomEQP(top, m);
for k =1:1
j=1;
start = cputime;
[x, lambda, facTime_R] = EqualityQPSolver(H,g,A,b, "rangespace");
times(j, i) = min(cputime-start, times(j,i));
j = j+1;
start = cputime;
[x, lambda, facTime_N] = EqualityQPSolver(H,g,A,b, "nullspace");
times(j, i) = min(cputime-start, times(j,i));
j = j+1;
end
disp("m="+m)
end
figure
hold off
for i=1:2
plot(ms, times(i,:))
hold on
end
plot(ms, times(1,:)-facTime_R)
plot(ms, times(2,:)-facTime_N)
xline(1950)
legend([names,'range space -Cholesky','null space -QR','Theoretical tipping point'], 'Location','northwest')
xlabel("m")
ylabel("time [s]")
title("Growing constraints problem, n=3000")