Param Class Reference

This structure stores one parameter, its definition and its string argument. More...

#include <param.h>

Collaboration diagram for Param:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 Param (string &Name, param_t Type, bool mandatory, bool bounded, double low_bnd, double up_bnd, SimComponent *owner, ParamUpdaterBase *updater)
 Constructor.
 Param (const Param &P)
 Copy cstor, provides shallow copy of the parameter definition, do not copy arguments.
virtual ~Param ()
void reset ()
 Clears the _isSet flag and the argument string.
void setName (string value)
 Sets the parameter's name.
void setArg (string value)
 Sets the parameter's argument.
void setType (param_t value)
 Sets the parameter's type (see types.h).
void setIsSet (bool value)
 Sets the _isSet flag.
void setIsBounded (bool value)
 Sets the _isBounded flag.
void setBounds (double low_bnd, double up_bnd)
 Sets the bounds.
void setOwner (SimComponent *owner)
 Sets the pointer to owner.
void setUpdater (ParamUpdaterBase *updater)
 Sets the pointer to the updater object.
void setAtGeneration (unsigned int generation)
string getName ()
string getArg ()
param_t getType ()
bool isSet ()
bool isBounded ()
bool isRequired ()
double getBound (unsigned int i)
SimComponentgetOwner ()
ParamUpdaterBasegetUpdater ()
deque< unsigned int > getUpdatingDates ()
deque< string > getTemporalArgs ()
bool set (string arg, string &errmsg)
 Sets the _isSet flag to true and _arg to arg if the arg is of the right type and whithin the bounds.
bool update (unsigned int generation)
 Updates the parameter value at a given generation during the simulation.
double getValue ()
 Returns the argument value according to its type.
bool isMatrix ()
 Checks if the argument is of matrix type.
void getMatrix (TMatrix *mat)
 Sets the matrix from the argument string if the parameter is set and of matrix type.
void parse_matrix (TMatrix *mat)
 Parses the matrix from the argument string.
bool parseArgument (string &arg)
bool parseTemporalArgument (const string &arg)
bool parseAgeSpecArgument (const string &arg)
bool parseSubParamArgument (const string &arg)
void show_up ()
 Print state to stdout.

Private Attributes

string _name
 The name of the parameter as read in the init file.
string _arg
 The argument string, set by the ParamsParser upon initialization.
param_t _type
 The type of the argument, must one of DBL, INT, BOOL, STR or MAT (see types.h).
bool _isSet
 Flag set once the parameter has recieved the right argument.
bool _isBounded
 Flag telling if the parameter has a bounded argument value.
bool _isRequired
 Flag telling if this parameter is mandatory.
double _bounds [2]
 The argument value boundaries.
unsigned int _setAtGeneration
 Generation at which the parameter has been set/updated.
SimComponent_myOwner
 Pointer to the component that declared this parameter.
ParamUpdaterBase_myUpdater
 Pointer to an ParamUpdater object.
map< string, Param * > _subParams
map< unsigned int, string > _temporalArgs
 The temporal arguments.
bool _isTemporal


Detailed Description

This structure stores one parameter, its definition and its string argument.

Parameters are aggregated into a ParamSet.

Definition at line 52 of file param.h.


Constructor & Destructor Documentation

Param::Param ( string &  Name,
param_t  Type,
bool  mandatory,
bool  bounded,
double  low_bnd,
double  up_bnd,
SimComponent owner,
ParamUpdaterBase updater 
)

Constructor.

Parameters:
Name the name of the parameter as read in the init file
Type the type of the parameter argument (see types.h), used to convert the argument string into a value
mandatory specifies whether this parameter is mandatory for the ParamSet owning it
bounded specifies whether the values this parameter can take are bounded
low_bnd the lower bound of the parameter value
up_bnd the upper bound
owner pointer to the SimComponents that owns this parameter
updater the param updater object used to update the parameter's state during a simulation

Definition at line 41 of file param.cc.

00043 : _name(Name),_arg(""),_type(Type),_isSet(0),_isBounded(bounded),_isRequired(mandatory),_setAtGeneration(0),
00044 _myOwner(owner), _myUpdater(updater)
00045 
00046 {
00047   _bounds[0] = low_bnd;
00048   _bounds[1] = up_bnd;
00049 }

Param::Param ( const Param P  ) 

Copy cstor, provides shallow copy of the parameter definition, do not copy arguments.

Definition at line 53 of file param.cc.

00054 : _name(P._name),_arg(P._arg),_type(P._type),_isSet(P._isSet),_isBounded(P._isBounded),
00055 _isRequired(P._isRequired),_setAtGeneration(P._setAtGeneration),_myOwner(P._myOwner),
00056 _myUpdater(0)
00057 {
00058   _bounds[0] = P._bounds[0];
00059   _bounds[1] = P._bounds[1];
00060 }

Param::~Param (  )  [virtual]

Definition at line 64 of file param.cc.

00065 {
00066   if(_myUpdater) delete _myUpdater;
00067 }


Member Function Documentation

string Param::getArg (  )  [inline]

Definition at line 125 of file param.h.

Referenced by LCE_Breed_base::setFecundity(), and BinaryDataSaver::setParameters().

00125 {return _arg;}

double Param::getBound ( unsigned int  i  )  [inline]

Definition at line 130 of file param.h.

00130 {return _bounds[i];}

void Param::getMatrix ( TMatrix mat  ) 

Sets the matrix from the argument string if the parameter is set and of matrix type.

Passes the param 'mat' to function Param::parse_matrix.

Parameters:
mat a TMatrix ptr, mat dimensions and values will be reset to the values read in the init file.

Definition at line 257 of file param.cc.

Referenced by LCE_Patch_Extinction::set_matrix_param(), and LCE_Selection_base::set_sel_model().

00258 {
00259   if( isMatrix() && _isSet ){
00260     
00261         parse_matrix(mat);
00262 
00263   } else
00264         warning("param \"%s\" is not a matrix!\n",_name.c_str());
00265 }

string Param::getName (  )  [inline]

Definition at line 124 of file param.h.

Referenced by ParamSet::add_param().

00124 {return _name;}

SimComponent* Param::getOwner (  )  [inline]

Definition at line 131 of file param.h.

00131 {return _myOwner;}

deque< string > Param::getTemporalArgs (  ) 

Definition at line 93 of file param.cc.

00094 {
00095   deque< string > args;
00096   
00097   for(map< unsigned int, string >::iterator tmp_it = _temporalArgs.begin();
00098       tmp_it != _temporalArgs.end(); tmp_it++)
00099     args.push_back( tmp_it->second );
00100   
00101   return args;
00102 }

param_t Param::getType (  )  [inline]

Definition at line 126 of file param.h.

00126 {return _type;}

ParamUpdaterBase* Param::getUpdater (  )  [inline]

Definition at line 132 of file param.h.

00132 {return _myUpdater;}

deque< unsigned int > Param::getUpdatingDates (  ) 

Definition at line 80 of file param.cc.

00081 {
00082   deque< unsigned int > dates;
00083   
00084   for(map< unsigned int, string >::iterator tmp_it = _temporalArgs.begin();
00085       tmp_it != _temporalArgs.end(); tmp_it++)
00086     dates.push_back( tmp_it->first );
00087   
00088   return dates;
00089 }

double Param::getValue (  ) 

Returns the argument value according to its type.

Returns:
-1 if the parameter is not set or not of a the right type

Definition at line 247 of file param.cc.

Referenced by TProtoNeutralGenes::loadFileServices(), TProtoDeletMutations_bitstring::loadFileServices(), and LCE_Patch_Extinction::set_matrix_param().

00248 {
00249   if( !(isMatrix() || _type == STR) && _isSet)
00250         return atof(_arg.c_str());
00251   else
00252         return -1.0;
00253 }

bool Param::isBounded (  )  [inline]

Definition at line 128 of file param.h.

00128 {return _isBounded;}

bool Param::isMatrix (  )  [inline]

Checks if the argument is of matrix type.

Definition at line 151 of file param.h.

Referenced by getMatrix(), getValue(), and LCE_Patch_Extinction::set_matrix_param().

00152     {  return (_type == MAT || (_arg.size() != 0 ? _arg[0] == '{' : false) ); }

bool Param::isRequired (  )  [inline]

Definition at line 129 of file param.h.

00129 {return _isRequired;}

bool Param::isSet (  )  [inline]

void Param::parse_matrix ( TMatrix mat  ) 

Parses the matrix from the argument string.

Parameters:
mat a TMatrix ptr, mat dimensions and values will be reset to the values read in the init file.

Definition at line 270 of file param.cc.

Referenced by getMatrix().

00271 {
00272   std::vector< std::vector<double>* > tmpMat;
00273   std::istringstream IN;
00274   
00275   IN.str(_arg);
00276   
00277   unsigned int cols = 0;
00278   double elmnt;
00279   char c;
00280 
00281   
00282   int rows = -1, pos = -1;
00283   //we count the number of rows
00284   do {
00285     pos = _arg.find("{", pos + 1);
00286     //message("pos %i, arg %s\n",pos,_arg.c_str());
00287     rows++;
00288   }while(pos != (int)string::npos);
00289   //decrement by 1, the first doesn't count for a row
00290   rows--;
00291   
00292   for(int i = 0; i < rows; i++)
00293     tmpMat.push_back( new vector<double>);
00294   
00295   //remove the first enclosing bracket
00296   IN>>c;
00297   //then read the rows
00298   for(unsigned int i = 0; i < tmpMat.size(); i++) {
00299     
00300     cols = 0;
00301     
00302     //read a row enclosed by {...}:
00303     while(IN) {
00304       
00305       //first character:
00306       IN>>c;
00307           
00308           if(c == '{' || c == ',' || c == ';') {
00309                 //read a row element:
00310                 IN>>elmnt;
00311                 cols++;
00312                 tmpMat[i]->push_back(elmnt);
00313                 
00314           } else if(c == '}')
00315                 //go to next row
00316                 break;
00317                 
00318         }
00319   }
00320   //check for matrix coherence:
00321   for(unsigned int i = 0; i < tmpMat.size(); i++) {
00322         if(tmpMat[i]->size() != cols)
00323           fatal("%s: not same number of elements in all rows of matrix! (%i, cols %i)\n",_name.c_str(),tmpMat[i]->size(),cols);
00324         cols = tmpMat[i]->size();
00325   }
00326   
00327   //copy to input TMatrix:
00328   mat->reset(rows, cols);
00329   for(int i = 0; i < rows; ++i)
00330         for(unsigned int j = 0; j < cols; ++j)
00331           mat->set(i,j,(*tmpMat[i])[j]);
00332 }

bool Param::parseAgeSpecArgument ( const string &  arg  ) 

Definition at line 233 of file param.cc.

Referenced by parseArgument().

00234 {
00235   return true;
00236 }

bool Param::parseArgument ( string &  arg  ) 

Definition at line 155 of file param.cc.

Referenced by set().

00156 {
00157   arg = tstring::removeEnclosingChar(arg, '(', ')');
00158   
00159   vector< string > args = tstring::splitExcludeEnclosedDelimiters(arg);
00160   
00161   _temporalArgs.clear();
00162 //  _ageSpecArgs.clear();
00163 //  _subParams.clear();
00164   
00165 //  cout<< "Param::parseArgument:" << endl;
00166   
00167   for(unsigned int i = 0; i < args.size(); ++i) {
00168     //cout << args[i] << endl;
00169     if(args[i][0] == '@') {
00170       if(args[i][1] == 'g') if(!parseTemporalArgument(args[i])) return false;
00171       else if(args[i][1] == 'a') if(!parseAgeSpecArgument(args[i])) return false;
00172       else {
00173         error("parameter \"%s\"'s argument string \"%s\" has unrecognized specifier.\n", _name.c_str(), args[i].c_str());
00174         return false;
00175       }
00176     } else
00177       parseSubParamArgument(args[i]);
00178   }
00179 
00180   
00181   if(_temporalArgs.size() != 0) {
00182     
00183     //cout << "temporal arg values:\n";
00184     //    for(map< unsigned int , string >::iterator iter = _temporalArgs.begin();
00185     //        iter != _temporalArgs.end();
00186     //        iter++)
00187     //        cout << iter->first <<" "<< iter->second << endl;
00188     
00189     if(_myUpdater != 0) {
00190       
00191       if(_temporalArgs.find(0) != _temporalArgs.end()) {
00192         _isTemporal = true;
00193         //set the value for the first generation
00194         arg = _temporalArgs.find(0)->second;
00195       }
00196       else {
00197         error("first generation argument value for temporal parameter \"%s\" is missing; no \"@g0\".\n", _name.c_str());
00198         return false;
00199       }
00200       
00201     } else {
00202       warning("trying to pass a temporal argument to a non temporal parameter (\"%s\").\n", _name.c_str());
00203       _isTemporal = false;
00204       _temporalArgs.clear();
00205     }
00206     
00207   } else _isTemporal = false;
00208   
00209   
00210   return true;
00211 }

bool Param::parseSubParamArgument ( const string &  arg  ) 

Definition at line 240 of file param.cc.

Referenced by parseArgument().

00241 {
00242   return true;
00243 }

bool Param::parseTemporalArgument ( const string &  arg  ) 

Definition at line 215 of file param.cc.

Referenced by parseArgument().

00216 {
00217   vector< string > args = tstring::split(arg, ' ', true);
00218   
00219   if(args.size() != 2) {
00220     error("Param::parseTemporalArgument:: missing argument value in \"%s %s\".\n", _name.c_str(), arg.c_str());
00221     return false;
00222   }
00223   
00224   unsigned int gen = tstring::str2uint(tstring::removeFirstCharOf(tstring::removeFirstCharOf(args[0], '@'), 'g'));
00225   
00226   _temporalArgs[gen] = args[1];
00227 
00228   return true;
00229 }

void Param::reset (  ) 

Clears the _isSet flag and the argument string.

Definition at line 71 of file param.cc.

00072 {
00073   _arg = "";
00074   _isSet = 0;
00075   _setAtGeneration = 0;
00076 }

bool Param::set ( string  arg,
string &  errmsg 
)

Sets the _isSet flag to true and _arg to arg if the arg is of the right type and whithin the bounds.

Called at simulation setup (i.e. generation 0). For parameter update during the simulation, check the Param::update member function.

!a matrix may also be specified!! in that case, we don't check values here

Definition at line 106 of file param.cc.

00107 {  
00108   //cout << "Param::set ("<<arg<<")\n";
00109   if( arg[0] == '(' ) 
00110     if( !parseArgument( arg ) ) {
00111       errmsg = "could not parse the argument string";
00112       return false;
00113     }
00114   //integer or decimal: 
00115   if(_type == INT || _type == DBL) {
00117         if(arg[0] != '{') {
00118           
00119           if(!tstring::isanumber(arg)) {
00120         errmsg = "argument is not a number";
00121         return false;
00122           }
00123       
00124           double val = tstring::str2dble(arg);
00125           
00126           if(_isBounded && (val < _bounds[0] || val > _bounds[1]) ){
00127         errmsg = "argument is out of bounds";
00128         return false;
00129           }
00130         }
00131         
00132   //boolean:
00133   } else if(_type == BOOL) {
00134         
00135         if( !tstring::isanumber(arg) ){
00136       errmsg = "argument is not a boolean";
00137       return false;
00138     }
00139         
00140   } else if(_type == MAT) {
00141         if(arg[0] != '{'){
00142       errmsg = "argument is not a matrix";
00143       return false;
00144     }
00145   }
00146   // else if(_type == P || _type == S): no conditions to check
00147   
00148   _isSet = (_type == BOOL && tstring::str2int(arg) == 0 ? false : true);
00149   _arg = arg;
00150   return true;  
00151 }

void Param::setArg ( string  value  )  [inline]

Sets the parameter's argument.

Definition at line 107 of file param.h.

00107 {_arg = value;}

void Param::setAtGeneration ( unsigned int  generation  )  [inline]

Definition at line 121 of file param.h.

00121 {_setAtGeneration = generation;}

void Param::setBounds ( double  low_bnd,
double  up_bnd 
) [inline]

Sets the bounds.

Definition at line 115 of file param.h.

00115 {_bounds[0]=low_bnd; _bounds[1]=up_bnd;}

void Param::setIsBounded ( bool  value  )  [inline]

Sets the _isBounded flag.

Definition at line 113 of file param.h.

00113 {_isBounded = value;}

void Param::setIsSet ( bool  value  )  [inline]

Sets the _isSet flag.

Definition at line 111 of file param.h.

00111 {_isSet = value;}

void Param::setName ( string  value  )  [inline]

Sets the parameter's name.

Definition at line 105 of file param.h.

00105 {_name = value;}

void Param::setOwner ( SimComponent owner  )  [inline]

Sets the pointer to owner.

Definition at line 117 of file param.h.

00117 {_myOwner = owner;}

void Param::setType ( param_t  value  )  [inline]

Sets the parameter's type (see types.h).

Definition at line 109 of file param.h.

00109 {_type = value;}

void Param::setUpdater ( ParamUpdaterBase updater  )  [inline]

Sets the pointer to the updater object.

Definition at line 119 of file param.h.

00119 {_myUpdater = updater;}

void Param::show_up (  ) 

Print state to stdout.

Definition at line 352 of file param.cc.

00353 {
00354   message("%s = %s",_name.c_str(),_arg.c_str());
00355   if(_isSet)
00356     message(" (set at gen %i)\n",_setAtGeneration);
00357   else
00358     message(" (not set)\n");
00359 }

bool Param::update ( unsigned int  generation  ) 

Updates the parameter value at a given generation during the simulation.

Definition at line 336 of file param.cc.

00337 {
00338   string error_msg;
00339   if(_temporalArgs.find( generation ) != _temporalArgs.end()) {
00340     if( !set( _temporalArgs[generation], error_msg ) ){
00341       error("could not set \"%s\": %s", _name.c_str(), error_msg.c_str());
00342       return false;
00343     } else
00344       return true;
00345   } else
00346     error("parameters \"%s\" does not have an argument value for generation %i.\n", _name.c_str(), generation);
00347   return false; 
00348 }


Member Data Documentation

string Param::_arg [private]

The argument string, set by the ParamsParser upon initialization.

Definition at line 57 of file param.h.

Referenced by getArg(), getValue(), isMatrix(), parse_matrix(), reset(), set(), setArg(), and show_up().

double Param::_bounds[2] [private]

The argument value boundaries.

Definition at line 68 of file param.h.

Referenced by getBound(), Param(), set(), and setBounds().

bool Param::_isBounded [private]

Flag telling if the parameter has a bounded argument value.

Definition at line 63 of file param.h.

Referenced by isBounded(), set(), and setIsBounded().

bool Param::_isRequired [private]

Flag telling if this parameter is mandatory.

A SimComponent with a mandatory parameter in the 'unset' state will not be selected to be part of a running simulation component.

Definition at line 66 of file param.h.

Referenced by isRequired().

bool Param::_isSet [private]

Flag set once the parameter has recieved the right argument.

Definition at line 61 of file param.h.

Referenced by getMatrix(), getValue(), isSet(), reset(), set(), setIsSet(), and show_up().

bool Param::_isTemporal [private]

Definition at line 79 of file param.h.

Referenced by parseArgument().

Pointer to the component that declared this parameter.

Definition at line 72 of file param.h.

Referenced by getOwner(), and setOwner().

Pointer to an ParamUpdater object.

Definition at line 74 of file param.h.

Referenced by getUpdater(), parseArgument(), setUpdater(), and ~Param().

string Param::_name [private]

The name of the parameter as read in the init file.

Definition at line 55 of file param.h.

Referenced by getMatrix(), getName(), parse_matrix(), parseArgument(), parseTemporalArgument(), setName(), show_up(), and update().

unsigned int Param::_setAtGeneration [private]

Generation at which the parameter has been set/updated.

Definition at line 70 of file param.h.

Referenced by reset(), setAtGeneration(), and show_up().

map<string, Param*> Param::_subParams [private]

Definition at line 76 of file param.h.

map< unsigned int, string > Param::_temporalArgs [private]

The temporal arguments.

Definition at line 78 of file param.h.

Referenced by getTemporalArgs(), getUpdatingDates(), parseArgument(), parseTemporalArgument(), and update().

param_t Param::_type [private]

The type of the argument, must one of DBL, INT, BOOL, STR or MAT (see types.h).

Definition at line 59 of file param.h.

Referenced by getType(), getValue(), isMatrix(), set(), and setType().


The documentation for this class was generated from the following files:

Generated for Nemo v2.1.2 by  doxygen 1.5.8 -- Nemo is hosted by  SourceForge.net Logo