graph_blockmodel_gibbs.hh 4.47 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-2020 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_GIBBS_HH
#define GRAPH_BLOCKMODEL_GIBBS_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
#include "graph_blockmodel_util.hh"
#include <boost/mpl/vector.hpp>

namespace graph_tool
{
using namespace boost;
using namespace std;

#define GIBBS_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))                                                       \
41
    ((oentropy_args,, python::object, 0))                                      \
42
    ((allow_new_group,, bool, 0))                                              \
43
    ((sequential,, bool, 0))                                                   \
44
    ((deterministic,, bool, 0))                                                \
45
46
47
48
    ((verbose,, bool, 0))                                                      \
    ((niter,, size_t, 0))


49
template <class State>
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
struct Gibbs
{
    GEN_STATE_BASE(GibbsBlockStateBase, GIBBS_BLOCK_STATE_params(State))

    template <class... Ts>
    class GibbsBlockState
        : public GibbsBlockStateBase<Ts...>
    {
    public:
        GET_PARAMS_USING(GibbsBlockStateBase<Ts...>,
                         GIBBS_BLOCK_STATE_params(State))
        GET_PARAMS_TYPEDEF(Ts, GIBBS_BLOCK_STATE_params(State))

        template <class... ATs,
                  typename std::enable_if_t<sizeof...(ATs) ==
                                            sizeof...(Ts)>* = nullptr>
        GibbsBlockState(ATs&&... as)
           : GibbsBlockStateBase<Ts...>(as...),
68
            _m_entries(num_vertices(_state._bg))
69
70
71
,
            _entropy_args(python::extract<typename State::_entropy_args_t&>(_oentropy_args))        {
            _state.init_mcmc(*this);
72
73
        }

74
        typename state_t::m_entries_t _m_entries;
75
76
77
        typename State::_entropy_args_t& _entropy_args;

        double _c = numeric_limits<double>::infinity();
78

79
80
81
82
83
84
        std::vector<size_t> _candidate_blocks;

        auto& get_moves(size_t)
        {
            return _state._candidate_blocks;
        }
85
86
87
88
89
90

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

91
        size_t node_weight(size_t v)
92
        {
93
            return _state.node_weight(v);
94
95
        }

96
97
        size_t _nr;
        double virtual_move_dS(size_t v, size_t nr, rng_t& rng)
98
        {
99
            size_t r = _state._b[v];
100
101
            if (!_allow_new_group && nr != r && _state.virtual_remove_size(v) == 0)
                return numeric_limits<double>::infinity();
102
103
            if (nr == null_group)
            {
104
                if (!_allow_new_group ||
105
106
                    _state._candidate_blocks.size() - 1 == num_vertices(_state._g) ||
                    _state.virtual_remove_size(v) == 0)
107
                    return numeric_limits<double>::infinity();
108
109
110
111
112
113
                _state.get_empty_block(v);
                _nr = nr = uniform_sample(_state._empty_blocks, rng);
                if (_state._coupled_state != nullptr)
                    _state._coupled_state->sample_branch(nr, r, rng);
                _state._bclabel[nr] = _state._bclabel[r];

114
            }
115
            return _state.virtual_move(v, r, nr, _entropy_args, _m_entries);
116
117
        }

118
        void perform_move(size_t v, size_t nr)
119
        {
120
            if (nr == null_group)
121
                nr = _nr;
122
123
124
125
126
127
128
129
130
            _state.move_vertex(v, nr);
        }
    };
};


} // graph_tool namespace

#endif //GRAPH_BLOCKMODEL_GIBBS_HH