RepastHPC  2.3.1
NetworkBuilder.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * NetworkBuilder.h
36  *
37  * Created on: Oct 30, 2009
38  * Author: nick
39  */
40 
41 #ifndef NETWORKBUILDER_H_
42 #define NETWORKBUILDER_H_
43 
44 #include "Graph.h"
45 #include "Properties.h"
46 #include "Utilities.h"
47 #include "Random.h"
48 
49 #include <string>
50 
51 namespace repast {
52 
57 class ProbItem {
58 private:
59  int _index;
60  double lowerBound, upperBound;
61 
62 public:
63  ProbItem(int i, double lb, double ub);
64  bool contains(double val);
65 
66  int index() const {
67  return _index;
68  }
69 };
70 
76 template<typename V, typename E, typename Ec, typename EcM>
77 class KEBuilder {
78 
79 private:
80  static const std::string M;
81 
82 public:
92 };
93 
94 template<typename V, typename E, typename Ec, typename EcM>
95 const std::string KEBuilder<V, E, Ec, EcM>::M = "ke.model.m";
96 
97 template<typename V, typename E, typename Ec, typename EcM>
99  int m = strToInt(props.getProperty(M));
101  int k = 0;
102  // advance iter m - 1 number of elements.
103  for (iter = graph->verticesBegin(); k < m; ++k, ++iter);
104  std::vector<V*> activeNodes(graph->verticesBegin(), iter);
105  // fully connect all the active nodes
106  for (int i = 0; i < m; i++) {
107  V* source = activeNodes[i];
108  for (int j = 0; j < m; j++) {
109  if (i != j) {
110  graph->addEdge(source, (activeNodes[j]));
111  }
112  }
113  }
114  // add the remaining nodes -- iter through verticesEnd()
115  while (iter != graph->verticesEnd()) {
116  V* source = *iter;
117  double sum = 0;
118  // make an edge between iter and all the active nodes
119  for (int i = 0, n = activeNodes.size(); i < n; i++) {
120  V* target = activeNodes[i];
121  graph->addEdge(source, target);
122  sum += 1.0 / graph->inDegree(target);
123  }
124 
125  std::vector<ProbItem> probItems;
126  double lowerBound = 0;
127  for (int i = 0, n = activeNodes.size(); i < n; i++) {
128  V* node = activeNodes[i];
129  double upperBound = lowerBound + (1.0 / graph->inDegree(node) / sum);
130  probItems.push_back(ProbItem(i, lowerBound, upperBound));
131  lowerBound = upperBound;
132  }
133 
134  double p = repast::Random::instance()->nextDouble();
135  for (int i = 0, n = probItems.size(); i < n; i++) {
136  ProbItem& item = probItems[i];
137  if (item.contains(p)) {
138  activeNodes.erase(activeNodes.begin() + item.index());
139  break;
140  }
141  }
142  activeNodes.push_back(source);
143  ++iter;
144  }
145 }
146 
147 }
148 
149 #endif /* NETWORKBUILDER_H_ */
repast::Random::nextDouble
double nextDouble()
Gets the current seed.
Definition: Random.cpp:96
repast::Graph::verticesEnd
vertex_iterator verticesEnd()
Gets the end of an iterator over all the vertices in this graph.
Definition: Graph.h:252
repast::Graph::vertex_iterator
boost::transform_iterator< NodeGetter< V, E >, typename VertexMap::const_iterator > vertex_iterator
An iterator over the agents that are the vertices in this Graph.
Definition: Graph.h:100
repast::Graph::verticesBegin
vertex_iterator verticesBegin()
Gets the start of an iterator over all the vertices in this graph.
Definition: Graph.h:242
repast::Random::instance
static Random * instance()
Gets the singleton instance of this Random.
Definition: Random.cpp:80
repast::KEBuilder::build
void build(repast::Properties &props, repast::Graph< V, E, Ec, EcM > *graph)
Builds the network.
Definition: NetworkBuilder.h:98
repast::ProbItem
Helper class for calculating outcomes based on a set of probabilities that sum to 1.
Definition: NetworkBuilder.h:57
repast::Graph::addEdge
virtual boost::shared_ptr< E > addEdge(V *source, V *target)
Adds an edge between source and target to this Graph.
Definition: Graph.h:350
repast::Graph::inDegree
virtual int inDegree(V *vertex)
Gets the in-degree of the specified vertex.
Definition: Graph.h:402
repast::Properties::getProperty
std::string getProperty(const std::string &key) const
Gets the property with the specified key.
Definition: Properties.cpp:90
repast::Properties
Map type object that contains key, value(string) properties.
Definition: Properties.h:77
repast::Graph
Graph / Network implementation where agents are vertices in the graph.
Definition: Graph.h:72
repast::KEBuilder
Buils KE type networks.
Definition: NetworkBuilder.h:77