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
RandomSampler.m
Go to the documentation of this file.
1 namespace sampling{
2 
3 
4 /* (Autoinserted by mtoc++)
5  * This source code has been filtered by the mtoc++ executable,
6  * which generates code that can be processed by the doxygen documentation tool.
7  *
8  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
9  * Except for the comments, the function bodies of your M-file functions are untouched.
10  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
11  * attached source files that are highly readable by humans.
12  *
13  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
14  * the correct locations in the source code browser.
15  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
16  */
17 
19  :public sampling.BaseSampler {
34  public: /* ( setObservable ) */
35 
36  Samples = 30;
52  integer Seed = "[]";
69  public: /* ( setObservable ) */
70 
71 
73  this.registerProps(" Samples ");
74  }
75 
76 
77 
78 #if 0 //mtoc++: 'set.Samples'
79 function Samples(value) {
80  if ~isposintscalar(value)
81  error(" Value must be a positive integer scalar ");
82  end
83  this.Samples= value;
84  }
85 
86 #endif
87 
88 
89  function matrix<double>samples = performSampling(params) {
90  if isempty(this.Seed)
91  seed = round(cputime*100);
92  else
93  seed = this.Seed;
94  end
95  r = RandStream(" mt19937ar "," Seed ",seed);
96 
97  nparams = length(params);
98 
99  if isempty(this.Domain)
100  factor = r.rand(nparams,this.Samples);
101  miv = repmat([params(:).MinVal]^t,1,this.Samples);
102  mav = repmat([params(:).MaxVal]^t,1,this.Samples);
103  islog = strcmp([params(:).Spacing]," log ")^t;
104  if any(islog)
105  miv(islog,:) = log10(miv(islog,:));
106  mav(islog,:) = log10(mav(islog,:));
107  end
108  samples = miv + factor.*(mav-miv);
109  if any(islog)
110  samples(islog,:) = 10.^samples(islog,:);
111  end
112  /* Create samples according to the given Domain */
113  else
114  if size(this.Domain.Points,1) ~= nparams
115  erros(" Dimension of Domain does not match the parameter count. ");
116  end
117  samples = zeros(nparams,this.Samples);
118  pos = 1;
119  while pos < this.Samples
120  /* create new samples */
121  factor = r.rand(nparams,this.Samples);
122  miv = repmat([params(:).MinVal]^t,1,this.Samples);
123  mav = repmat([params(:).MaxVal]^t,1,this.Samples);
124  islog = strcmp([params(:).Spacing]," log ")^t;
125  if any(islog)
126  miv(islog,:) = log10(miv(islog,:));
127  mav(islog,:) = log10(mav(islog,:));
128  end
129  newsamples = miv + factor.*(mav-miv);
130  if any(islog)
131  newsamples(islog,:) = 10.^newsamples(islog,:);
132  end
133 
134  /* Filter the new samples by the domain and augment */
135  newsamples = this.Domain.filter(newsamples);
136  numnew = size(newsamples,2);
137  spos = pos:min(pos+numnew-1, this.Samples);
138  npos = 1:min(numnew,this.Samples-pos+1);
139  samples(:,spos) = newsamples(:,npos);
140  pos = spos(end) + 1;
141  end
142  end
143  /* Sort samples if one-dimensional */
144  if size(samples,1) == 1
145  samples = sort(samples);
146  end
147  }
163  public: /* ( Static ) */ /* ( setObservable ) */
164 
165 
166  static function res = test_DomainSampling() {
167  m = models.BaseFullModel;
168  s = models.BaseFirstOrderSystem(m);
169  m.System=s;
170  s.addParam(" param_a ", 1);
171  s.addParam(" param_b ", 34);
172  /* points = rand(2,200); */
173  points = [0.69*sin(0:0.05:pi/2); 0.69*cos(0:0.05:pi/2)];
174  points = [points [0.7*sin(0:0.05:pi/2); 0.7*cos(0:0.05:pi/2)]];
175  valid = Norm.L2(points) >= .7;
176  m.Sampler= sampling.RandomSampler;
177  d = sampling.Domain(points, valid);
178  m.Sampler.Domain= d;
179  m.Sampler.Samples= 500;
180  samples = m.Sampler.generateSamples(m);
181  res = all(Norm.L2(samples) > .66);
182  plot(samples(1,:),samples(2,:)," o ",...
183  d.Points(1,:),d.Points(2,:)," * ",...
184  0.7*sin(0:0.05:pi/2),0.7*cos(0:0.05:pi/2))
185  }
194  protected: /* ( Static ) */ /* ( setObservable ) */
195 
196  static function obj = loadobj(obj) {
197  if ~isa(obj," sampling.RandomSampler ")
198  from = obj;
199  obj = sampling.RandomSampler;
200  obj.Samples= from.Samples;
201  obj.Seed= from.Seed;
202  obj = loadobj@sampling.BaseSampler(obj,from);
203  end
204  }
205 
206 
212 };
213 }
214 
215 
216 
function [ matrix< double > params , rowvec< integer > idx ] = filter(matrix< double > params)
Filters from the given parameters only those that belong to this domain.
Definition: Domain.m:81
static function obj = loadobj(obj)
sort
ort the handle objects in any array in ascending or descending order.
function registerProps(varargin)
Call this method at any class that defines DPCM observed properties.
Definition: DPCMObject.m:125
An integer value.
BaseSampler Basis class for parameter sampling classes.
Definition: BaseSampler.m:18
static function res = test_DomainSampling()
setup parameter domain etc domain are all points in unit square with norm > 0.7
Speed test * all(1:3)
matrix< double > Points
Samples from the full parameter domain box specified by the parameter's MinVal and MaxVal...
Definition: Domain.m:40
Samples
The number of samples to take.
Definition: RandomSampler.m:36
function matrix< double > samples = performSampling(params)
Randomly generates input samples by choosing params and time parameter by chance. ...
Definition: RandomSampler.m:89
function res = isposintscalar(value)
isposintscalar: Backwards-compatibility function for matlab versions greater than 2012a ...
static function rowvec< double > n = L2(matrix< double > x)
Returns the discrete norm for each column vector in x.
Definition: Norm.m:39
RandomSampler Selects Samples many random parameters.
Definition: RandomSampler.m:18
Norm: Static class for commonly used norms on sets of vectors.
Definition: Norm.m:17
Domain:
Definition: Domain.m:18
integer Seed
The seed for the random number generator.
Definition: RandomSampler.m:52