-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubgradMethodAlt.m
More file actions
77 lines (63 loc) · 2.4 KB
/
subgradMethodAlt.m
File metadata and controls
77 lines (63 loc) · 2.4 KB
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
function [x, k, stored_steps] = subgradMethodAlt(A, b, e, x0, eps, optim_f, ...
max_iter, step_size_flag)
%SUBGRADMETHODALT Ordinary subgradient method
% Goal: Perform the subgradient method
% Inputs: A - m*n matrix representing coefficients of the inequalities
% representing the half spaces
% b - m*1 vector representing constant value of the inequalities
% e - n*m matrix, each column of e represents the initial point e_i.
% x0 - n*1 vector representing starting point of the
% subgradient method
% eps - tolerance of error in the subgradient method
% optim_f - optimal f, to be used with Polyak's method
% max_iter - maximum number of iterations we would like to run
% step_size_flag - 0 if we're using 1/k as step size.
% 1 if we're using eps/|g|^2 as step size.
% 2 if we're using (f(x_k)-f')/|g|^2
%
% Outputs: k - number of iteration used
% x - final vector obtained
% Fetch the dimension of matrices
m = size(A, 1);
n = size(A, 2);
% Before we start the subgradient method, check if some e_i already
% satisfy the conditions. If yes, print that value.
flag_complete = 0;
for i=1:1:n
if (min(A * e(:, i) - b) >= 0)
fprintf('One of initial points already satisfy the conditions.\n')
x = e(:, i);
k = 0;
stored_steps = x;
flag_complete = 1;
break
end
end
if (flag_complete == 0)
% Init index and initial value of x
k = 1;
x = x0;
% Initialize matrix to store the coordinates of each step
stored_steps = zeros(n, max_iter+1);
stored_steps(:, 1) = x0;
% Precompute the constant b-diag(A*e)
temp_numer = b-diag(A*e);
fprintf('Starting subgradient method.\n')
while (k <= max_iter)
if (mod(k, 10000) == 0)
fprintf('Iteration %d\n', k);
end
% Check convergence of current solution
if (min(A * x - b) > 0)
fprintf('Subgradient method has converged to a solution.\n')
break
end
% If current solution doesn't fit, proceed to subgrad step:
x = subgradStep(A, b, e, x, eps, optim_f, k, temp_numer, step_size_flag);
% Store the vector x in the current iteration
stored_steps(:, k+1) = x;
% Update index
k = k + 1;
end
end
end