-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiplecheese_irrsm.m
149 lines (144 loc) · 5.1 KB
/
multiplecheese_irrsm.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
% IMLICIT-RESPONSE RESIDUAL SPACE MAPPING ALGORITHM-T1
% MULTIPLE-CHEESE CUTTER ILLUSTRATION
%% House keeping
clc; close all;
clearvars;
co = [4 2]; fo =[4 4];
% ic = 2;
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
% 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]);
% aim
Raim = [Raim Raim Raim];
% display
fprintf('\nIteration.%g\n', 0)
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;
itTab = []; % store parameters per iteration
chck = norm(Raim - R_f); % [] store norm error
itTab = [itTab; [0 0 0 0 0 l_c w_c R_c 0 0 0 R_f (chck(id)*100/(sum(Raim)/3))]];
optl = Inf;
Rf = R_f; % [] store fine model response
%% SM Iteration
while chck > 1e-3
%% Parameter Extraction -> width
% rng default % For reproducibility
if id ==1
fun_x = @(x)norm( R_f - Rcoarse([l_c, c, w_c+x]) ); % cost function
else
fun_x = @(x)norm( R_f - (Rsurrogate([l_c, c, w_c+x],dR) )); % cost function
end
% options = optimoptions(@fminunc, 'Algorithm', 'quasi-newton')
% options = optimoptions(@simulannealbnd, 'HybridFcn', 'fminunc')
% options = optimoptions(@particleswarm, 'SwarmSize',64,'HybridFcn', @fminsearch)
% fminsearch - derivative free- simplex method.
% options = optimoptions('fminimax','AbsoluteMaxObjectiveCount',3);
x = fminunc(fun_x,w_c); % alternative:fminsearch, or fminunc, but slower convergence
%
w_c = w_c+x;
%% Residual enhanced Surrogate
% coarse model
R_c = Rcoarse([l_c, c, w_c]);
% calculate residual
dR = R_f - R_c;
% new surrogate with residual
% R_s = (Rcoarse([l_c + l, c, w_c])+ dR);
R_s = Rsurrogate([l_c,c,w_c],dR);
% display
fprintf('\nIteration.%g\n', id)
fprintf('q1: %g\n',x)
fprintf('l:%g\n', l_c)
fprintf('w:%g\n', w_c)
fprintf('R_s: %g\n',R_s')
fprintf('R_c: %g\n',R_c')
fprintf('R_f: %g\n',R_f')
fprintf('Fine aim: %g\n',Raim')
% itTab = [itTab; [id x dR l_c w_c R_s R_c R_f (chck(id)*100/(norm(Raim)))]];
%% Optmise Surrogate, Prediction -> length
[l_c, lopt] = optim_sug(Raim,[l_c,c,w_c],dR);
l_f = l_c;
%% Fine Model 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];
% display
fprintf('\nIteration.%g\n', id)
fprintf('q1: %g\n',x)
fprintf('l:%g\n', l_c)
fprintf('w:%g\n', w_c)
fprintf('R_s: %g\n',R_s')
fprintf('R_c: %g\n',R_c')
fprintf('R_f: %g\n',R_f')
fprintf('Fine aim: %g\n',Raim')
% dR = R_f - R_c
%% Termination Condition
% calculate error-accuracy
chck = [chck; norm(Raim - R_f)]; %#ok<*AGROW>
% store parameters
itTab = [itTab; [id x dR l_c w_c R_s R_c R_f (chck(id)*100/(norm(Raim)))]];
% terminate iteration if a limit attractor reached.
if (id >= 2)
if abs(chck(id) - chck(id-1)) <= 1e-4
break;
end
end
id = id + 1;
end
% store the root fine model solution
if ic == 1
chck_opt = chck;
l_opt = l_f;
else
% display
formatSpec = 'Fine Model Parameters are: %g, %g, %g (units)\n';
fprintf(formatSpec,[l_f; w_f; 1])
formatSpec = 'Fine Model Cheese Volume are: %g, %g, %g (cubic units)\n';
fprintf(formatSpec,R_f')
end
end
%% Visualization
figure(1);
% subplot 1
subplot(211)
plot(1:numel(chck),chck,'-.sr','MarkerSize',5,'LineWidth',0.75)
grid on;
xlabel('Iteration','Interpreter','latex')
ylabel('$$Norm-2 Error$$',...
'FontSize',10,'Interpreter','latex')
title('Multiple Cheese Cutter: Implicit Response Residual Space Mapping Optimization-2',...
'FontSize',10,'Interpreter','latex')
axis([1,inf,min(chck)-0.01,inf])
%
subplot(212)
for ix = 1:id
l_err(ix) = norm(l_opt - itTab(ix,6));%#ok<SAGROW>
end
plot(l_err,'-.','Marker','.','MarkerSize',20,'LineWidth',0.25)
grid on;
xlabel('Iteration','Interpreter','latex')
ylabel('Error, $$||l_{f}-{l_{f}}^{\ast}||$$',...
'FontSize',10,'Interpreter','latex')
title('Multiple Cheese Cutter: Fine-Model Response',...
'FontSize',10,'Interpreter','latex')
axis([1,inf,min(l_err)-0.01,inf])