-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopg3_lp_comparison_driver.m
179 lines (157 loc) · 4.58 KB
/
opg3_lp_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
%{
This is the driver for exercise 3.
This file contains:
- A test for efficiency and correctness under a growing problem using
a random LP generator with bounds. The size of every increment is
controlled by n_large and the number of iterations is controlled
by iter_test.
%}
%% Test efficiency and correctness
%Configure how many tests and how large an LP to test on
iter_test = 20;
n_large = 10;
sizes = n_large:n_large:(iter_test*n_large);
times = zeros(iter_test, 4);
iterations = zeros(iter_test, 4);
solution = zeros(iter_test,2);
%Plot settings
plotIterations = true;
plotTimes = true;
plotLogTimes = false;
plotSolution = true;
plotcvx = true;
plotsimplex = true;
if plotcvx
plotsimplex = true;
end
%We test on a number of random LP problems of different sizes with our
%solver, cvx, and linprog both using simplex and interior-point.
for i = 1:iter_test
%Create a quadrastart = cputime; program and solve it using cvx.
n = n_large*i;
[H, g, A, b] = generateRandomEQP(n,n/2);
l = zeros(n,1);
u = ones(n,1);
if plotcvx
start = cputime;
cvx_begin quiet
%cvx_precision low
variable x(n)
minimize(g'*x)
subject to
A' * x == b
l <= x
x <= u
cvx_end
times(i,1) = cputime-start;
iterations(i,1) = cvx_slvitr;
end
start = cputime;
options = optimset('Display', 'off');
options = optimset(options, 'Algorithm', 'dual-simplex');
[x2, optval, exitflag,output] = linprog(g, [],[], A', b,l,u, options);
times(i,2) = cputime-start;
iterations(i,2) = output.iterations;
start = cputime;
options = optimset('Display', 'off');
options = optimset(options, 'Algorithm', 'interior-point');
[x3, optval, exitflag,output] = linprog(g, [],[], A', b,l,u, options);
times(i,3) = cputime-start;
iterations(i,3) = output.iterations;
x0 = zeros(n,1);
s0 = ones(2*n,1);
y0 = ones(length(b),1);
z0 = ones(2*n,1);
start = cputime;
[x,y,z,s, iter] = LinearPDIM_box(g,A,b,l,u,x0,y0,z0,s0);
times(i, 4) = cputime-start;
iterations(i, 4) = iter;
disp("Iteration " +i+"/"+iter_test);
disp("Mean error wrt simplex: " + mean(sqrt((x-x2).^2)))
disp("Mean error wrt interior: " + mean(sqrt((x-x3).^2)))
solution(i,1) = mean(sqrt((x-x2).^2));
solution(i,2) = mean(sqrt((x-x3).^2));
if exitflag ~= 1
disp("Iteration "+i+" is infeasible!!")
end
end
% PLOT ITERATIONS
if plotIterations
disp("Plotting # of iterations!")
figure;
if(plotcvx)
plot(sizes, iterations(:,1))
hold on
end
if(plotsimplex)
plot(sizes, iterations(:,2))
hold on
end
plot(sizes, iterations(:,3))
hold on
plot(sizes, iterations(:,4))
hold off
if(plotcvx)
legend(["cvx iterations", "linprog dual-simplex iterations", "linprog interior-point iterations", "our solver"])
elseif(plotsimplex)
legend(["linprog dual-simplex iterations", "linprog interior-point iterations", "our solver"])
else
legend([ "linprog interior-point iterations", "our solver"])
end
ylabel("iterations")
xlabel("n")
title("iterations vs n")
end
%Plot time spent for the different solvers
if plotTimes
disp("Plotting times!")
figure;
if(plotcvx)
plot(sizes, times(:,1))
hold on
end
plot(sizes, times(:,2))
hold on
plot(sizes, times(:,3))
plot(sizes, times(:,4))
hold off
if(plotcvx)
legend(["cvx time", "linprog dual-simplex time", "linprog interior-point time", "our solver"])
else
legend(["linprog dual-simplex time", "linprog interior-point time", "our solver"])
end
ylabel("t [s]")
xlabel("n")
title("time vs n")
end
if plotLogTimes
disp("Plotting times!")
clear log
figure;
if(plotcvx)
loglog(sizes, times(:,1))
hold on
end
loglog(sizes, times(:,2))
hold on
loglog(sizes, times(:,3))
loglog(sizes, times(:,4))
hold off
if(plotcvx)
legend(["cvx time", "linprog dual-simplex time", "linprog interior-point time", "our solver"])
else
legend(["linprog dual-simplex time", "linprog interior-point time", "our solver"])
end
ylabel("t [s]")
xlabel("n")
title("time vs n")
end
if plotSolution
disp("Plotting solution!")
figure;
plot(sizes, solution)
title("Correctness")
ylabel("Mean Squared Error")
xlabel("n")
legend(["Relative to Simplex","Relative to interior point"])
end