graph_blockmodel_dynamics.hh 9.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2018 Tiago de Paula Peixoto <tiago@skewed.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_BLOCKMODEL_DYNAMICS_HH
#define GRAPH_BLOCKMODEL_DYNAMICS_HH

#include "config.h"

#include <vector>

#include "../support/graph_state.hh"
#include "../blockmodel/graph_blockmodel_util.hh"
#include "graph_blockmodel_uncertain_util.hh"

namespace graph_tool
{
using namespace boost;
using namespace std;

typedef typename eprop_map_t<double>::type xmap_t;


#define DYNAMICS_STATE_params                                                  \
    ((__class__,&, mpl::vector<python::object>, 1))                            \
    ((g, &, all_graph_views, 1))                                               \
    ((params,, python::dict, 0))                                               \
    ((ot,, python::list, 0))                                                   \
    ((os,, python::list, 0))                                                   \
    ((x,, xmap_t, 0))                                                          \
    ((aE,, double, 0))                                                         \
    ((E_prior,, bool, 0))                                                      \
    ((self_loops,, bool, 0))


template <class Type>
std::vector<typename Type::unchecked_t> from_list(python::object list)
{
    vector<typename Type::unchecked_t> v;
    for (int i = 0; i < python::len(list); ++i)
    {
        boost::any a = python::extract<boost::any>(list[i])();
        v.push_back(boost::any_cast<Type>(a).get_unchecked());
    }
    return v;
};

template <class BlockState, class DState>
struct Dynamics
{
    GEN_STATE_BASE(DynamicsStateBase, DYNAMICS_STATE_params)

    template <class... Ts>
    class DynamicsState
        : public DynamicsStateBase<Ts...>
    {
    public:
        GET_PARAMS_USING(DynamicsStateBase<Ts...>,
                         DYNAMICS_STATE_params)
        GET_PARAMS_TYPEDEF(Ts, DYNAMICS_STATE_params)

        typedef typename DState::tmap_t tmap_t;
        typedef typename DState::smap_t smap_t;

        template <class... ATs,
                  typename std::enable_if_t<sizeof...(ATs) == sizeof...(Ts)>* = nullptr>
        DynamicsState(BlockState& block_state, ATs&&... args)
            : DynamicsStateBase<Ts...>(std::forward<ATs>(args)...),
              _block_state(block_state),
              _t(from_list<tmap_t>(_ot)),
              _s(from_list<smap_t>(_os)),
              _dstate(*this, _params),
              _xc(_x.get_checked())
        {
            _u_edges.resize(num_vertices(_u));
            for (auto e : edges_range(_u))
            {
                get_u_edge<true>(source(e, _u), target(e, _u)) = e;
                _E += _eweight[e];
            }
            _block_state.enable_partition_stats();
        }

        DynamicsState(const DynamicsState& other)
            : DynamicsStateBase<Ts...>(static_cast<const DynamicsStateBase<Ts...>&>(other)),
              _block_state(other._block_state),
              _t(other._t),
              _s(other._s),
              _u_edges(other._u_edges),
              _pe(other._pe),
              _E(other._E),
              _dstate(*this, _params),
              _xc(_x.get_checked())
        {
            _block_state.enable_partition_stats();
        }

        typedef BlockState block_state_t;
        BlockState& _block_state;
        std::vector<typename tmap_t::unchecked_t> _t;
        std::vector<typename smap_t::unchecked_t> _s;
        typename BlockState::g_t& _u = _block_state._g;
        typename BlockState::eweight_t& _eweight = _block_state._eweight;
        GraphInterface::edge_t _null_edge;
        std::vector<double> _recs;

        std::vector<gt_hash_map<size_t, GraphInterface::edge_t>> _u_edges;

        double _pe = log(_aE);
        size_t _E = 0;

        DState _dstate;

        xmap_t _xc;

        template <bool insert, class Graph, class Elist>
        auto& _get_edge(size_t u, size_t v, Graph& g, Elist& edges)
        {
            if (!graph_tool::is_directed(g) && u > v)
                std::swap(u, v);
            auto& qe = edges[u];
            if (insert)
                return qe[v];
            auto iter = qe.find(v);
            if (iter != qe.end())
                return iter->second;
            return _null_edge;
        }

        template <bool insert=false>
        auto& get_u_edge(size_t u, size_t v)
        {
            return _get_edge<insert>(u, v, _u, _u_edges);
        }

        double get_node_prob(size_t u)
        {
            return _dstate.get_node_prob(u);
        }

        double entropy(bool latent_edges, bool density)
        {
            double S = 0;
            if (latent_edges)
            {
                for (auto v : vertices_range(_u))
                    S += _dstate.get_node_prob(v);
            }

            if (density && _E_prior)
                S += _E * _pe - lgamma_fast(_E + 1) - exp(_pe);

            return -S;
        }

        double remove_edge_dS(size_t u, size_t v, uentropy_args_t ea)
        {
            auto& e = get_u_edge(u, v);
            auto x = _xc[e];
            double dS = _block_state.template modify_edge_dS<false>(source(e, _u),
                                                                    target(e, _u),
                                                                    e, _recs, ea);
            _xc[e] = x;

            if (ea.density && _E_prior)
            {
                dS += _pe;
                dS += lgamma_fast(_E) - lgamma_fast(_E + 1);
            }

            if (ea.latent_edges)
            {
                if (_eweight[e] == 1 && (_self_loops || u != v))
                {
                    dS += _dstate.template get_edge_dS<false>(u, v, _xc[e]);
                    if (u != v && !graph_tool::is_directed(_u))
                        dS += _dstate.template get_edge_dS<false>(v, u, _xc[e]);
                }
            }
            return dS;
        }

        double add_edge_dS(size_t u, size_t v, double x, uentropy_args_t ea)
        {
            auto& e = get_u_edge(u, v);
            double dS = _block_state.template modify_edge_dS<true>(u, v, e,
                                                                   _recs, ea);
            if (ea.density && _E_prior)
            {
                dS -= _pe;
                dS += lgamma_fast(_E + 2) - lgamma_fast(_E + 1);
            }

            if (ea.latent_edges)
            {
                if ((e == _null_edge || _eweight[e] == 0) && (_self_loops || u != v))
                {
                    dS += _dstate.template get_edge_dS<true>(u, v, x);
                    if (u != v && !graph_tool::is_directed(_u))
                        dS += _dstate.template get_edge_dS<true>(v, u, x);
                }
            }
            return dS;
        }

        double update_edge_dS(size_t u, size_t v, double dx, uentropy_args_t ea)
        {
            double dS = 0;
            if (ea.latent_edges)
            {
                if (_self_loops || u != v)
                {
                    dS += _dstate.template get_edge_dS<true>(u, v, dx);
                    if (u != v && !graph_tool::is_directed(_u))
                        dS += _dstate.template get_edge_dS<true>(v, u, dx);
                }
            }
            return dS;
        }

        void remove_edge(size_t u, size_t v)
        {
            auto& e = get_u_edge(u, v);
            auto x = _xc[e];

            _block_state.template modify_edge<false>(u, v, e,
                                                     _recs);

            if ((e == _null_edge || _eweight[e] == 0) && (_self_loops || u != v))
            {
                _dstate.template update_edge<false>(u, v, x);
                if (u != v && !graph_tool::is_directed(_u))
                    _dstate.template update_edge<false>(v, u, x);
            }

            _E--;
        }

        void add_edge(size_t u, size_t v, double x)
        {
            auto& e = get_u_edge<true>(u, v);
            _block_state.template modify_edge<true>(u, v, e,
                                                    _recs);

            if (_eweight[e] == 1 && (_self_loops || u != v))
            {
                _xc[e] = x;
                _dstate.template update_edge<true>(u, v, x);
                if (u != v && !graph_tool::is_directed(_u))
                    _dstate.template update_edge<true>(v, u, x);
            }
            _E++;
        }

        void update_edge(size_t u, size_t v, double dx)
        {
            if (_self_loops || u != v)
            {
                auto& e = get_u_edge(u, v);
                _xc[e] += dx;
                _dstate.template update_edge<true>(u, v, dx);
                if (u != v && !graph_tool::is_directed(_u))
                    _dstate.template update_edge<true>(v, u, dx);
            }
        }

        void set_params(python::dict params)
        {
            _dstate.set_params(params);
        }
    };
};

} // graph_tool namespace

#endif //GRAPH_BLOCKMODEL_DYNAMICS_HH