KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ABase.m
Go to the documentation of this file.
1 namespace approx{
2 namespace algorithms{
3 
4 
5 /* (Autoinserted by mtoc++)
6  * This source code has been filtered by the mtoc++ executable,
7  * which generates code that can be processed by the doxygen documentation tool.
8  *
9  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
10  * Except for the comments, the function bodies of your M-file functions are untouched.
11  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
12  * attached source files that are highly readable by humans.
13  *
14  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
15  * the correct locations in the source code browser.
16  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
17  */
18 
19 class ABase
20  :public KerMorObject,
21  public ICloneable,
53  public: /* ( setObservable ) */
54 
70  UsefScaling = false;
109  public:
110 
199  public:
200 
211  protected:
212 
230  public:
231 
232  ABase() {
233  this = this@KerMorObject;
234 
235  this.registerProps(" ExpConfig "," UsefScaling ");
236  }
237 
238 
239  function copy = clone(copy) {
240  if ~isempty(this.ExpConfig)
241  copy.ExpConfig= this.ExpConfig.clone;
242  end
243  copy.UsefScaling= this.UsefScaling;
244  copy.ErrorFun= this.ErrorFun;
245  copy.MaxErrors= this.MaxErrors;
246  copy.MaxRelErrors= this.MaxRelErrors;
247  copy.ValidationErrors= this.ValidationErrors;
248  copy.ValidationRelErrors= this.ValidationRelErrors;
249  copy.LastCompTime= this.LastCompTime;
250  copy.StopFlags= this.StopFlags;
251  copy.ScalingG= this.ScalingG;
252  copy.BestExpConfig= this.BestExpConfig;
253  }
254 
255 
256  function kernels.KernelExpansionkexp = computeApproximation(data.ApproxTrainData atd,avd) {
257  time = tic;
258 
259  if nargin < 3
260  avd = atd;
261  end
262 
263  if isempty(this.ExpConfig)
264  error(" No ExpConfig set. Aborting. ");
265  elseif this.ExpConfig.getNumConfigurations == 0
266  error(" Need at least one expansion configuration. ");
267  end
268 
269  /* Scale f-values if wanted */
270  if this.UsefScaling
271  [fm,fM] = atd.fxi.getColBoundingBox;
272  s = max(abs(fm),abs(fM));
273  s(s==0) = 1;
274  oldfxi = atd.fxi.toMemoryMatrix;
275  oldvalfxi = avd.fxi.toMemoryMatrix;
276  atd.fxi(:,:) = oldfxi ./ repmat(s,1,size(atd.fxi,2));
277  /* Apply same scaling to validation data */
278  if nargin > 2
279  avd.fxi(:,:) = oldvalfxi ./ repmat(s,1,size(avd.fxi,2));
280  end
281  this.ScalingG= diag(s);
282  else
283  this.ScalingG= 1;
284  end
285 
286  /* Call template method for component wise approximation */
287  kexp = this.templateComputeApproximation(atd, avd);
288 
289  /* Rescale if set */
290  if this.UsefScaling
291  kexp.Ma= this.ScalingG*kexp.Ma;
292  atd.fxi(:,:) = oldfxi;
293  if nargin > 2
294  avd.fxi(:,:) = oldvalfxi;
295  end
296  end
297 
298  /* Reduce the snapshot array and coeff data to the
299  * really used ones! This means if any snapshot x_n is
300  * not used in any dimension, it is kicked out at this
301  * stage. */
302  n = size(kexp.Centers.xi,2);
303  hlp = sum(abs(kexp.Ma) ~= 0,1);
304  usedidx = find(hlp > 0);
305  /* If all is zero, create a zero approx with one center and zero
306  * coefficient */
307  if isempty(usedidx)
308  usedidx = 1;
309  end
310  if length(usedidx) < n
311  kexp.Ma= kexp.Ma(:,usedidx);
312  kexp.Centers.xi= kexp.Centers.xi(:,usedidx);
313  if isfield(kexp.Centers," ti ") && ~isempty(kexp.Centers.ti)
314  kexp.Centers.ti= kexp.Centers.ti(:,usedidx);
315  end
316  if isfield(kexp.Centers," mui ") && ~isempty(kexp.Centers.mui)
317  kexp.Centers.mui= kexp.Centers.mui(:,usedidx);
318  end
319  end
320 
321  /* Create sparse representation if many elements are zero */
322  if sum(hlp) / numel(kexp.Ma) < .5
323  kexp.Ma= sparse(kexp.Ma);
324  end
325  this.LastCompTime= toc(time);
326  }
327 
328 
329  function [str , rangetab ] = getApproximationSummary() {
330  str = [object2str(this,1) char(13)];
331 
332  ec = this.ExpConfig;
333  /* Computation time */
334  str = [str sprintf(" Total computation time for %d configurations: %gs (%gmin,%gh)\n ",...
335  ec.getNumConfigurations,this.LastCompTime,...
336  this.LastCompTime/60,this.LastCompTime/3600)];
337  /* Configuration */
338  str = [str sprintf(" Best expansion configuration at index %d:\n%s\n ",this.BestExpConfig,...
339  ec.getConfigurationString(this.BestExpConfig))];
340  rangetab = ec.getValueRanges;
341  if nargout < 2
342  str = [str sprintf(" Expansion configuration ranges:\n%s\n ",...
343  rangetab.toString)];
344  if nargout < 1
345  disp(str);
346  end
347  end
348  }
356  function plotSummary(pm,context) {
357  if nargin < 3
358  context = " Summary ";
359  if nargin < 2
360  pm = PlotManager(false,1,2);
361  pm.LeaveOpen= true;
362  end
363  end
364 
365  str = sprintf(" %s: Max absolute training errors ",context);
366  h = pm.nextPlot(" maxerrors ",str,...
367  " expansion size "," error ");
368  ph = semilogy(h,1:size(this.MaxErrors,2),this.MaxErrors^t);
369  if ~isempty(this.ExpConfig)
370  set(ph(this.BestExpConfig)," LineWidth ",2);
371  end
372  str = sprintf(" %s: Max relative training errors ",context);
373  h = pm.nextPlot(" maxrelerrors ",str,...
374  " expansion size "," error ");
375  ph = semilogy(h,1:size(this.MaxRelErrors,2),this.MaxRelErrors^t);
376  if ~isempty(this.ExpConfig)
377  set(ph(this.BestExpConfig)," LineWidth ",2);
378  end
379  str = sprintf(" %s: Absolute validation errors ",context);
380  h = pm.nextPlot(" valerrors ",str,...
381  " configuration nr "," error ");
382  semilogy(h,1:length(this.ValidationErrors),this.ValidationErrors);
383  legend(" max "," mean ");
384 
385  str = sprintf(" %s: Relative validation errors ",context);
386  h = pm.nextPlot(" valrelerrors ",str,...
387  " configuration nr "," error ");
388  semilogy(h,1:length(this.ValidationRelErrors),this.ValidationRelErrors);
389  legend(" max "," mean ");
390 
391 
392  if nargin < 2
393  pm.done;
394  end
395  }
396 
397 
398  function nc = getTotalNumConfigurations() {
399  nc = 1;
400  if ~isempty(this.ExpConfig)
402  end
403  }
404 
405 
406  protected:
407 
408  function [doubleval , integeridx , rowvecerrs ] = getError(kernels.KernelExpansion kexp,data.ApproxTrainData atd) {
409  e = atd.fxi - kexp.evaluate(atd.xi, atd.ti, atd.mui);
410  errs = this.ErrorFun(this.ScalingG*e);
411  [val, idx] = max(errs);
412  }
435  protected: /* ( Abstract ) */
436 
449  protected: /* ( Static ) */
450 
451  static function this = loadobj(this,initfrom) {
452  if nargin > 1
453  this.ErrorFun= initfrom.ErrorFun;
454  this.LastCompTime= initfrom.LastCompTime;
455  this.UsefScaling= initfrom.UsefScaling;
456  if isfield(initfrom," ExpConfig ")
457  this.ExpConfig= initfrom.ExpConfig;
458  end
459  if isfield(initfrom," MaxErrors ") && ~isempty(initfrom.MaxErrors)
460  this.MaxErrors= initfrom.MaxErrors;
461  elseif isfield(initfrom," err ")
462  this.MaxErrors= initfrom.err;
463  end
464  if isfield(initfrom," MaxRelErrors ") && ~isempty(initfrom.MaxRelErrors)
465  this.MaxRelErrors= initfrom.MaxRelErrors;
466  elseif isfield(initfrom," relerr ")
467  this.MaxRelErrors= initfrom.relerr;
468  end
469  if isfield(initfrom," ValidationErrors ") && ~isempty(initfrom.ValidationErrors)
470  this.ValidationErrors= initfrom.ValidationErrors;
471  end
472  if isfield(initfrom," ValidationRelErrors ") && ~isempty(initfrom.ValidationRelErrors)
473  this.ValidationRelErrors= initfrom.ValidationRelErrors;
474  end
475  if ~isfield(initfrom," StopFlags ") || isempty(initfrom.StopFlags)
476  this.StopFlags= zeros(size(this.MaxErrors));
477  end
478  this = loadobj@KerMorObject(this, initfrom);
479  else
480  if isempty(this.StopFlags)
481  this.StopFlags= zeros(size(this.MaxErrors));
482  end
483  this = loadobj@KerMorObject(this);
484  end
485  }
486 
487 
488 };
489 }
490 }
491 
matrix< double > ValidationErrors
For each configuration, contains a row with the maximum errors on the validation data. The number of columns depends on the type of algorithm implemented by the subclasses.
Definition: ABase.m:154
function double t = getValueRanges()
Definition: IClassConfig.m:80
matrix< double > MaxRelErrors
For each configuration, contains a row with the maximum relative errors on the training data...
Definition: ABase.m:139
matrix< integer > StopFlags
For each effective configuration, the stop flags are stored here.
Definition: ABase.m:184
data.FileMatrix xi
The state space samples , stored row-wise in a data.FileMatrix instance.
function nc = getTotalNumConfigurations()
Definition: ABase.m:398
ICLONEABLE Interface for cloneable handle classes.
Definition: ICloneable.m:17
virtual function integer n = getNumConfigurations()
Returns the number of configurations that can be applied.
data.FileMatrix fxi
The evaluations of , stored row-wise in a data.FileMatrix.
IClassConfig StateConfig
The state space kernel configuration.
integer BestExpConfig
Index of the best expansion config determined by the algorithm.
Definition: ABase.m:201
A double value.
Base class for any KerMor class.
Definition: KerMorObject.m:17
function registerProps(varargin)
Call this method at any class that defines DPCM observed properties.
Definition: DPCMObject.m:125
An integer value.
matrix< double > ValidationRelErrors
For each configuration, contains a row with the maximum relative errors on the validation data...
Definition: ABase.m:169
kernels.config.ExpansionConfig ExpConfig
The different kernel expansion configurations to try.
Definition: ABase.m:55
A MatLab function handle.
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
function copy = clone(copy)
The interface method with returns a copy of the current class instance.
Definition: ABase.m:239
rowvec ti
The time samples .
function fx = evaluate(matrix x, varargin)
Evaluates the kernel expansion.
double LastCompTime
The computation time for the last run in seconds.
Definition: ABase.m:111
function kernels.KernelExpansion kexp = computeApproximation(data.ApproxTrainData atd, avd)
Definition: ABase.m:256
KerMorObject()
Constructs a new KerMor object.
Definition: KerMorObject.m:55
function [ double val , integer idx , rowvec errs ] = getError(kernels.KernelExpansion kexp,data.ApproxTrainData atd)
Computes the error according to the chosen error function (see ErrorFun property) with respect to the...
Definition: ABase.m:408
function_handle ErrorFun
The error function to apply on each data vector.
Definition: ABase.m:87
IReductionSummaryPlotProvider:
disp
Handle object disp method which is called by the display method. See the MATLAB disp function...
function n = getNumConfigurations()
Returns the number of configurations that can be applied.
virtual function templateComputeApproximation(kernels.KernelExpansion kexp,data.ApproxTrainData atd, avd)
Performs the actual approximation after scaling.
matrix mui
The parameter samples used computing the parent trajectories of .
UsefScaling
Flag that determines whether the approximation center f values should be scaled to [-1...
Definition: ABase.m:70
ScalingG
If UsefScaling is set to true, this matrix contains the scaling matrix which has to be used in sub-al...
Definition: ABase.m:213
matrix< double > MaxErrors
For each configuration, contains a row with the maximum errors on the training data. The number of columns depends on the type of algorithm implemented by the subclasses.
Definition: ABase.m:124
function str = object2str(handle obj,integer maxdepth)
object2str: Generic matlab object to string converter.
Definition: object2str.m:17
ABase: Base class for any approximation generation algorithms for kernel expansions,.
Definition: ABase.m:19
function copy = clone(copy)
The interface method with returns a copy of the current class instance.
function [ str , rangetab ] = getApproximationSummary()
Setup.
Definition: ABase.m:329
static function rowvec< double > n = L2(matrix< double > x)
Returns the discrete norm for each column vector in x.
Definition: Norm.m:39
ApproxTrainData: Data class for approximation training data, containing several useful bounding box p...
ExpansionConfig: Base class config for kernel expansions.
Norm: Static class for commonly used norms on sets of vectors.
Definition: Norm.m:17
function A = toMemoryMatrix()
Converts this FileMatrix to a full double matrix.
Definition: ABlockedData.m:210
A MatLab character array.
static function this = loadobj(this, initfrom)
Definition: ABase.m:451
function [ rowmin , rowmax ] = getColBoundingBox()
Computes the bounding box of the matrix with respect to columns.
Definition: FileMatrix.m:298
function plotSummary(pm, context)
Definition: ABase.m:356
KernelExpansion: Base class for state-space kernel expansions.