gaussian_noise.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
34 
35 #include <stdlib.h>
36 #include <time.h>
37 
38 
39 
41 
42 
43 
44 
46 {
47 }
48 
49 
51  const DVector& _variance
52  ) : Noise( )
53 {
54  if ( _mean.getDim( ) == _variance.getDim( ) )
55  {
56  w.init( _mean.getDim( ),1 );
57  mean = _mean;
58  variance = _variance;
59  }
60  else
62 }
63 
64 
66  double _mean,
67  double _variance
68  ) : Noise( )
69 {
70  w.init( _dim,1 );
71 
72  mean.init( _dim );
73  variance.init( _dim );
74 
75  for( uint i=0; i<_dim; ++i )
76  {
77  mean(i) = _mean;
78  variance(i) = _variance;
79  }
80 }
81 
82 
84 {
85  mean = rhs.mean;
86  variance = rhs.variance;
87 }
88 
89 
91 {
92 }
93 
94 
96 {
97  if( this != &rhs )
98  {
99  Noise::operator=( rhs );
100 
101  mean = rhs.mean;
102  variance = rhs.variance;
103  }
104 
105  return *this;
106 }
107 
108 
110 {
111  return ( new GaussianNoise( *this ) );
112 }
113 
114 
116  ) const
117 {
118  if ( idx >= getDim( ) )
119  return 0;
120 
121  GaussianNoise tmp( DVector(1),DVector(1) );
122  tmp.Noise::operator=( *this );
123  tmp.w.init( 1,1 );
124  tmp.mean(0) = mean(idx);
125  tmp.variance(0) = variance(idx);
126 
127  return ( new GaussianNoise( tmp ) );
128 }
129 
130 
131 
133  )
134 {
135  if ( mean.getDim( ) != variance.getDim( ) )
137 
138  if ( mean.getDim( ) == 0 )
140 
141  /* initialize random seed: */
142  if ( seed == 0 )
143  srand( (unsigned int)time(0) );
144  else
145  srand( seed );
146 
147  setStatus( BS_READY );
148 
149  return SUCCESSFUL_RETURN;
150 }
151 
152 
154  )
155 {
156  if ( getStatus( ) != BS_READY )
158 
159  if ( getDim( ) != _w.getDim( ) )
161 
162  if ( w.getNumPoints( ) != 1 )
163  w.init( 1,getDim() );
164 
165  for( uint j=0; j<getDim( ); ++j )
166  w(0,j) = getGaussianRandomNumber( mean(j),variance(j) );
167 
168  _w = w.getVector( 0 );
169 
170  return SUCCESSFUL_RETURN;
171 }
172 
173 
175  )
176 {
177  if ( getStatus( ) != BS_READY )
179 
180  if ( getDim( ) != _w.getNumValues( ) )
182 
183  if ( w.getNumPoints( ) != _w.getNumPoints( ) )
184  w.init( getDim(),_w.getNumPoints( ) );
185 
186  for( uint i=0; i<_w.getNumPoints( ); ++i )
187  for( uint j=0; j<getDim( ); ++j )
188  w(i,j) = getGaussianRandomNumber( mean(j),variance(j) );
189 
190  _w = w;
191 
192  return SUCCESSFUL_RETURN;
193 }
194 
195 
196 //
197 // PROTECTED MEMBER FUNCTIONS:
198 //
199 
201  double _variance
202  ) const
203 {
204  // Box-Muller method
205  double norm = 2.0;
206  double uniformRandomNumber1, uniformRandomNumber2;
207 
208  while ( norm >= 1.0 )
209  {
210  uniformRandomNumber1 = getUniformRandomNumber( -1.0,1.0 );
211  uniformRandomNumber2 = getUniformRandomNumber( -1.0,1.0 );
212  norm = uniformRandomNumber1*uniformRandomNumber1 + uniformRandomNumber2*uniformRandomNumber2;
213  }
214 
215  double gaussianRandomNumber1 = sqrt( -2.0 * log(norm)/norm ) * uniformRandomNumber1;
216  double gaussianRandomNumber2 = sqrt( -2.0 * log(norm)/norm ) * uniformRandomNumber2;
217 
218  return _mean + sqrt( _variance ) * (gaussianRandomNumber1+gaussianRandomNumber2)/2.0;
219 }
220 
221 
222 
223 
225 
226 // end of file.
IntermediateState sqrt(const Expression &arg)
void init(unsigned _dim=0)
Definition: vector.hpp:155
Provides a time grid consisting of vector-valued optimization variables at each grid point...
Allows to pass back messages to the calling function.
Base class for generating pseudo-random noise for simulating the Process.
Definition: noise.hpp:56
VariablesGrid w
Definition: noise.hpp:194
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
returnValue setStatus(BlockStatus _status)
#define CLOSE_NAMESPACE_ACADO
double getUniformRandomNumber(double _lowerLimit, double _upperLimit) const
BlockStatus getStatus() const
virtual returnValue step(DVector &_w)
unsigned getDim() const
Definition: vector.hpp:172
returnValue init()
void rhs(const real_t *x, real_t *f)
Generates pseudo-random Gaussian noise for simulating the Process.
GenericVector< double > DVector
Definition: vector.hpp:329
Noise & operator=(const Noise &rhs)
Definition: noise.cpp:56
uint getNumPoints() const
DVector getVector(uint pointIdx) const
GaussianNoise & operator=(const GaussianNoise &rhs)
virtual returnValue init(uint seed=0)
virtual GaussianNoise * clone() const
#define BEGIN_NAMESPACE_ACADO
uint getDim() const
double getGaussianRandomNumber(double _mean, double _variance) const
#define ACADOERROR(retval)
virtual ~GaussianNoise()
IntermediateState log(const Expression &arg)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:37