graph_blockmodel_layers.hh 37.6 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-2022 Tiago de Paula Peixoto <tiago@skewed.de>
4
//
5
6
7
8
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
9
//
10
11
12
13
// 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 Lesser General Public License for more
// details.
14
//
15
// You should have received a copy of the GNU Lesser General Public License
16
17
18
19
20
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_BLOCKMODEL_LAYERS_HH
#define GRAPH_BLOCKMODEL_LAYERS_HH

21
22
#define GRAPH_BLOCKMODEL_LAYERS_ENABLE

23
24
25
26
#include "config.h"

#include <vector>

27
28
#include "../support/graph_state.hh"
#include "../blockmodel/graph_blockmodel_util.hh"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "graph_blockmodel_layers_util.hh"

namespace graph_tool
{
using namespace boost;
using namespace std;

typedef eprop_map_t<int32_t>::type emap_t;
typedef vprop_map_t<std::vector<int32_t>>::type vcvmap_t;

typedef gt_hash_map<size_t, size_t> bmap_t;
typedef std::vector<bmap_t> vbmap_t;

#define LAYERED_BLOCK_STATE_params                                             \
    ((__class__,&, mpl::vector<python::object>, 1))                            \
    ((layer_states,, python::object, 0))                                       \
    ((ec,, emap_t, 0))                                                         \
    ((vc,, vcvmap_t, 0))                                                       \
    ((vmap,, vcvmap_t, 0))                                                     \
    ((block_map, &, vbmap_t&, 0))                                              \
    ((master,, bool, 0))

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

class LayeredBlockStateVirtualBase
    : public BlockStateVirtualBase
{
public:
    virtual BlockStateVirtualBase& get_layer(size_t l) = 0;
    virtual size_t get_block(size_t l, size_t v) = 0;
    virtual void set_block(size_t l, size_t v, size_t r) = 0;
    virtual size_t get_vweight(size_t l, size_t v) = 0;
    virtual void add_layer_node(size_t l, size_t v, size_t u) = 0;
    virtual void remove_layer_node(size_t l, size_t v, size_t u) = 0;
    virtual size_t get_layer_node(size_t l, size_t v) = 0;
    virtual size_t get_block_map(size_t l, size_t r, bool put_new=true) = 0;
    virtual bool check_layers() = 0;
};

67
68
69
70
71
72
73
74
template <class BaseState>
struct Layers
{
    GEN_STATE_BASE(LayeredBlockStateBase, LAYERED_BLOCK_STATE_params)

    template <class... Ts>
    class LayeredBlockState
        : public LayeredBlockStateBase<Ts...>,
75
76
          public BaseState,
          public LayeredBlockStateVirtualBase
77
78
79
80
81
82
83
84
85
86
87
    {
    public:
        GET_PARAMS_USING(LayeredBlockStateBase<Ts...>,
                         LAYERED_BLOCK_STATE_params)
        GET_PARAMS_TYPEDEF(Ts, LAYERED_BLOCK_STATE_params)

        GET_PARAMS_USING(BaseState, BASE_STATE_params)
        using BaseState::_bg;
        using BaseState::_m_entries;
        using BaseState::_emat;
        using BaseState::_partition_stats;
88
        using BaseState::get_move_entries;
89
90
91
92
93
94
95

        typedef vprop_map_t<int32_t>::type block_rmap_t;

        class LayerState
            : public BaseState
        {
        public:
96
            LayerState(const BaseState& base_state, LayeredBlockState& lstate,
97
                       bmap_t& block_map, block_rmap_t block_rmap, size_t l)
98
                : BaseState(base_state),
99
                  _lstate(&lstate),
100
101
                  _block_map(block_map),
                  _block_rmap(block_rmap),
102
                  _l(l), _E(0)
103
104
105
106
            {
                for (auto e : edges_range(BaseState::_g))
                    _E += BaseState::_eweight[e];
            }
Tiago Peixoto's avatar
Tiago Peixoto committed
107
            virtual ~LayerState(){}
108

109
            LayeredBlockState* _lstate;
110
111
            bmap_t& _block_map;
            block_rmap_t _block_rmap;
112
            size_t _l;
113
114
            size_t _E;

115
116
            using BaseState::_bg;
            using BaseState::_wr;
117
            using BaseState::_empty_groups;
118
119
            using BaseState::add_block;

120
121
122
123
124
125
            size_t get_block_map(size_t r, bool put_new=true)
            {
                size_t r_u;
                auto iter = _block_map.find(r);
                if (iter == _block_map.end())
                {
126
                    r_u = null_group;
127
                    for (auto s : _empty_groups)
128
129
130
131
132
133
134
135
136
137
138
                    {
                        if (_block_rmap[s] != -1)
                            continue;
                        r_u = s;
                        break;
                    }
                    if (r_u == null_group)
                    {
                        r_u = add_block();
                        _block_rmap[r_u] = -1;
                    }
139
                    assert(r_u < num_vertices(_bg));
140

141
142
143
144
                    if (put_new)
                    {
                        _block_map[r] = r_u;
                        _block_rmap[r_u] = r;
145
146
147
                        if (_lstate->_lcoupled_state != nullptr)
                        {
                            _lstate->_lcoupled_state->add_layer_node(_l, r, r_u);
148
149
150
151
152
153
154
                            auto& hb = _lstate->_lcoupled_state->get_b();
                            auto& hb_u = BaseState::_coupled_state->get_b();
                            hb_u[r_u] = _lstate->_lcoupled_state->get_block_map(_l, hb[r]);
                            // assert(_lstate->_lcoupled_state->get_vweight(_l, r_u) == (_wr[r_u] > 0));
                            // if (_wr[r_u] == 0)
                            //     _lstate->_lcoupled_state->set_block(_l, r_u, hb[r_u]);
                            // assert(_lstate->_lcoupled_state->get_block(_l, r_u) == size_t(hb[r_u]));
155
156
157
                        }
                        assert(_lstate->_lcoupled_state == nullptr ||
                               r_u == _lstate->_lcoupled_state->get_layer_node(_l, r));
158
159
160
161
162
163
164
165
166
167
168
169
170
                        // assert(_lstate->_lcoupled_state == nullptr ||
                        //        size_t(_bclabel[r_u]) ==
                        //        _lstate->_lcoupled_state->
                        //        get_block_map(_l, _lstate->_bclabel[r], false));
                    }
                    else
                    {
                        if (_lstate->_lcoupled_state != nullptr)
                        {
                            auto& hb = _lstate->_lcoupled_state->get_b();
                            auto& hb_u = BaseState::_coupled_state->get_b();
                            hb_u[r_u] = _lstate->_lcoupled_state->get_block_map(_l, hb[r], false);
                        }
171
172
173
174
175
                    }
                }
                else
                {
                    r_u = iter->second;
176
177
178
                    assert(size_t(_block_rmap[r_u]) == r);
                    assert(_lstate->_lcoupled_state == nullptr ||
                           r_u == _lstate->_lcoupled_state->get_layer_node(_l, r));
179
                }
180
181
182
183
184
185
186
187

                if (_lstate->_lcoupled_state != nullptr)
                {
                    auto& hb = _lstate->_lcoupled_state->get_b();
                    auto& hb_u = BaseState::_coupled_state->get_b();
                    hb_u[r_u] = _lstate->_lcoupled_state->get_block_map(_l, hb[r], put_new);
                }

188
                assert(r_u < num_vertices(_bg));
189
190
191
192
193
194
195
196
197
198
199
200
201
                return r_u;
            }

            bool has_block_map(size_t r)
            {
                return _block_map.find(r) != _block_map.end();
            }
        };

        template <class... ATs,
                  typename std::enable_if_t<sizeof...(ATs) == sizeof...(Ts)>* = nullptr>
        LayeredBlockState(const BaseState& base_state, ATs&&... args)
            : LayeredBlockStateBase<Ts...>(std::forward<ATs>(args)...),
202
203
              BaseState(base_state), _vc_c(_vc.get_checked()),
              _vmap_c(_vmap.get_checked())
204
        {
205
            for (int l = 0; l < python::len(_layer_states); ++l)
206
            {
207
                auto ostate = _layer_states[l];
208
209
210
211
                BaseState& state = python::extract<BaseState&>(ostate.attr("_state"));
                boost::python::object temp = ostate.attr("block_rmap").attr("_get_any")();
                boost::any& a = python::extract<boost::any&>(temp);
                block_rmap_t block_rmap = boost::any_cast<block_rmap_t>(a);
212
                bmap_t& block_map = _block_map[l];
213
                _layers.emplace_back(state, *this, block_map, block_rmap, l);
214
215
            }
            for (auto r : vertices_range(BaseState::_bg))
216
            {
217
                if (BaseState::_wr[r] > 0)
218
                    _actual_B++;
219
220
221
222
223
224
            }
            _N = BaseState::get_N();
            // assert(check_layers());
            // assert(check_edge_counts());
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
225
        virtual ~LayeredBlockState() {}
226
227

        std::vector<LayerState> _layers;
228
229
        size_t _actual_B = 0;
        size_t _N = 0;
230
        typedef entropy_args_t _entropy_args_t;
231
232
233
        LayeredBlockStateVirtualBase* _lcoupled_state = nullptr;
        typename vc_t::checked_t _vc_c;
        typename vmap_t::checked_t _vmap_c;
234
235
236

        void move_vertex(size_t v, size_t s)
        {
237
            // assert(check_layers());
238
            // assert(check_edge_counts());
239

240
241
242
243
244
245
246
247
248
249
250
251
            if (BaseState::_vweight[v] == 0)
            {
                _b[v] = s;
                return;
            }

            size_t r = _b[v];

            if (r == s)
                return;

            if (_wr[s] == 0)
252
                _bclabel[s] = _bclabel[r];
253

254
255
            assert((_bclabel[r] == _bclabel[s]));

256
257
258
259
260
261
262
263
264
            auto& ls = _vc[v];
            auto& vs = _vmap[v];
            for (size_t j = 0; j < ls.size(); ++j)
            {
                int l = ls[j];
                size_t u = vs[j];

                auto& state = _layers[l];

265
266
                if (state._vweight[u] == 0)
                    continue;
267

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
                assert(state.has_block_map(r));
                assert(size_t(state._b[u]) == state.get_block_map(r, false));
                assert(_lcoupled_state == nullptr ||
                       _lcoupled_state->get_vweight(l, state._b[u]) > 0);
                assert(state._wr[state._b[u]] > 0);
                size_t s_u = state.get_block_map(s);

                assert(size_t(state._b[u]) != s_u);

                state.move_vertex(u, s_u);

                assert(state._wr[s_u] > 0);
                assert(s_u == state.get_block_map(s, false));
            }

            // bottom update needs to be last, due to _coupled_state, and the
            // fact that the upper levels are affected by get_block_map()

            if (_wr[s] == 0)
                _actual_B++;

289
            // BaseState::check_edge_counts();
290
            BaseState::move_vertex(v, s);
291
            // BaseState::check_edge_counts();
292
293
294
295
296
297
298

            if (_wr[r] == 0)
                _actual_B--;

            if (_lcoupled_state != nullptr)
            {
                for (size_t j = 0; j < ls.size(); ++j)
299
                {
300
301
302
303
304
305
306
307
308
309
310
311
312
313
                    int l = ls[j];
                    size_t u = vs[j];
                    auto& state = _layers[l];

                    if (state._vweight[u] == 0)
                        continue;

                    size_t r_u = state._b[u];
                    assert(r_u == state.get_block_map(s));
                    assert(state._wr[r_u] > 0);

                    _lcoupled_state->get_layer(l).set_vertex_weight(r_u, 1);

                    r_u = state.get_block_map(r);
314
                    if (state._wr[r_u] == 0)
315
316
                        _lcoupled_state->get_layer(l).set_vertex_weight(r_u, 0);
                    assert(state._wr[r_u] == 0 || BaseState::_wr[r] != 0);
317
318
                }
            }
319
320

            // assert(check_layers());
321
            // assert(check_edge_counts());
322
323
        }

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
        template <class Vec>
        void move_vertices(Vec& v, Vec& nr)
        {
            for (size_t i = 0; i < std::min(v.size(), nr.size()); ++i)
                move_vertex(v[i], nr[i]);
        }

        void move_vertices(python::object ovs, python::object ors)
        {
            multi_array_ref<uint64_t, 1> vs = get_array<uint64_t, 1>(ovs);
            multi_array_ref<uint64_t, 1> rs = get_array<uint64_t, 1>(ors);
            if (vs.size() != rs.size())
                throw ValueException("vertex and group lists do not have the same size");
            move_vertices(vs, rs);
        }

340
341
342
343
344
345
346
347
348
349
350
351
352
        void remove_vertex(size_t v)
        {
            size_t r = _b[v];
            auto& ls = _vc[v];
            auto& vs = _vmap[v];
            for (size_t j = 0; j < ls.size(); ++j)
            {
                int l = ls[j];
                size_t u = vs[j];
                auto& state = _layers[l];
                state.remove_vertex(u);
            }
            BaseState::remove_vertex(v);
353
354
            if (_wr[r] == 0)
                _actual_B--;
355
356
        }

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
        template <class Vec>
        void remove_vertices(Vec& vs)
        {
            gt_hash_map<size_t, vector<size_t>> lvs;
            gt_hash_set<size_t> rs;
            for (auto v : vs)
            {
                for (auto l : _vc[v])
                    lvs[l].push_back(v);
                rs.insert(_b[v]);
            }
            for (auto& lv : lvs)
            {
                auto l = lv.first;
                auto& state = _layers[l];
                vector<size_t> us;
                gt_hash_map<size_t, size_t> rus;
                for (auto v : lv.second)
                {
                    auto u = _vmap[v][l];
                    us.push_back(u);
                    size_t r = _b[v];
                    size_t r_u = state._b[u];
                    rus[r] = r_u;
                }
                state.remove_vertices(us);

                // for (auto rr_u : rus)
                // {
                //     if (state._wr[rr_u.second] == 0)
                //         state.remove_block_map(rr_u.first);
                // }
            }
            BaseState::remove_vertices(vs);
            for (auto r : rs)
            {
                if (_wr[r] == 0)
                    _actual_B--;
            }
        }

        void remove_vertices(python::object ovs)
        {
            multi_array_ref<uint64_t, 1> vs = get_array<uint64_t, 1>(ovs);
            remove_vertices(vs);
        }

404
405
406
407
408
409
410
411
412
413
414
415
        void add_vertex(size_t v, size_t r)
        {
            auto& ls = _vc[v];
            auto& vs = _vmap[v];
            for (size_t j = 0; j < ls.size(); ++j)
            {
                int l = ls[j];
                size_t u = vs[j];
                auto& state = _layers[l];
                size_t r_u = state.get_block_map(r);
                state.add_vertex(u, r_u);
            }
416
417
            if (_wr[r] == 0)
                _actual_B++;
418
419
420
            BaseState::add_vertex(v, r);
        }

421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
        template <class Vs, class Rs>
        void add_vertices(Vs& vs, Rs& rs)
        {
            if (vs.size() != rs.size())
                throw ValueException("vertex and group lists do not have the same size");

            gt_hash_map<size_t, vector<size_t>> lvs;
            gt_hash_map<size_t, size_t> vrs;
            for (size_t i = 0; i < vs.size(); ++i)
            {
                auto v = vs[i];
                vrs[v] = rs[i];
                for (auto l : _vc[v])
                    lvs[l].push_back(v);
            }

            for (auto& lv : lvs)
            {
                auto l = lv.first;
                auto& state = _layers[l];
                vector<size_t> us;
                vector<size_t> rus;
                for (auto v : lv.second)
                {
                    us.emplace_back(_vmap[v][l]);
                    rus.emplace_back(state.get_block_map(vrs[v]));
                }
                state.add_vertices(us, rus);
            }
            for (auto r : rs)
            {
                if (_wr[r] == 0)
                    _actual_B++;
            }
            BaseState::add_vertices(vs, rs);
        }

        void add_vertices(python::object ovs, python::object ors)
        {
            multi_array_ref<uint64_t, 1> vs = get_array<uint64_t, 1>(ovs);
            multi_array_ref<uint64_t, 1> rs = get_array<uint64_t, 1>(ors);
            add_vertices(vs, rs);
        }

465
466
467
468
469
470
471
472
473
474
475
476
477
478
        template <class VMap>
        void set_partition(VMap&& b)
        {
            for (auto v : vertices_range(_g))
                LayeredBlockState::move_vertex(v, b[v]);
        }

        void set_partition(boost::any& ab)
        {
            typename BaseState::b_t::checked_t& b
                = boost::any_cast<typename BaseState::b_t::checked_t&>(ab);
            set_partition(b.get_unchecked());
        }

479
        bool allow_move(size_t r, size_t nr)
480
        {
481
            return BaseState::allow_move(r, nr);
482
483
        }

484
        template <class MEntries>
485
486
        double virtual_move(size_t v, size_t r, size_t s,
                            const entropy_args_t& ea, MEntries& m_entries)
487
488
        {
            if (s == r)
489
490
            {
                m_entries.set_move(r, s, num_vertices(BaseState::_bg));
491
                return 0;
492
493
            }

494
495
496
            if (!allow_move(r, s))
                return std::numeric_limits<double>::infinity();

497
            // assert(check_layers());
498
499
500

            double dS = 0;

501
502
503
504
505
            entropy_args_t mea(ea);
            mea.edges_dl = false;
            mea.recs = false;

            if (!_master)
506
            {
507
508
                mea.adjacency = false;
                mea.degree_dl = false;
509
            }
510
511
512
513

            dS += BaseState::virtual_move(v, r, s, mea, m_entries);

            if (_master)
514
            {
515
516
                if (ea.adjacency)
                    dS -= virtual_move_covariate(v, r, s, *this, m_entries, false);
517
518

                if (ea.edges_dl)
519
                    dS += ea.beta_dl * get_delta_edges_dl(v, r, s);
520
521
            }

522
            // assert(check_layers());
523

524
            if (ea.adjacency || ea.recs || ea.edges_dl || _lcoupled_state != nullptr)
525
            {
526
527
                entropy_args_t lea(ea);
                lea.partition_dl = false;
528

529
530
531
532
533
534
535
                if (_master)
                {
                    lea.adjacency = false;
                    lea.degree_dl = false;
                    lea.edges_dl = false;
                }

536
537
538
539
540
541
                auto& ls = _vc[v];
                auto& vs = _vmap[v];
                for (size_t j = 0; j < ls.size(); ++j)
                {
                    size_t l = ls[j];
                    size_t u = vs[j];
542

543
                    auto& state = _layers[l];
544

545
546
547
                    if (state._vweight[u] == 0)
                        continue;

548
549
550
551
552
                    size_t s_u = (s != null_group) ?
                        state.get_block_map(s, false) : null_group;
                    size_t r_u = (r != null_group) ?
                        state._b[u] : null_group;

553
554
555
556
                    assert(r == null_group || state.has_block_map(r));
                    assert(r == null_group || r_u == state.get_block_map(r, false));

                    if (_master && ea.adjacency)
557
558
                        dS += virtual_move_covariate(u, r_u, s_u, state,
                                                     m_entries, true);
559

560
                    dS += state.virtual_move(u, r_u, s_u, lea, m_entries);
561
                }
562
            }
563
564
565

            // assert(check_layers());

566
567
568
            return dS;
        }

569
570
        double virtual_move(size_t v, size_t r, size_t s,
                            const entropy_args_t& ea)
571
        {
572
            return virtual_move(v, r, s, ea, _m_entries);
573
574
        }

575
        template <class MEntries>
576
        double get_move_prob(size_t v, size_t r, size_t s, double c, double d,
577
578
579
580
581
                             bool reverse, MEntries& m_entries)
        {
            // m_entries may include entries from different levels
            if (!reverse)
                BaseState::get_move_entries(v, r, s, m_entries);
582
            return BaseState::get_move_prob(v, r, s, c, d, reverse, m_entries);
583
584
        }

585
586
587
588
589
590
591
        double get_move_prob(size_t v, size_t r, size_t s, double c, double d,
                             bool reverse,
                             std::vector<std::tuple<size_t, size_t, int>>& p_entries)
        {
            return BaseState::get_move_prob(v, r, s, c, d, reverse, p_entries);
        }

592
        double get_move_prob(size_t v, size_t r, size_t s, double c, double d,
593
594
                             bool reverse)
        {
595
            return BaseState::get_move_prob(v, r, s, c, d, reverse);
596
597
        }

598
599
600
601
602
        size_t sample_block(size_t v, double c, double d, rng_t& rng)
        {
            return BaseState::sample_block(v, c, d, rng);
        }

603
604
605
606
607
        size_t sample_block_local(size_t v, rng_t& rng)
        {
            return BaseState::sample_block_local(v, rng);
        }

608
609
610
611
612
        void sample_branch(size_t v, size_t u, rng_t& rng)
        {
            BaseState::sample_branch(v, u, rng);
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
613
614
615
616
        void copy_branch(size_t, BlockStateVirtualBase&)
        {
        }

617
        double entropy(const entropy_args_t& ea, bool propagate=false)
618
        {
619
            double S = 0, S_dl = 0;
620
621
            if (_master)
            {
622
623
624
625
626
627
628
629
                entropy_args_t mea(ea);
                mea.edges_dl = false;
                mea.recs = false;
                mea.recs_dl = false;

                S += BaseState::entropy(mea);

                if (ea.adjacency)
630
                {
631
                    S -= covariate_entropy(_bg, _mrs);
632
                    if (ea.multigraph)
633
634
635
636
                        S -= BaseState::get_parallel_entropy();
                    for (auto& state : _layers)
                    {
                        S += covariate_entropy(state._bg, state._mrs);
637
                        if (ea.multigraph)
638
639
640
641
                            S += state.get_parallel_entropy();
                    }
                }

642
                if (ea.edges_dl)
643
644
                {
                    for (auto& state : _layers)
645
                        S_dl += get_edges_dl(_actual_B, state._E, _g);
646
647
648
649
650
651
652
                }

                if (ea.recs)
                {
                    entropy_args_t mea = {false, false, false, false, true,
                                          false, false, false,
                                          ea.degree_dl_kind, false, ea.recs_dl,
653
                                          ea.beta_dl, false};
654
655
                    for (auto& state : _layers)
                        S += state.entropy(mea);
656
657
658
659
                }
            }
            else
            {
660
661
662
                entropy_args_t mea(ea);
                mea.partition_dl = false;
                mea.edges_dl = false;
663

664
                for (auto& state : _layers)
665
                    S += state.entropy(mea);
666

667
668
                if (ea.partition_dl)
                    S_dl += BaseState::get_partition_dl();
669

670
671
672
673
674
                if (ea.edges_dl)
                {
                    for (auto& state : _layers)
                    {
                        size_t actual_B = 0;
675
676
677
                        for (auto r : vertices_range(state._bg))
                            if (state._wr[r] > 0)
                                actual_B++;
678
679
680
                        S_dl += get_edges_dl(actual_B, state._E, _g);
                    }
                }
681
                int L = _layers.size();
682
                S_dl += _N * (L * std::log(2) + std::log1p(-std::pow(2., -L)));
683
            }
684

685
686
687
            if (BaseState::_coupled_state != nullptr && propagate)
                S_dl += BaseState::_coupled_state->entropy(BaseState::_coupled_entropy_args, true);

688
            return S + S_dl * ea.beta_dl;
689
690
        }

691
692
        double get_delta_edges_dl(size_t v, size_t r, size_t s)
        {
693
            if (r == s)
694
695
696
697
698
699
700
701
702
703
704
705
706
                return 0;
            if (BaseState::_vweight[v] == 0)
                return 0;
            int dB = 0;
            if (r != null_group && BaseState::virtual_remove_size(v) == 0)
                --dB;
            if (s != null_group && _wr[s] == 0)
                ++dB;
            double S_a = 0, S_b = 0;
            if (dB != 0)
            {
                auto get_x = [](size_t B)
                    {
707
                        if constexpr (is_directed_::apply<typename BaseState::g_t>::type::value)
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
                            return B * B;
                        else
                            return (B * (B + 1)) / 2;
                    };

                for (auto& state : _layers)
                {
                    S_b += lbinom(get_x(_actual_B) + state._E - 1, state._E);
                    S_a += lbinom(get_x(_actual_B + dB) + state._E - 1, state._E);
                }
            }
            return S_a - S_b;
        }

        double get_deg_dl(int kind)
723
724
725
        {
            if (_master)
            {
726
                return BaseState::get_deg_dl(kind);
727
728
729
730
731
            }
            else
            {
                double S = 0;
                for (auto& state : _layers)
732
                    S += state.get_deg_dl(kind);
733
734
735
736
                return S;
            }
        }

737
        double edge_entropy_term(size_t, size_t, int, const entropy_args_t&) { return 0; }
738

739
740
        template <class MCMCState>
        void init_mcmc(MCMCState& state)
741
        {
742
743
744
745
746
747
            BaseState::init_mcmc(state);
            double c = state._c;
            state._c = numeric_limits<double>::infinity();
            for (auto& lstate : _layers)
                lstate.init_mcmc(state);
            state._c = c;
748
        }
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771

        LayerState& get_layer(size_t l)
        {
            assert(l < _layers.size());
            return _layers[l];
        }

        size_t get_block(size_t l, size_t v)
        {
            return _layers[l]._b[v];
        }

        void set_block(size_t l, size_t v, size_t r)
        {
            _layers[l]._b[v] = r;
        }

        size_t get_vweight(size_t l, size_t v)
        {
            return _layers[l]._vweight[v];
        }

        void couple_state(LayeredBlockStateVirtualBase& s,
772
                          const entropy_args_t& ea)
773
774
775
776
        {
            _lcoupled_state = &s;

            entropy_args_t lea(ea);
777
            //lea.edges_dl = false;
778
779
780
781
            lea.partition_dl = false;
            for (size_t l = 0; l < _layers.size(); ++l)
                _layers[l].couple_state(s.get_layer(l), lea);

782
            lea.partition_dl = ea.partition_dl;
783
784
            lea.adjacency = false;
            lea.recs = false;
785
            lea.recs_dl = false;
786
            lea.degree_dl = false;
787
            lea.edges_dl = false;
788
789
790
791
792
793
794
795
796
797
798
799
800
801

            BaseState::couple_state(s, lea);

            // assert(check_layers());
        }

        void decouple_state()
        {
            BaseState::decouple_state();
            _lcoupled_state = nullptr;
            for (auto& state : _layers)
                state.decouple_state();
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
802
803
804
805
806
        BlockStateVirtualBase* get_coupled_state()
        {
            return _lcoupled_state;
        }

807
        void couple_state(BlockStateVirtualBase& s,
808
                          const entropy_args_t& ea)
809
810
811
812
813
814
        {
            BaseState::couple_state(s, ea);
        }

        void add_partition_node(size_t v, size_t r)
        {
815
            if (_wr[r] == 0 && BaseState::_vweight[v] > 0)
816
817
818
819
820
821
822
                _actual_B++;
            BaseState::add_partition_node(v, r);
        }

        void remove_partition_node(size_t v, size_t r)
        {
            BaseState::remove_partition_node(v, r);
823
            if (_wr[r] == 0 && BaseState::_vweight[v] > 0)
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
                _actual_B--;
        }

        size_t get_layer_node(size_t l, size_t v)
        {
            auto& ls = _vc[v];
            auto& vs = _vmap[v];

            auto pos = std::lower_bound(ls.begin(), ls.end(), l);

            if (pos == ls.end() || size_t(*pos) != l)
                return null_group;

            return *(vs.begin() + (pos - ls.begin()));
        }

        void add_layer_node(size_t l, size_t v, size_t u)
        {
            auto& ls = _vc_c[v];
            auto& vs = _vmap_c[v];

            auto pos = std::lower_bound(ls.begin(), ls.end(), l);
            assert(pos == ls.end() || size_t(*pos) != l);

            vs.insert(vs.begin() + (pos - ls.begin()), u);
            ls.insert(pos, l);

            auto& state = _layers[l];
            state.set_vertex_weight(u, 0);
        }

        void remove_layer_node(size_t l, size_t v, size_t)
        {
            auto& ls = _vc[v];
            auto& vs = _vmap[v];

            auto pos = std::lower_bound(ls.begin(), ls.end(), l);

            assert(pos != ls.end());
            assert(size_t(*pos) == l);
            //assert(u == size_t(*(vs.begin() + (pos - ls.begin()))));

            vs.erase(vs.begin() + (pos - ls.begin()));
            ls.erase(pos);
        }

        size_t get_block_map(size_t l, size_t r, bool put_new=true)
        {
            return _layers[l].get_block_map(r, put_new);
        }

        void set_vertex_weight(size_t v, int w)
        {
            if (w == 0 && BaseState::_vweight[v] > 0)
                _N--;
            if (w == 1 && BaseState::_vweight[v] == 0)
                _N++;
            BaseState::set_vertex_weight(v, w);
        }

884
        size_t add_block(size_t n = 1)
885
        {
886
            return BaseState::add_block(n);
887
888
889
890
891
892
893
894
            // for (size_t l = 0; l < _layers.size(); ++l)
            // {
            //     auto& state = _layers[l];
            //     size_t r_u = state.add_block();
            //     if (_lcoupled_state != nullptr)
            //         _lcoupled_state->get_layer(l).coupled_resize_vertex(r_u);
            // }
            // return r;
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
        }

        void coupled_resize_vertex(size_t v)
        {
            BaseState::coupled_resize_vertex(v);
            auto& ls = _vc_c[v];
            auto& vs = _vmap_c[v];
            for (size_t j = 0; j < ls.size(); ++j)
            {
                int l = ls[j];
                size_t u = vs[j];
                auto& state = _layers[l];
                state.coupled_resize_vertex(u);
            }
        }

        void add_edge(const GraphInterface::edge_t& e)
        {
            BaseState::add_edge(e);
        }

        void remove_edge(const GraphInterface::edge_t& e)
        {
            BaseState::remove_edge(e);
        }

921
        void add_edge_rec(const GraphInterface::edge_t& e)
922
        {
923
924
925
926
927
928
929
930
931
932
933
934
            BaseState::add_edge_rec(e);
        }

        void remove_edge_rec(const GraphInterface::edge_t& e)
        {
            BaseState::remove_edge_rec(e);
        }

        void update_edge_rec(const GraphInterface::edge_t& e,
                             const std::vector<double>& delta)
        {
            BaseState::update_edge_rec(e, delta);
935
936
        }

937
        void add_edge(size_t, size_t, GraphInterface::edge_t&, int)
938
939
940
        {
        }

941
        void remove_edge(size_t, size_t, GraphInterface::edge_t&, int)
942
943
944
        {
        }

945
946
947
        double propagate_entries_dS(size_t u, size_t v, int du, int dv,
                                    std::vector<std::tuple<size_t, size_t, GraphInterface::edge_t, int,
                                                           std::vector<double>>>& entries,
948
                                    const entropy_args_t& ea, std::vector<double>& dBdx,
949
950
951
952
953
954
955
956
957
958
959
960
961
962
                                    int dL)
        {
            double dS = BaseState::propagate_entries_dS(u, v, du, dv, entries, ea, dBdx, dL);
            if (!_master && u != v)
            {
                int L = _layers.size();
                double SL = ea.beta_dl * (L * std::log(2) + std::log1p(-std::pow(2., -L)));
                dS += (du + dv) * SL;
            }
            return dS;
        }

        void propagate_delta(size_t u, size_t v,
                             std::vector<std::tuple<size_t, size_t,
963
                                                    GraphInterface::edge_t, int,
964
                                                    std::vector<double>>>& entries)
965
        {
966
967
968
969
            return BaseState::propagate_delta(u, v, entries);
        }

        double get_delta_partition_dl(size_t v, size_t r, size_t nr,
970
                                      const entropy_args_t& ea)
971
972
        {
            return BaseState::get_delta_partition_dl(v, r, nr, ea);
973
974
975
976
977
978
979
        }

        void clear_egroups()
        {
            BaseState::clear_egroups();
        }

980
981
982
983
984
985
        virtual void relax_update(bool relax)
        {
            BaseState::relax_update(relax);
        }


986
987
988
989
990
        vprop_map_t<int32_t>::type::unchecked_t& get_b()
        {
            return BaseState::_b;
        }

991
992
993
994
995
        vprop_map_t<int32_t>::type::unchecked_t& get_pclabel()
        {
            return BaseState::_pclabel;
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
996
997
998
999
1000
        vprop_map_t<int32_t>::type::unchecked_t& get_bclabel()
        {
            return BaseState::_bclabel;
        }

For faster browsing, not all history is shown. View entire blame