26 #include <boost/integer_traits.hpp>
27 #include <boost/random/uniform_int.hpp>
28 #include <boost/numeric/conversion/cast.hpp>
34 #include "../exceptions.h"
37 #include "watts_strogatz.h"
39 namespace pagmo {
namespace topology {
53 if (m_k < 2 || m_k % 2) {
54 pagmo_throw(value_error,
"the K parameter must be even and at least 2");
58 if (m_k == boost::integer_traits<std::size_t>::const_max) {
59 pagmo_throw(std::overflow_error,
"overflow error in Watts-Strogatz model");
61 if (m_beta < 0 || m_beta > 1) {
62 pagmo_throw(value_error,
"the beta parameter must be in the [0,1] range");
80 void watts_strogatz::rewire()
83 pagmo_assert(size > 0);
85 for (std::pair<v_iterator,v_iterator> vertices =
get_vertices(); vertices.first != vertices.second; ++vertices.first) {
88 for (std::size_t j = 1; j <= m_k / 2; ++j) {
91 if (tmp == vertices.second) {
98 for (std::size_t j = 1; j <= m_k / 2; ++j) {
101 tmp = vertices.second;
108 boost::uniform_int<vertices_size_type> uint(0,size - 1);
109 for (std::pair<v_iterator,v_iterator> vertices =
get_vertices(); vertices.first != vertices.second; ++vertices.first) {
118 for (std::size_t j = 1; tmp != vertices.second && j <= m_k / 2 && n_adj_vertices < size - 1; ++j, ++tmp) {
119 if (m_drng() < m_beta) {
125 }
while (rng == *vertices.first ||
are_adjacent(*vertices.first,rng));
140 if (size <= m_k + 1) {
142 for (std::pair<v_iterator,v_iterator> vertices =
get_vertices(); vertices.first != vertices.second; ++vertices.first) {
144 if (n != *vertices.first) {
150 pagmo_assert(size > 0);
159 return "Watts-Strogatz";
void add_vertex()
Add a vertex.
Generic thread-safe generator of pseudo-random number generators.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base topology.
This rng returns an unsigned integer in the [0,2**32-1] range.
graph_type::edges_size_type edges_size_type
Edges size type.
void remove_edge(const vertices_size_type &, const vertices_size_type &)
Remove an edge.
boost::graph_traits< graph_type >::vertex_iterator v_iterator
Iterator over the vertices.
base_ptr clone() const
Clone method.
vertices_size_type get_number_of_vertices() const
Get number of vertices.
watts_strogatz(int=10, const double &=0.05, int=0)
Constructor from K, beta and size.
graph_type::vertices_size_type vertices_size_type
Vertices size type.
edges_size_type get_num_adjacent_vertices(const vertices_size_type &) const
Return the number of adjacent vertices.
std::string get_name() const
Get name of the topology.
Watts-Strogatz network model.
void connect(const vertices_size_type &)
Establish connections between islands during a push_back() operation.
void remove_all_edges()
Remove all edges.
void add_edge(const vertices_size_type &, const vertices_size_type &)
Add an edge.
This rng returns a double in the [0,1[ range.
std::pair< v_iterator, v_iterator > get_vertices() const
Return iterator range to vertices.
bool are_adjacent(const vertices_size_type &, const vertices_size_type &) const
Return true if two vertices are adjacent.