graph_blockmodel_uncertain_mcmc.hh 5.5 KB
Newer Older
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2018 Tiago de Paula Peixoto <tiago@skewed.de>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//
// 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_UNCERTAIN_MCMC_HH
#define GRAPH_BLOCKMODEL_UNCERTAIN_MCMC_HH

#include "config.h"

#include <vector>

#include "graph_tool.hh"
#include "../support/graph_state.hh"
#include "graph_blockmodel_uncertain.hh"
28
#include "graph_blockmodel_sample_edge.hh"
29 30 31 32 33 34 35 36 37 38 39 40

namespace graph_tool
{
using namespace boost;
using namespace std;

typedef std::vector<size_t> vlist_t;

#define MCMC_UNCERTAIN_STATE_params(State)                                     \
    ((__class__,&, mpl::vector<python::object>, 1))                            \
    ((state, &, State&, 0))                                                    \
    ((beta,, double, 0))                                                       \
41
    ((entropy_args,, uentropy_args_t, 0))                                      \
42
    ((edges_only,, bool, 0))                                                   \
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    ((verbose,, bool, 0))                                                      \
    ((niter,, size_t, 0))


template <class State>
struct MCMC
{
    GEN_STATE_BASE(MCMCUncertainStateBase, MCMC_UNCERTAIN_STATE_params(State))

    template <class... Ts>
    class MCMCUncertainState
        : public MCMCUncertainStateBase<Ts...>
    {
    public:
        GET_PARAMS_USING(MCMCUncertainStateBase<Ts...>,
                         MCMC_UNCERTAIN_STATE_params(State))
        GET_PARAMS_TYPEDEF(Ts, MCMC_UNCERTAIN_STATE_params(State))

        template <class... ATs,
                  typename std::enable_if_t<sizeof...(ATs) ==
                                            sizeof...(Ts)>* = nullptr>
        MCMCUncertainState(ATs&&... as)
            : MCMCUncertainStateBase<Ts...>(as...),
66
              _edge_sampler(_state._block_state, _edges_only),
67
              _vlist(num_vertices(_state._u))
68 69 70
        {
        }

71 72
        SBMEdgeSampler<typename State::block_state_t> _edge_sampler;

73
        std::tuple<size_t, size_t> _e;
74 75 76
        std::vector<size_t> _vlist;
        int _null_move = 0;

77
        std::tuple<size_t, size_t> get_edge()
78
        {
79
            return _e;
80 81 82 83
        }

        size_t node_state(size_t u, size_t v)
        {
84 85
            auto&& e = _state.get_u_edge(u, v);
            if (e == _state._null_edge)
86
                return 0;
87
            return _state._eweight[e];
88 89
        }

90
        size_t node_state(size_t)
91 92
        {
            size_t u, v;
93
            std::tie(u, v) = get_edge();
94 95 96
            return node_state(u, v);
        }

97 98
        template <class T>
        bool skip_node(T&)
99 100 101 102
        {
            return false;
        }

103 104
        template <class T>
        size_t node_weight(T&)
105 106 107 108 109
        {
            return 1;
        }

        template <class RNG>
110
        int move_proposal(size_t, RNG& rng)
111
        {
112
            _e = _edge_sampler.sample(rng);
113 114

            std::bernoulli_distribution coin(.5);
115 116
            size_t m = node_state(get<0>(_e), get<1>(_e));
            if (m > 0 && coin(rng))
117
            {
118
                return -1;
119 120 121 122 123 124 125 126
            }
            else
            {
                return 1;
            }
        }

        std::tuple<double, double>
127
        virtual_move_dS(size_t, int dm)
128 129
        {
            size_t u, v;
130
            std::tie(u, v) = get_edge();
131 132 133 134 135 136 137

            double dS = 0;
            if (dm < 0)
                dS = _state.remove_edge_dS(u, v, _entropy_args);
            else
                dS = _state.add_edge_dS(u, v, _entropy_args);

138 139 140 141 142 143 144 145 146 147 148 149 150 151
            size_t m = node_state(u, v);
            double a = (_edge_sampler.log_prob(u, v, m + dm, dm) -
                        _edge_sampler.log_prob(u, v, m, 0));

            if (m > 0)
            {
                a -= -log(2);
            }
            if (m + dm > 0)
            {
                a += -log(2);
            }

            return std::make_tuple(dS, a);
152 153
        }

154
        void perform_move(size_t, int dm)
155 156 157 158
        {
            if (dm == 0)
                return;
            size_t u, v;
159 160
            std::tie(u, v) = get_edge();
            size_t m = node_state(u, v);
161
            if (dm < 0)
162 163
            {
                _edge_sampler.template update_edge<false>(u, v, m);
164
                _state.remove_edge(u, v);
165
            }
166
            else
167
            {
168
                _state.add_edge(u, v);
169 170
                _edge_sampler.template update_edge<true>(u, v, m);
            }
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
        }

        bool is_deterministic()
        {
            return false;
        }

        bool is_sequential()
        {
            return false;
        }

        auto& get_vlist()
        {
            return _vlist;
        }

        double get_beta()
        {
            return _beta;
        }

        size_t get_niter()
        {
            return _niter;
        }

198 199
        template <class T>
        void step(T&, int)
200 201 202 203 204 205 206 207 208
        {
        }
    };
};


} // graph_tool namespace

#endif //GRAPH_BLOCKMODEL_UNCERTAIN_MCMC_HH