graph_blockmodel_layers.hh 36.7 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-2017 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
//
// 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_LAYERS_HH
#define GRAPH_BLOCKMODEL_LAYERS_HH

#include "config.h"

#include <vector>

25
26
#include "../support/graph_state.hh"
#include "../blockmodel/graph_blockmodel_util.hh"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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))

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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;
};

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

    template <class... Ts>
    class LayeredBlockState
        : public LayeredBlockStateBase<Ts...>,
73
74
          public BaseState,
          public LayeredBlockStateVirtualBase
75
76
77
78
79
80
81
82
83
84
85
86
    {
    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;
        using BaseState::is_partition_stats_enabled;
87
        using BaseState::get_move_entries;
88
89
90
91
92
93
94

        typedef vprop_map_t<int32_t>::type block_rmap_t;

        class LayerState
            : public BaseState
        {
        public:
95
96
97
            LayerState(const BaseState& base_state, LayeredBlockState& lstate,
                       bmap_t& block_map, block_rmap_t block_rmap,
                       vector<size_t>& free_blocks, size_t l)
98
                : BaseState(base_state),
99
                  _lstate(&lstate),
100
101
102
                  _block_map(block_map),
                  _block_rmap(block_rmap),
                  _free_blocks(free_blocks),
103
                  _l(l), _E(0)
104
105
106
107
108
            {
                for (auto e : edges_range(BaseState::_g))
                    _E += BaseState::_eweight[e];
            }

109
            LayeredBlockState* _lstate;
110
111
112
            bmap_t& _block_map;
            block_rmap_t _block_rmap;
            vector<size_t>& _free_blocks;
113
            size_t _l;
114
115
            size_t _E;

116
117
118
119
120
            using BaseState::_bg;
            using BaseState::_wr;
            using BaseState::_bclabel;
            using BaseState::add_block;

121
122
123
124
125
126
127
            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())
                {
                    if (_free_blocks.empty())
128
129
                        _free_blocks.push_back(_block_map.size());
                    r_u = _free_blocks.back();
130
                    while (r_u >= num_vertices(_bg))
131
                        add_block();
132
                    assert(r_u < num_vertices(_bg));
133

134
135
136
137
                    if (put_new)
                    {
                        _block_map[r] = r_u;
                        _block_rmap[r_u] = r;
138
139
140
                        if (_lstate->_lcoupled_state != nullptr)
                        {
                            _lstate->_lcoupled_state->add_layer_node(_l, r, r_u);
141
                            _bclabel[r_u] =
142
143
144
                                _lstate->_lcoupled_state->get_block_map(_l, _lstate->_bclabel[r]);
                            assert(_lstate->_lcoupled_state->get_vweight(_l, r_u) == (_wr[r_u] > 0));
                            if (_wr[r_u] == 0)
145
146
                                _lstate->_lcoupled_state->set_block(_l, r_u, _bclabel[r_u]);
                            assert(_lstate->_lcoupled_state->get_block(_l, r_u) == size_t(_bclabel[r_u]));
147
148
149
150
151
                        }
                        _free_blocks.pop_back();
                        assert(_lstate->_lcoupled_state == nullptr ||
                               r_u == _lstate->_lcoupled_state->get_layer_node(_l, r));
                        assert(_lstate->_lcoupled_state == nullptr ||
152
                               size_t(_bclabel[r_u]) ==
153
154
                               _lstate->_lcoupled_state->
                               get_block_map(_l, _lstate->_bclabel[r], false));
155
156
157
158
159
                    }
                }
                else
                {
                    r_u = iter->second;
160
161
162
                    assert(size_t(_block_rmap[r_u]) == r);
                    assert(_lstate->_lcoupled_state == nullptr ||
                           r_u == _lstate->_lcoupled_state->get_layer_node(_l, r));
163
                }
164
                assert(r_u < num_vertices(_bg));
165
166
167
168
169
170
171
                return r_u;
            }

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

173
174
175
176
177
178
        };

        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)...),
179
180
              BaseState(base_state), _vc_c(_vc.get_checked()),
              _vmap_c(_vmap.get_checked())
181
        {
182
            for (int l = 0; l < python::len(_layer_states); ++l)
183
            {
184
                auto ostate = _layer_states[l];
185
186
187
188
189
190
                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);
                std::vector<size_t>& free_blocks =
                    python::extract<std::vector<size_t>&>(ostate.attr("free_blocks"));
191
192
                bmap_t& block_map = _block_map[l];
                _layers.emplace_back(state, *this, block_map, block_rmap, free_blocks, l);
193
194
            }
            for (auto r : vertices_range(BaseState::_bg))
195
            {
196
                if (BaseState::_wr[r] > 0)
197
                    _actual_B++;
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
            }
            _N = BaseState::get_N();
            // assert(check_layers());
            // assert(check_edge_counts());
        }

        LayeredBlockState(const LayeredBlockState& other)
            : LayeredBlockStateBase<Ts...>(static_cast<const LayeredBlockStateBase<Ts...>&>(other)),
              BaseState(other),
              _layers(other._layers),
              _actual_B(other._actual_B),
              _N(other._N),
              _is_partition_stats_enabled(other._is_partition_stats_enabled),
              _lcoupled_state(other._lcoupled_state),
              _vc_c(_vc.get_checked()),
            _vmap_c(_vmap.get_checked())
        {
            for (auto& state : _layers)
                state._lstate = this;
217
218
219
        }

        std::vector<LayerState> _layers;
220
221
222
223
224
225
226
        size_t _actual_B = 0;
        size_t _N = 0;
        bool _is_partition_stats_enabled = false;
        LayeredBlockStateVirtualBase* _lcoupled_state = nullptr;
        typename vc_t::checked_t _vc_c;
        typename vmap_t::checked_t _vmap_c;
        openmp_mutex _llock;
227
228
229

        void move_vertex(size_t v, size_t s)
        {
230
231
            // assert(check_layers());

232
233
234
235
236
237
238
239
240
241
242
243
            if (BaseState::_vweight[v] == 0)
            {
                _b[v] = s;
                return;
            }

            size_t r = _b[v];

            if (r == s)
                return;

            if (_wr[s] == 0)
244
                _bclabel[s] = _bclabel[r];
245
246
247
248
249
250
251
252
253
254

            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];

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

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
                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++;

            BaseState::move_vertex(v, s);

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

            if (_lcoupled_state != nullptr)
            {
                for (size_t j = 0; j < ls.size(); ++j)
287
                {
288
289
290
291
292
293
294
295
296
297
298
299
300
301
                    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);
302
                    if (state._wr[r_u] == 0)
303
304
                        _lcoupled_state->get_layer(l).set_vertex_weight(r_u, 0);
                    assert(state._wr[r_u] == 0 || BaseState::_wr[r] != 0);
305
306
                }
            }
307
308

            // assert(check_layers());
309
310
        }

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
        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);
        }

327
328
329
330
331
332
333
334
335
336
337
338
339
        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);
340
341
            if (_wr[r] == 0)
                _actual_B--;
342
343
        }

344
345
346
347
        template <class Vec>
        void remove_vertices(Vec& vs)
        {
            gt_hash_map<size_t, vector<size_t>> lvs;
348
            gt_hash_set<size_t> rs;
349
            for (auto v : vs)
350
            {
351
352
                for (auto l : _vc[v])
                    lvs[l].push_back(v);
353
354
                rs.insert(_b[v]);
            }
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
            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);

371
372
373
374
375
                // for (auto rr_u : rus)
                // {
                //     if (state._wr[rr_u.second] == 0)
                //         state.remove_block_map(rr_u.first);
                // }
376
377
            }
            BaseState::remove_vertices(vs);
378
379
380
381
382
            for (auto r : rs)
            {
                if (_wr[r] == 0)
                    _actual_B--;
            }
383
384
385
386
387
388
389
390
        }

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

391
392
393
394
395
396
397
398
399
400
401
402
        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);
            }
403
404
            if (_wr[r] == 0)
                _actual_B++;
405
406
407
            BaseState::add_vertex(v, r);
        }

408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
        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);
            }
437
438
439
440
441
            for (auto r : rs)
            {
                if (_wr[r] == 0)
                    _actual_B++;
            }
442
443
444
445
446
447
448
449
450
451
            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);
        }

452
453
454
455
456
457
458
459
460
461
462
463
464
465
        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());
        }

466
        template <class MEntries>
467
        double virtual_move(size_t v, size_t r, size_t s, entropy_args_t ea,
468
469
470
471
                            MEntries& m_entries)
        {
            if (s == r)
                return 0;
472
            // assert(check_layers());
473
474
475

            double dS = 0;

476
477
478
479
480
            entropy_args_t mea(ea);
            mea.edges_dl = false;
            mea.recs = false;

            if (!_master)
481
            {
482
483
                mea.adjacency = false;
                mea.degree_dl = false;
484
            }
485
486
487
488

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

            if (_master)
489
            {
490
491
492
                dS -= virtual_move_covariate(v, r, s, *this, m_entries, false);

                if (ea.edges_dl)
493
                    dS += ea.beta_dl * get_delta_edges_dl(v, r, s);
494
495
            }

496
            // assert(check_layers());
497

498
            if (ea.adjacency || ea.recs || ea.edges_dl)
499
            {
500
501
                scoped_lock lck(_llock);

502
503
                entropy_args_t lea(ea);
                lea.partition_dl = false;
504

505
506
507
508
509
510
511
                if (_master)
                {
                    lea.adjacency = false;
                    lea.degree_dl = false;
                    lea.edges_dl = false;
                }

512
513
514
515
516
517
                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];
518

519
                    auto& state = _layers[l];
520

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

524
525
526
527
528
                    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;

529
530
531
532
                    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)
533
534
                        dS += virtual_move_covariate(u, r_u, s_u, state,
                                                     m_entries, true);
535
                    dS += state.virtual_move(u, r_u, s_u, lea, m_entries);
536
                }
537
            }
538
539
540
541
542
543
544

            if (BaseState::_coupled_state != nullptr)
            {
                bool r_vacate = (r != null_group) && (BaseState::virtual_remove_size(v) == 0);
                bool nr_occupy = (s != null_group) && (_wr[s] == 0);

                int L = _layers.size();
545
                dS -= ea.beta_dl * _actual_B * (L * std::log(2) + std::log1p(-std::pow(2., -L)));
546
547
548
549
550
                size_t B = _actual_B;
                if (r_vacate)
                    B--;
                if (nr_occupy)
                    B++;
551
                dS += ea.beta_dl * B * (L * std::log(2) + std::log1p(-std::pow(2., -L)));
552
553
554
            }
            // assert(check_layers());

555
556
557
            return dS;
        }

558
        double virtual_move(size_t v, size_t r, size_t s, entropy_args_t ea)
559
        {
560
            return virtual_move(v, r, s, ea, _m_entries);
561
562
        }

563
        template <class MEntries>
564
        double get_move_prob(size_t v, size_t r, size_t s, double c, double d,
565
566
567
568
569
                             bool reverse, MEntries& m_entries)
        {
            // m_entries may include entries from different levels
            if (!reverse)
                BaseState::get_move_entries(v, r, s, m_entries);
570
            return BaseState::get_move_prob(v, r, s, c, d, reverse, m_entries);
571
572
        }

573
        double get_move_prob(size_t v, size_t r, size_t s, double c, double d,
574
575
                             bool reverse)
        {
576
            return BaseState::get_move_prob(v, r, s, c, d, reverse);
577
578
        }

579
580
        void merge_vertices(size_t u, size_t v)
        {
581
582
583
584
585
586
            if (u == v)
                return;

            assert(BaseState::_vweight[v] > 0);
            assert(BaseState::_vweight[u] > 0);

587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
            std::set<size_t> ls;
            gt_hash_map<size_t, size_t> ls_u, ls_v;
            for (size_t i = 0; i < _vc[u].size(); ++i)
            {
                size_t l = _vc[u][i];
                ls_u[l] = _vmap[u][i];
                ls.insert(l);
            }

            for (size_t i = 0; i < _vc[v].size(); ++i)
            {
                size_t l = _vc[v][i];
                ls_v[l] = _vmap[v][i];
                ls.insert(l);
            }

            _vc[u].clear();
            _vmap[u].clear();
            _vc[v].clear();
            _vmap[v].clear();

            for (auto l : ls)
            {
                auto iter_u = ls_u.find(l);
                auto iter_v = ls_v.find(l);

                size_t uu = (iter_u != ls_u.end()) ? iter_u->second : iter_v->second;
                size_t vv = (iter_v != ls_v.end()) ? iter_v->second : iter_u->second;

616
617
618
619
620
621
622
623
624
625
626
                auto& state = _layers[l];
                assert(state._vweight[vv] > 0);
                assert(state._vweight[uu] > 0);

                state.merge_vertices(uu, vv);

                assert(state._b[uu] == state._b[vv]);
                assert(size_t(state._b[vv]) == state.get_block_map(_b[v], false));

                assert(state._vweight[uu] > 0 || total_degreeS()(uu, state._g, state._eweight) == 0);
                assert(state._vweight[vv] > 0 || total_degreeS()(vv, state._g, state._eweight) == 0);
627
628
629
630
631

                _vc[v].push_back(l);
                _vmap[v].push_back(vv);
            }

632
633
634
635
            BaseState::merge_vertices(u, v, _ec.get_checked());

            // assert(check_layers());
            // assert(check_edge_counts());
636
637
        }

638
        double entropy(entropy_args_t ea)
639
        {
640
            double S = 0, S_dl = 0;
641
642
            if (_master)
            {
643
644
645
646
647
648
649
650
                entropy_args_t mea(ea);
                mea.edges_dl = false;
                mea.recs = false;
                mea.recs_dl = false;

                S += BaseState::entropy(mea);

                if (ea.adjacency)
651
                {
652
                    S -= covariate_entropy(_bg, _mrs);
653
                    if (ea.multigraph)
654
655
656
657
                        S -= BaseState::get_parallel_entropy();
                    for (auto& state : _layers)
                    {
                        S += covariate_entropy(state._bg, state._mrs);
658
                        if (ea.multigraph)
659
660
661
662
                            S += state.get_parallel_entropy();
                    }
                }

663
                if (ea.edges_dl)
664
                {
665
666
667
                    size_t actual_B = _actual_B;
                    if (BaseState::_allow_empty)
                        actual_B = num_vertices(BaseState::_bg);
668
                    for (auto& state : _layers)
669
670
671
672
673
674
675
676
677
678
679
                        S_dl += get_edges_dl(actual_B, state._E, _g);
                }

                if (ea.recs)
                {
                    entropy_args_t mea = {false, false, false, false, true,
                                          false, false, false,
                                          ea.degree_dl_kind, false, ea.recs_dl,
                                          ea.beta_dl};
                    for (auto& state : _layers)
                        S += state.entropy(mea);
680
681
682
683
                }
            }
            else
            {
684
685
686
                entropy_args_t mea(ea);
                mea.partition_dl = false;
                mea.edges_dl = false;
687
                for (auto& state : _layers)
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
                    S += state.entropy(mea);
                if (ea.partition_dl)
                    S_dl += BaseState::get_partition_dl();
                if (ea.edges_dl)
                {
                    for (auto& state : _layers)
                    {
                        size_t actual_B = 0;
                        if (BaseState::_allow_empty)
                        {
                            actual_B = num_vertices(_bg);
                        }
                        else
                        {
                            for (auto r : vertices_range(state._bg))
                                if (state._wr[r] > 0)
                                    actual_B++;
                        }
                        S_dl += get_edges_dl(actual_B, state._E, _g);
                    }
                }
709
                int L = _layers.size();
710
                S_dl += _N * (L * std::log(2) + std::log1p(-std::pow(2., -L)));
711
            }
712
713

            return S + S_dl * ea.beta_dl;
714
715
        }

716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
        double get_delta_edges_dl(size_t v, size_t r, size_t s)
        {
            if (r == s || BaseState::_allow_empty)
                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)
                    {
                        if (is_directed::apply<typename BaseState::g_t>::type::value)
                            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)
748
749
750
        {
            if (_master)
            {
751
                return BaseState::get_deg_dl(kind);
752
753
754
755
756
            }
            else
            {
                double S = 0;
                for (auto& state : _layers)
757
                    S += state.get_deg_dl(kind);
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
                return S;
            }
        }

        void enable_partition_stats()
        {
            if (!_is_partition_stats_enabled)
            {
                BaseState::enable_partition_stats();
                for (auto& state : _layers)
                    state.enable_partition_stats();
                _is_partition_stats_enabled = true;
            }
        }

        void disable_partition_stats()
        {
            BaseState::disable_partition_stats();
            for (auto& state : _layers)
                state.disable_partition_stats();
            _is_partition_stats_enabled = false;
        }

        void init_mcmc(double c, double dl)
        {
            BaseState::init_mcmc(c, dl);
            for (auto& state : _layers)
                state.init_mcmc(numeric_limits<double>::infinity(), dl);
        }
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
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
884
885
886
887
888
889
890
891
892
893
894
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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971

        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,
                          entropy_args_t ea)
        {
            _lcoupled_state = &s;

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

            lea.partition_dl = true;
            lea.adjacency = false;
            lea.recs = false;
            lea.degree_dl = false;

            BaseState::couple_state(s, lea);

            // assert(check_layers());
        }

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

        void couple_state(BlockStateVirtualBase& s,
                          entropy_args_t ea)
        {
            BaseState::couple_state(s, ea);
        }

        void add_partition_node(size_t v, size_t r)
        {
            if (_wr[r] == 0)
                _actual_B++;
            BaseState::add_partition_node(v, r);
        }

        void remove_partition_node(size_t v, size_t r)
        {
            BaseState::remove_partition_node(v, r);
            if (_wr[r] == 0)
                _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);
        }

        size_t add_block()
        {
            auto r = BaseState::add_block();
            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;
        }

        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);
        }

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

        double recs_dS(size_t, size_t,
                       const std::vector<std::tuple<size_t, size_t,
                                                    GraphInterface::edge_t, int,
                                                    std::vector<double>>>&,
                       std::vector<double>&, int)
        {
            return 0;
        }

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

972
        void rebuild_neighbor_sampler()
973
        {
974
            BaseState::rebuild_neighbor_sampler();
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        }

        void sync_emat()
        {
            BaseState::sync_emat();
            for (auto& state : _layers)
                state.sync_emat();
        }

        void sync_bclabel()
        {
            if (_lcoupled_state == nullptr)
                return;
            for (size_t l = 0; l < _layers.size(); ++ l)
            {
                auto& state = _layers[l];
                for (auto r_u : vertices_range(state._bg))
                {
                    if (state._wr[r_u] == 0)
                        continue;
                    state._bclabel[r_u] = _lcoupled_state->get_block(l, r_u);
                    assert(size_t(state._bclabel[r_u]) ==
                           _lcoupled_state->
                           get_block_map(l, _bclabel[state._block_rmap[r_u]], false));
                    assert(r_u == _lcoupled_state->get_layer_node(l, state._block_rmap[r_u]));
                }
For faster browsing, not all history is shown. View entire blame