Nemo  2.2.0
Public Member Functions | Private Attributes
Param Class Reference

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

#include <param.h>

+ Collaboration diagram for Param:

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:
Namethe name of the parameter as read in the init file
Typethe type of the parameter argument (see types.h), used to convert the argument string into a value
mandatoryspecifies whether this parameter is mandatory for the ParamSet owning it
boundedspecifies whether the values this parameter can take are bounded
low_bndthe lower bound of the parameter value
up_bndthe upper bound
ownerpointer to the SimComponents that owns this parameter
updaterthe param updater object used to update the parameter's state during a simulation

Definition at line 41 of file param.cc.

References _bounds.

: _name(Name),_arg(""),_type(Type),_isSet(0),_isBounded(bounded),_isRequired(mandatory),_setAtGeneration(0),
_myOwner(owner), _myUpdater(updater)

{
  _bounds[0] = low_bnd;
  _bounds[1] = up_bnd;
}
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.

References _bounds.

Param::~Param ( ) [virtual]

Definition at line 64 of file param.cc.

References _myUpdater.

{
  if(_myUpdater) delete _myUpdater;
}

Member Function Documentation

string Param::getArg ( ) [inline]

Definition at line 125 of file param.h.

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

{return _arg;}
double Param::getBound ( unsigned int  i) [inline]

Definition at line 130 of file param.h.

{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:
mata 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.

References _isSet, _name, isMatrix(), parse_matrix(), and warning().

Referenced by LCE_Selection_base::set_sel_model().

{
  if( isMatrix() && _isSet ){
    
        parse_matrix(mat);

  } else
        warning("param \"%s\" is not a matrix!\n",_name.c_str());
}
string Param::getName ( ) [inline]

Definition at line 124 of file param.h.

Referenced by ParamSet::add_param().

{return _name;}
SimComponent* Param::getOwner ( ) [inline]

Definition at line 131 of file param.h.

{return _myOwner;}
deque< string > Param::getTemporalArgs ( )

Definition at line 93 of file param.cc.

References _temporalArgs.

{
  deque< string > args;
  
  for(map< unsigned int, string >::iterator tmp_it = _temporalArgs.begin();
      tmp_it != _temporalArgs.end(); tmp_it++)
    args.push_back( tmp_it->second );
  
  return args;
}
param_t Param::getType ( ) [inline]

Definition at line 126 of file param.h.

{return _type;}
ParamUpdaterBase* Param::getUpdater ( ) [inline]

Definition at line 132 of file param.h.

{return _myUpdater;}
deque< unsigned int > Param::getUpdatingDates ( )

Definition at line 80 of file param.cc.

References _temporalArgs.

{
  deque< unsigned int > dates;
  
  for(map< unsigned int, string >::iterator tmp_it = _temporalArgs.begin();
      tmp_it != _temporalArgs.end(); tmp_it++)
    dates.push_back( tmp_it->first );
  
  return dates;
}
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.

References _arg, _isSet, _type, isMatrix(), and STR.

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

{
  if( !(isMatrix() || _type == STR) && _isSet)
        return atof(_arg.c_str());
  else
        return -1.0;
}
bool Param::isBounded ( ) [inline]

Definition at line 128 of file param.h.

{return _isBounded;}
bool Param::isMatrix ( ) [inline]

Checks if the argument is of matrix type.

Definition at line 151 of file param.h.

References MAT.

Referenced by getMatrix(), and getValue().

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

Definition at line 129 of file param.h.

{return _isRequired;}
bool Param::isSet ( ) [inline]
void Param::parse_matrix ( TMatrix mat)

Parses the matrix from the argument string.

Parameters:
mata 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.

References _arg, _name, fatal(), TMatrix::reset(), and TMatrix::set().

Referenced by getMatrix().

{
  std::vector< std::vector<double>* > tmpMat;
  std::istringstream IN;
  
  IN.str(_arg);
  
  unsigned int cols = 0;
  double elmnt;
  char c;

  
  int rows = -1, pos = -1;
  //we count the number of rows
  do {
    pos = _arg.find("{", pos + 1);
    //message("pos %i, arg %s\n",pos,_arg.c_str());
    rows++;
  }while(pos != (int)string::npos);
  //decrement by 1, the first doesn't count for a row
  rows--;
  
  for(int i = 0; i < rows; i++)
    tmpMat.push_back( new vector<double>);
  
  //remove the first enclosing bracket
  IN>>c;
  //then read the rows
  for(unsigned int i = 0; i < tmpMat.size(); i++) {
    
    cols = 0;
    
    //read a row enclosed by {...}:
    while(IN) {
      
      //first character:
      IN>>c;
          
          if(c == '{' || c == ',' || c == ';') {
                //read a row element:
                IN>>elmnt;
                cols++;
                tmpMat[i]->push_back(elmnt);
                
          } else if(c == '}')
                //go to next row
                break;
                
        }
  }
  //check for matrix coherence:
  for(unsigned int i = 0; i < tmpMat.size(); i++) {
        if(tmpMat[i]->size() != cols)
          fatal("%s: not same number of elements in all rows of matrix! (%i, cols %i)\n",_name.c_str(),tmpMat[i]->size(),cols);
        cols = tmpMat[i]->size();
  }
  
  //copy to input TMatrix:
  mat->reset(rows, cols);
  for(int i = 0; i < rows; ++i)
        for(unsigned int j = 0; j < cols; ++j)
          mat->set(i,j,(*tmpMat[i])[j]);
}
bool Param::parseAgeSpecArgument ( const string &  arg)

Definition at line 233 of file param.cc.

Referenced by parseArgument().

{
  return true;
}
bool Param::parseArgument ( string &  arg)

Definition at line 155 of file param.cc.

References _isTemporal, _myUpdater, _name, _temporalArgs, error(), parseAgeSpecArgument(), parseSubParamArgument(), parseTemporalArgument(), tstring::removeEnclosingChar(), tstring::splitExcludeEnclosedDelimiters(), and warning().

Referenced by set().

{
  arg = tstring::removeEnclosingChar(arg, '(', ')');
  
  vector< string > args = tstring::splitExcludeEnclosedDelimiters(arg);
  
  _temporalArgs.clear();
//  _ageSpecArgs.clear();
//  _subParams.clear();
  
//  cout<< "Param::parseArgument:" << endl;
  
  for(unsigned int i = 0; i < args.size(); ++i) {
    //cout << args[i] << endl;
    if(args[i][0] == '@') {
      if(args[i][1] == 'g') if(!parseTemporalArgument(args[i])) return false;
      else if(args[i][1] == 'a') if(!parseAgeSpecArgument(args[i])) return false;
      else {
        error("parameter \"%s\"'s argument string \"%s\" has unrecognized specifier.\n", _name.c_str(), args[i].c_str());
        return false;
      }
    } else
      parseSubParamArgument(args[i]);
  }

  
  if(_temporalArgs.size() != 0) {
    
    //cout << "temporal arg values:\n";
    //    for(map< unsigned int , string >::iterator iter = _temporalArgs.begin();
    //        iter != _temporalArgs.end();
    //        iter++)
    //        cout << iter->first <<" "<< iter->second << endl;
    
    if(_myUpdater != 0) {
      
      if(_temporalArgs.find(0) != _temporalArgs.end()) {
        _isTemporal = true;
        //set the value for the first generation
        arg = _temporalArgs.find(0)->second;
      }
      else {
        error("first generation argument value for temporal parameter \"%s\" is missing; no \"@g0\".\n", _name.c_str());
        return false;
      }
      
    } else {
      warning("trying to pass a temporal argument to a non temporal parameter (\"%s\").\n", _name.c_str());
      _isTemporal = false;
      _temporalArgs.clear();
    }
    
  } else _isTemporal = false;
  
  
  return true;
}
bool Param::parseSubParamArgument ( const string &  arg)

Definition at line 240 of file param.cc.

Referenced by parseArgument().

{
  return true;
}
bool Param::parseTemporalArgument ( const string &  arg)

Definition at line 215 of file param.cc.

References _name, _temporalArgs, error(), tstring::removeFirstCharOf(), tstring::split(), and tstring::str2uint().

Referenced by parseArgument().

{
  vector< string > args = tstring::split(arg, ' ', true);
  
  if(args.size() != 2) {
    error("Param::parseTemporalArgument:: missing argument value in \"%s %s\".\n", _name.c_str(), arg.c_str());
    return false;
  }
  
  unsigned int gen = tstring::str2uint(tstring::removeFirstCharOf(tstring::removeFirstCharOf(args[0], '@'), 'g'));
  
  _temporalArgs[gen] = args[1];

  return true;
}
void Param::reset ( )

Clears the _isSet flag and the argument string.

Definition at line 71 of file param.cc.

References _arg, _isSet, and _setAtGeneration.

{
  _arg = "";
  _isSet = 0;
  _setAtGeneration = 0;
}
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.

References _arg, _bounds, _isBounded, _isSet, _type, BOOL, DBL, INT, tstring::isanumber(), MAT, parseArgument(), tstring::str2dble(), and tstring::str2int().

{  
  //cout << "Param::set ("<<arg<<")\n";
  if( arg[0] == '(' ) 
    if( !parseArgument( arg ) ) {
      errmsg = "could not parse the argument string";
      return false;
    }
  //integer or decimal: 
  if(_type == INT || _type == DBL) {
        if(arg[0] != '{') {
          
          if(!tstring::isanumber(arg)) {
        errmsg = "argument is not a number";
        return false;
          }
      
          double val = tstring::str2dble(arg);
          
          if(_isBounded && (val < _bounds[0] || val > _bounds[1]) ){
        errmsg = "argument is out of bounds";
        return false;
          }
        }
        
  //boolean:
  } else if(_type == BOOL) {
        
        if( !tstring::isanumber(arg) ){
      errmsg = "argument is not a boolean";
      return false;
    }
        
  } else if(_type == MAT) {
        if(arg[0] != '{'){
      errmsg = "argument is not a matrix";
      return false;
    }
  }
  // else if(_type == P || _type == S): no conditions to check
  
  _isSet = (_type == BOOL && tstring::str2int(arg) == 0 ? false : true);
  _arg = arg;
  return true;  
}
void Param::setArg ( string  value) [inline]

Sets the parameter's argument.

Definition at line 107 of file param.h.

{_arg = value;}
void Param::setAtGeneration ( unsigned int  generation) [inline]

Definition at line 121 of file param.h.

{_setAtGeneration = generation;}
void Param::setBounds ( double  low_bnd,
double  up_bnd 
) [inline]

Sets the bounds.

Definition at line 115 of file param.h.

{_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.

{_isBounded = value;}
void Param::setIsSet ( bool  value) [inline]

Sets the _isSet flag.

Definition at line 111 of file param.h.

{_isSet = value;}
void Param::setName ( string  value) [inline]

Sets the parameter's name.

Definition at line 105 of file param.h.

{_name = value;}
void Param::setOwner ( SimComponent owner) [inline]

Sets the pointer to owner.

Definition at line 117 of file param.h.

{_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.

{_type = value;}
void Param::setUpdater ( ParamUpdaterBase updater) [inline]

Sets the pointer to the updater object.

Definition at line 119 of file param.h.

{_myUpdater = updater;}
void Param::show_up ( )

Print state to stdout.

Definition at line 352 of file param.cc.

References _arg, _isSet, _name, _setAtGeneration, and message().

{
  message("%s = %s",_name.c_str(),_arg.c_str());
  if(_isSet)
    message(" (set at gen %i)\n",_setAtGeneration);
  else
    message(" (not set)\n");
}
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.

References _name, _temporalArgs, and error().

{
  string error_msg;
  if(_temporalArgs.find( generation ) != _temporalArgs.end()) {
    if( !set( _temporalArgs[generation], error_msg ) ){
      error("could not set \"%s\": %s", _name.c_str(), error_msg.c_str());
      return false;
    } else
      return true;
  } else
    error("parameters \"%s\" does not have an argument value for generation %i.\n", _name.c_str(), generation);
  return false; 
}

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 getValue(), parse_matrix(), reset(), set(), and show_up().

double Param::_bounds[2] [private]

The argument value boundaries.

Definition at line 68 of file param.h.

Referenced by Param(), and set().

bool Param::_isBounded [private]

Flag telling if the parameter has a bounded argument value.

Definition at line 63 of file param.h.

Referenced by set().

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.

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(), reset(), set(), 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.

Pointer to an ParamUpdater object.

Definition at line 74 of file param.h.

Referenced by parseArgument(), 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(), parse_matrix(), parseArgument(), parseTemporalArgument(), 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(), 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 getValue(), and set().


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

Generated for Nemo v2.2.0 by  doxygen 1.7.5.1 -- Nemo is hosted by  SourceForge.net Logo