-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
312 lines (264 loc) · 12.8 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
clear;
clc;
close all;
format long
global coordX coordY iter
iter = 0;
% graph initialization
figure(1)
figure(2)
% ==================== Select a starting point ====================
startingpoint = 4; % Choose a starting point between 1-4 by changing the starting point variable
X0 = [2 0.5; 1 -1.5; -1 -1.5; -1 0.5]; % starting points
x0 = [X0(startingpoint,1) X0(startingpoint,2)]; % starting point vector value
figure(1) % 2D contour plot of a function with marked trajectories
hold on
axis tight
[X, Y] = meshgrid(-4:0.001:4,-4:0.001:4);
Z = plotRosenbrock(X, Y); % compute the contour value of the Rosenbrock function
[M, c] = contourf(X,Y,log(Z),'ShowText','on');
c.Fill = 0; % exclusion of colors
c.LineWidth = 0.33; % reduction of line size for better visibility
% If there is no graphs folder, create one.
if ~exist("./graphs", 'dir')
mkdir("./graphs")
end
% ==================== Calling individual methods ====================
% select a method by changing the value of the choice variable, where:
% 1 - means the Quasi-Newton method
% 2 - means the Region of Trust method without the given Hessian
% 3 - means the Region of Trust method with the given Hessian
% 4 - means the Nelder-Mead method
choice = 4;
switch choice
case 1
[x, value] = optimQuasiNewton(x0, startingpoint);
case 2
[x, value] = optimTrustRegion(x0, startingpoint);
case 3
[x, value] = optimTrustRegionHessian(x0, startingpoint);
case 4
[x, value] = optimSimplex(x0, startingpoint);
end
% !!! before running the script, you must install: Optimization Toolbox
% source: https://www.google.com/search?q=Optimization+Toolbox&sourceid=chrome&ie=UTF-8
function [x, value] = optimQuasiNewton(x0, startingpoint) % the function responsible for the optimization of the unconsidered function by the Quasi-Newton method
global coordX coordY iter
% setting the appropriate settings for the solver
options = optimoptions(@fminunc,'OutputFcn',@outputQuasiNewton,'Display', ...
'iter-detailed','Algorithm','quasi-newton','MaxIterations', 1000, 'StepTolerance', ...
1e-12, 'FunctionTolerance', 1e-12);
[x, value] = fminunc(@rosenbrock, x0, options); % solver invocation
function stop = outputQuasiNewton(x, optimValues, state)
stop = false; % setting the stop variable responsible for the operation of the function
if isequal(state,'init') % preparation of charts for marking subsequent iterations
figure(1)
hold on
title('Quasi-Newton method')
xlim([-3 3])
ylim([-3 3])
xlabel("x value")
ylabel("y value")
figure(2)
hold on
title('Quasi-Newton method (objective function values)');
xlabel("number of iterations [n]")
ylabel("function value [log(y)]")
set(gca, 'YScale', 'log') % displaying the Y axis on a logarithmic scale
elseif isequal(state,'iter') % updating the graph in subsequent iterations
iter = iter + 1;
figure(1)
coordX(iter) = x(1);
coordY(iter) = x(2);
figure(2)
plot(optimValues.iteration, optimValues.fval, 'bx')
elseif isequal(state,'done') % support for the final iteration of a function
figure(1)
plot(coordX(1,1:iter), coordY(1,1:iter), 'ro-', 'MarkerSize', 4)
xlim([min(coordX)-0.05 max(coordX)+0.05]);
ylim([min(coordY)-0.05 max(coordY)+0.05]);
path = char("./graphs/QuasiNewton1_" + startingpoint + ".png");
saveas(gcf,path);
fprintf('Grapgh no. 1 has been saved\n')
hold off
figure(2)
path = char("./graphs/QuasiNewton2_" + startingpoint + ".png");
saveas(gcf,path);
fprintf('Grapgh no. 2 has been saved\n')
hold off
end
end
end
% !!! before running the script, you must install: Optimization Toolbox
% source: https://www.google.com/search?q=Optimization+Toolbox&sourceid=chrome&ie=UTF-8
function [x, value] = optimTrustRegion(x0, startingpoint) % the function responsible for the optimization of the unconsidered function by the Trust-Region method without the given Hessian
global coordX coordY iter
% setting the appropriate settings for the solver
options = optimoptions(@fminunc,'OutputFcn',@outputTrustRegion,'Display', ...
'iter-detailed','Algorithm','trust-region', 'SpecifyObjectiveGradient',true, ...
'MaxIterations', 1000, 'StepTolerance', 1e-12, 'FunctionTolerance', 1e-12);
[x, value] = fminunc(@rosenbrockwithgrad, x0, options); % solver invocation
function stop = outputTrustRegion(x, optimValues, state)
stop = false; % setting the stop variable responsible for the operation of the function
if isequal(state,'init') % preparation of charts for marking subsequent iterations
figure(1)
hold on
title('Trust-Region method without the given Hessian')
xlim([-3 3])
ylim([-3 3])
xlabel("x value")
ylabel("y value")
figure(2)
hold on
title('Trust-Region method without the given Hessian (objective function values)');
xlabel("number of iterations [n]")
ylabel("function value [log(y)]")
set(gca, 'YScale', 'log') % displaying the Y axis on a logarithmic scale
elseif isequal(state,'iter') % updating the graph in subsequent iterations
iter = iter + 1;
figure(1)
coordX(iter) = x(1);
coordY(iter) = x(2);
figure(2)
plot(optimValues.iteration, optimValues.fval, 'bx')
elseif isequal(state,'done') % support for the final iteration of a function
fprintf('done \n')
figure(1)
plot(coordX(1,1:iter), coordY(1,1:iter), 'ro-', 'MarkerSize', 4)
xlim([min(coordX)-0.05 max(coordX)+0.05]);
ylim([min(coordY)-0.05 max(coordY)+0.05]);
path = "./graphs/TrustRegion1_" + startingpoint + ".png";
saveas(gcf,path);
fprintf('Grapgh no. 1 has been saved\n')
hold off
figure(2)
path = "./graphs/TrustRegion2_" + startingpoint + ".png";
saveas(gcf,path);
fprintf('Grapgh no. 2 has been saved\n')
hold off
end
end
end
% !!! before running the script, you must install: Optimization Toolbox
% source: https://www.google.com/search?q=Optimization+Toolbox&sourceid=chrome&ie=UTF-8
function [x, value] = optimTrustRegionHessian(x0, startingpoint) % function responsible for optimizing the considered function using the Trust-Region method with the given Hessiann
global coordX coordY iter
% setting the appropriate settings for the solver
options = optimoptions(@fminunc,'OutputFcn',@outputTrustRegionHessian,'Display', ...
'iter-detailed','Algorithm','trust-region', 'SpecifyObjectiveGradient',true, ...
'MaxIterations', 1000, 'StepTolerance', 1e-12, 'FunctionTolerance', 1e-12, 'HessianFcn', 'objective');
[x, value] = fminunc(@rosenbrockwithhes, x0, options); % solver invocation
function stop = outputTrustRegionHessian(x, optimValues, state)
stop = false; % setting the stop variable responsible for the operation of the function
if isequal(state,'init') % preparation of charts for marking subsequent iterations
figure(1)
hold on
title('Trust-Region method with the given Hessiann')
xlim([-3 3])
ylim([-3 3])
xlabel("x value")
ylabel("y value")
figure(2)
hold on
title('Trust-Region method with the given Hessiann (objective function values)');
xlabel("number of iterations [n]")
ylabel("function value [log(y)]")
set(gca, 'YScale', 'log') % displaying the Y axis on a logarithmic scale
elseif isequal(state,'iter') % updating the graph in subsequent iterations
iter = iter + 1;
figure(1)
coordX(iter) = x(1);
coordY(iter) = x(2);
figure(2)
plot(optimValues.iteration, optimValues.fval, 'bx')
elseif isequal(state,'done') % support for the final iteration of a function
fprintf('done \n')
figure(1)
plot(coordX(1,1:iter), coordY(1,1:iter), 'ro-', 'MarkerSize', 4)
xlim([min(coordX)-0.05 max(coordX)+0.05]);
ylim([min(coordY)-0.05 max(coordY)+0.05]);
path = "./graphs/TrustRegionHessian1_" + startingpoint + ".png";
saveas(gcf,path);
fprintf('Grapgh no. 1 has been saved\n')
hold off
figure(2)
path = "./graphs/TrustRegionHessian2_" + startingpoint + ".png";
saveas(gcf,path);
fprintf('Grapgh no. 2 has been saved\n')
hold off
end
end
end
function [x, value] = optimSimplex(x0, startingpoint) % the function responsible for the optimization of the considered function by the Nelder-Mead method
global coordX coordY iter
options = optimset('OutputFcn', @outputSimplex,...
'MaxFunEvals', 1000, 'TolX', 1e-12, 'TolFun', 1e-12);
[x, value, exitflag, output] = fminsearch(@rosenbrock, x0, options); % solver invocation
function stop = outputSimplex(x, optimValues, state)
stop = false; % setting the stop variable responsible for the operation of the function
if isequal(state,'init') % preparation of charts for marking subsequent iterations
figure(1)
hold on
title('Nelder-Mead method')
xlim([-3 3])
ylim([-3 3])
xlabel("x value")
ylabel("y value")
figure(2)
hold on
title('Nelder-Mead method (objective function values)');
xlabel("number of iterations [n]")
ylabel("function value [log(y)]")
set(gca, 'YScale', 'log') % displaying the Y axis on a logarithmic scale
elseif isequal(state,'iter') % updating the graph in subsequent iterations
iter = iter + 1;
figure(1)
coordX(iter) = x(1);
coordY(iter) = x(2);
figure(2)
plot(optimValues.iteration, optimValues.fval, 'bx')
elseif isequal(state,'done') % support for the final iteration of a function
fprintf('done \n')
figure(1)
plot(coordX(1,1:iter), coordY(1,1:iter), 'ro-', 'MarkerSize', 4)
xlim([min(coordX)-0.05 max(coordX)+0.05]);
ylim([min(coordY)-0.05 max(coordY)+0.05]);
path = "./graphs/Nelder-Mead1_" + startingpoint + ".png";
saveas(gcf,path);
fprintf('Grapgh no. 1 has been saved\n')
hold off
figure(2)
path = "./graphs/Nelder-Mead2_" + startingpoint + ".png";
saveas(gcf,path);
fprintf('Grapgh no. 2 has been saved\n')
hold off
end
end
output
end
% The values of the constants a = 0 and b = -0.5 for the Rosenbrock ("banana") function:
% f(x) = (1-x+a)^2 + 100[y-b-(x-a)^2]^2
function f = plotRosenbrock(x, y) % function required to plot the Rosenbrock function
f = (1-x).^2 + 100 * (y + 0.5 - x.^2).^2;
end
function f = rosenbrock(x) % proper function used to optimize Rosenbrock function
f = (1-x(1)).^2 + 100 * (x(2) + 0.5 - x(1).^2).^2;
end
function [f,g] = rosenbrockwithgrad(x) % correct function used to optimize Rosenbrock functions with defined gradient
f = (1-x(1)).^2 + 100 * (x(2) + 0.5 - x(1).^2).^2;
if nargout > 1 % if there is more than one argument that the function returns
g = [400*((-x(2)-0.495)*x(1)+x(1).^3-0.005); % gradient calculated, according to the formula: https://wikimedia.org/api/rest_v1/media/math/render/svg/d632a346cd0677aef80d9fa32f476a5b5bf4dc58
200*(x(2)-x(1).^2+0.5)];
end
end
function [f, g, H] = rosenbrockwithhes(x) % function used to optimize a Rosenbrock function with a defined gradient and given hesian
f = (1 - x(1)).^2 + 100 * (x(2) + 0.5 - x(1).^2).^2;
if nargout > 1
g = [400 * ((-x(2) - 0.495) * x(1) + x(1).^3 - 0.005);
200 * (x(2) - x(1).^2 + 0.5)];
if nargout > 2
H = [-400*(-1 * x(1).^2 + x(2) + 0.5) + 800 * x(1).^2 + 2, -400 * x(1);
-400 * x(1), 200];
end
end
end