Nemo  2.4.1
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
param.h
Go to the documentation of this file.
1 
29 #ifndef PARAM_H
30 #define PARAM_H
31 
32 #include <iostream>
33 #include <fstream>
34 #include <string>
35 #include <string.h>
36 #include <assert.h>
37 #include <map>
38 #include <deque>
39 #include <list>
40 #include <vector>
41 #include "handler.h"
42 #include "tmatrix.h"
43 #include "types.h"
44 #include "output.h"
45 
46 using namespace std;
47 class SimComponent;
48 class ParamUpdaterBase;
52 class Param
53  {
55  string _name;
57  string _arg;
59  string _input_arg;
63  bool _isSet;
65  bool _isBounded;
70  double _bounds[2];
72  unsigned int _setAtGeneration;
77 
79  vector< string > _externalFile;
81 
83  map< unsigned int, string > _temporalArgs;
86 
88  vector< string > _multiArgs;
90 
96 
97  public:
108  Param (string& Name, param_t Type, bool mandatory, bool bounded, double low_bnd, double up_bnd,
109  SimComponent* owner, ParamUpdaterBase* updater);
110 
112  Param (const Param& P);
113 
114  virtual ~Param ();
115 
117  void reset ();
118 
119  //accessors:
121  void setName (string value) {_name = value;}
123  void setArg (string value) {_arg = value; _input_arg = value;}
125  void setType (param_t value) {_type = value;}
127  void setIsSet (bool value) {_isSet = value;}
129  void setIsBounded (bool value) {_isBounded = value;}
131  void setIsRequired (bool value) {_isRequired = value;}
133  void setBounds (double low_bnd,double up_bnd) {_bounds[0]=low_bnd; _bounds[1]=up_bnd;}
135  void setOwner (SimComponent *owner) {_myOwner = owner;}
137  void setUpdater (ParamUpdaterBase* updater) {_myUpdater = updater;}
138 
139  void setAtGeneration (unsigned int generation) {_setAtGeneration = generation;}
140 
141 
142  string getName () {return _name;}
143  string getArg () {return _arg;}
145  string getInputArg () {return _input_arg;}
146  param_t getType () {return _type;}
147  bool isSet () {return _isSet;}
148  bool isBounded () {return _isBounded;}
149  bool isRequired () {return _isRequired;}
150  bool isTemporal () {return _isTemporal;}
151  bool hasMultipleArgs () {return _hasMultipleArgs;}
152  bool hasPreservedMacro () {return _hasPreservedMacro;}
153  void clearPreservedMacro () {_hasPreservedMacro = false;}
154  bool hasExternalFile () {return _hasExternalFile;}
155  double getBound (unsigned int i) {return _bounds[i];}
156  SimComponent* getOwner () {return _myOwner;}
157  ParamUpdaterBase* getUpdater () {return _myUpdater;}
158 
159  deque< unsigned int > getUpdatingDates ();
160 
161  deque< string > getTemporalArgs();
162 
163  vector< string > getMultiArgs();
164 
165  vector< string > getExternalFiles() {return _externalFile;}
166 
170  bool set (string arg, string& errmsg);
171 
173  bool update (unsigned int generation);
174 
178  double getValue ();
179 
181  bool isMatrix ()
182  { return (_type == MAT || (_arg.size() != 0 ? _arg[0] == '{' : false) ); }
183 
188  void getMatrix (TMatrix* mat);
189 
190  void getVariableMatrix (vector< vector <double> >* mat);
191 
195  void parse_matrix (TMatrix* mat);
196  void parse_variable_matrix (vector< vector <double> >* mat);
197  bool parseArgument (string& arg);
198  bool parseTemporalArgument (const string& arg);
199  bool parseAgeSpecArgument (const string& arg);
200  bool parseSubParamArgument (const string& arg);
201  string checkArgumentForExpansion (string arg);
202  string getArgumentFromFile (string file);
203 
205  void show_up ();
206 
207  };
208 
209 
214 class ParamSet
215  {
217  string _name;
219  bool _isSet;
223  map<string, Param*> _params;
226 
227  public:
228 
229  ParamSet ( ) : _isSet(0), _isRequired(0), _myOwner(0) { }
230  ParamSet (const ParamSet& PS);
231  ~ParamSet ( );
233  void reset ( );
234 
236  void clear ( ) {_params.clear();}
240  bool check_consistency ( );
242  void show_up ( );
244  void print (ofstream& FILE, bool commentOut = false);
246  int size ( ) {return _params.size();}
248  map<string, Param*>& getAllParams ( ) {return _params;}
251 
252  void add_param (Param* param) {_params[param->getName()] = param;}
261  void add_param (string Name, param_t Type, bool mandatory, bool isBounded,
262  double low_bnd, double up_bnd)
263  {add_param(Name, Type, mandatory, isBounded, low_bnd, up_bnd, 0);}
264 
265  void add_param (string Name, param_t Type, bool mandatory, bool isBounded,
266  double low_bnd, double up_bnd, ParamUpdaterBase* updater);
273  bool set_param (string Name, string Arg);
277  Param* find_param (string name);
281  Param* get_param (string name);
282 
286  bool has_param (string name);
287 
289  bool update_param (string Name, unsigned int generation);
291  void setName (string value) {_name = value;}
293  void setIsRequired (bool value) {_isRequired = value;}
295  void setOwner (SimComponent* owner) {_myOwner = owner;}
297  bool isSet () {return _isSet;}
299  string getName () {return _name;}
301  bool isRequired () {return _isRequired;}
303  bool isSet (string name) {return (get_param(name))->isSet();}
305  bool isMatrix (string name) {return (get_param(name))->isMatrix();}
307  bool isTemporal (string name) {return (get_param(name))->isTemporal();}
309  string getArg (string name) {return (get_param(name))->getArg();}
311  double getValue (string name) {return (get_param(name))->getValue();}
313  void getMatrix (string name, TMatrix* mat) {return (get_param(name))->getMatrix(mat);}
315  list<ParamUpdaterBase*> getUpdaters();
317  ParamSet& operator= (const ParamSet& PS);
318  };
319 
320 
321 //class ParamUpdaterBase
322 //
324 class ParamUpdaterBase : public Handler {
325 
326 protected:
328  list< Param* > _params;
329 
330 public:
331 
332  typedef list< Param* >::iterator PIT;
333 
334 public:
335 
337 
339  {
340  _params.assign(PU._params.begin(), PU._params.end());
341  }
342 
343  virtual ~ParamUpdaterBase() {}
344 
345  virtual void init () = 0;
346  virtual void update () {}
348  virtual bool update (unsigned int generation) = 0;
349 
350  virtual SimComponent* getComponent () = 0;
352  virtual void addParam (Param* param) {_params.push_back( param );}
354  virtual void reset ( ) {_params.clear();}
356  list< Param* > getParams ( ) {return _params;}
357 
358 };
359 
360 
361 //class ParamUpdater
362 //
372 template <class SC> class ParamUpdater : public virtual ParamUpdaterBase {
373 
374  bool (SC::*_myUpdaterFunction) (void);
376 
377 public:
378 
379  ParamUpdater(bool (SC::*updateFuncPtr) (void) ): _myComponent(0)
380  {
381  _myUpdaterFunction = updateFuncPtr;
382  }
383 
385  {
386  _params.assign(PU._params.begin(), PU._params.end());
387  _myUpdaterFunction = PU._myUpdaterFunction;
388  _myComponent = PU._myComponent;
389  }
390 
391  virtual ~ParamUpdater ()
392  {
393  for(PIT pit = _params.begin(); pit != _params.end(); pit++)
394  if((*pit)->getUpdater() == this) (*pit)->setUpdater(0);
395  }
396 
398  virtual void init ()
399  {
400  _myComponent = dynamic_cast<SC*> ( (*_params.begin())->getOwner() );
401 
402  for(PIT pit = ++_params.begin(); pit != _params.end(); pit++)
403  assert(_myComponent == (*pit)->getOwner());
404  }
406  virtual bool update (unsigned int generation)
407  {
408  return ((( dynamic_cast<SC*> (_myComponent))->*_myUpdaterFunction) ());
409  }
411  void setFuncPtr ( bool (SC::*updateFuncPtr) (void))
412  {
413  _myUpdaterFunction = updateFuncPtr;
414  }
416  virtual SC* getComponent () {return _myComponent;}
417 };
418 
419 #endif
Service handler (an observer).
Definition: handler.h:35
Parameters container, implemented in each SimComponent.
Definition: param.h:215
map< string, Param * > _params
List of parameters indexed by name.
Definition: param.h:223
map< string, Param * > & getAllParams()
Returns the complete list of parameters.
Definition: param.h:248
SimComponent * _myOwner
Pointer to the component that declared this parameter.
Definition: param.h:225
void clear()
Empties the parameter containers (no delete).
Definition: param.h:236
void getMatrix(string name, TMatrix *mat)
Accessor to the parameters matrix.
Definition: param.h:313
string getArg(string name)
Accessor to the parameters argument string.
Definition: param.h:309
int size()
Returns the number of parameters contained.
Definition: param.h:246
double getValue(string name)
Accessor the parameters value.
Definition: param.h:311
ParamSet()
Definition: param.h:229
bool _isRequired
Flaf is true if component is required (mandatory) to run a simulation (e.g.
Definition: param.h:221
string getName()
Name accessor.
Definition: param.h:299
string _name
The name of the component.
Definition: param.h:217
bool isSet(string name)
Accessor to the parameters status flag.
Definition: param.h:303
void add_param(Param *param)
Adds the param argument to the list.
Definition: param.h:252
void setName(string value)
Sets the container's name.
Definition: param.h:291
bool isMatrix(string name)
Check if the parameter "name" is of matrix type.
Definition: param.h:305
void setOwner(SimComponent *owner)
Sets the pointer to the SimComponents that owns this set.
Definition: param.h:295
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:293
bool _isSet
Flag is true if all mandatory parameters are correctly set from user's input file.
Definition: param.h:219
bool isSet()
Accessor to the status flag.
Definition: param.h:297
bool isRequired()
Accessor to the mandatory flag.
Definition: param.h:301
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:261
bool isTemporal(string name)
Check if the parameter "name" has temporal arguments.
Definition: param.h:307
Base class of the ParamUpdater class used to handle the temporal parameter argument values.
Definition: param.h:324
virtual ~ParamUpdaterBase()
Definition: param.h:343
virtual void addParam(Param *param)
Adds a parameter to the stack.
Definition: param.h:352
list< Param * > getParams()
Returns the list of parameters.
Definition: param.h:356
list< Param * >::iterator PIT
Definition: param.h:332
virtual void reset()
Clears the parameters stack.
Definition: param.h:354
virtual SimComponent * getComponent()=0
virtual void update()
Definition: param.h:346
ParamUpdaterBase(const ParamUpdaterBase &PU)
Definition: param.h:338
ParamUpdaterBase()
Definition: param.h:336
virtual bool update(unsigned int generation)=0
Updating procedure.
list< Param * > _params
List of the parameters affected by this updater.
Definition: param.h:328
virtual void init()=0
Implementation of the ParamUpdaterBase interface.
Definition: param.h:372
bool(SC::* _myUpdaterFunction)(void)
Definition: param.h:374
virtual bool update(unsigned int generation)
Calls the SimComponent's updating function using its pointer.
Definition: param.h:406
virtual void init()
Sets the pointer to the SimComponent.
Definition: param.h:398
ParamUpdater(bool(SC::*updateFuncPtr)(void))
Definition: param.h:379
SC * _myComponent
Definition: param.h:375
virtual ~ParamUpdater()
Definition: param.h:391
virtual SC * getComponent()
Accessor to the SimComponent.
Definition: param.h:416
void setFuncPtr(bool(SC::*updateFuncPtr)(void))
Sets the pointer to the updating function.
Definition: param.h:411
ParamUpdater(const ParamUpdater< SC > &PU)
Definition: param.h:384
This structure stores one parameter, its definition and its string argument.
Definition: param.h:53
bool hasMultipleArgs()
Definition: param.h:151
bool _hasMultipleArgs
Definition: param.h:89
unsigned int _setAtGeneration
Generation at which the parameter has been set/updated.
Definition: param.h:72
bool _hasPreservedMacro
Flag telling if this parameter's argument holds a macro preserved by the P() macro,...
Definition: param.h:95
void setArg(string value)
Sets the parameter's argument.
Definition: param.h:123
bool _isSet
Flag set once the parameter has recieved the right argument.
Definition: param.h:63
param_t getType()
Definition: param.h:146
bool isMatrix()
Checks if the argument is of matrix type.
Definition: param.h:181
vector< string > getExternalFiles()
Definition: param.h:165
bool isRequired()
Definition: param.h:149
param_t _type
The type of the argument, must one of DBL, INT, BOOL, STR or MAT (see types.h).
Definition: param.h:61
bool _isRequired
Flag telling if this parameter is mandatory.
Definition: param.h:68
bool isTemporal()
Definition: param.h:150
string getName()
Definition: param.h:142
map< unsigned int, string > _temporalArgs
The temporal arguments.
Definition: param.h:83
bool isBounded()
Definition: param.h:148
void clearPreservedMacro()
Definition: param.h:153
SimComponent * _myOwner
Pointer to the component that declared this parameter.
Definition: param.h:74
vector< string > _externalFile
External argument file.
Definition: param.h:79
bool _isBounded
Flag telling if the parameter has a bounded argument value.
Definition: param.h:65
void setBounds(double low_bnd, double up_bnd)
Sets the bounds.
Definition: param.h:133
vector< string > _multiArgs
Multiple arguments.
Definition: param.h:88
double getBound(unsigned int i)
Definition: param.h:155
string _arg
The argument string, set by the ParamsParser upon initialization.
Definition: param.h:57
string getArg()
Definition: param.h:143
void setType(param_t value)
Sets the parameter's type (see types.h)
Definition: param.h:125
string getInputArg()
Returns the original, untouched input argument string (for logging).
Definition: param.h:145
ParamUpdaterBase * _myUpdater
Pointer to an ParamUpdater object.
Definition: param.h:76
bool _hasExternalFile
Definition: param.h:80
void setName(string value)
Sets the parameter's name.
Definition: param.h:121
ParamUpdaterBase * getUpdater()
Definition: param.h:157
void setUpdater(ParamUpdaterBase *updater)
Sets the pointer to the updater object.
Definition: param.h:137
string _name
The name of the parameter as read in the init file.
Definition: param.h:55
bool hasExternalFile()
Definition: param.h:154
bool isSet()
Definition: param.h:147
void setIsSet(bool value)
Sets the _isSet flag.
Definition: param.h:127
void setIsBounded(bool value)
Sets the _isBounded flag.
Definition: param.h:129
bool _isTemporal
Flag telling if this parameter has temporal arguments.
Definition: param.h:85
void setAtGeneration(unsigned int generation)
Definition: param.h:139
void setIsRequired(bool value)
Sets the isRequired flag.
Definition: param.h:131
SimComponent * getOwner()
Definition: param.h:156
string _input_arg
The input argument string, kept untouched as a backup for logging.
Definition: param.h:59
void setOwner(SimComponent *owner)
Sets the pointer to owner.
Definition: param.h:135
bool hasPreservedMacro()
Definition: param.h:152
Interface to all basic components of a simulation (traits, life cycle events, pop,...
Definition: simcomponent.h:44
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49
Nemo2.
Nemo2.
Nemo2.
Nemo2.
param_t
Param's types.
Definition: types.h:76
@ MAT
Definition: types.h:77

Generated for Nemo v2.4.1 by  doxygen 1.9.1

Catalogued on GSR