Virtual Belgium  2.0
A micro-simulation platform for the Belgian population
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Network.hpp
Go to the documentation of this file.
1 /****************************************************************
2  * NETWORK.HPP
3  *
4  * This file contains all the data related class and methods.
5  *
6  * Authors: J. Barthelemy
7  * Date : 08 april 2013
8  ****************************************************************/
9 
14 #ifndef NETWORK_HPP_
15 #define NETWORK_HPP_
16 
17 #include <map>
18 #include <vector>
19 #include <iostream>
20 #include <functional>
21 #include <limits>
22 #include <utility>
23 #include <sstream>
24 #include <set>
25 #include "Random.hpp"
26 #include "FiboHeap.hpp"
27 
29 
36 class Node {
37 
38 private:
39 
40  long _id;
41  float _x;
42  float _y;
43  int _ins;
44 
45  std::vector<long> _links_out_id;
46  float _key;
47  int _index;
48 
49  std::map<std::string,long> _indicators;
50 
51 
52 public:
53 
55  Node() : _id(0), _x(0), _y(0), _ins(0), _links_out_id(), _key(-1), _index(-1), _indicators() {};
56 
58 
64  Node(long id, float x, float y, int ins);
65 
67  virtual ~Node() {};
68 
70 
73  long getId() const {
74  return _id;
75  }
76 
78 
81  void setId(long id) {
82  _id = id;
83  }
84 
86 
89  int getIns() const {
90  return _ins;
91  }
92 
94 
97  void setIns(int ins) {
98  _ins = ins;
99  }
100 
102 
105  const std::vector<long>& getLinksOutId() const {
106  return _links_out_id;
107  }
108 
110 
113  void setLinksOutId(const std::vector<long>& linksOutId) {
114  _links_out_id = linksOutId;
115  }
116 
118 
121  void addLinkOutId( long linkId ) {
122  _links_out_id.push_back(linkId);
123  }
124 
126 
129  float getKey() const {
130  return _key;
131  }
132 
134 
137  void setKey(float key) {
138  _key = key;
139  }
140 
142 
145  float getX() const {
146  return _x;
147  }
148 
150 
153  void setX(float x) {
154  _x = x;
155  }
156 
158 
161  float getY() const {
162  return _y;
163  }
164 
166 
169  void setY(float y) {
170  _y = y;
171  }
172 
174 
177  int getIndex() const {
178  return _index;
179  }
180 
182 
185  void setIndex(int index) {
186  _index = index;
187  }
188 
190 
193  const std::map<std::string, long>& getIndicators() const {
194  return _indicators;
195  }
196 
198 
201  void setIndicators(const std::map<std::string, long>& indicators) {
202  _indicators = indicators;
203  }
204 
206 
210  void addIndicator(std::string aIndicator, long aIndicatorValue );
211 
213 
219  friend bool operator<(const Node &aNode1, const Node &aNode2);
220 
222 
228  friend bool operator>(const Node &aNode1, const Node &aNode2);
229 
231 
237  bool operator<(Node &aNode) {
238  return this->_key < aNode.getKey();
239  }
240 
242 
248  bool operator>(Node &aNode) {
249  return this->_key > aNode.getKey();
250  }
251 
252 };
253 
254 
256 
261 class Link {
262 
263 private:
264 
265  long _id;
268  float _length;
269 
270 public:
271 
273  Link() : _id(0), _start_node_id(0), _end_node_id(0), _length(0) {};
274 
276 
282  Link(long id, long start_node, long end_node, float length);
283 
285  virtual ~Link() {};
286 
288 
291  long getEndNodeId() const {
292  return _end_node_id;
293  }
294 
296 
299  void setEndNodeId(long endNodeId) {
300  _end_node_id = endNodeId;
301  }
302 
304 
307  long getId() const {
308  return _id;
309  }
310 
312 
315  void setId(long id) {
316  _id = id;
317  }
318 
320 
323  float getLength() const {
324  return _length;
325  }
326 
328 
331  void setLength(float length) {
332  _length = length;
333  }
334 
336 
339  long getStartNodeId() const {
340  return _start_node_id;
341  }
342 
344 
347  void setStartNodeId(long startNodeId) {
348  _start_node_id = startNodeId;
349  }
350 
351 };
352 
353 
355 
365 class DHeap {
366 
367 private:
368 
369  std::vector<Node> heap;
370  std::map<long,int> _map_nodeid_index;
371  int _d;
372 
374 
377  int parent(int child);
378 
380 
384  int child(int parent, int ith);
385 
387 
394  void heapifyup(int index);
395 
397 
406  void heapifydown(int index);
407 
408 public:
409 
411  DHeap();
412 
414 
418  DHeap(unsigned long size, unsigned int d);
419 
421 
426  DHeap(unsigned int d, long node_id, std::map<long, Node> &Nodes);
427 
429  virtual ~DHeap();
430 
432 
435  void insert(Node &element);
436 
438 
441  void pushBack(Node &element);
442 
444 
447  const Node deletemin();
448 
450 
454  void decreaseKey( int index, float key );
455 
457 
460  std::map<long, int> getMapNodeidIndex() const {
461  return _map_nodeid_index;
462  }
463 
465 
468  int size() {
469  return heap.size();
470  }
471 
472 };
473 
475 
479 class Network {
480 
481 private:
482 
483  std::map<long, Node> _Nodes;
484  std::map<long, Link> _Links;
485 
486  float min_x;
487  float max_x;
488  float min_y;
489  float max_y;
490 
491 public:
492 
495 
496  min_x = 0.0;
497  max_x = 0.0;
498  min_y = 0.0;
499  max_y = 0.0;
500 
501  };
502 
504  virtual ~Network() {};
505 
507 
510  const std::map<long, Link>& getLinks() const {
511  return _Links;
512  }
513 
515 
518  void setLinks(const std::map<long, Link>& links) {
519  _Links = links;
520  }
521 
523 
526  const std::map<long, Node>& getNodes() const {
527  return _Nodes;
528  }
529 
531 
534  void setNodes(const std::map<long, Node>& nodes) {
535  _Nodes = nodes;
536  }
537 
539  /*
540  /param nodeId the source node's id
541  /param linkId the id of link to be added
542  */
543  void addLinkOutToNode(long nodeId, long linkId) {
544  _Nodes[nodeId].addLinkOutId(linkId);
545  }
546 
548 
551  void addNode(Node aNode);
552 
554 
557  void addLink(Link aLink);
558 
560 
574  long getDestFromSource(long source_id, float dist);
575 
577 
583  float getDistanceNodes(long source_id, long dest_id);
584 
586 
589  float getMaxX() const {
590  return max_x;
591  }
592 
594 
597  void setMaxX(float maxX) {
598  max_x = maxX;
599  }
600 
602 
605  float getMaxY() const {
606  return max_y;
607  }
608 
610 
613  void setMaxY(float maxY) {
614  max_y = maxY;
615  }
616 
618 
621  float getMinX() const {
622  return min_x;
623  }
624 
626 
629  void setMinX(float minX) {
630  min_x = minX;
631  }
632 
634 
637  float getMinY() const {
638  return min_y;
639  }
640 
642 
645  void setMinY(float minY) {
646  min_y = minY;
647  }
648 
649 };
650 
651 #endif /* NETWORK_HPP_ */
void setMaxY(float maxY)
Set maximum x coordinate.
Definition: Network.hpp:613
long _id
id of the node.
Definition: Network.hpp:40
void heapifydown(int index)
Ensure the heap invariant (up -> bottom), used during removal of a node.
Definition: Network.cpp:96
friend bool operator<(const Node &aNode1, const Node &aNode2)
Overloading '<' operator.
Definition: Network.cpp:339
std::map< long, Link > _Links
Links of the network (see Link class)
Definition: Network.hpp:484
void setKey(float key)
Setting the key value (used by Dijkstra algorithm).
Definition: Network.hpp:137
const std::map< long, Link > & getLinks() const
Returns the network's links.
Definition: Network.hpp:510
void setMinX(float minX)
Set minimum x coordinate.
Definition: Network.hpp:629
float getMinY() const
Return the minimum y coordinate.
Definition: Network.hpp:637
void setMinY(float minY)
Set minimum y coordinate.
Definition: Network.hpp:645
A node class.
Definition: Network.hpp:36
Node()
Default Constructor.
Definition: Network.hpp:55
friend bool operator>(const Node &aNode1, const Node &aNode2)
Overloading '>' operator.
Definition: Network.cpp:345
virtual ~DHeap()
Destructor.
Definition: Network.cpp:54
void setMaxX(float maxX)
Set maximum x coordinate.
Definition: Network.hpp:597
bool operator<(Node &aNode)
Overloading '<' operator.
Definition: Network.hpp:237
float getX() const
Return the x coordinate.
Definition: Network.hpp:145
float getY() const
Return the y coordinate.
Definition: Network.hpp:161
float getMaxY() const
Return the maximum y coordinate.
Definition: Network.hpp:605
int parent(int child)
Return the index of the parent of child node.
Definition: Network.cpp:58
int _index
given the value of key, correspond to the index of a node inside a D-Heap
Definition: Network.hpp:47
Random number generators and related tools.
void decreaseKey(int index, float key)
Decrease the key of a given Node and update the heap structure.
Definition: Network.cpp:124
void addIndicator(std::string aIndicator, long aIndicatorValue)
Add an indicator and its value to the set of node's indicator.
Definition: Network.cpp:333
int getIndex() const
Get the index value (used by Dijkstra algorithm).
Definition: Network.hpp:177
void addLink(Link aLink)
Add a link to the network.
Definition: Network.cpp:182
float getKey() const
Get the key value (used by Dijkstra algorithm).
Definition: Network.hpp:129
int _d
degree of the heap, i.e. maximum number of childxs
Definition: Network.hpp:371
const Node deletemin()
Return the node's id with maximum priority (i.e. the first of the heap) and removes it...
Definition: Network.cpp:159
float getDistanceNodes(long source_id, long dest_id)
Compute the distance between two nodes in the network.
Definition: Network.cpp:266
float getMaxX() const
Return the maximum x coordinate.
Definition: Network.hpp:589
A Network class.
Definition: Network.hpp:479
void setIndicators(const std::map< std::string, long > &indicators)
Set the indicators of the node.
Definition: Network.hpp:201
std::vector< long > _links_out_id
set of outgoing links.
Definition: Network.hpp:45
void setNodes(const std::map< long, Node > &nodes)
Setting the network's nodes.
Definition: Network.hpp:534
void addNode(Node aNode)
Add a node to the network.
Definition: Network.cpp:175
float _x
x coordinate.
Definition: Network.hpp:41
Network()
Constructor.
Definition: Network.hpp:494
void addLinkOutId(long linkId)
Add an link to the set of outgoing links.
Definition: Network.hpp:121
float getMinX() const
Return the minimum x coordinate.
Definition: Network.hpp:621
A n-ary tree data structure used by Dijkstra's shortest path algorithm.
Definition: Network.hpp:365
int child(int parent, int ith)
Return the index of i-th child of a node.
Definition: Network.cpp:69
virtual ~Network()
Destructor.
Definition: Network.hpp:504
void setIns(int ins)
Set the INS code of the node (corresponding to the municipality to which the node belongs)...
Definition: Network.hpp:97
Fibonacci heap data structure implementation.
void addLinkOutToNode(long nodeId, long linkId)
Add a link to the set of outgoing link of a node.
Definition: Network.hpp:543
void setLinks(const std::map< long, Link > &links)
Setting the network's links.
Definition: Network.hpp:518
std::vector< Node > heap
heap of Node.
Definition: Network.hpp:369
std::map< long, Node > _Nodes
Nodes of the network (see Node class)
Definition: Network.hpp:483
const std::map< std::string, long > & getIndicators() const
Return the indicators of the node.
Definition: Network.hpp:193
void setLinksOutId(const std::vector< long > &linksOutId)
Setter for the set of outgoing links of the node.
Definition: Network.hpp:113
const std::vector< long > & getLinksOutId() const
Return the set of outgoing links of the node.
Definition: Network.hpp:105
int size()
Compute the size of the heap.
Definition: Network.hpp:468
virtual ~Node()
Destructor.
Definition: Network.hpp:67
DHeap()
Default constructor.
Definition: Network.cpp:17
void insert(Node &element)
Insert a node in the heap.
Definition: Network.cpp:132
float max_x
Maximum x coordinate.
Definition: Network.hpp:487
float min_y
Minimum y coordinate.
Definition: Network.hpp:488
std::map< long, int > getMapNodeidIndex() const
Return the map <node id, node index>.
Definition: Network.hpp:460
void setId(long id)
Set the id of the node.
Definition: Network.hpp:81
void heapifyup(int index)
Ensure the heap invariant (bottom -> up), used to add a node to the heap.
Definition: Network.cpp:79
float _key
key value of a node, corresponding to the distance between the node and a source one ...
Definition: Network.hpp:46
float _y
y coordinate.
Definition: Network.hpp:42
long getDestFromSource(long source_id, float dist)
Compute the set of destination nodes at a given distance from a source node.
Definition: Network.cpp:189
int _ins
ins code of the node, corresponding to the municipality to which the node belongs.
Definition: Network.hpp:43
bool operator>(Node &aNode)
Overloading '>' operator.
Definition: Network.hpp:248
const std::map< long, Node > & getNodes() const
Return the network's nodes.
Definition: Network.hpp:526
void setX(float x)
Set the x coordinate.
Definition: Network.hpp:153
long getId() const
Return the id of the node.
Definition: Network.hpp:73
float min_x
Minimum x coordinate.
Definition: Network.hpp:486
int getIns() const
Return the INS code of the node (corresponding to the municipality to which the node belongs)...
Definition: Network.hpp:89
void pushBack(Node &element)
Insert a node at the end of the heap.
Definition: Network.cpp:146
std::map< std::string, long > _indicators
a map of indicators related to the municipality to which the node belongs.
Definition: Network.hpp:49
std::map< long, int > _map_nodeid_index
map of the node's id and their respective index.
Definition: Network.hpp:370
void setY(float y)
Set the y coordinate.
Definition: Network.hpp:169
float max_y
Maximum y coordinate.
Definition: Network.hpp:489
void setIndex(int index)
Set the index value (used by Dijkstra algorithm).
Definition: Network.hpp:185