-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiplecheese_arrsm.m
158 lines (149 loc) · 5.17 KB
/
multiplecheese_arrsm.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
% AGGRESSIVE-RESPONSE RESIDUAL SPACE MAPPING ALGORITHM
% MULTIPLE-CHEESE CUTTER ILLUSTRATION
%% House keeping
clc; close all;
clearvars;
co = [4 0]; fo =[4 4];
for ic = 1:length(co)
%% Inits
% desired fine model volume response
Raim = 10;
% candidate designable parameters
% initial guess; length and width
% n.b: if c ~= f then there is a misalignment
% to get the root solution, first calculate without misalignment, c == f
% then calculate with misalignment.
l=1; c = co(ic); w_c = 1;
l_c = moptm_coarse(Raim,[l, c, w_c]);
%
l_f = l_c; f1=fo(ic); f2=fo(ic); w_f=1; w_f1=1; w_f2=1;
%% Initial Model Guess
%coarse model
R_c = Rcoarse([l_c, c, w_c]);
%fine model
R_f = Rfine([l_f, f1, f2, w_f, w_f1, w_f2]);
Raim = [Raim Raim Raim];
% display
fprintf('l:%g\n', l_c)
fprintf('w:%g\n', w_c)
fprintf('R_c: %g\n',R_c)
fprintf('R_f: %g\n',R_f)
fprintf('Fine aim: %g\n',Raim)
id = 1;
% Parameter Extraction -> new coarse length mapping
rng default % For reproducibility
fun_x = @(x)norm(R_f-Rcoarse([l_c+x, c, w_c])); % cost function
options = optimoptions(@fminunc, 'Algorithm', 'quasi-newton');
x = fminunc(fun_x,1,options);
%
l = l_c + x;
f = l-l_c; % error vector
% Mapping Jacobian
h = Inf;
B = eye(1,1); % unit mapping
% init. surrogate and residual response
R_s = [0 0 0]; dR = 0;
chck = norm(Raim - R_f); % [] store error
Rf = R_f; % [] store fine model response
itTab = []; % store parameters per iteration
%%
while norm(h) > 1e-4
%% Prediction, Evaluate fine length
% Inverse mapping of coarse length;
h = -(f) / B; % quasi-newton step in fine space
l_f = l_f + h; % update
%% Verification
% coarse model
R_c = Rcoarse([l_c,c,w_c]);
% fine model
R_f = Rfine([l_f, f1, f2, w_f, w_f1, w_f2]);
Rf = [Rf; R_f];
% save tab
itTab= [itTab; [id f B h l_c R_c R_s l_f R_f (chck(id)*100/(sum(Raim)/3))]];
%% Next Iterate Prediction
fun_x = @(x)norm(R_f-Rcoarse([l_c+x, c w_c])); % cost function
options = optimoptions(@fminunc, 'Algorithm', 'quasi-newton');
x = fminunc(fun_x,1,options); % alternative: fminunc, but slower convergence
%
l = l_c + x;
f = l-l_c; % update error vector
if norm(f) < 1e-4
% update response residual
dR = R_f - R_c;
% optimise coarse length-> minimise surrogate
l_c = optim_sug(Raim,[l_c, c, w_c],dR);
f = l - l_c; % update error vector
% Inverse mapping of coarse length;
h = -(f) ./ B; % quasi-newton step in fine space
l_f = l_f + h;
%
R_s = Rsurrogate([l_c, c, w_c],dR);
B = eye(1,1); % constrain B to an identity mapping
else
% broyden rank-one update
B = B + ((f.*h')/(h'.*h));
end
%
% display
fprintf('\nIteration.%g\n', id)
fprintf('f: %g\n',f)
fprintf('l:%g\n', l_c)
fprintf('R_c: %g\n',R_c)
fprintf('R_s: %g\n',R_s)
fprintf('lf:%g\n', l_f)
fprintf('R_f: %g\n',R_f)
fprintf('Fine aim: %g\n',Raim)
%
chck = [chck; norm(Raim - R_f)]; %#ok<*AGROW>
itTab= [itTab; [id f B h l_c R_c R_s l_f R_f (chck(id)*100/(sum(Raim)/3))]];
% stop if limit attractor reached
% not converging
if (id >= 2)
if abs(chck(id) - chck(id-1)) <= 1e-6
break;
end
end
id = id + 1;
end
fprintf('Fine Model Parameters are: l=%g, w=%g, h=%g, volume=%g\n',...
l_f,w_f,1,R_f(1))
fprintf('Fine Model Parameters are: l=%g, w=%g, h=%g, volume=%g\n',...
l_f,w_f1,1,R_f(2))
fprintf('Fine Model Parameters are: l=%g, w=%g, h=%g, volume=%g\n',...
l_f,w_f2,1,R_f(3))
if ic == 1
chck_opt = chck;
l_opt = l_f;
end
end
%% Visualization
figure(1);
% subplot 1
subplot(211)
plot(1:numel(chck),chck,'-.sr','LineWidth',1.25)
grid on;
xlabel('Iteration','Interpreter','latex')
ylabel('Error, $$||R_{f}-{R_{f}}^{\ast}||$$',...
'FontSize',12,'Interpreter','latex')
title('Multiple Cheese Cutter:Response Residual Aggressive Space Mapping Optimization',...
'FontSize',10,'Interpreter','latex')
axis([1,inf,min(chck)-0.01,inf])
% subplot 2
subplot(212)
% figure(2);
for ix = 1:id
% l_err(ix) = norm(l_opt-itTab(ix,3));
l_err(ix) = norm(chck_opt(end)- chck(ix));%#ok<SAGROW>
end
plot(l_err,'Marker','.','MarkerSize',10,'LineWidth',1.25)
% plot(Rf,'-.o','LineWidth',1.25)
grid on;
xlabel('Iteration','Interpreter','latex')
ylabel('Error, $$||l_{f}-{l_{f}}^{\ast}||$$',...
'FontSize',12,'Interpreter','latex')
% ylabel('$$R_{f}$$',...
% 'FontSize',12,'Interpreter','latex')
title('Multiple Cheese Cutter: Fine-Model Response',...
'FontSize',10,'Interpreter','latex')
axis([1,inf,min(l_err)-0.01,inf])
% legend({'Cheese1','Cheese2','Cheese3'})