Virtual Belgium  2.0
A micro-simulation platform for the Belgian population
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Data.hpp
Go to the documentation of this file.
1 /****************************************************************
2  * DATA.HPP
3  *
4  * This file contains all the data related class and methods.
5  *
6  * Authors: J. Barthelemy and L. Hollaert
7  * Date : 17 july 2012
8  ****************************************************************/
9 
14 #ifndef DATA_HPP_
15 #define DATA_HPP_
16 
17 #include <iostream>
18 #include <fstream>
19 #include <sstream>
20 #include <map>
21 #include <vector>
22 #include <limits>
23 #include <string>
24 #include <stdexcept>
25 #include <boost/tokenizer.hpp>
26 #include <boost/lexical_cast.hpp>
27 #include <boost/algorithm/string.hpp>
28 #include <math.h>
29 #include "repast_hpc/Properties.h"
30 #include "repast_hpc/RepastProcess.h"
31 #include "Network.hpp"
32 #include "tinyxml2.hpp"
33 #include "Random.hpp"
34 #include "repast_hpc/TDataSource.h"
35 #include "repast_hpc/SVDataSet.h"
36 
37 
38 const int MODEL_AGENT_IND_TYPE = 0;
39 const int MODEL_AGENT_HH_TYPE = 1;
40 
41 
43 template <typename T>
44 class Singleton {
45 
46 protected:
47 
49  Singleton () {}
50 
52  ~Singleton () {}
53 
54 public:
55 
57 
60  static void makeInstance ( repast::Properties aProps ) {
61  if (NULL == _singleton) {
62  _singleton = new T(aProps);
63  }
64  }
65 
67 
70  static T *getInstance () {
71  if (NULL == _singleton) {
72  std::cerr << "No instance already created!" << std::endl;
73  }
74  return (static_cast<T*> (_singleton));
75  }
76 
78  static void kill () {
79  if (NULL != _singleton) {
80  delete _singleton;
81  _singleton = NULL;
82  }
83  }
84 
85 private:
86 
87  static T *_singleton;
88 
89 };
90 
92 template <typename T> T *Singleton<T>::_singleton = NULL;
93 
95 
100 class Data : public Singleton<Data> {
101 
102  friend class Singleton<Data>;
103 
104 private:
105 
106  std::map<int, std::vector<long> > _mun_age_men;
107  std::map<int, std::vector<long> > _mun_age_women;
108  std::map<int, float> _death_age_men;
109  std::map<int, float> _death_age_women;
110  std::map<int, float> _birth_age;
111  std::map<int, float> _birth_men;
112  std::multimap<int, long> _map_ins_node;
113  std::map<long,int> _map_node_ins;
114  std::map<int,char> _map_act_intToChar;
115  std::map<char,int> _map_act_charToInt;
116  std::map<int,dist_param_mixture> _map_act_dist_par_dist;
117  std::map<int,dist_param_mixture> _map_act_tdep_par_dist;
118  std::map<int, dist_param_mixture_2d> _map_act_start_x_dur;
121  std::map<int, long> _indic_mun_size;
122  std::map<int, int> _map_ins_id_mun;
123  std::map<int, int> _map_id_mun_ins;
124  repast::Properties _props;
125 
126 public:
127 
129 
134  Data( repast::Properties aProps ) {
135 
136  if (repast::RepastProcess::instance()->rank() == 0) {
137  std::cout << "Data reading" << std::endl;
138  }
139 
140  // Models properties
141 
142  this->_props = aProps;
143 
144  // Socio-demographics data
145 
148  read_death_age();
149  read_birth_age();
150 
151  // Network data
152 
153  read_node_ins();
154  read_network();
155  read_indicators();
156  read_ins_id_mun();
157 
158  // Activity model data
159 
165 
166 
167  }
168 
170  virtual ~Data() {};
171 
173  void read_mun_age_men();
174 
176  void read_mun_age_women();
177 
179  void read_death_age();
180 
182  void read_birth_age();
183 
185  void read_node_ins();
186 
188  void read_activity_cdb();
189 
191  void read_network();
192 
195 
198 
201 
204 
206  void read_indicators();
207 
209  void read_ins_id_mun();
210 
212 
218  std::vector<long int> getMunAge(int municipality, char gender);
219 
221 
227  float getDeathProba(char gender, int age);
228 
230 
235  float getBirthProba(int age);
236 
238 
243  float getBirthsex(int age);
244 
246 
251  std::vector<long> getNodesIdFromIns(int aIns);
252 
254 
259  long getOneNodeIdFromIns(int aIns);
260 
262 
267  dist_param_mixture getActDistParDist(int aActivityType );
268 
269 
270 
272 
277  dist_param_mixture getActHouseTDepParDist(int aActivityType );
278 
279 
281 
287  dist_param_mixture getActDurationCondiStartParDist(int aActivityType, int aStartTime);
288 
290 
296 
298 
301  const Network & getNetwork() const {
302  return _network;
303  }
304 
306 
309  const std::map<char, int>& getMapActCharToInt() const {
310  return _map_act_charToInt;
311  }
312 
314 
317  const std::map<int, char>& getMapActIntToChar() const {
318  return _map_act_intToChar;
319  }
320 
322 
325  const std::map<int, int>& getMapInsIdMun() const {
326  return _map_ins_id_mun;
327  }
328 
330 
333  const std::map<int, int>& getMapIdMunIns() const {
334  return _map_id_mun_ins;
335  }
336 
337 };
338 
339 
341 
344 class AggregateSum : public repast::TDataSource<int> {
345 
346 private :
347  int _sum;
348 
349 public :
350 
352  AggregateSum();
353 
355  virtual ~AggregateSum() {};
356 
358  void increment();
359 
361  void reset();
362 
364 
367  int getData();
368 
369 };
370 
371 
373 // Some useful routines. //
375 
377 
382 inline std::string secToTime( float n_sec ) {
383 
384  unsigned long n_sec_int = (unsigned long) floor(n_sec);
385 
386  std::ostringstream result;
387 
388  unsigned int hour = n_sec_int / 3600;
389  unsigned int min = (n_sec_int / 60) % 60;
390  unsigned int sec = n_sec_int % 60;
391 
392  result << hour << ":" << min << ":" << sec;
393 
394  return result.str();
395 
396 };
397 
399 
404 inline unsigned int secToHour( float n_sec ) {
405  return (unsigned long)floor(n_sec / 3600);
406 };
407 
409 
414 inline unsigned int secToHalfHour( float n_sec ) {
415  return (unsigned long)floor(n_sec / 1800);
416 };
417 
419 
424 long int linesCount(std::string filename);
425 
427 
433 template<typename T> std::vector<T> split(const std::string & msg,
434  const std::string & separators) {
435 
436  std::vector<T> result; // resulting vector
437  T token; // one token of the string
438  boost::char_separator<char> sep(separators.c_str()); // separator
439 
440  boost::tokenizer<boost::char_separator<char> > tok(msg, sep); // token's generation
441 
442  // string decomposition into token
443  for (boost::tokenizer<boost::char_separator<char> >::const_iterator i = tok.begin(); i != tok.end(); i++) {
444  std::stringstream s(*i);
445  string s_string = s.str(); // getting the string
446  boost::algorithm::trim(s_string); // removing trailing blanks
447  token = boost::lexical_cast<T>( s_string ); // casting operator
448  result.push_back(token); // pushing the casted string to the vector of results
449  }
450 
451  // returning the resulting decomposition
452  return result;
453 }
454 
455 #endif /* DATA_HPP_ */
void read_distribution_parameters_distance_x_duration_trip()
Read the distribution parameters for distance x duration of a trip.
Definition: Data.cpp:440
const std::map< char, int > & getMapActCharToInt() const
Return the character to integer activity code-book.
Definition: Data.hpp:309
std::map< int, long > _indic_mun_size
size indicator of a municipality
Definition: Data.hpp:121
std::map< int, std::vector< long > > _mun_age_women
women's age distribution by municipality
Definition: Data.hpp:107
std::map< int, float > _death_age_women
death's probability for a woman by age
Definition: Data.hpp:109
std::map< int, dist_param_mixture_2d > _map_act_start_x_dur
distribution parameters for activities' log(starting time) x log(duration)
Definition: Data.hpp:118
Definition: Random.hpp:83
const int MODEL_AGENT_IND_TYPE
constant for the individual agent type
Definition: Data.hpp:38
~Singleton()
Destructor.
Definition: Data.hpp:52
void reset()
Reset sum to 0.
Definition: Data.cpp:766
std::map< int, dist_param_mixture > _map_act_tdep_par_dist
distribution parameters for activities' house departure time (log normal)
Definition: Data.hpp:117
void read_distribution_parameters_house_tdep()
Read the distribution parameters for activities' house time departure.
Definition: Data.cpp:513
static T * _singleton
unique instance of the Data class
Definition: Data.hpp:87
dist_param_mixture getDurationCondiDistTripParDist(int aDistance)
Return a journey duration distribution's parameters conditional to the journey distance.
Definition: Data.cpp:728
std::multimap< int, long > _map_ins_node
set of node by municipalities' ins code
Definition: Data.hpp:112
std::map< int, float > _death_age_men
death's probability for a man by age
Definition: Data.hpp:108
dist_param_mixture_2d _act_dist_x_dur_trip_dist
distribution parameters for log(distance) x log(duration of the trip)
Definition: Data.hpp:119
const int MODEL_AGENT_HH_TYPE
constant for the household agent type
Definition: Data.hpp:39
Random number generators and related tools.
Singleton()
Constructor.
Definition: Data.hpp:49
void read_mun_age_men()
Read the age's distribution by municipality for the men.
Definition: Data.cpp:18
A Network class.
Definition: Network.hpp:479
dist_param_mixture getActDurationCondiStartParDist(int aActivityType, int aStartTime)
Return the activity duration distribution's parameter conditional to a starting time for a given acti...
Definition: Data.cpp:700
dist_param_mixture getActDistParDist(int aActivityType)
Return the distance's distribution's parameter of a given activity type.
Definition: Data.cpp:688
void read_birth_age()
Read the birth probability by age and the probability to have a boy or a girl.
Definition: Data.cpp:100
long int linesCount(std::string filename)
Compute the number of lines in a file.
std::vector< T > split(const std::string &msg, const std::string &separators)
Decompose a string according to a separator into a vector of type T.
Definition: Data.hpp:433
const Network & getNetwork() const
Return the road network.
Definition: Data.hpp:301
std::map< long, int > _map_node_ins
ins code of node
Definition: Data.hpp:113
Network _network
road network
Definition: Data.hpp:120
std::vector< long int > getMunAge(int municipality, char gender)
Get the age's distribution of a municipality for a given gender.
Definition: Data.cpp:634
std::map< char, int > _map_act_charToInt
activities' code-book (from character to integer encoding)
Definition: Data.hpp:115
unsigned int secToHour(float n_sec)
Convert a number of seconds to hours.
Definition: Data.hpp:404
std::map< int, dist_param_mixture > _map_act_dist_par_dist
distribution parameters for activities' distance (log normal)
Definition: Data.hpp:116
float getDeathProba(char gender, int age)
Compute and return the death's probability of an individual.
Definition: Data.cpp:641
unsigned int secToHalfHour(float n_sec)
Convert a number of seconds to half-hours.
Definition: Data.hpp:414
Definition: Random.hpp:55
const std::map< int, int > & getMapInsIdMun() const
Return the codebook of municipalities ins code to their id (1 to 589).
Definition: Data.hpp:325
const std::map< int, char > & getMapActIntToChar() const
Return the integer to character activity code-book.
Definition: Data.hpp:317
void read_activity_cdb()
Read the activities' codebook.
Definition: Data.cpp:157
void read_mun_age_women()
Read the age's distribution by municipality for the women.
Definition: Data.cpp:45
void increment()
Increment sum.
Definition: Data.cpp:762
dist_param_mixture getActHouseTDepParDist(int aActivityType)
Return the house departure time distribution's parameter for a given activity type.
Definition: Data.cpp:694
std::map< int, float > _birth_age
birth's probability by women's age
Definition: Data.hpp:110
int _sum
the aggregate sum computed over all processes used to run the simulation
Definition: Data.hpp:347
int getData()
Return the current state of sum.
Definition: Data.cpp:770
void read_network()
Read the road network.
Definition: Data.cpp:197
std::map< int, std::vector< long > > _mun_age_men
men's age distribution by municipality
Definition: Data.hpp:106
Data(repast::Properties aProps)
Constructor.
Definition: Data.hpp:134
void read_ins_id_mun()
Read the codebooks of ins code and municipality id (1 to 589).
Definition: Data.cpp:599
std::vector< long > getNodesIdFromIns(int aIns)
Return the list of node of a given municipality identified by its INS code.
Definition: Data.cpp:656
Road network related class and methods (Heap, Node, Link and Network classes).
void read_indicators()
Read the various indicators used by the activity localization model.
Definition: Data.cpp:570
void read_distribution_parameters_start_duration()
Read the distribution parameters for activities' starting time x duration.
Definition: Data.cpp:356
float getBirthProba(int age)
Compute and return the birth's probability of an individual by women's age.
Definition: Data.cpp:648
static void kill()
Free the memory.
Definition: Data.hpp:78
Singleton class for the Data class.
Definition: Data.hpp:44
repast::Properties _props
properties of simulation
Definition: Data.hpp:124
A data class.
Definition: Data.hpp:100
Aggregate output data class.
Definition: Data.hpp:344
static T * getInstance()
Return the generated, unique, instance of a T object (if already instanciated).
Definition: Data.hpp:70
std::map< int, int > _map_id_mun_ins
map of id of municipality (key) x ins code (value)
Definition: Data.hpp:123
void read_death_age()
Read the death probability by age and gender.
Definition: Data.cpp:72
std::map< int, char > _map_act_intToChar
activities' code-book (from integer to character encoding)
Definition: Data.hpp:114
std::map< int, int > _map_ins_id_mun
map of ins code (key) x id of municipality (value)
Definition: Data.hpp:122
const std::map< int, int > & getMapIdMunIns() const
Return the codebook of municipalities id (1 to 589) to their ins code.
Definition: Data.hpp:333
long getOneNodeIdFromIns(int aIns)
Return one node of a given municipality identified by its INS code.
Definition: Data.cpp:671
virtual ~Data()
Destructor.
Definition: Data.hpp:170
void read_distribution_parameters_distance()
Read the distribution parameters for activities' distance.
Definition: Data.cpp:297
std::map< int, float > _birth_men
birth's probability to have a boy
Definition: Data.hpp:111
float getBirthsex(int age)
Compute and return the probability to be a boy or girl.
Definition: Data.cpp:652
static void makeInstance(repast::Properties aProps)
Generates a singleton instance of a T object.
Definition: Data.hpp:60
virtual ~AggregateSum()
Destructor.
Definition: Data.hpp:355
void read_node_ins()
Read the municipalities's node.
Definition: Data.cpp:128
AggregateSum()
Constructor.
Definition: Data.cpp:758
std::string secToTime(float n_sec)
Convert a number of seconds to the format hour:min:sec.
Definition: Data.hpp:382