rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
RB_greedy_extension.m
Go to the documentation of this file.
1 function [detailed_data, reduced_data] = RB_greedy_extension(model,detailed_data)
2 %function [detailed_data,reduced_data] = ...
3 % RB_greedy_extension(model,detailed_data);
4 % function performing a greedy search loop for reduced basis
5 % generation.
6 %
7 % In each iteration the parameter vector with maximum posterior estimator or
8 % true error is selected for "refinement". An orthonormal basis is generated.
9 %
10 % The detailed_data is expected to be reasonably filled, that rb_simulations
11 % are possible with the given arguments, i.e. gen_reduced_data(model,
12 % detailed_data). That means, that it also can contain an existing basis,
13 % which is then extended in this loop. the offline_data for after the last
14 % extension is optionally returned.
15 %
16 % The stopping of the extention is performed, if
17 %
18 % - 'model.RB_stop_epsilon' is reached: basis vectors are added until
19 % the maximum posterior estimator is smaller than this
20 % value. M_val can be ommited in this case.
21 % - 'model.RB_stop_max_val_train_ratio': basis vectors are added until the
22 % ratio r of the max estimator on a validation set M_val with the
23 % estimator on the training set M is larger than r_max. M_val is
24 % required in this case
25 % - 'model.RB_stop_timeout': basis vectors are added until toc gives a
26 % time exceeding a maximum level given
27 % - 'model.RB_stop_Nmax': If given number of basis vectors in full
28 % basis is obtained.
29 % - 'model.p_part_early_refinement' is set to one and the estimated
30 % number of basis vectors necessary to reach etol is higher
31 % than 'RB_stop_Nmax'.
32 %
33 % The following fields are created/extended in 'detailed_data.RB_info':
34 % - 'M_first_errs' : a vector with the initial error-estimator for each
35 % M_train. Is determined to check, whether any
36 % extension has to be performed or not.
37 % - 'stopped_on_epsilon' : flag indicating whether epsilon-stop-crit is reached
38 % - 'stopped_on_timeout': flag indicating whether timeout is reached
39 % - 'stopped_on_Nmax': flag indicating whether Nmax is reached
40 % - 'stopped_on_max_val_train_ratio': flag indicating whether
41 % epsilon-stop-crit is reached
42 % - 'stopped_on_empty_extension' : flag indicating, whether the last
43 % extension failed due to accuracy problems, i.e. new
44 % basis-vector already in span of existing.
45 % - 'stopped_on_Nlimit_estimation': flag indicating ifthe extension
46 % process was stopped, because it was estimated that more
47 % than Nmax basis vectoras are needed to reach etol.
48 % Partition of the parameter domain can be initiated.
49 % - 'max_err_sequence' : the decreasing maximum posterior estimators or true
50 % errors in each iteration, which are present AFTER the
51 % respective basis extension. So in case of 'stopped_on_epsilon',
52 % the last entry of this field should be smaller than the desired
53 % epsilon.
54 % - 'mu_sequence' : sequence of mu_values chosen for
55 % basis extension i.e. list of worst-estimator mu_values as the
56 % columns of the matrix mu_values.
57 % - 'r_value_sequence' : ratios of val/train error-estimator in case of
58 % validation set providing, AFTER the respective basis-extension.
59 % - 'toc_value_sequence' : toc_values after each basis extension
60 % - 'M_last_errs' : the posterior estimators for all mu in M_train at final
61 % time AFTER the last respective basis extension
62 %
63 % Required fields in 'detailed_data.RB_info':
64 % - 'M_train' : matrix with parameter-vectors as columns, over which greedy
65 % search is to be performed.
66 % - 'M_val' : matrix with parameter-vectors as columns, which are used
67 % for validation. Only required, if the field
68 % 'model.stop_on_max_val_train_ratio' is set)
69 %
70 % The following fields of detailed_data are created/extended:
71 % - RB : is extended by the number of basis-vectors, overall being the
72 % orthonormal basis
73 % - RB_info : the fields of this info-structure are extended, as described
74 % above
75 %
76 % required fields of model:
77 % RB_extension_algorithm: function pointer to either
78 % RB_extension_max_error_snapshot() or
79 % RB_extension_PCA_fixspace()
80 % RB_error_indicator: either 'estimator' or 'error' indicating,
81 % whether an aposteriori indicator or true error
82 % is to be used
83 % RB_detailed_val_savepath : path to the detailed data of the
84 % validation set in validation mode. Is generated
85 % if not available.
86 % RB_detailed_train_savepath : path to the detailed data of the
87 % training set. Is generated if not available. Is
88 % only required in case of 'error' mode instead of
89 % 'estimator'
90 %
91 % additional fields as required by the selected
92 % extension algorithm and the reduced-basis-simulation must be set in model.
93 
94 % Bernard Haasdonk 26.5.2007
95 
96 if ~isfield(model,'RB_stop_timeout')
97  model.RB_stop_timeout = inf;
98 end;
99 
100 if ~isfield(model,'RB_stop_Nmax')
101  model.RB_stop_Nmax = inf;
102 end;
103 
104 if ~isfield(model,'RB_stop_epsilon')
105  model.RB_stop_epsilon = 0;
106 end;
107 
108 if ~isfield(model,'RB_stop_max_val_train_ratio')
109  model.RB_stop_max_val_train_ratio = inf;
110 end;
111 
112 if ~isfield(model,'RB_save_temp_detailed_data')
113  model.RB_save_temp_detailed_data = 0;
114 end;
115 
116 % check, whether val-error is wanted
117 check_val_error = 0;
118 if model.RB_stop_max_val_train_ratio < inf
119  check_val_error = 1;
120 end;
121 
122 if isfield(model,'p_part_early_refinement')
123  enable_p_partition = 1;
124 else
125  enable_p_partition = 0;
126 end
127 
128 %hp-greedy:
129 if isfield(model,'h_part')
130  enable_h_partition = 1;
131 else
132  enable_h_partition = 0;
133 end
134 
135 
136 
137 % make sure, that detailed-simulations for validation set are available
138 % in case of active validation criterion and wanted true error
139 if check_val_error
140  if isempty(detailed_data.RB_info.M_val)
141  error ('M_val must be provided in case of max_val_train_ratio mode!');
142  end;
143  if isequal(model.RB_error_indicator,'error')
144  detailed_val_savepath = model.RB_detailed_val_savepath;
145  save_detailed_simulations_ptr = model.save_detailed_simulations;
146  save_detailed_simulations_ptr(model,detailed_data, ...
147  detailed_data.RB_info.M_val, ...
148  detailed_val_savepath);
149  else
150  detailed_val_savepath = [];
151  end;
152 end;
153 
154 % make sure, that detailed-simulations for the training set are available in
155 % case of wanted true error
156 if isequal(model.RB_error_indicator,'error') | isequal(model.RB_error_indicator,'projection-error')
157  save_detailed_simulations_ptr = model.save_detailed_simulations;
158  save_detailed_simulations_ptr(model,detailed_data,...
159  detailed_data.RB_info.M_train, ...
160  model.RB_detailed_train_savepath);
161  detailed_train_savepath = model.RB_detailed_train_savepath;
162 else
163  detailed_train_savepath = 'dummy-string';
164 end;
165 
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 % determine mu-worst error-indicator for current training set %
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 
170 reduced_data = gen_reduced_data(model,detailed_data);
171 
172 model.N = reduced_data.N;
173 post_errs = rb_test_indicator(model, ...
174  detailed_data, reduced_data,...
175  detailed_data.RB_info.M_train,...
176  detailed_train_savepath);
177 % store real error as well, if estimator and error are computed for
178 % 'ei_estimator_test'
179 if isequal(model.RB_error_indicator,'ei_estimator_test')
180  post_errs_sequence = post_errs.error;
181  post_est_sequence = post_errs.estimator;
182  post_errs = post_errs.estimator;
183 end
184 
185 [max_errs, mu_inds] = max(post_errs);
186 max_err = max_errs(1); % maximum error (estimation)
187 mu_ind = mu_inds(1); % parameter index in RB_info.M_train
188 
189 % hp-greedy: first chosen mu is the first training parameter
190 if enable_h_partition
191 mu_ind = 1;
192 end;
193 
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 % initialize loop-break conditions %
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 stopped_on_epsilon = 0;
198 stopped_on_timeout = 0;
199 stopped_on_Nmax = 0;
200 stopped_on_max_val_train_ratio = 0;
201 stopped_on_empty_extension = 0;
202 stopped_on_Nlimit_estimation = 0;
203 N_limit_est_av=[]; % average over all estimations (pessimistic)
204 continue_loop = 1;
205 if isnan(max_err)
206  keyboard
207 end;
208 
209 % check whether epsilon has been reached
210 if max_err < model.RB_stop_epsilon
211  continue_loop = 0;
212  stopped_on_epsilon = 1;
213 end;
214 
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % initialize output quantities %
217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 max_pe = [];
219 mu_values = [];
220 mu_ind_seq = [];
221 toc_values = [];
222 if check_val_error
223  r_values = [];
224 end;
225 N_limit_est = [];% estimated amount of N needed
226 
227 
228 % initialize detailed_data.RB_info
229 if ~isfield(detailed_data,'RB_info')
230  detailed_data.RB_info = [];
231 end;
232 
233 N = model.get_rb_size(model,detailed_data);
234 
235 if ~isfield(detailed_data.RB_info,'max_err_sequence')
236  detailed_data.RB_info.max_err_sequence = nan(1,N);
237 end
238 if ~isfield(detailed_data.RB_info,'mu_sequence')
239  detailed_data.RB_info.mu_sequence = ...
240  nan(length(model.mu_names), N);
241 end
242 if ~isfield(detailed_data.RB_info,'mu_ind_seq')
243  detailed_data.RB_info.mu_ind_seq = ...
244  nan(1, N);
245 end
246 
247 if check_val_error && ~isfield(detailed_data.RB_info,'r_value_sequence')
248  detailed_data.RB_info.r_value_sequence = nan(1,N);
249 end
250 
251 if ~isfield(detailed_data.RB_info,'toc_value_sequence')
252  detailed_data.RB_info.toc_value_sequence = nan(1,N);
253 end
254 
255 detailed_data.RB_info.M_first_errs = post_errs;
256 
257 % perform extension loop:
258 while continue_loop
259  % Assume that mu_ind points to the current update mu-vector
260  disp(['Detected maximum error prediction ',num2str(max_err),...
261  ' for mu=[',...
262  num2str(detailed_data.RB_info.M_train(:,mu_ind)'),']']);
263 
264  % Use original params with new mu such that filecaching works in the
265  % extension routine!
266  mu = detailed_data.RB_info.M_train(:,mu_ind);
267 
268  oldmu = model.get_mu(model);
269  model = model.set_mu(model, mu);
270  [RBext, par] = model.RB_extension_algorithm(model, ...
271  detailed_data);
272  model = model.set_mu(model, oldmu);
273 
274  % if empty: no further processing has to be performed
275  if isempty(RBext)
276  stopped_on_empty_extension = 1;
277  continue_loop = 0;
278  else
279  stopped_on_empty_extension = 0;
280  if model.verbose > 29
281  disp(par);
282  end;
283  max_pe = [max_pe, max_err];
284  mu_ind_seq = [mu_ind_seq, mu_ind];
285  mu_values = [mu_values, detailed_data.RB_info.M_train(:,mu_ind)];
286  toc_values = [toc_values, toc];
287  RB = [model.get_rb_from_detailed_data(detailed_data), RBext];
288  % perform permutation that maintains original order if possible:
289  %RBo = model_orthonormalize_qr(model,detailed_data,RB,eps);
290  %K = model.inner_product(model,detailed_data,RBo,RB);
291  %[m,i] = max(abs(K));
292  %RB = RBo(:,i);
293 % if model.debug
294  % K = model.inner_product(model,detailed_data,RB,RB);
295  % if max(max(abs(K-eye(size(K)))))>1e-10
296  % %error('error in orthonormal basis extension!');
297  % end;
298 % % end;
299  detailed_data = model.set_rb_in_detailed_data(...
300  detailed_data,...
301  RB);
302  %detailed_data.RB = [detailed_data.RB, RBext];
303  N = model.get_rb_size(model,detailed_data);
304  detailed_data.N = N;
305  disp(['Extended RB to length ', num2str(N)]);
306 
307  if model.RB_save_temp_detailed_data
308  save(fullfile(rbmatlabtemp,'temp_detailed_data.mat'),'detailed_data', ...
309  'model')
310  end;
311 
312  end;
313 
314 
315  %orthogonality check
316 
317  RB = model.get_rb_from_detailed_data(detailed_data);
318  W = model.get_inner_product_matrix(detailed_data);
319  disp(['checking othogonality with tolerance ',num2str(1e-12),...
320  '...',...
321  num2str(max(max(RB'*W*RB-eye(size(RB,2)))))]);
322  if(max(max(RB'*W*RB-eye(size(RB,2))))>1e-12)
323  disp('orthogonalising again...')
324  detailed_data = model.set_rb_in_detailed_data(detailed_data,...
325  model_orthonormalize_qr(model, ...
326  detailed_data,RB));
327  RB = model.get_rb_from_detailed_data(detailed_data);
328  disp(['new orthogonality value: ',num2str(max(max(RB'*W*RB-eye(size(RB,2)))))]);
329  end
330 
331 % keyboard;
332 
333  % check on accuracy problem (non-decreasing error)
334 % if length(max_pe)>1 & max_pe(end)>max_pe(end-1)
335 % stopped_on_error_increase = 1;
336 % continue_loop = 0;
337 % disp('stopped on error-increase! Please check!');
338 % keyboard;
339 % else
340 % stopped_on_error_increase = 0;
341 % end;
342 
343 
344  if ~stopped_on_empty_extension
345  % determine error estimators for extended basis for next loop
346  reduced_data = gen_reduced_data(model,detailed_data);
347  model.Nmax = model.get_rb_size(model,detailed_data);
348  model.N = model.Nmax;
349  % model = set_mu(detailed_data.RB_info.M_train(:,mu_ind), ...
350  % model);
351  post_errs = rb_test_indicator(model,...
352  detailed_data, reduced_data,...
353  detailed_data.RB_info.M_train,...
354  detailed_train_savepath);
355  if isequal(model.RB_error_indicator,'ei_estimator_test')
356  post_errs_sequence = [post_errs_sequence, post_errs.error];
357  post_est_sequence = [post_est_sequence, post_errs.estimator];
358  post_errs = post_errs.estimator;
359  end
360  [max_errs, mu_inds] = max(post_errs);
361  max_err = max_errs(1);
362  mu_ind = mu_inds(1);
363 
364 % model.Nmax = size(detailed_data.RB,2);
365 % model.N = model.Nmax;
366 % [mu_ind, post_errs] = detect_mu_worst(detailed_data,model);
367 % max_err = max(post_errs);
368 
369  % check loop-break conditions
370  if max_err < model.RB_stop_epsilon
371  continue_loop = 0;
372  stopped_on_epsilon = 1;
373  end;
374 
375  if model.get_rb_size(model,detailed_data) >= model.RB_stop_Nmax
376  continue_loop = 0;
377  stopped_on_Nmax = 1;
378  end;
379 
380  if check_val_error
381  val_est = rb_test_indicator(model,...
382  detailed_data,reduced_data,...
383  detailed_data.RB_info.M_val, ...
384  detailed_val_savepath);
385 
386  r_values = [r_values, max(val_est) / max_err];
387  %debug
388  disp(['val_train ratio: ',num2str(r_values(end))]);
389  %end debug
390  if (r_values(end) > model.RB_stop_max_val_train_ratio)
391  stopped_on_max_val_train_ratio = 1;
392  continue_loop = 0;
393  end;
394  end;
395 
396  %in models using adaptive p-partition early stopping:
397  %an estimater extrapolates the curve N->epsilon and estimates if the
398  %epsilon_tol can be reached with a basis size smaller than Nmax. If no,
399  %the field RB_stopped_on_Nlimit_estimation is set to 1
400 
401 
402  if enable_p_partition && model.p_part_early_refinement
403  %estimate N total needed to reach etol:
404  err_seq=max_pe;
405  etol = model.RB_stop_epsilon;
406  Nmax = model.RB_stop_Nmax;
407 
408  s=20; %stepsize backwards
409 
410 
411  if(length(err_seq)>(s))
412 
413  %for i=(s+1):length(err_seq);
414  i=length(err_seq);
415  a=(log(err_seq(i-s)/err_seq(i)))/(s);
416  N_limit_est=[N_limit_est,i-1/a*log(etol/err_seq(i))];
417  %end;
418 
419 
420 
421  %for i=1:length(N_limit_est);
422  %summe = summe+N_limit_est(i);
423  if(length(N_limit_est_av>0))
424  N_1=length(N_limit_est_av);
425  newAv=N_1/(N_1+1)*N_limit_est_av(end)+(1/(N_1+1))*N_limit_est(end);
426  N_limit_est_av=[N_limit_est_av,newAv];
427 
428  else
429  N_limit_est_av=N_limit_est(end);
430  end
431  %end;
432  disp(['actual estimation of N needed to reach etol: ',num2str(N_limit_est_av(end))]);
433  if(N_limit_est_av(end)>Nmax)
434  stopped_on_Nlimit_estimation = 1;
435  continue_loop = 0;
436  end
437  end
438 
439 
440  end %of P-Part_early_refinement
441 
442  end; % of ~stopped_on_empty_extension
443 
444  if toc > model.RB_stop_timeout
445  stopped_on_timeout = 1;
446  continue_loop = 0;
447  end;
448 
449 end; % end while loop
450 
451 % prepare output data : RB is already updated
452 
453 RB_info = detailed_data.RB_info;
454 
455 RB_info.stopped_on_epsilon = stopped_on_epsilon;
456 RB_info.stopped_on_max_val_train_ratio = stopped_on_max_val_train_ratio;
457 RB_info.stopped_on_timeout = stopped_on_timeout;
458 RB_info.stopped_on_Nmax = stopped_on_Nmax;
459 RB_info.stopped_on_empty_extension = stopped_on_empty_extension;
460 RB_info.stopped_on_Nlimit_estimation = stopped_on_Nlimit_estimation;
461 
462 RB_info.M_last_errs = post_errs;
463 RB_info.max_err_sequence = [RB_info.max_err_sequence, max_pe];
464 RB_info.mu_ind_seq = [RB_info.mu_ind_seq, mu_ind_seq];
465 RB_info.mu_sequence = [RB_info.mu_sequence, mu_values];
466 if isequal(model.RB_error_indicator,'ei_estimator_test')
467  RB_info.post_errs_sequence = post_errs_sequence;
468  RB_info.post_est_sequence = post_est_sequence;
469 end
470 if check_val_error
471  RB_info.r_value_sequence = [RB_info.r_value_sequence, r_values];
472 end;
473 RB_info.toc_value_sequence = [RB_info.toc_value_sequence, toc_values];
474 
475 if enable_p_partition && model.p_part_early_refinement
476  RB_info.N_limit_est = N_limit_est;
477  RB_info.N_limit_est_av = N_limit_est_av;
478 end
479 
480 
481 detailed_data.RB_info = RB_info;
482