uniform_noise.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00033 #include <acado/noise/uniform_noise.hpp>
00034 
00035 #include <stdlib.h>
00036 #include <time.h>
00037 
00038 
00039 
00040 BEGIN_NAMESPACE_ACADO
00041 
00042 
00043 
00044 
00045 UniformNoise::UniformNoise( ) : Noise( )
00046 {
00047 }
00048 
00049 
00050 UniformNoise::UniformNoise(     const DVector& _lowerLimit,
00051                                                         const DVector& _upperLimit
00052                                                         ) : Noise( )
00053 {
00054         if ( _lowerLimit.getDim( ) == _upperLimit.getDim( ) )
00055         {
00056                 w.init( _lowerLimit.getDim( ),1 );
00057                 lowerLimit = _lowerLimit;
00058                 upperLimit = _upperLimit;
00059         }
00060         else
00061                 ACADOERROR( RET_INVALID_NOISE_SETTINGS );
00062 }
00063 
00064 
00065 UniformNoise::UniformNoise(     uint _dim,
00066                                                         double _lowerLimit,
00067                                                         double _upperLimit
00068                                                         ) : Noise( )
00069 {
00070         w.init( _dim,1 );
00071         lowerLimit.init(_dim);
00072         upperLimit.init(_dim);
00073         
00074         for( uint i=0; i<_dim; ++i )
00075         {
00076                 lowerLimit(i) = _lowerLimit;
00077                 upperLimit(i) = _upperLimit;
00078         }
00079 }
00080 
00081 
00082 UniformNoise::UniformNoise( const UniformNoise &rhs ) : Noise( rhs )
00083 {
00084         lowerLimit = rhs.lowerLimit;
00085         upperLimit = rhs.upperLimit;
00086 }
00087 
00088 
00089 UniformNoise::~UniformNoise( )
00090 {
00091 }
00092 
00093 
00094 UniformNoise& UniformNoise::operator=( const UniformNoise &rhs )
00095 {
00096         if( this != &rhs )
00097         {
00098                 Noise::operator=( rhs );
00099 
00100                 lowerLimit = rhs.lowerLimit;
00101                 upperLimit = rhs.upperLimit;
00102         }
00103 
00104         return *this;
00105 }
00106 
00107 
00108 UniformNoise* UniformNoise::clone( ) const
00109 {
00110     return ( new UniformNoise( *this ) );
00111 }
00112 
00113 
00114 UniformNoise* UniformNoise::clone(      uint idx
00115                                                                         ) const
00116 {
00117         if ( idx >= getDim( ) )
00118                 return 0;
00119 
00120         UniformNoise tmp( DVector(1),DVector(1) );
00121         tmp.Noise::operator=( *this );
00122         tmp.w.init( 1,1 );
00123         tmp.lowerLimit(0) = lowerLimit(idx);
00124         tmp.upperLimit(0) = upperLimit(idx);
00125 
00126     return ( new UniformNoise( tmp ) );
00127 }
00128 
00129 
00130 
00131 returnValue UniformNoise::setLimits(    const DVector& _lowerLimit,
00132                                                                                 const DVector& _upperLimit
00133                                                                                 )
00134 {
00135         if ( lowerLimit.getDim( ) != _lowerLimit.getDim( ) )
00136                 return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH );
00137 
00138         if ( upperLimit.getDim( ) != _upperLimit.getDim( ) )
00139                 return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH );
00140 
00141         for( uint i=0; i<_lowerLimit.getDim( ); ++i )
00142                 if ( _lowerLimit(i) > _upperLimit(i) )
00143                         return ACADOERROR( RET_INVALID_ARGUMENTS );
00144 
00145         lowerLimit = _lowerLimit;
00146         upperLimit = _upperLimit;
00147 
00148         return SUCCESSFUL_RETURN;
00149 }
00150 
00151 
00152 returnValue UniformNoise::setLimits(    double _lowerLimit,
00153                                                                                 double _upperLimit
00154                                                                                 )
00155 {
00156         if ( _lowerLimit > _upperLimit )
00157                 return ACADOERROR( RET_INVALID_ARGUMENTS );
00158 
00159         for( uint i=0; i<getDim( ); ++i )
00160         {
00161                 lowerLimit(i) = _lowerLimit;
00162                 upperLimit(i) = _upperLimit;
00163         }
00164 
00165         return SUCCESSFUL_RETURN;
00166 }
00167 
00168 
00169 returnValue UniformNoise::setLimit(     uint idx,
00170                                                                         double _lowerLimit,
00171                                                                         double _upperLimit
00172                                                                         )
00173 {
00174         if ( idx >= getDim( ) )
00175                 return ACADOERROR( RET_INDEX_OUT_OF_BOUNDS );
00176 
00177         if ( _lowerLimit > _upperLimit )
00178                 return ACADOERROR( RET_INVALID_ARGUMENTS );
00179 
00180         lowerLimit(idx) = _lowerLimit;
00181         upperLimit(idx) = _upperLimit;
00182 
00183         return SUCCESSFUL_RETURN;
00184 }
00185 
00186 
00187 
00188 returnValue UniformNoise::init( uint seed
00189                                                                 )
00190 {
00191         if ( lowerLimit.getDim( ) != upperLimit.getDim( ) )
00192                 return ACADOERROR( RET_INVALID_NOISE_SETTINGS );
00193 
00194         if ( lowerLimit.getDim( ) == 0 )
00195                 return ACADOERROR( RET_NO_NOISE_SETTINGS );
00196 
00197         /* initialize random seed: */
00198         if ( seed == 0 )
00199                 srand( (unsigned int)time(0) );
00200         else
00201                 srand( seed );
00202 
00203         setStatus( BS_READY );
00204 
00205         return SUCCESSFUL_RETURN;
00206 }
00207 
00208 
00209 returnValue UniformNoise::step( DVector& _w
00210                                                                 )
00211 {
00212         if ( getStatus( ) != BS_READY )
00213                 return ACADOERROR( RET_BLOCK_NOT_READY );
00214 
00215         if ( getDim( ) != _w.getDim( ) )
00216                 return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH );
00217 
00218         if ( w.getNumPoints( ) != 1 )
00219                 w.init( 1,getDim() );
00220 
00221         for( uint j=0; j<getDim( ); ++j )
00222                 w(0,j) = getUniformRandomNumber( lowerLimit(j),upperLimit(j) );
00223 
00224         _w = w.getVector( 0 );
00225 
00226         return SUCCESSFUL_RETURN;
00227 }
00228 
00229 
00230 returnValue UniformNoise::step( VariablesGrid& _w
00231                                                                 )
00232 {
00233         if ( getStatus( ) != BS_READY )
00234                 return ACADOERROR( RET_BLOCK_NOT_READY );
00235 
00236         if ( getDim( ) != _w.getNumValues( ) )
00237                 return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH );
00238 
00239         if ( w.getNumPoints( ) != _w.getNumPoints( ) )
00240                 w.init( getDim(),_w.getNumPoints( ) );
00241 
00242         for( uint i=0; i<_w.getNumPoints( ); ++i )
00243                 for( uint j=0; j<getDim( ); ++j )
00244                         w(i,j) = getUniformRandomNumber( lowerLimit(j),upperLimit(j) );
00245 
00246         _w = w;
00247 
00248         return SUCCESSFUL_RETURN;
00249 }
00250 
00251 
00252 //
00253 // PROTECTED MEMBER FUNCTIONS:
00254 //
00255 
00256 
00257 
00258 CLOSE_NAMESPACE_ACADO
00259 
00260 // end of file.


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 12:01:27