Commit 9c825c08 authored by Tiago Peixoto's avatar Tiago Peixoto

Fix absolute trust weighting and add reverse trust

parent b93b5010
......@@ -18,7 +18,6 @@
#include "graph_filtering.hh"
#include <boost/python.hpp>
#include <boost/lambda/bind.hpp>
#include "graph.hh"
#include "graph_selectors.hh"
......@@ -33,7 +32,8 @@ using namespace boost;
using namespace graph_tool;
void absolute_trust(GraphInterface& g, int64_t source, boost::any c,
boost::any t, double epslon, size_t max_iter, size_t seed)
boost::any t, double epslon, size_t max_iter, bool reversed,
size_t seed)
{
rng_t rng(static_cast<rng_t::result_type>(seed));
......@@ -45,8 +45,8 @@ void absolute_trust(GraphInterface& g, int64_t source, boost::any c,
run_action<>()
(g, bind<void>
(get_absolute_trust(),
_1, g.GetVertexIndex(), g.GetEdgeIndex(), source, _2,
_3, epslon, max_iter, ref(rng)),
_1, g.GetVertexIndex(), source, _2,
_3, epslon, max_iter, reversed, ref(rng)),
edge_floating_properties(),
vertex_floating_vector_properties())(c,t);
}
......
......@@ -33,11 +33,11 @@ using namespace boost;
struct get_absolute_trust
{
template <class Graph, class VertexIndex, class EdgeIndex, class TrustMap,
template <class Graph, class VertexIndex, class TrustMap,
class InferredTrustMap>
void operator()(Graph& g, VertexIndex vertex_index, EdgeIndex edge_index,
int64_t source, TrustMap c, InferredTrustMap t,
double epslon, size_t max_iter, rng_t& rng)
void operator()(Graph& g, VertexIndex vertex_index, int64_t source,
TrustMap c, InferredTrustMap t, double epslon,
size_t max_iter, bool reversed, rng_t& rng)
const
{
typedef typename property_traits<TrustMap>::value_type c_type;
......@@ -86,6 +86,7 @@ struct get_absolute_trust
typename graph_traits<Graph>::vertex_descriptor pos = v;
t_type pos_t = 1.0;
t_type pweight = 1.0;
v_mark[v][vertex_index[v]] = iter+1;
size_t path_hash = salt;
......@@ -133,24 +134,30 @@ struct get_absolute_trust
pos = target(e,g);
size_t posi = vertex_index[pos];
//update current path trust
pos_t *= c[e];
if (reversed && boost::source(e, g) != v)
pweight *= c[e];
// get path hash
hash_combine(path_hash, posi);
if (path_set.find(path_hash) == path_set.end())
{
// if new path, modify vertex trust score
path_set.insert(path_hash);
//update current path trust, and update new vertex
t_type pweight = pos_t;
pos_t *= c[e];
t_type old = 0;
if (t_count[v][posi] > 0)
old = t[v][posi]/t_count[v][posi];
t[v][posi] += pos_t*pweight;
t_count[v][posi] += pweight;
delta += abs(old - t[v][posi]/t_count[v][posi]);
}
if (!reversed)
pweight *= c[e];
v_mark[v][posi] = iter+1; // mark vertex
}
}
......@@ -161,8 +168,7 @@ struct get_absolute_trust
}
}
#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)
{
......
......@@ -380,7 +380,7 @@ def eigentrust(g, trust_map, vprop=None, norm=False, epslon=1e-6, max_iter=0,
return vprop
def absolute_trust(g, trust_map, source=None, vprop=None, epslon=0.001,
max_iter=None, seed=None, ret_iter=False):
max_iter=None, reversed=False, seed=None, ret_iter=False):
r"""
Samples the absolute trust centrality of each vertex in the graph, or only
for a given source, if one is provided.
......@@ -402,6 +402,10 @@ def absolute_trust(g, trust_map, source=None, vprop=None, epslon=0.001,
vertices are below this value.
max_iter : int, optional (default: None)
If supplied, this will limit the total number of iterations.
reversed : bool, optional (default: False)
Calculates the "reversed" trust instead: The direction of the edges are
inverted, but the path weighting is preserved in the original direction
(see Notes below).
seed : int, optional (default: None)
The initializing seed for the random number generator. If not supplied
a different random value will be chosen each time.
......@@ -499,10 +503,17 @@ def absolute_trust(g, trust_map, source=None, vprop=None, epslon=0.001,
if max_iter == None:
max_iter = 0
if reversed:
g.stash_filter(reversed=True)
g.set_reversed(True)
ic = libgraph_tool_centrality.\
get_absolute_trust(g._Graph__graph, source,
_prop("e", g, trust_map), _prop("v", g, vprop),
epslon, max_iter, seed)
epslon, max_iter, reversed, seed)
if reversed:
g.pop_filter(reversed=True)
if source != -1:
vprop_temp.get_array()[:] = numpy.array(vprop[g.vertex(source)])
vprop = vprop_temp
......
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