-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
219 lines (177 loc) · 4.55 KB
/
main.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
set(0, 'DefaultLineLineWidth', 2);
%% Primal - Dual (Chambolle and Pock)
n = 100;
left = -1;
right = 1;
x = linspace(left, right, n);
% y = -50 + (50-(-50)) .* rand(n,1);
% y = floor(x);
% y = x - floor(x);
% y = sign(x);
y = abs(x);
% y = rectangularPulse(x);
y = y';
% K = randn(n, n);
K = kernelGenerator(n, x);
lambda = 1e-3;
mu = 0.1;
delta = 0.1;
tol = 1e-6;
max_iters = 1e4;
tic
[alpha, cost_pd, error_pd] = primal_dual(K, y, lambda, mu, delta, tol, max_iters);
toc
% disp('Estimated Solution set (alpha):');
% disp(alpha);
% loss = (1/(2*n)) * norm(K*alpha - y, 2)^2 + (lambda/2) * norm(alpha, 2)^2;
F = K * alpha;
% disp('Predicted values (F):');
% disp(F);
%
% disp('Actual values (y):');
% disp(y);
% disp('MAE:');
% disp(sum(abs(F-y)));
%
% disp('Loss function error:');
% disp(loss);
figure;
plot(y, 'b');
xlabel('Data Points');
ylabel('Values');
hold on;
plot(F, 'r');
title("Primal Dual");
legend('Original curve', 'Approximation Curve', 'Location', 'best');
grid on;
hold off;
figure;
subplot(2, 1, 1);
semilogy(cost_pd);
title('Cost vs Iterations');
xlabel('Iterations');
ylabel('Cost');
subplot(2, 1, 2);
semilogy(error_pd);
title('Error vs Iterations');
xlabel('Iterations');
ylabel('Error');
sgtitle("Primal Dual");
%% Gradient Descent (Nesterov Accelerated)
x_ = linspace(-1, 1, 100);
f_ = @(x_) abs(x_);
grad_f_ = gradient(f_(x_), x_);
max_iters = 1e4;
approx_curve = zeros(size(x_));
tic
for j = 1:max_iters
for i = 2:length(x_)
approx_curve(i) = approx_curve(i-1) + grad_f_(i-1) * (x_(i) - x_(i-1));
end
end
toc
approx_curve = approx_curve + 1;
f = @(x) abs(x);
grad_f = @(x) sign(x);
x0 = 5;
max_iter = 1e4;
alpha = 0.1;
% [x, cost_gd, error_gd, x_values, x_history] = gradient_descent(f, grad_f, x0, max_iter, alpha);
[~, cost_gd, error_gd, ~, x_history] = gradient_descent(f, grad_f, x0, max_iter, alpha);
figure;
plot(approx_curve);
hold on;
plot(f_(x_));
hold off;
title('Gradient Descent');
xlabel('x');
ylabel('f(x)');
legend('Approximation', 'Original f(x)', 'Location', 'best');
figure;
subplot(2, 1, 1);
semilogy(1:max_iter, cost_gd);
title('Cost vs Iterations');
xlabel('Iterations');
ylabel('Cost');
subplot(2, 1, 2);
semilogy(1:max_iter, error_gd);
title('Error vs Iterations');
xlabel('Iterations');
ylabel('Error');
sgtitle("Gradient Descent");
%% Adam Boost
% [optimal_x, min_loss, approximated_function, x_values, cost_values, error_values, y_values_original, y_values_approximated] = adam_optimization();
[~, ~, ~, ~, cost_values_adam, error_values_adam, ~, ~] = adam_optimization();
num_iterations = 1e4;
tic
[~, ~, ~, x_values_, y_values_original_, y_values_approximated_adam] = nonSmoothApproximation();
toc
figure;
plot(y_values_original_);
hold on;
plot(y_values_approximated_adam);
title("Adam Boost");
legend(["Original" "Approximation"]);
hold off;
% Plotting
% figure;
% plot(x_values, y_values_original, 'r-', 'LineWidth', 2); % Original function
% hold on;
% plot(x_values, y_values_approximated, 'b--', 'LineWidth', 2); % Approximated function
% xlabel('x');
% ylabel('y');
% title('Original Function vs. Approximated Function');
% legend('Original Function', 'Approximated Function');
% grid on;
% Plotting cost and error per iteration
figure;
subplot(2, 1, 1);
plot(1:num_iterations, cost_values_adam, 'b-', 'LineWidth', 2);
xlabel('Iterations');
ylabel('Cost');
title('Cost vs Iterations');
grid on;
subplot(2, 1, 2);
plot(1:num_iterations, error_values_adam, 'r-', 'LineWidth', 2);
xlabel('Iterations');
ylabel('Error');
title('Error vs Iterations');
grid on;
sgtitle("Adam Boost");
% Display final cost/error
fprintf('Final Cost: %.4f\n', cost_values_adam(num_iterations));
fprintf('Final Error: %.4f\n', error_values_adam(num_iterations));
%% Comparision of Algorithms
figure;
plot(y, 'b');
hold on;
plot(F, 'r');
plot(approx_curve);
plot(y_values_approximated_adam);
hold off;
title('Approximation Curves');
xlabel('Data Points');
ylabel('Values');
legend('Original', 'Primal-Dual', 'GD', 'Adam');
grid on;
hold off;
figure;
semilogy(cost_pd);
hold on;
semilogy(cost_gd);
semilogy(cost_values_adam);
hold off;
title('Cost vs Iterations');
xlabel('Iterations');
ylabel('Cost');
legend('Primal-Dual', 'GD', 'Adam');
figure;
semilogy(error_pd);
hold on;
semilogy(error_gd);
semilogy(error_values_adam);
hold off;
title('Error vs Iterations');
xlabel('Iterations');
ylabel('Error');
legend('Primal-Dual', 'GD', 'Adam');