hw3: done MATLAB

This commit is contained in:
Claudio Maggioni (maggicl) 2021-04-20 15:04:00 +02:00
parent b29cdc34e3
commit ca2a294fc7
15 changed files with 85216 additions and 44 deletions

39
Claudio_Maggioni_3/BGFS.m Normal file
View File

@ -0,0 +1,39 @@
function [xk, xs, gs] = BGFS(f, x0, H, max_itr, tol)
syms x y;
xk = x0;
gf = gradient(f);
fl = matlabFunction(f);
gfl = matlabFunction(gf);
tnorm = norm(at(gf, xk), 2);
xs = zeros(size(x0, 1), max_itr + 1);
xs(:, 1) = x0;
gs = zeros(1, max_itr + 1);
gs(1) = tnorm;
k = 1;
I = eye(2);
while tnorm > tol && k <= max_itr
grad = gfl(xk(1), xk(2));
p = -H * grad;
alpha = backtracking(fl, gfl, p, xk, 1, 0.9, 1e-4);
xk = xk + alpha * p;
s = alpha * p;
y = gfl(xk(1), xk(2)) - grad;
rho = 1 / (y' * s);
H = (I - (rho * s * y')) * H * (I - (rho * y * s')) ...
+ (1 / (y' * s) * (s * s'));
k = k + 1;
xs(:, k) = xk;
tnorm = norm(gfl(xk(1), xk(2)), 2);
gs(k) = tnorm;
end
xs = xs(:, 1:k);
gs = gs(:, 1:k);
end

View File

@ -1,17 +1,11 @@
syms x y;
f = (1 - x)^2 + 100 * (y - x^2)^2;
[x, xs, gs] = gd(f, [0;0], 500, 1e-6, true);
display(x);
display(xs);
display(gs);
plot(xs(1, :), xs(2, :), '-o');
function [xk, xs, gs] = gd(f, x0, max_itr, tol, bt)
function [xk, xs, gs] = GD(f, x0, max_itr, tol, bt)
syms x y;
xk = x0;
gf = gradient(f);
hf = hessian(f, [x, y]);
fl = matlabFunction(f);
gfl = matlabFunction(gf);
tnorm = norm(at(gf, xk), 2);
xs = zeros(size(x0, 1), max_itr + 1);
xs(:, 1) = x0;
@ -20,18 +14,20 @@ function [xk, xs, gs] = gd(f, x0, max_itr, tol, bt)
k = 1;
while tnorm > tol && k <= max_itr
p = -at(gf, xk);
H = at(hf, xk);
p = -gfl(xk(1), xk(2));
if bt
alpha = backtracking(f, xk, 1, p, 0.01, 1e-4)
alpha = backtracking(fl, gfl, p, xk, 1, 0.9, 1e-4);
else
alpha = dot(-H * xk, p) / dot(H * p, p)
alpha = 1;
end
xk = xk + alpha * p
xk = xk + alpha * p;
k = k + 1;
xs(:, k) = xk;
tnorm = norm(gfl(xk(1), xk(2)), 2);
gs(k) = tnorm;
end
xs = xs(:, 1:k);
gs = gs(:, 1:k);
end
end

View File

@ -1,24 +1,13 @@
syms x y;
f = (1 - x)^2 + 100 * (y - x^2)^2;
[x, xs, gs] = newton(f, [0;0], 50000, 1e-6);
display(x);
display(xs);
display(gs);
plot(xs(1, :), xs(2, :), '-o');
function y = at(f, xk)
syms x y;
y = double(subs(f, [x, y], [xk(1), xk(2)]));
end
function [xk, xs, gs] = newton(f, x0, max_itr, tol)
function [xk, xs, gs] = Newton(f, x0, max_itr, tol, bt)
syms x y;
xk = x0;
gf = gradient(f);
hf = hessian(f, [x, y]);
fl = matlabFunction(f);
gfl = matlabFunction(gf);
tnorm = norm(at(gf, xk), 2);
xs = zeros(size(x0, 1), max_itr + 1);
xs(:, 1) = x0;
@ -27,7 +16,13 @@ function [xk, xs, gs] = newton(f, x0, max_itr, tol)
k = 1;
while tnorm > tol && k <= max_itr + 1
xk = xk - (at(hf, xk) \ at(gf, xk));
pk = - (at(hf, xk) \ at(gf, xk));
if bt
alpha = backtracking(fl, gfl, pk, xk, 1, 0.9, 1e-4);
else
alpha = 1;
end
xk = xk + alpha * pk;
tnorm = norm(at(gf, xk), 2);
k = k + 1;
xs(:, k) = xk;
@ -35,4 +30,9 @@ function [xk, xs, gs] = newton(f, x0, max_itr, tol)
end
xs = xs(:, 1:k);
gs = gs(:, 1:k);
end
end
function y = at(f, xk)
syms x y;
y = double(subs(f, [x, y], [xk(1), xk(2)]));
end

View File

@ -1,8 +1,8 @@
function alpha = backtracking(f, xk, alpha_bar, pk, rho, c)
alpha = alpha_bar;
gf = gradient(f);
while at(f, xk + alpha * pk) > at(f, xk) - c * alpha * dot(at(gf, xk), pk)
alpha = alpha * rho
end
end
function alpha = backtracking(f, gf, p, x, alpha, rho, c)
xn = x + alpha * p;
while f(xn(1), xn(2)) > f(x(1), x(2)) + c * alpha * gf(x(1), x(2))' * p
alpha = rho * alpha;
xn = x + alpha * p;
end
end

View File

@ -0,0 +1,31 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=4.617in,
height=3.641in,
at={(0.774in,0.491in)},
scale only axis,
unbounded coords=jump,
xmin=-5e+127,
xmax=3e+128,
ymin=0,
ymax=1.8e+86,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
0 0\\
2 0\\
-3200 800\\
13106176003202 2047840800\\
-9.00508836393827e+41 3.43543698853816e+28\\
2.92094868655132e+128 1.62183232884673e+86\\
-inf 1.70638824589317e+259\\
nan inf\\
};
\end{axis}
\end{tikzpicture}%

View File

@ -0,0 +1,34 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=4.617in,
height=3.641in,
at={(0.774in,0.491in)},
scale only axis,
unbounded coords=jump,
xmin=-5e+127,
xmax=3e+128,
ymin=0,
ymax=1.8e+86,
axis background/.style={fill=white},
legend style={legend cell align=left, align=left, draw=white!15!black}
]
\addplot [color=mycolor1, mark=*, mark options={solid, mycolor1}]
table[row sep=crcr]{%
0 0\\
2 0\\
-3200 800\\
13106176003202 2047840800\\
-9.00508836393827e+41 3.43543698853816e+28\\
2.92094868655132e+128 1.62183232884673e+86\\
-inf 1.70638824589317e+259\\
nan inf\\
};
\addlegendentry{GD (alpha=1)}
\end{axis}
\end{tikzpicture}%

21191
Claudio_Maggioni_3/ex1-3.tex Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,36 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=4.617in,
height=3.641in,
at={(0.774in,0.491in)},
scale only axis,
unbounded coords=jump,
xmin=0,
xmax=4,
ymode=log,
ymin=1,
ymax=1e+200,
yminorticks=true,
axis background/.style={fill=white},
legend style={legend cell align=left, align=left, draw=white!15!black}
]
\addplot [color=mycolor1, mark=*, mark options={solid, mycolor1}]
table[row sep=crcr]{%
0 1\\
1 1601\\
2 1.04841216742464e+16\\
3 2.95055682555403e+54\\
4 6.57585025723101e+169\\
5 inf\\
6 inf\\
7 nan\\
};
\addlegendentry{GD + backtracking}
\end{axis}
\end{tikzpicture}%

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,49 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=4.617in,
height=3.641in,
at={(0.774in,0.491in)},
scale only axis,
xmin=0,
xmax=1.2,
ymin=0,
ymax=1.2,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
0 0\\
0.243153309181139 0\\
0.385574283516823 0.0768839344022268\\
0.406269115526804 0.228605903873976\\
0.454394143039344 0.204136926895131\\
0.498557614055172 0.23892892868596\\
0.620979332605692 0.353718595337535\\
0.57936460358975 0.327866742545248\\
0.616121196551721 0.375114354375997\\
0.776077091062027 0.572038852536037\\
0.697592763590646 0.487371411589351\\
0.737499732511574 0.541927499854004\\
0.851186743684398 0.702934657899626\\
0.792280441629556 0.62507087775202\\
0.817946864998747 0.665679934708557\\
0.92248773523621 0.835150610480633\\
0.873933239049152 0.761975932983517\\
0.896369084677219 0.801548786456236\\
0.968508670106431 0.930592370870331\\
0.94922990019977 0.90004747521168\\
0.965038718894672 0.930647241632658\\
0.994184674394427 0.98709143799204\\
0.995726402584417 0.991358496483784\\
0.999560495084773 0.999098978460342\\
0.999985154690863 0.999968759175614\\
0.999999722333829 0.999999474706011\\
1.00000001502439 1.00000002865953\\
};
\end{axis}
\end{tikzpicture}%

View File

@ -0,0 +1,51 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=4.617in,
height=3.641in,
at={(0.774in,0.491in)},
scale only axis,
xmin=0,
xmax=30,
ymode=log,
ymin=1e-08,
ymax=100,
yminorticks=true,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
0 2\\
1 12.5607978484932\\
2 17.4065065901439\\
3 17.1507084737438\\
4 0.814005132835468\\
5 2.13360285320214\\
6 9.59328224461729\\
7 1.83406220417747\\
8 0.960051885187564\\
9 10.7994609810302\\
10 0.823371092958359\\
11 0.399989068016143\\
12 8.26772606877548\\
13 0.674513531933847\\
14 0.994977406942212\\
15 6.50941712025139\\
16 0.514849723958323\\
17 0.619141853913074\\
18 3.17771232534436\\
19 0.338310420250268\\
20 0.223907430737766\\
21 0.573528649173901\\
22 0.0427061378580603\\
23 0.00914914389448632\\
24 0.000666939747400779\\
25 1.39324401637967e-05\\
26 6.48313521241376e-07\\
};
\end{axis}
\end{tikzpicture}%

View File

@ -0,0 +1,51 @@
% This file was created by matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}
\begin{axis}[%
width=4.617in,
height=3.641in,
at={(0.774in,0.491in)},
scale only axis,
xmin=0,
xmax=30,
ymode=log,
ymin=4.18737896779034e-16,
ymax=1,
yminorticks=true,
axis background/.style={fill=white}
]
\addplot [color=mycolor1, mark=*, mark options={solid, mycolor1}, forget plot]
table[row sep=crcr]{%
0 1\\
1 0.922376114248889\\
2 0.89280739362912\\
3 0.756393258911934\\
4 0.298231959620787\\
5 0.260719631500909\\
6 0.245396844243162\\
7 0.18301283597033\\
8 0.149379820900806\\
9 0.141688856117852\\
10 0.0915042691116316\\
11 0.0692977795197531\\
12 0.0687332177822504\\
13 0.0438430135856068\\
14 0.0342703823600005\\
15 0.0310765754933112\\
16 0.0162108702605621\\
17 0.0111113740774223\\
18 0.00649240799550402\\
19 0.00267559882211434\\
20 0.00126486514852597\\
21 0.000205881258659308\\
22 1.95308875917095e-05\\
23 2.42470212441251e-07\\
24 4.60765435070989e-10\\
25 1.67328301523398e-13\\
26 4.18737896779034e-16\\
};
\end{axis}
\end{tikzpicture}%

143
Claudio_Maggioni_3/main.m Normal file
View File

@ -0,0 +1,143 @@
%% Homework 3 - Optimization Methods
% Author: Claudio Maggioni
%
% Note: exercises are not in the right order due to matlab constraints of
% functions inside of scripts.
clc
clear
close all
% Set to non-zero to generate LaTeX for graphs
enable_m2tikz = 1;
if enable_m2tikz
addpath /home/claudio/git/matlab2tikz/src
else
matlab2tikz = @(a,b,c) 0;
end
syms x y;
f = (1 - x)^2 + 100 * (y - x^2)^2;
global fl
fl = matlabFunction(f);
%% 1.3 - Newton and GD solutions and energy plot
[x1, xs1, gs1] = Newton(f, [0;0], 50000, 1e-6, true);
plot(xs1(1, :), xs1(2, :), 'Marker', '.');
fprintf("Newton backtracking: it=%d\n", size(xs1, 2)-1);
xlim([-0.01 1.01])
ylim([-0.01 1.01])
hold on;
[x2, xs2, gs2] = Newton(f, [0;0], 50000, 1e-6, false);
plot(xs2(1, :), xs2(2, :), 'Marker', '.');
fprintf("Newton: it=%d\n", size(xs2, 2)-1);
[x3, xs3, gs3] = GD(f, [0;0], 50000, 1e-6, true);
plot(xs3(1, :), xs3(2, :), 'Marker', '.');
fprintf("GD backtracking: it=%d\n", size(xs3, 2)-1);
[x4, xs4, gs4] = GD(f, [0;0], 50000, 1e-6, false);
plot(xs4(1, :), xs4(2, :), 'Marker', '.');
fprintf("GD: it=%d\n", size(xs4, 2)-1);
hold off;
legend('Newton + backtracking', 'Newton', 'GD + backtracking', 'GD (alpha=1)')
sgtitle("Iterations of Newton and Gradient descent methods over 2D energy landscape");
matlab2tikz('showInfo', false, './ex1-3.tex');
figure;
plot(xs4(1, :), xs4(2, :), 'Marker', '.');
legend('GD (alpha=1)')
sgtitle("Iterations of Newton and Gradient descent methods over 2D energy landscape");
matlab2tikz('showInfo', false, './ex1-3-large.tex');
%% 2.3 - BGFS solution and energy plot
figure;
[x5, xs5, gs5] = BGFS(f, [0;0], eye(2), 50000, 1e-6);
xlim([-0.01 1.01])
ylim([-0.01 1.01])
plot(xs5(1, :), xs5(2, :), 'Marker', '.');
fprintf("BGFS backtracking: it=%d\n", size(xs5, 2)-1);
sgtitle("Iterations of BGFS method over 2D energy landscape");
matlab2tikz('showInfo', false, './ex2-3.tex');
%% 1.4 - Newton and GD gradient norm log
figure;
semilogy(0:size(xs1, 2)-1, gs1, 'Marker', '.');
ylim([5e-10, 1e12]);
xlim([-1, 30]);
hold on
semilogy(0:size(xs2, 2)-1, gs2, 'Marker', '.');
semilogy(0:size(xs3, 2)-1, gs3, 'Marker', '.');
semilogy(0:size(xs4, 2)-1, gs4, 'Marker', '.');
hold off
legend('Newton + backtracking', 'Newton', 'GD + backtracking', 'GD (alpha=1)')
sgtitle("Gradient norm w.r.t. iteration number for Newton and GD methods");
matlab2tikz('showInfo', false, './ex1-4-grad.tex');
figure;
semilogy(0:size(xs3, 2)-1, gs3, 'Marker', '.');
ylim([1e-7, 1e10]);
hold on
semilogy(0:size(xs4, 2)-1, gs4, 'Marker', '.');
hold off
legend('GD + backtracking', 'GD (alpha=1)')
sgtitle("Gradient norm w.r.t. iteration number for Newton and GD methods");
matlab2tikz('showInfo', false, './ex1-4-grad-large.tex');
figure;
ys1 = funvalues(xs1);
ys2 = funvalues(xs2);
ys3 = funvalues(xs3);
ys4 = funvalues(xs4);
ys5 = funvalues(xs5);
semilogy(0:size(xs1, 2)-1, ys1, 'Marker', '.');
ylim([5e-19, 1e12]);
xlim([-1, 30]);
hold on
semilogy(0:size(xs2, 2)-1, ys2, 'Marker', '.');
semilogy(0:size(xs3, 2)-1, ys3, 'Marker', '.');
semilogy(0:size(xs4, 2)-1, ys4, 'Marker', '.');
hold off
legend('Newton + backtracking', 'Newton', 'GD + backtracking', 'GD (alpha=1)')
sgtitle("Objective function value w.r.t. iteration number for Newton and GD methods");
matlab2tikz('showInfo', false, './ex1-4-ys.tex');
figure;
semilogy(0:size(xs3, 2)-1, ys3, 'Marker', '.');
semilogy(0:size(xs4, 2)-1, ys4, 'Marker', '.');
legend('GD + backtracking', 'GD (alpha=1)')
sgtitle("Objective function value w.r.t. iteration number for Newton and GD methods");
matlab2tikz('showInfo', false, './ex1-4-ys-large.tex');
%% 2.4 - BGFS gradient norms plot
figure;
semilogy(0:size(xs5, 2)-1, gs5, 'Marker', '.');
sgtitle("Gradient norm w.r.t. iteration number for BGFS method");
matlab2tikz('showInfo', false, './ex2-4-grad.tex');
%% 2.4 - BGFS objective values plot
figure;
semilogy(0:size(xs5, 2)-1, ys5, 'Marker', '.');
sgtitle("Objective function value w.r.t. iteration number for BGFS methods");
matlab2tikz('showInfo', false, './ex2-4-ys.tex');
function ys = funvalues(xs)
ys = zeros(1, size(xs, 2));
global fl
for i = 1:size(xs, 2)
ys(i) = fl(xs(1,i), xs(2,i));
end
end