PaGMO  1.1.5
erdos_renyi.cpp
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #include <sstream>
26 #include <string>
27 
28 #include "../exceptions.h"
29 #include "../rng.h"
30 #include "base.h"
31 #include "erdos_renyi.h"
32 
33 namespace pagmo { namespace topology {
34 
36 
43 erdos_renyi::erdos_renyi(const double &prob):base(),m_prob(prob),m_drng(rng_generator::get<rng_double>())
44 {
45  if (prob < 0 || prob > 1) {
46  pagmo_throw(value_error,"probability must be in the [0,1] range");
47  }
48 }
49 
51 {
52  return base_ptr(new erdos_renyi(*this));
53 }
54 
56 {
57  for (std::pair<v_iterator,v_iterator> vertices = get_vertices(); vertices.first != vertices.second; ++vertices.first) {
58  // Connect n bidirectionally to the other nodes with probability m_prob. Also, avoid to connect n with itself.
59  if (*vertices.first != n && m_drng() < m_prob) {
60  add_edge(n,*vertices.first);
61  add_edge(*vertices.first,n);
62  }
63  }
64 }
65 
67 {
68  std::ostringstream oss;
69  oss << "\tprobability: " << m_prob << '\n';
70  return oss.str();
71 }
72 
73 std::string erdos_renyi::get_name() const
74 {
75  return "Erdos-Renyi";
76 }
77 
78 }} //namespaces
79 
80 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::topology::erdos_renyi)
Root PaGMO namespace.
Generic thread-safe generator of pseudo-random number generators.
Definition: rng.h:138
Erdős-Rényi graph topology.
Definition: erdos_renyi.h:54
base_ptr clone() const
Clone method.
Definition: erdos_renyi.cpp:50
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base topology.
Definition: topology/base.h:47
Base topology class.
Definition: topology/base.h:75
std::string get_name() const
Get name of the topology.
Definition: erdos_renyi.cpp:73
erdos_renyi(const double &prob=0.01)
Constructor from probability.
Definition: erdos_renyi.cpp:43
graph_type::vertices_size_type vertices_size_type
Vertices size type.
std::string human_readable_extra() const
Return extra information for human readable representation.
Definition: erdos_renyi.cpp:66
void add_edge(const vertices_size_type &, const vertices_size_type &)
Add an edge.
void connect(const vertices_size_type &)
Establish connections between islands during a push_back() operation.
Definition: erdos_renyi.cpp:55
This rng returns a double in the [0,1[ range.
Definition: rng.h:89
std::pair< v_iterator, v_iterator > get_vertices() const
Return iterator range to vertices.