graph_blockmodel_dynamics_pseudo_ising_mcmc_h.hh 4.11 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
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_DYNAMICS_PSEUDO_ISING_MCMC_H_HH
#define GRAPH_BLOCKMODEL_DYNAMICS_PSEUDO_ISING_MCMC_H_HH

#include "config.h"

#include <vector>

#include "graph_tool.hh"
#include "../support/graph_state.hh"
#include "graph_blockmodel_dynamics.hh"
28
#include "graph_python_interface.hh"
29
30
31
32
33
34
35
36
37
38
39
40
41
42

namespace graph_tool
{
using namespace boost;
using namespace std;

typedef std::vector<size_t> vlist_t;

#define MCMC_PSEUDO_ISING_STATE_params(State)                                  \
    ((__class__,&, mpl::vector<python::object>, 1))                            \
    ((state, &, State&, 0))                                                    \
    ((beta,, double, 0))                                                       \
    ((n,, size_t, 0))                                                          \
    ((hstep,, double, 0))                                                      \
43
    ((verbose,, int, 0))                                                       \
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
    ((niter,, size_t, 0))


template <class State>
struct MCMC
{
    GEN_STATE_BASE(MCMCPseudoIsingStateBase, MCMC_PSEUDO_ISING_STATE_params(State))

    template <class... Ts>
    class MCMCPseudoIsingState
        : public MCMCPseudoIsingStateBase<Ts...>
    {
    public:
        GET_PARAMS_USING(MCMCPseudoIsingStateBase<Ts...>,
                         MCMC_PSEUDO_ISING_STATE_params(State))
        GET_PARAMS_TYPEDEF(Ts, MCMC_PSEUDO_ISING_STATE_params(State))

        template <class... ATs,
                  typename std::enable_if_t<sizeof...(ATs) ==
                                            sizeof...(Ts)>* = nullptr>
        MCMCPseudoIsingState(ATs&&... as)
            : MCMCPseudoIsingStateBase<Ts...>(as...)
        {
            for (auto v : vertices_range(_state._u))
                _vlist.push_back(v);
        }

        std::vector<size_t> _vlist;
        double _null_move = std::numeric_limits<double>::quiet_NaN();

        double node_state(size_t v)
        {
            return _state._dstate._h[_n][v];
        }

        template <class T>
        bool skip_node(T&)
        {
            return false;
        }

        template <class RNG>
        double move_proposal(size_t v, RNG& rng)
        {
            double h = _state._dstate._h[_n][v];
            std::uniform_real_distribution<> step(h - _hstep, h + _hstep);
            return step(rng);
        }

        std::tuple<double, double>
        virtual_move_dS(size_t v, double h)
        {
            auto& dstate = _state._dstate;

            double old_h = dstate._h[_n][v];

            dstate._h[_n][v] = h;
            double Sa = -dstate.get_node_prob(v);

            dstate._h[_n][v] = old_h;
            double Sb = -dstate.get_node_prob(v);

            return {Sa - Sb, .0};
        }

        void perform_move(size_t v, double h)
        {
            _state._dstate._h[_n][v] = h;
        }

        bool is_deterministic()
        {
            return true;
        }

        bool is_sequential()
        {
            return true;
        }

        auto& get_vlist()
        {
            return _vlist;
        }

        double get_beta()
        {
            return _beta;
        }

        size_t get_niter()
        {
            return _niter;
        }

        template <class T>
        void step(T&, int)
        {
        }
    };
};


} // graph_tool namespace

#endif //GRAPH_BLOCKMODEL_DYNAMICS_PSEUDO_ISING_MCMC_H_HH