Nemo  2.4.2
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
param.h
Go to the documentation of this file.
1 
28 #ifndef PARAM_H
29 #define PARAM_H
30 
31 #include <iostream>
32 #include <fstream>
33 #include <string>
34 #include <string.h>
35 #include <assert.h>
36 #include <map>
37 #include <deque>
38 #include <list>
39 #include <vector>
40 #include "handler.h"
41 #include "tmatrix.h"
42 #include "types.h"
43 #include "output.h"
44 
45 using namespace std;
46 class SimComponent;
47 class ParamUpdaterBase;
51 class Param
52  {
54  string _name;
56  string _arg;
58  string _input_arg;
62  bool _isSet;
64  bool _isBounded;
69  double _bounds[2];
71  unsigned int _setAtGeneration;
76 
78  vector< string > _externalFile;
80 
82  map< unsigned int, string > _temporalArgs;
85 
87  vector< string > _multiArgs;
89 
95 
96  public:
107  Param (string& Name, param_t Type, bool mandatory, bool bounded, double low_bnd, double up_bnd,
108  SimComponent* owner, ParamUpdaterBase* updater);
109 
111  Param (const Param& P);
112 
113  virtual ~Param ();
114 
116  void reset ();
117 
118  //accessors:
120  void setName (string value) {_name = value;}
122  void setArg (string value) {_arg = value; _input_arg = value;}
124  void setType (param_t value) {_type = value;}
126  void setIsSet (bool value) {_isSet = value;}
128  void setIsBounded (bool value) {_isBounded = value;}
130  void setIsRequired (bool value) {_isRequired = value;}
132  void setBounds (double low_bnd,double up_bnd) {_bounds[0]=low_bnd; _bounds[1]=up_bnd;}
134  void setOwner (SimComponent *owner) {_myOwner = owner;}
136  void setUpdater (ParamUpdaterBase* updater) {_myUpdater = updater;}
137 
138  void setAtGeneration (unsigned int generation) {_setAtGeneration = generation;}
139 
140 
141  string getName () {return _name;}
142  string getArg () {return _arg;}
144  string getInputArg () {return _input_arg;}
145  param_t getType () {return _type;}
146  bool isSet () {return _isSet;}
147  bool isBounded () {return _isBounded;}
148  bool isRequired () {return _isRequired;}
149  bool isTemporal () {return _isTemporal;}
150  bool hasMultipleArgs () {return _hasMultipleArgs;}
151  bool hasPreservedMacro () {return _hasPreservedMacro;}
152  void clearPreservedMacro () {_hasPreservedMacro = false;}
153  bool hasExternalFile () {return _hasExternalFile;}
154  double getBound (unsigned int i) {return _bounds[i];}
155  SimComponent* getOwner () {return _myOwner;}
156  ParamUpdaterBase* getUpdater () {return _myUpdater;}
157 
158  deque< unsigned int > getUpdatingDates ();
159 
160  deque< string > getTemporalArgs();
161 
162  vector< string > getMultiArgs();
163 
164  vector< string > getExternalFiles() {return _externalFile;}
165 
169  bool set (string arg, string& errmsg);
170 
172  bool update (unsigned int generation);
173 
177  double getValue ();
178 
180  bool isMatrix ()
181  { return (_type == MAT || (_arg.size() != 0 ? _arg[0] == '{' : false) ); }
182 
187  void getMatrix (TMatrix* mat);
188 
189  void getVariableMatrix (vector< vector <double> >* mat);
190 
194  void parse_matrix (TMatrix* mat);
195  void parse_variable_matrix (vector< vector <double> >* mat);
196  bool parseArgument (string& arg);
197  bool parseTemporalArgument (const string& arg);
198  bool parseAgeSpecArgument (const string& arg);
199  bool parseSubParamArgument (const string& arg);
200  string checkArgumentForExpansion (string arg);
201  string getArgumentFromFile (string file);
202 
204  void show_up ();
205 
206  };
207 
208 
213 class ParamSet
214  {
216  string _name;
218  bool _isSet;
222  map<string, Param*> _params;
225 
226  public:
227 
228  ParamSet ( ) : _isSet(0), _isRequired(0), _myOwner(0) { }
229  ParamSet (const ParamSet& PS);
230  ~ParamSet ( );
232  void reset ( );
233 
235  void clear ( ) {_params.clear();}
239  bool check_consistency ( );
241  void show_up ( );
243  void print (ofstream& FILE, bool commentOut = false);
245  int size ( ) {return _params.size();}
247  map<string, Param*>& getAllParams ( ) {return _params;}
250 
251  void add_param (Param* param) {_params[param->getName()] = param;}
260  void add_param (string Name, param_t Type, bool mandatory, bool isBounded,
261  double low_bnd, double up_bnd)
262  {add_param(Name, Type, mandatory, isBounded, low_bnd, up_bnd, 0);}
263 
264  void add_param (string Name, param_t Type, bool mandatory, bool isBounded,
265  double low_bnd, double up_bnd, ParamUpdaterBase* updater);
272  bool set_param (string Name, string Arg);
276  Param* find_param (string name);
280  Param* get_param (string name);
281 
285  bool has_param (string name);
286 
288  bool update_param (string Name, unsigned int generation);
290  void setName (string value) {_name = value;}
292  void setIsRequired (bool value) {_isRequired = value;}
294  void setOwner (SimComponent* owner) {_myOwner = owner;}
296  bool isSet () {return _isSet;}
298  string getName () {return _name;}
300  bool isRequired () {return _isRequired;}
302  bool isSet (string name) {return (get_param(name))->isSet();}
304  bool isMatrix (string name) {return (get_param(name))->isMatrix();}
306  bool isTemporal (string name) {return (get_param(name))->isTemporal();}
308  string getArg (string name) {return (get_param(name))->getArg();}
310  double getValue (string name) {return (get_param(name))->getValue();}
312  void getMatrix (string name, TMatrix* mat) {return (get_param(name))->getMatrix(mat);}
314  list<ParamUpdaterBase*> getUpdaters();
316  ParamSet& operator= (const ParamSet& PS);
317  };
318 
319 
320 //class ParamUpdaterBase
321 //
323 class ParamUpdaterBase : public Handler {
324 
325 protected:
327  list< Param* > _params;
328 
329 public:
330 
331  typedef list< Param* >::iterator PIT;
332 
333 public:
334 
336 
338  {
339  _params.assign(PU._params.begin(), PU._params.end());
340  }
341 
342  virtual ~ParamUpdaterBase() {}
343 
344  virtual void init () = 0;
345  virtual void update () {}
347  virtual bool update (unsigned int generation) = 0;
348 
349  virtual SimComponent* getComponent () = 0;
351  virtual void addParam (Param* param) {_params.push_back( param );}
353  virtual void reset ( ) {_params.clear();}
355  list< Param* > getParams ( ) {return _params;}
356 
357 };
358 
359 
360 //class ParamUpdater
361 //
371 template <class SC> class ParamUpdater : public virtual ParamUpdaterBase {
372 
373  bool (SC::*_myUpdaterFunction) (void);
375 
376 public:
377 
378  ParamUpdater(bool (SC::*updateFuncPtr) (void) ): _myComponent(0)
379  {
380  _myUpdaterFunction = updateFuncPtr;
381  }
382 
384  {
385  _params.assign(PU._params.begin(), PU._params.end());
386  _myUpdaterFunction = PU._myUpdaterFunction;
387  _myComponent = PU._myComponent;
388  }
389 
390  virtual ~ParamUpdater ()
391  {
392  for(PIT pit = _params.begin(); pit != _params.end(); pit++)
393  if((*pit)->getUpdater() == this) (*pit)->setUpdater(0);
394  }
395 
397  virtual void init ()
398  {
399  _myComponent = dynamic_cast<SC*> ( (*_params.begin())->getOwner() );
400 
401  for(PIT pit = ++_params.begin(); pit != _params.end(); pit++)
402  assert(_myComponent == (*pit)->getOwner());
403  }
405  virtual bool update (unsigned int generation)
406  {
407  return ((( dynamic_cast<SC*> (_myComponent))->*_myUpdaterFunction) ());
408  }
410  void setFuncPtr ( bool (SC::*updateFuncPtr) (void))
411  {
412  _myUpdaterFunction = updateFuncPtr;
413  }
415  virtual SC* getComponent () {return _myComponent;}
416 };
417 
418 #endif
Service handler (an observer).
Definition: handler.h:34
Parameters container, implemented in each SimComponent.
Definition: param.h:214
map< string, Param * > _params
List of parameters indexed by name.
Definition: param.h:222
map< string, Param * > & getAllParams()
Returns the complete list of parameters.
Definition: param.h:247
SimComponent * _myOwner
Pointer to the component that declared this parameter.
Definition: param.h:224
void clear()
Empties the parameter containers (no delete).
Definition: param.h:235
void getMatrix(string name, TMatrix *mat)
Accessor to the parameters matrix.
Definition: param.h:312
string getArg(string name)
Accessor to the parameters argument string.
Definition: param.h:308
int size()
Returns the number of parameters contained.
Definition: param.h:245
double getValue(string name)
Accessor the parameters value.
Definition: param.h:310
ParamSet()
Definition: param.h:228
bool _isRequired
Flaf is true if component is required (mandatory) to run a simulation (e.g.
Definition: param.h:220
string getName()
Name accessor.
Definition: param.h:298
string _name
The name of the component.
Definition: param.h:216
bool isSet(string name)
Accessor to the parameters status flag.
Definition: param.h:302
void add_param(Param *param)
Adds the param argument to the list.
Definition: param.h:251
void setName(string value)
Sets the container's name.
Definition: param.h:290
bool isMatrix(string name)
Check if the parameter "name" is of matrix type.
Definition: param.h:304
void setOwner(SimComponent *owner)
Sets the pointer to the SimComponents that owns this set.
Definition: param.h:294
void setIsRequired(bool value)
Sets the _isRequired flag meaning this container is mandatory and must be set in order to run a simul...
Definition: param.h:292
bool _isSet
Flag is true if all mandatory parameters are correctly set from user's input file.
Definition: param.h:218
bool isSet()
Accessor to the status flag.
Definition: param.h:296
bool isRequired()
Accessor to the mandatory flag.
Definition: param.h:300
void add_param(string Name, param_t Type, bool mandatory, bool isBounded, double low_bnd, double up_bnd)
Adds a new param specified by arguments to the list.
Definition: param.h:260
bool isTemporal(string name)
Check if the parameter "name" has temporal arguments.
Definition: param.h:306
Base class of the ParamUpdater class used to handle the temporal parameter argument values.
Definition: param.h:323
virtual ~ParamUpdaterBase()
Definition: param.h:342
virtual void addParam(Param *param)
Adds a parameter to the stack.
Definition: param.h:351
list< Param * > getParams()
Returns the list of parameters.
Definition: param.h:355
list< Param * >::iterator PIT
Definition: param.h:331
virtual void reset()
Clears the parameters stack.
Definition: param.h:353
virtual SimComponent * getComponent()=0
virtual void update()
Definition: param.h:345
ParamUpdaterBase(const ParamUpdaterBase &PU)
Definition: param.h:337
ParamUpdaterBase()
Definition: param.h:335
virtual bool update(unsigned int generation)=0
Updating procedure.
list< Param * > _params
List of the parameters affected by this updater.
Definition: param.h:327
virtual void init()=0
Implementation of the ParamUpdaterBase interface.
Definition: param.h:371
bool(SC::* _myUpdaterFunction)(void)
Definition: param.h:373
virtual bool update(unsigned int generation)
Calls the SimComponent's updating function using its pointer.
Definition: param.h:405
virtual void init()
Sets the pointer to the SimComponent.
Definition: param.h:397
ParamUpdater(bool(SC::*updateFuncPtr)(void))
Definition: param.h:378
SC * _myComponent
Definition: param.h:374
virtual ~ParamUpdater()
Definition: param.h:390
virtual SC * getComponent()
Accessor to the SimComponent.
Definition: param.h:415
void setFuncPtr(bool(SC::*updateFuncPtr)(void))
Sets the pointer to the updating function.
Definition: param.h:410
ParamUpdater(const ParamUpdater< SC > &PU)
Definition: param.h:383
This structure stores one parameter, its definition and its string argument.
Definition: param.h:52
bool hasMultipleArgs()
Definition: param.h:150
bool _hasMultipleArgs
Definition: param.h:88
unsigned int _setAtGeneration
Generation at which the parameter has been set/updated.
Definition: param.h:71
bool _hasPreservedMacro
Flag telling if this parameter's argument holds a macro preserved by the P() macro,...
Definition: param.h:94
void setArg(string value)
Sets the parameter's argument.
Definition: param.h:122
bool _isSet
Flag set once the parameter has recieved the right argument.
Definition: param.h:62
param_t getType()
Definition: param.h:145
bool isMatrix()
Checks if the argument is of matrix type.
Definition: param.h:180
vector< string > getExternalFiles()
Definition: param.h:164
bool isRequired()
Definition: param.h:148
param_t _type
The type of the argument, must one of DBL, INT, BOOL, STR or MAT (see types.h).
Definition: param.h:60
bool _isRequired
Flag telling if this parameter is mandatory.
Definition: param.h:67
bool isTemporal()
Definition: param.h:149
string getName()
Definition: param.h:141
map< unsigned int, string > _temporalArgs
The temporal arguments.
Definition: param.h:82
bool isBounded()
Definition: param.h:147
void clearPreservedMacro()
Definition: param.h:152
SimComponent * _myOwner
Pointer to the component that declared this parameter.
Definition: param.h:73
vector< string > _externalFile
External argument file.
Definition: param.h:78
bool _isBounded
Flag telling if the parameter has a bounded argument value.
Definition: param.h:64
void setBounds(double low_bnd, double up_bnd)
Sets the bounds.
Definition: param.h:132
vector< string > _multiArgs
Multiple arguments.
Definition: param.h:87
double getBound(unsigned int i)
Definition: param.h:154
string _arg
The argument string, set by the ParamsParser upon initialization.
Definition: param.h:56
string getArg()
Definition: param.h:142
void setType(param_t value)
Sets the parameter's type (see types.h)
Definition: param.h:124
string getInputArg()
Returns the original, untouched input argument string (for logging).
Definition: param.h:144
ParamUpdaterBase * _myUpdater
Pointer to an ParamUpdater object.
Definition: param.h:75
bool _hasExternalFile
Definition: param.h:79
void setName(string value)
Sets the parameter's name.
Definition: param.h:120
ParamUpdaterBase * getUpdater()
Definition: param.h:156
void setUpdater(ParamUpdaterBase *updater)
Sets the pointer to the updater object.
Definition: param.h:136
string _name
The name of the parameter as read in the init file.
Definition: param.h:54
bool hasExternalFile()
Definition: param.h:153
bool isSet()
Definition: param.h:146
void setIsSet(bool value)
Sets the _isSet flag.
Definition: param.h:126
void setIsBounded(bool value)
Sets the _isBounded flag.
Definition: param.h:128
bool _isTemporal
Flag telling if this parameter has temporal arguments.
Definition: param.h:84
void setAtGeneration(unsigned int generation)
Definition: param.h:138
void setIsRequired(bool value)
Sets the isRequired flag.
Definition: param.h:130
SimComponent * getOwner()
Definition: param.h:155
string _input_arg
The input argument string, kept untouched as a backup for logging.
Definition: param.h:58
void setOwner(SimComponent *owner)
Sets the pointer to owner.
Definition: param.h:134
bool hasPreservedMacro()
Definition: param.h:151
Interface to all basic components of a simulation (traits, life cycle events, pop,...
Definition: simcomponent.h:43
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:48
Nemo2.
Nemo2.
Nemo2.
Nemo2.
param_t
Param's types.
Definition: types.h:75
@ MAT
Definition: types.h:76

Generated for Nemo v2.4.2 by  doxygen 1.9.1

Catalogued on GSR