Commit 7505190f authored by Tiago Peixoto's avatar Tiago Peixoto

Switch from boost::random to tr1::random

The generators from boost::random seem to have a bug which causes them
to be biased. The generators from tr1::random seem to be in better
shape.

See: http://thread.gmane.org/gmane.comp.lib.boost.user/48006
parent b334e1cd
......@@ -22,8 +22,8 @@
#include "graph.hh"
#include "graph_selectors.hh"
#include <boost/random.hpp>
typedef boost::mt19937 rng_t;
#include <tr1/random>
typedef std::tr1::mt19937 rng_t;
#include "graph_absolute_trust.hh"
......
......@@ -23,9 +23,11 @@
#include "graph_util.hh"
#include <tr1/unordered_set>
#include <boost/random/uniform_int.hpp>
#include <tr1/random>
#include <boost/functional/hash.hpp>
#include <iostream>
namespace graph_tool
{
using namespace std;
......@@ -51,8 +53,7 @@ struct get_absolute_trust
// init inferred trust t
int i, N = num_vertices(g);
#pragma omp parallel for default(shared) private(i) \
schedule(dynamic)
#pragma omp parallel for default(shared) private(i) schedule(dynamic)
for (i = (source == -1) ? 0 : source;
i < ((source == -1) ? N : source + 1); ++i)
{
......@@ -71,7 +72,8 @@ struct get_absolute_trust
{
// walk hash set
tr1::unordered_set<size_t> path_set;
uniform_int<size_t> random_salt(0, numeric_limits<size_t>::max());
tr1::uniform_int<size_t>
random_salt(0, numeric_limits<size_t>::max());
size_t salt = random_salt(rng);
t_type delta = 2*epslon;
......@@ -121,12 +123,14 @@ struct get_absolute_trust
{
// select edge according to its probability
typename graph_traits<Graph>::edge_descriptor e;
uniform_real<t_type> random(0,out_prob.back());
typedef tr1::uniform_real<t_type> dist_t;
tr1::variate_generator<rng_t, dist_t>
random(rng, dist_t(0, out_prob.back()));
t_type u;
{
#pragma omp critical
u = random(rng);
u = random();
}
e = out_es[lower_bound(out_prob.begin(),
out_prob.end(), u) -
......
......@@ -22,17 +22,17 @@
#include <omp.h>
#endif
#include <boost/random.hpp>
#include <boost/functional/hash.hpp>
#include <boost/graph/copy.hpp>
#include <boost/graph/isomorphism.hpp>
#include <tr1/unordered_map>
#include <tr1/random>
#include <algorithm>
namespace graph_tool
{
typedef boost::mt19937 rng_t;
typedef tr1::mt19937 rng_t;
template <class Value>
void insert_sorted(vector<Value>& v, const Value& val)
......@@ -154,8 +154,9 @@ struct sample_some
template <class val_type>
void operator()(vector<val_type>& extend, size_t d)
{
uniform_01<rng_t> random(*_rng);
random_number_generator<rng_t> std_random(*_rng);
typedef tr1::uniform_real<double> rdist_t;
tr1::variate_generator<rng_t, rdist_t> random(*_rng, rdist_t());
double pd = (*_p)[d+1];
size_t nc = extend.size();
double u = nc*pd - floor(nc*pd);
......@@ -178,12 +179,15 @@ struct sample_some
return;
}
typedef tr1::uniform_int<size_t> idist_t;
for (size_t i = 0; i < n; ++i)
{
tr1::variate_generator<rng_t, idist_t>
random_v(*_rng, idist_t(0, extend.size()-i-1));
size_t j;
{
#pragma omp critical
j = i + std_random(extend.size()-i);
j = i + random_v();
}
swap(extend[i], extend[j]);
}
......@@ -300,18 +304,22 @@ struct get_all_motifs
for (tie(v, v_end) = vertices(g); v != v_end; ++v)
V.push_back(*v);
uniform_01<rng_t> random(rng);
random_number_generator<rng_t> std_random(rng);
size_t n;
typedef tr1::uniform_real<double> rdist_t;
tr1::variate_generator<rng_t, rdist_t> random(rng, rdist_t());
size_t n;
if (random() < p)
n = ceil(V.size()*p);
else
n = floor(V.size()*p);
typedef tr1::uniform_int<size_t> idist_t;
for (size_t i = 0; i < n; ++i)
{
size_t j = i + std_random(V.size()-i);
tr1::variate_generator<rng_t, idist_t>
random_v(rng, idist_t(0, V.size()-i-1));
size_t j = i + random_v();
swap(V[i], V[j]);
}
V.resize(n);
......
......@@ -18,8 +18,9 @@
#ifndef GRAPH_COMMUNITY_HH
#define GRAPH_COMMUNITY_HH
#include <boost/random.hpp>
#include <tr1/random>
#include <tr1/unordered_set>
#include <iostream>
#include <fstream>
#include <iomanip>
......@@ -35,7 +36,7 @@ using namespace boost;
using std::tr1::unordered_map;
using std::tr1::unordered_set;
typedef boost::mt19937 rng_t;
typedef tr1::mt19937 rng_t;
// computes the community structure through a spin glass system with
// simulated annealing
......@@ -71,7 +72,6 @@ struct get_communities
double Tmax = Tinterval.second;
rng_t rng(static_cast<rng_t::result_type>(seed));
boost::uniform_real<double> uniform_p(0.0,1.0);
if (Nspins.first == 0)
Nspins.first = HardNumVertices()(g);
......@@ -81,8 +81,8 @@ struct get_communities
unordered_map<size_t, map<double, unordered_set<size_t> > > global_term;
// init spins from [0,N-1] and global info
uniform_int<size_t> sample_spin(0, Nspins.first-1);
unordered_set<size_t> deg_set;
tr1::uniform_int<size_t> sample_spin(0, Nspins.first-1);
tr1::unordered_set<size_t> deg_set;
typename graph_traits<Graph>::vertex_iterator v,v_end;
for (tie(v,v_end) = vertices(g); v != v_end; ++v)
{
......@@ -261,9 +261,13 @@ struct get_communities
else
{
// sample energy according to its probability
uniform_real<long double> prob_sample
(0.0, max(cumm_prob_k.rbegin()->first,
numeric_limits<long double>::epsilon()));
typedef tr1::uniform_real<double> rdist_t;
tr1::variate_generator<rng_t, rdist_t>
prob_sample(rng,
rdist_t(0.0,
max(cumm_prob_k.rbegin()->first,
numeric_limits<long double>
::epsilon())));
bool accept = false;
while (!accept)
{
......@@ -271,7 +275,7 @@ struct get_communities
#pragma omp critical
{
upper = cumm_prob_k.upper_bound(prob_sample(rng));
upper = cumm_prob_k.upper_bound(prob_sample());
}
if (upper == cumm_prob_k.end())
......@@ -282,7 +286,8 @@ struct get_communities
}
//new spin (randomly chosen amongst those with equal energy)
uniform_int<size_t> sample_spin(0,global_term_k[E].size()-1);
tr1::uniform_int<size_t>
sample_spin(0, global_term_k[E].size()-1);
typeof(global_term_k[E].begin()) iter =
global_term_k[E].begin();
......
......@@ -26,7 +26,7 @@ using namespace boost;
using namespace boost::lambda;
using namespace graph_tool;
typedef boost::mt19937 rng_t;
typedef tr1::mt19937 rng_t;
class PythonFuncWrap
{
......
......@@ -23,15 +23,17 @@
#include <boost/array.hpp>
#define BOOST_DISABLE_ASSERTS
#include <boost/multi_array.hpp>
#include <boost/random.hpp>
#include <boost/functional/hash.hpp>
#include <tr1/random>
#include <iostream>
namespace graph_tool
{
using namespace std;
using namespace boost;
typedef boost::mt19937 rng_t;
typedef tr1::mt19937 rng_t;
// Graph Generation
// ================
......@@ -104,7 +106,7 @@ public:
ValueType operator()(rng_t& rng, bool remove = true)
{
uniform_int<> sample(0, _candidates.size() - 1);
tr1::uniform_int<> sample(0, _candidates.size() - 1);
int i = sample(rng);
if (remove)
{
......@@ -211,7 +213,7 @@ public:
}
// <j> and <k> must be the same. Re-sample random pairs until this holds
uniform_int<size_t> vertex_sample(0, _N-1);
tr1::uniform_int<size_t> vertex_sample(0, _N-1);
size_t count = 0;
while (sum_j != sum_k)
{
......@@ -299,7 +301,7 @@ public:
// sum_k must be an even number (2*num_edges). Re-sample degrees until
// this holds
uniform_int<size_t> vertex_sample(0, _N-1);
tr1::uniform_int<size_t> vertex_sample(0, _N-1);
size_t count = 0;
while (sum_k % 2 != 0)
{
......@@ -474,7 +476,7 @@ struct gen_random_graph
// shuffle sources
for (size_t i = 0; i < sources.size(); ++i)
{
uniform_int<size_t> source_sample(i, sources.size()-1);
tr1::uniform_int<size_t> source_sample(i, sources.size()-1);
swap(sources[i], sources[source_sample(rng)]);
}
......@@ -534,7 +536,7 @@ struct gen_random_graph
}
size_t s_index;
uniform_int<size_t> source_sample(0, j);
tr1::uniform_int<size_t> source_sample(0, j);
// keep trying: we don't want the same source again, and no
// sources with zero out-degree (which can happen when the graph
// is undirected, when they're orphaned after a removed edge)
......
......@@ -20,8 +20,8 @@
#include "graph.hh"
#include "graph_filtering.hh"
#include <boost/random.hpp>
typedef boost::mt19937 rng_t;
#include <tr1/random>
typedef std::tr1::mt19937 rng_t;
#include "graph_rewiring.hh"
......
......@@ -19,7 +19,8 @@
#define GRAPH_REWIRING_HH
#include <tr1/unordered_set>
#include <boost/random.hpp>
#include <tr1/random>
#include <boost/random/random_number_generator.hpp> // just the decorator
#include <boost/functional/hash.hpp>
#include <boost/vector_property_map.hpp>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment