Nemo  2.4.0b
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
param.h
Go to the documentation of this file.
1 
30 #ifndef PARAM_H
31 #define PARAM_H
32 
33 #include <iostream>
34 #include <fstream>
35 #include <string>
36 #include <string.h>
37 #include <assert.h>
38 #include <map>
39 #include <deque>
40 #include <list>
41 #include <vector>
42 #include "handler.h"
43 #include "tmatrix.h"
44 #include "types.h"
45 #include "output.h"
46 
47 using namespace std;
48 class SimComponent;
49 class ParamUpdaterBase;
53 class Param
54  {
56  string _name;
58  string _arg;
60  string _input_arg;
64  bool _isSet;
66  bool _isBounded;
71  double _bounds[2];
73  unsigned int _setAtGeneration;
78 
80  vector< string > _externalFile;
82 
84  map< unsigned int, string > _temporalArgs;
87 
89  vector< string > _multiArgs;
91 
92  public:
103  Param (string& Name, param_t Type, bool mandatory, bool bounded, double low_bnd, double up_bnd,
104  SimComponent* owner, ParamUpdaterBase* updater);
105 
107  Param (const Param& P);
108 
109  virtual ~Param ();
110 
112  void reset ();
113 
114  //accessors:
116  void setName (string value) {_name = value;}
118  void setArg (string value) {_arg = value; _input_arg = value;}
120  void setType (param_t value) {_type = value;}
122  void setIsSet (bool value) {_isSet = value;}
124  void setIsBounded (bool value) {_isBounded = value;}
126  void setIsRequired (bool value) {_isRequired = value;}
128  void setBounds (double low_bnd,double up_bnd) {_bounds[0]=low_bnd; _bounds[1]=up_bnd;}
130  void setOwner (SimComponent *owner) {_myOwner = owner;}
132  void setUpdater (ParamUpdaterBase* updater) {_myUpdater = updater;}
133 
134  void setAtGeneration (unsigned int generation) {_setAtGeneration = generation;}
135 
136 
137  string getName () {return _name;}
138  string getArg () {return _input_arg;}
139  param_t getType () {return _type;}
140  bool isSet () {return _isSet;}
141  bool isBounded () {return _isBounded;}
142  bool isRequired () {return _isRequired;}
143  bool isTemporal () {return _isTemporal;}
144  bool hasMultipleArgs () {return _hasMultipleArgs;}
145  bool hasExternalFile () {return _hasExternalFile;}
146  double getBound (unsigned int i) {return _bounds[i];}
147  SimComponent* getOwner () {return _myOwner;}
148  ParamUpdaterBase* getUpdater () {return _myUpdater;}
149 
150  deque< unsigned int > getUpdatingDates ();
151 
152  deque< string > getTemporalArgs();
153 
154  vector< string > getMultiArgs();
155 
156  vector< string > getExternalFiles() {return _externalFile;}
157 
161  bool set (string arg, string& errmsg);
162 
164  bool update (unsigned int generation);
165 
169  double getValue ();
170 
172  bool isMatrix ()
173  { return (_type == MAT || (_arg.size() != 0 ? _arg[0] == '{' : false) ); }
174 
179  void getMatrix (TMatrix* mat);
180 
181  void getVariableMatrix (vector< vector <double> >* mat);
182 
186  void parse_matrix (TMatrix* mat);
187  void parse_variable_matrix (vector< vector <double> >* mat);
188  bool parseArgument (string& arg);
189  bool parseTemporalArgument (const string& arg);
190  bool parseAgeSpecArgument (const string& arg);
191  bool parseSubParamArgument (const string& arg);
192  string checkArgumentForExpansion (string arg);
193  string getArgumentFromFile (string file);
194 
196  void show_up ();
197 
198  };
199 
200 
205 class ParamSet
206  {
208  string _name;
210  bool _isSet;
214  map<string, Param*> _params;
217 
218  public:
219 
220  ParamSet ( ) : _isSet(0), _isRequired(0), _myOwner(0) { }
221  ParamSet (const ParamSet& PS);
222  ~ParamSet ( );
224  void reset ( );
225 
227  void clear ( ) {_params.clear();}
231  bool check_consistency ( );
233  void show_up ( );
235  void print (ofstream& FILE, bool commentOut = false);
237  int size ( ) {return _params.size();}
239  map<string, Param*>& getAllParams ( ) {return _params;}
242 
243  void add_param (Param* param) {_params[param->getName()] = param;}
252  void add_param (string Name, param_t Type, bool mandatory, bool isBounded,
253  double low_bnd, double up_bnd)
254  {add_param(Name, Type, mandatory, isBounded, low_bnd, up_bnd, 0);}
255 
256  void add_param (string Name, param_t Type, bool mandatory, bool isBounded,
257  double low_bnd, double up_bnd, ParamUpdaterBase* updater);
264  bool set_param (string Name, string Arg);
268  Param* find_param (string name);
272  Param* get_param (string name);
273 
277  bool has_param (string name);
278 
280  bool update_param (string Name, unsigned int generation);
282  void setName (string value) {_name = value;}
284  void setIsRequired (bool value) {_isRequired = value;}
286  void setOwner (SimComponent* owner) {_myOwner = owner;}
288  bool isSet () {return _isSet;}
290  string getName () {return _name;}
292  bool isRequired () {return _isRequired;}
294  bool isSet (string name) {return (get_param(name))->isSet();}
296  bool isMatrix (string name) {return (get_param(name))->isMatrix();}
298  bool isTemporal (string name) {return (get_param(name))->isTemporal();}
300  string getArg (string name) {return (get_param(name))->getArg();}
302  double getValue (string name) {return (get_param(name))->getValue();}
304  void getMatrix (string name, TMatrix* mat) {return (get_param(name))->getMatrix(mat);}
306  list<ParamUpdaterBase*> getUpdaters();
308  ParamSet& operator= (const ParamSet& PS);
309  };
310 
311 
312 //class ParamUpdaterBase
313 //
315 class ParamUpdaterBase : public Handler {
316 
317 protected:
319  list< Param* > _params;
320 
321 public:
322 
323  typedef list< Param* >::iterator PIT;
324 
325 public:
326 
328 
330  {
331  _params.assign(PU._params.begin(), PU._params.end());
332  }
333 
334  virtual ~ParamUpdaterBase() {}
335 
336  virtual void init () = 0;
337  virtual void update () {}
339  virtual bool update (unsigned int generation) = 0;
340 
341  virtual SimComponent* getComponent () = 0;
343  virtual void addParam (Param* param) {_params.push_back( param );}
345  virtual void reset ( ) {_params.clear();}
347  list< Param* > getParams ( ) {return _params;}
348 
349 };
350 
351 
352 //class ParamUpdater
353 //
363 template <class SC> class ParamUpdater : public virtual ParamUpdaterBase {
364 
365  bool (SC::*_myUpdaterFunction) (void);
367 
368 public:
369 
370  ParamUpdater(bool (SC::*updateFuncPtr) (void) ): _myComponent(0)
371  {
372  _myUpdaterFunction = updateFuncPtr;
373  }
374 
376  {
377  _params.assign(PU._params.begin(), PU._params.end());
378  _myUpdaterFunction = PU._myUpdaterFunction;
379  _myComponent = PU._myComponent;
380  }
381 
382  virtual ~ParamUpdater ()
383  {
384  for(PIT pit = _params.begin(); pit != _params.end(); pit++)
385  if((*pit)->getUpdater() == this) (*pit)->setUpdater(0);
386  }
387 
389  virtual void init ()
390  {
391  _myComponent = dynamic_cast<SC*> ( (*_params.begin())->getOwner() );
392 
393  for(PIT pit = ++_params.begin(); pit != _params.end(); pit++)
394  assert(_myComponent == (*pit)->getOwner());
395  }
397  virtual bool update (unsigned int generation)
398  {
399  return ((( dynamic_cast<SC*> (_myComponent))->*_myUpdaterFunction) ());
400  }
402  void setFuncPtr ( bool (SC::*updateFuncPtr) (void))
403  {
404  _myUpdaterFunction = updateFuncPtr;
405  }
407  virtual SC* getComponent () {return _myComponent;}
408 };
409 
410 #endif
Service handler (an observer).
Definition: handler.h:36
Parameters container, implemented in each SimComponent.
Definition: param.h:206
map< string, Param * > _params
List of parameters indexed by name.
Definition: param.h:214
map< string, Param * > & getAllParams()
Returns the complete list of parameters.
Definition: param.h:239
SimComponent * _myOwner
Pointer to the component that declared this parameter.
Definition: param.h:216
void clear()
Empties the parameter containers (no delete).
Definition: param.h:227
void getMatrix(string name, TMatrix *mat)
Accessor to the parameters matrix.
Definition: param.h:304
string getArg(string name)
Accessor to the parameters argument string.
Definition: param.h:300
int size()
Returns the number of parameters contained.
Definition: param.h:237
double getValue(string name)
Accessor the parameters value.
Definition: param.h:302
ParamSet()
Definition: param.h:220
bool _isRequired
Flaf is true if component is required (mandatory) to run a simulation (e.g.
Definition: param.h:212
string getName()
Name accessor.
Definition: param.h:290
string _name
The name of the component.
Definition: param.h:208
bool isSet(string name)
Accessor to the parameters status flag.
Definition: param.h:294
void add_param(Param *param)
Adds the param argument to the list.
Definition: param.h:243
void setName(string value)
Sets the container's name.
Definition: param.h:282
bool isMatrix(string name)
Check if the parameter "name" is of matrix type.
Definition: param.h:296
void setOwner(SimComponent *owner)
Sets the pointer to the SimComponents that owns this set.
Definition: param.h:286
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:284
bool _isSet
Flag is true if all mandatory parameters are correctly set from user's input file.
Definition: param.h:210
bool isSet()
Accessor to the status flag.
Definition: param.h:288
bool isRequired()
Accessor to the mandatory flag.
Definition: param.h:292
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:252
bool isTemporal(string name)
Check if the parameter "name" has temporal arguments.
Definition: param.h:298
Base class of the ParamUpdater class used to handle the temporal parameter argument values.
Definition: param.h:315
virtual ~ParamUpdaterBase()
Definition: param.h:334
virtual void addParam(Param *param)
Adds a parameter to the stack.
Definition: param.h:343
list< Param * > getParams()
Returns the list of parameters.
Definition: param.h:347
list< Param * >::iterator PIT
Definition: param.h:323
virtual void reset()
Clears the parameters stack.
Definition: param.h:345
virtual SimComponent * getComponent()=0
virtual void update()
Definition: param.h:337
ParamUpdaterBase(const ParamUpdaterBase &PU)
Definition: param.h:329
ParamUpdaterBase()
Definition: param.h:327
virtual bool update(unsigned int generation)=0
Updating procedure.
list< Param * > _params
List of the parameters affected by this updater.
Definition: param.h:319
virtual void init()=0
Implementation of the ParamUpdaterBase interface.
Definition: param.h:363
bool(SC::* _myUpdaterFunction)(void)
Definition: param.h:365
virtual bool update(unsigned int generation)
Calls the SimComponent's updating function using its pointer.
Definition: param.h:397
virtual void init()
Sets the pointer to the SimComponent.
Definition: param.h:389
ParamUpdater(bool(SC::*updateFuncPtr)(void))
Definition: param.h:370
SC * _myComponent
Definition: param.h:366
virtual ~ParamUpdater()
Definition: param.h:382
virtual SC * getComponent()
Accessor to the SimComponent.
Definition: param.h:407
void setFuncPtr(bool(SC::*updateFuncPtr)(void))
Sets the pointer to the updating function.
Definition: param.h:402
ParamUpdater(const ParamUpdater< SC > &PU)
Definition: param.h:375
This structure stores one parameter, its definition and its string argument.
Definition: param.h:54
bool hasMultipleArgs()
Definition: param.h:144
bool _hasMultipleArgs
Definition: param.h:90
unsigned int _setAtGeneration
Generation at which the parameter has been set/updated.
Definition: param.h:73
void setArg(string value)
Sets the parameter's argument.
Definition: param.h:118
bool _isSet
Flag set once the parameter has recieved the right argument.
Definition: param.h:64
param_t getType()
Definition: param.h:139
bool isMatrix()
Checks if the argument is of matrix type.
Definition: param.h:172
vector< string > getExternalFiles()
Definition: param.h:156
bool isRequired()
Definition: param.h:142
param_t _type
The type of the argument, must one of DBL, INT, BOOL, STR or MAT (see types.h).
Definition: param.h:62
bool _isRequired
Flag telling if this parameter is mandatory.
Definition: param.h:69
bool isTemporal()
Definition: param.h:143
string getName()
Definition: param.h:137
map< unsigned int, string > _temporalArgs
The temporal arguments.
Definition: param.h:84
bool isBounded()
Definition: param.h:141
SimComponent * _myOwner
Pointer to the component that declared this parameter.
Definition: param.h:75
vector< string > _externalFile
External argument file.
Definition: param.h:80
bool _isBounded
Flag telling if the parameter has a bounded argument value.
Definition: param.h:66
void setBounds(double low_bnd, double up_bnd)
Sets the bounds.
Definition: param.h:128
vector< string > _multiArgs
Multiple arguments.
Definition: param.h:89
double getBound(unsigned int i)
Definition: param.h:146
string _arg
The argument string, set by the ParamsParser upon initialization.
Definition: param.h:58
string getArg()
Definition: param.h:138
void setType(param_t value)
Sets the parameter's type (see types.h)
Definition: param.h:120
ParamUpdaterBase * _myUpdater
Pointer to an ParamUpdater object.
Definition: param.h:77
bool _hasExternalFile
Definition: param.h:81
void setName(string value)
Sets the parameter's name.
Definition: param.h:116
ParamUpdaterBase * getUpdater()
Definition: param.h:148
void setUpdater(ParamUpdaterBase *updater)
Sets the pointer to the updater object.
Definition: param.h:132
string _name
The name of the parameter as read in the init file.
Definition: param.h:56
bool hasExternalFile()
Definition: param.h:145
bool isSet()
Definition: param.h:140
void setIsSet(bool value)
Sets the _isSet flag.
Definition: param.h:122
void setIsBounded(bool value)
Sets the _isBounded flag.
Definition: param.h:124
bool _isTemporal
Flag telling if this parameter has temporal arguments.
Definition: param.h:86
void setAtGeneration(unsigned int generation)
Definition: param.h:134
void setIsRequired(bool value)
Sets the isRequired flag.
Definition: param.h:126
SimComponent * getOwner()
Definition: param.h:147
string _input_arg
The input argument string, kept untouched as a backup for logging.
Definition: param.h:60
void setOwner(SimComponent *owner)
Sets the pointer to owner.
Definition: param.h:130
Interface to all basic components of a simulation (traits, life cycle events, pop,...
Definition: simcomponent.h:45
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:50
param_t
Param's types.
Definition: types.h:77
@ MAT
Definition: types.h:78

Generated for Nemo v2.4.0b by  doxygen 1.9.1 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR