graph_blockmodel_mcmc.hh 5.29 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
//
// 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_MCMC_HH
#define GRAPH_BLOCKMODEL_MCMC_HH

#include "config.h"

#include <vector>

#include "graph_tool.hh"
26
#include "../support/graph_state.hh"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "graph_blockmodel_util.hh"
#include <boost/mpl/vector.hpp>

namespace graph_tool
{
using namespace boost;
using namespace std;

#define MCMC_BLOCK_STATE_params(State)                                         \
    ((__class__,&, mpl::vector<python::object>, 1))                            \
    ((state, &, State&, 0))                                                    \
    ((E,, size_t, 0))                                                          \
    ((vlist,&, std::vector<size_t>&, 0))                                       \
    ((beta,, double, 0))                                                       \
    ((c,, double, 0))                                                          \
42
    ((d,, double, 0))                                                          \
43
44
    ((entropy_args,, entropy_args_t, 0))                                       \
    ((allow_vacate,, bool, 0))                                                 \
45
46
    ((parallel,, bool, 0))                                                     \
    ((sequential,, bool, 0))                                                   \
47
    ((deterministic,, bool, 0))                                                \
48
49
50
51
    ((verbose,, bool, 0))                                                      \
    ((niter,, size_t, 0))


52
template <class State>
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
struct MCMC
{
    GEN_STATE_BASE(MCMCBlockStateBase, MCMC_BLOCK_STATE_params(State))

    template <class... Ts>
    class MCMCBlockState
        : public MCMCBlockStateBase<Ts...>
    {
    public:
        GET_PARAMS_USING(MCMCBlockStateBase<Ts...>,
                         MCMC_BLOCK_STATE_params(State))
        GET_PARAMS_TYPEDEF(Ts, MCMC_BLOCK_STATE_params(State))

        template <class... ATs,
                  typename std::enable_if_t<sizeof...(ATs) ==
                                            sizeof...(Ts)>* = nullptr>
        MCMCBlockState(ATs&&... as)
           : MCMCBlockStateBase<Ts...>(as...),
            _g(_state._g),
            _m_entries(num_vertices(_state._bg))
        {
74
75
76
77
            _state.init_mcmc(_c,
                             (_entropy_args.partition_dl ||
                              _entropy_args.degree_dl ||
                              _entropy_args.edges_dl));
78
79
80
        }

        typename state_t::g_t& _g;
81
        typename state_t::m_entries_t _m_entries;
82
        size_t _null_move = null_group;
83
84
85
86
87
88

        size_t node_state(size_t v)
        {
            return _state._b[v];
        }

89
90
91
92
93
        bool skip_node(size_t v)
        {
            return _state.node_weight(v) == 0;
        }

94
95
96
97
98
        size_t node_weight(size_t v)
        {
            return _state.node_weight(v);
        }

99
100
101
102
103
        template <class RNG>
        size_t move_proposal(size_t v, RNG& rng)
        {
            auto r = _state._b[v];

104
            if (!_allow_vacate && _state.is_last(v))
105
                return null_group;
106

107
            size_t s = _state.sample_block(v, _c, _d, rng);
108

109
            if (!_state.allow_move(v, r, s))
110
                return null_group;
111
112
113
114

            return s;
        }

115
        std::tuple<double, double>
Tiago Peixoto's avatar
Tiago Peixoto committed
116
        virtual_move_dS(size_t v, size_t nr)
117
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
118
            size_t r = _state._b[v];
119
            if (r == nr)
120
                return std::make_tuple(0., 0.);
121

Tiago Peixoto's avatar
Tiago Peixoto committed
122
            double dS = _state.virtual_move(v, r, nr, _entropy_args,
123
                                            _m_entries);
124
            double a = 0;
125
            if (!std::isinf(_beta))
126
            {
127
                double pf = _state.get_move_prob(v, r, nr, _c, _d, false,
128
                                                 _m_entries);
129
                double pb = _state.get_move_prob(v, nr, r, _c, _d, true,
130
131
132
                                                 _m_entries);
                a = log(pb) - log(pf);
            }
133
            return std::make_tuple(dS, a);
134
135
136
137
138
139
        }

        void perform_move(size_t v, size_t nr)
        {
            _state.move_vertex(v, nr);
        }
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

        bool is_deterministic()
        {
            return _deterministic;
        }

        bool is_sequential()
        {
            return _sequential;
        }

        auto& get_vlist()
        {
            return _vlist;
        }

        double get_beta()
        {
            return _beta;
        }

        size_t get_niter()
        {
            return _niter;
        }

        void step(size_t, size_t)
        {
        }
169
170
171
172
173
174
175
    };
};


} // graph_tool namespace

#endif //GRAPH_BLOCKMODEL_MCMC_HH