graphnode.h 136 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#ifndef _SPEL_BAYES_GRAPH_NODE_H_
#define _SPEL_BAYES_GRAPH_NODE_H_

#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>
#include <deque>
#include <list>
#include <map>
11
#include <cstdio>
12

13
14
15
/*#include "factor.h"*/
#include "../pedigree.h"

16
#include "../error.h"
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "generalized_product.h"

template <typename V>
std::ostream&
operator << (std::ostream& os, const std::vector<std::vector<V>>& vv)
{
    auto i = vv.begin(), j = vv.end();
    if (i != j) {
        os << '{' << (*i) << '}';
        for (++i; i != j; ++i) {
            os << " {" << (*i) << '}';
        }
    }
    return os;
}
33
34
35
36
37
38
39
40
41
42

typedef int variable_index_type;
typedef size_t node_index_type;
typedef std::vector<node_index_type> node_vec;
typedef std::vector<variable_index_type> var_vec;
struct graph_type;
struct edge_type {
    const graph_type* graph;
    node_index_type first, second;

43
    edge_type() : graph(NULL), first(0), second(0) {}
44
45
46
47
48
49
50
51
52
53
54
    edge_type(const graph_type* g, node_index_type f, node_index_type s) : graph(g), first(f), second(s) {}

    bool
        operator < (const edge_type& other) const
        {
            return graph < other.graph
                || (graph == other.graph
                        && (first < other.first
                            || (first == other.first && second < other.second)));
        }

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    void
        file_io(ifile& fs, const graph_type* g)
        {
            graph = g;
            rw_base rw;
            rw(fs, first);
            rw(fs, second);
        }

    void
        file_io(ofile& fs, const graph_type*)
        {
            rw_base rw;
            rw(fs, first);
            rw(fs, second);
        }

72
73
74
75
    friend
        std::ostream&
        operator << (std::ostream& os, const edge_type& e);
};
76

77
78
79
80
81
82


struct colour_proxy_impl;
typedef std::shared_ptr<colour_proxy_impl> colour_proxy;
struct colour_proxy_impl {
    colour_proxy proxy;
83
    colour_proxy cache;
84
85
86
87
88

    friend
        colour_proxy
        get_colour_impl(colour_proxy col)
        {
89
90
            while (col->cache->proxy) { col->cache = col->cache->proxy; }
            return col->cache;
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        }

    friend
        colour_proxy
        assign_colour_impl(colour_proxy old_col, colour_proxy new_col)
        {
            get_colour_impl(old_col)->proxy = get_colour_impl(new_col);
            return old_col;
        }

    friend
        bool
        colour_equal(colour_proxy c1, colour_proxy c2)
        {
            return get_colour_impl(c1) == get_colour_impl(c2);
        }
};

inline
colour_proxy
111
create_colour() { auto ret = std::make_shared<colour_proxy_impl>(); ret->cache = ret; return ret; }
112
113


114
template <typename V>
115
void
116
sort_and_unique(std::vector<V>& v)
117
118
119
120
121
122
{
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());
}


123
enum node_type { Factor, Interface, Aggregate };
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
150
151
152
153
154
155
156
157

template <typename V>
std::ostream&
operator << (std::ostream& os, const std::list<V>& v)
{
    auto i = v.begin(), j = v.end();
    if (i != j) {
        os << (*i);
        for (++i; i != j; ++i) {
            os << " -- " << (*i);
        }
    }
    return os;
}


template <typename K, typename V>
std::ostream&
operator << (std::ostream& os, const std::map<K, V>& m)
{
    os << "{ ";
    for (const auto& kv: m) {
        os << kv.first << ": " << kv.second << ", ";
    }
    return os << '}';
}




typedef std::vector<genotype_comb_type> message_type;


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
inline
std::ostream&
operator << (std::ostream& os, const message_type& msg)
{
    auto i = msg.begin(), j = msg.end();
    if (i != j) {
        os << (*i);
        for (++i; i != j; ++i) {
            os << " | " << (*i);
        }
    }
    return os;
}


struct multiple_product_type {
    std::unordered_map<variable_index_type, colour_proxy> colours;
    std::vector<std::vector<var_vec>> vec_varsets;
    std::unordered_map<colour_proxy, std::vector<const genotype_comb_type*>> bins;
    std::unordered_map<colour_proxy, var_vec> varset_bins;
    std::vector<const message_type*> messages;

    void
        add(const message_type& msg)
        {
            size_t S = msg.size();
            vec_varsets.emplace_back();
            auto& varsets = vec_varsets.back();

            for (size_t i = 0; i < S; ++i) {
                varsets.emplace_back(get_parents(msg[i]));
                auto& varset = varsets.back();
                for (variable_index_type v: varset) {
                    auto& ptr = colours[v];
                    if (!ptr) {
                        ptr = create_colour();
                    }
                }
            }

            for (size_t i = 0; i < S; ++i) {
                if (varsets[i].size() == 0) { continue; }
                auto mcol = colours[varsets[i].front()];
                for (variable_index_type v: varsets[i]) {
                    auto& vcol = colours[v];
                    if (vcol && !colour_equal(vcol, mcol)) {
                        assign_colour_impl(vcol, mcol);
                    }
                }
            }
            messages.push_back(&msg);
        }

    void add(std::shared_ptr<message_type> ptr) { add(*ptr); }

    message_type
        compute(const var_vec& output, const std::map<var_vec, genotype_comb_type>& domains)
        {
            auto msg = messages.begin();
            for (const auto& varsets: vec_varsets) {
                for (size_t i = 0; i < varsets.size(); ++i) {
                    if (varsets[i].size() == 0) { continue; }
                    bins[get_colour_impl(colours[varsets[i].front()])].push_back(&(**msg)[i]);
                }
                for (const auto& kv: colours) {
                    varset_bins[get_colour_impl(kv.second)].push_back(kv.first);
                }
                ++msg;
            }

            for (auto& kv: varset_bins) {
                sort_and_unique(kv.second);
230
231
                MSG_DEBUG("varset " << kv.second << " assembles " << bins[kv.first].size() << " tables ");
                for (auto p: bins[kv.first]) { MSG_DEBUG(" * " << (*p)); }
232
233
234
235
236
237
            }

            message_type tmp;
            tmp.reserve(bins.size());
            for (const auto& kv: bins) {
                if ((output % varset_bins[kv.first]).size()) {
238
239
240
241
242
243
244
245
246
247
                    if (kv.second.size()) {
                        MSG_QUEUE_FLUSH();
                        if (kv.second.size() > 1) {
                            tmp.emplace_back(compute_product(kv.second.begin(), kv.second.end(), output, domains));
                        } else {
                            tmp.emplace_back(*kv.second.front());
                        }
                    } else {
                        tmp.push_back(*kv.second.front());
                    }
248
249
250
251
252
253
254
255
                }
            }
            MSG_DEBUG("result: " << tmp);
            return tmp;
        }
};


256
inline
257
message_type
258
product(const message_type& accum, const message_type& msg, const std::map<var_vec, genotype_comb_type>& domains)
259
{
260
261
262
263
264
265
266
267
268
269
270
271
    /*scoped_indent _(MESSAGE("[product] "));*/
    /*MSG_DEBUG("" << accum);*/
    /*MSG_DEBUG("" << msg);*/
    multiple_product_type mp;
    mp.add(accum);
    mp.add(msg);
    var_vec all;
    for (const auto& t: accum) { auto p = get_parents(t); all.insert(all.end(), p.begin(), p.end()); }
    for (const auto& t: msg) { auto p = get_parents(t); all.insert(all.end(), p.begin(), p.end()); }
    sort_and_unique(all);
    return mp.compute(all, domains);
#if 0
272
273
    message_type tmp;

274
    std::unordered_map<variable_index_type, colour_proxy> colours;
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

    std::vector<var_vec> accum_varsets, msg_varsets;

    for (size_t ai = 0; ai < accum.size(); ++ai) {
        accum_varsets.push_back(get_parents(accum[ai]));
        for (variable_index_type v: accum_varsets.back()) {
            auto& ptr = colours[v];
            if (!ptr) {
                ptr = create_colour();
            }
        }
    }
    for (size_t mi = 0; mi < msg.size(); ++mi) {
        msg_varsets.push_back(get_parents(msg[mi]));
        for (variable_index_type v: msg_varsets.back()) {
            auto& ptr = colours[v];
            if (!ptr) {
                ptr = create_colour();
            }
        }
    }

    for (size_t i = 0; i < accum.size(); ++i) {
298
        if (accum_varsets[i].size() == 0) { continue; }
299
300
301
302
303
304
305
306
307
308
        auto mcol = colours[accum_varsets[i].front()];
        for (variable_index_type v: accum_varsets[i]) {
            auto& vcol = colours[v];
            if (vcol && !colour_equal(vcol, mcol)) {
                assign_colour_impl(vcol, mcol);
            }
        }
    }

    for (size_t i = 0; i < msg.size(); ++i) {
309
        if (msg_varsets[i].size() == 0) { continue; }
310
311
312
313
314
315
316
317
318
        auto mcol = colours[msg_varsets[i].front()];
        for (variable_index_type v: msg_varsets[i]) {
            auto& vcol = colours[v];
            if (vcol && !colour_equal(vcol, mcol)) {
                assign_colour_impl(vcol, mcol);
            }
        }
    }

319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    std::unordered_map<colour_proxy, std::vector<const genotype_comb_type*>> bins;
    std::unordered_map<colour_proxy, var_vec> varsets;
    for (size_t i = 0; i < accum.size(); ++i) {
        if (accum_varsets[i].size() == 0) { continue; }
        bins[get_colour_impl(colours[accum_varsets[i].front()])].push_back(&accum[i]);
    }
    for (size_t i = 0; i < msg.size(); ++i) {
        if (msg_varsets[i].size() == 0) { continue; }
        bins[get_colour_impl(colours[msg_varsets[i].front()])].push_back(&msg[i]);
    }
    for (const auto& kv: colours) {
        varsets[get_colour_impl(kv.second)].push_back(kv.first);
    }

    tmp.reserve(bins.size());
    for (const auto& kv: bins) {
        tmp.emplace_back(compute_product(kv.second.begin(), kv.second.end(), varsets[kv.first], domains));
    }
    MSG_DEBUG("result: " << tmp);
    return tmp;

#if 0
341
342
343
344
345
346
347
348
    std::vector<colour_proxy> uniq;
    for (const auto& kv: colours) {
        uniq.push_back(get_colour_impl(kv.second));
    }
    sort_and_unique(uniq);

    std::vector<size_t> accum_dest, msg_dest;
    for (size_t i = 0; i < accum.size(); ++i) {
349
350
351
352
        if (accum_varsets[i].size() == 0) {
            accum_dest.push_back(0);
            continue;
        }
353
354
355
        accum_dest.push_back(std::find(uniq.begin(), uniq.end(), get_colour_impl(colours[accum_varsets[i].front()])) - uniq.begin());
    }
    for (size_t i = 0; i < msg.size(); ++i) {
356
357
358
359
        if (msg_varsets[i].size() == 0) {
            msg_dest.push_back(0);
            continue;
        }
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
        msg_dest.push_back(std::find(uniq.begin(), uniq.end(), get_colour_impl(colours[msg_varsets[i].front()])) - uniq.begin());
    }

    tmp.resize(uniq.size());

    for (size_t i = 0; i < accum.size(); ++i) {
        auto& table = tmp[accum_dest[i]];
        if (table.size()) {
            table = kronecker(table, accum[i]);
        } else {
            table = accum[i];
        }
    }
    for (size_t i = 0; i < msg.size(); ++i) {
        auto& table = tmp[msg_dest[i]];
        if (table.size()) {
            table = kronecker(table, msg[i]);
        } else {
            table = msg[i];
        }
    }

382
    return tmp;
383
384
#endif
#endif
385
}
386

387
388
inline
message_type&
389
accumulate(message_type& accum, const message_type& msg, const std::map<var_vec, genotype_comb_type>& domains)
390
{
391
392
    /*auto tmp = accum * msg;*/
    auto tmp = product(accum, msg, domains);
393
    accum.swap(tmp);
394
395
396
397
398
    return accum;
}

inline
std::shared_ptr<message_type>
399
accumulate(std::shared_ptr<message_type>& accum, const std::shared_ptr<message_type>& msg, const std::map<var_vec, genotype_comb_type>& domains)
400
{
401
402
403
404
    if (accum && accum->size()) {
        if (msg && msg->size()) {
            /**accum *= *msg;*/
            accumulate(*accum, *msg, domains);
405
406
407
408
        }
    } else {
        accum = msg;
    }
409
410
411
    return accum;
}

412
413
inline
std::shared_ptr<message_type>
414
product(std::shared_ptr<message_type> m1, std::shared_ptr<message_type> m2, const std::map<var_vec, genotype_comb_type>& domains)
415
416
417
418
419
420
{
    if (!m1) {
        return m2 ? std::make_shared<message_type>(*m2) : std::make_shared<message_type>();
    } else if (!m2) {
        return std::make_shared<message_type>(*m1);
    } else {
421
422
        /*return std::make_shared<message_type>(*m1 * *m2);*/
        return std::make_shared<message_type>(product(*m1, *m2, domains));
423
424
425
    }
}

426
427
428
429
430
431
432
433
434
435
436
437
438
439

inline
message_type
operator % (const message_type& msg, const var_vec& variables)
{
    message_type tmp;
    tmp.reserve(msg.size());
    for (const auto& table: msg) {
        auto varset = get_parents(table);
        if (varset == variables) {
            tmp.push_back(table);
        } else {
            auto proj = varset % variables;
            if (proj.size()) {
440
441
                /*var_vec norm = varset - proj;*/
                var_vec norm;
442
443
444
445
446
447
448
449
450
451
452
453
                tmp.push_back(project(table, proj, norm));
            }
        }
    }
    return tmp;
}


inline
std::shared_ptr<message_type>
operator % (std::shared_ptr<message_type> msg, const var_vec& variables)
{
454
455
456
457
458
    if (msg) {
        return std::make_shared<message_type>(*msg % variables);
    } else {
        return msg;
    }
459
460
461
}


462
463
464
465
466
467
468
469
470
471
inline
message_type&
operator %= (message_type& msg, const var_vec& variables)
{
    auto tmp = msg % variables;
    msg.swap(tmp);
    return msg;
}


472
473
474
475
476
477
478
479
480
481
482
483
inline
std::shared_ptr<message_type>
operator %= (std::shared_ptr<message_type> msg, const var_vec& variables)
{
    auto tmp = (*msg) % variables;
    msg->swap(tmp);
    return msg;
}


inline
genotype_comb_type&
484
accumulate(genotype_comb_type& table, const message_type& msg, const std::map<var_vec, genotype_comb_type>& domains)
485
{
486
487
488
489
    joint_variable_product_type jvp;
    jvp.add_table(table);
    for (const auto& mt: msg) {
        jvp.add_table(mt);
490
    }
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
    jvp.set_output(jvp.all_variable_names);
    jvp.compile(domains);
    table = jvp.compute();
    /*if (table.size()) {*/
        /*for (const auto& mt: msg) {*/
            /*table = kronecker(table, mt);*/
        /*}*/
    /*} else {*/
        /*auto i = msg.begin(), j = msg.end();*/
        /*if (i != j) {*/
            /*table = *i++;*/
        /*}*/
        /*for (; i != j; ++i) {*/
            /*table = kronecker(table, *i);*/
        /*}*/
    /*}*/
507
508
509
510
511
512
    return table;
}




513
514
515
516
517
518
519
520
521
522
inline
std::ostream&
operator << (std::ostream& os, std::shared_ptr<message_type> msg)
{
    if (msg) {
        return os << (*msg);
    } else {
        return os << "unif";
    }
}
523
524
525
526




527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
inline
void
normalize(genotype_comb_type& table)
{
    double accum = 0;
    for (const auto& e: table) {
        accum += e.coef;
    }
    if (accum != 0) {
        accum = 1. / accum;
        for (auto& e: table) {
            e.coef *= accum;
        }
    }
}


inline void normalize(message_type& msg) { for (auto& table: msg) { normalize(table); } }
inline void normalize(std::shared_ptr<message_type> msg) { normalize(*msg); }
546
547


548
549
550
enum VariableIO { None=0, Input=1, Output=2 };
inline VariableIO operator | (VariableIO v1, VariableIO v2) { return (VariableIO) (((int) v1) | ((int) v2)); }
inline VariableIO& operator |= (VariableIO& v1, VariableIO v2) { v1 = (VariableIO) (((int) v1) | ((int) v2)); return v1; }
551

552
struct graph_type {
553
554

    struct query_op_atom_type {
555
        const graph_type* g;
556
557
558
559
        var_vec variables;
        node_index_type node;
        size_t n_operands;

560
561
        query_op_atom_type() : g(NULL), variables(), node(0), n_operands(0) {}

562
        query_op_atom_type(const graph_type* _g, const var_vec& vv, node_index_type n, size_t no)
563
564
565
566
567
568
569
            : g(_g), variables(vv), node(n), n_operands(no)
        {}

        friend
            std::ostream&
            operator << (std::ostream& os, const query_op_atom_type& qoa)
            {
570
                return os << "<op out=" << qoa.variables << " inner=" << (qoa.g->is_interface(qoa.node) ? 'I' : 'F') << qoa.g->variables_of(qoa.node) << " n_opd=" << qoa.n_operands << '>';
571
572
            }

573
574
575
576
577
578
579
580
581
582
        template <typename STREAM_TYPE>
            void
            file_io(STREAM_TYPE& fs)
            {
                rw_base rw;
                rw(fs, variables);
                rw(fs, node);
                rw(fs, n_operands);
            }

583
584
585
        void
            operator () (std::vector<std::shared_ptr<message_type>>& stack, std::vector<const var_vec*>& var_stack) const
            {
586
587
588
589
590
591
                /*bool itf = g->is_interface(node);*/
                std::shared_ptr<message_type> ret;
                /*if (!itf) {*/
                    ret = std::make_shared<message_type>(g->state[node]);
                /*} else {*/
                /*}*/
592
                multiple_product_type mp;
593
                for (size_t i = 0; i < n_operands; ++i) {
594
595
596
597
598
599
600
601
602
                    mp.add(stack.back());
                }
                ret = std::make_shared<message_type>(mp.compute(variables, g->domains));
                /*for (size_t i = 0; i < n_operands; ++i) {*/
                    /*auto op = stack.back();*/
                    /*stack.pop_back();*/
                    /*var_stack.pop_back();*/
                    /*accumulate(ret, op, g->domains);*/
                /*}*/
603
604
605
606
                /*if (!itf) {*/
                    ret %= variables;
                /*}*/
                stack.emplace_back(ret);
607
608
609
610
611
612
613
                var_stack.push_back(&variables);
            }
    };


    typedef std::vector<query_op_atom_type> query_operation_type;

614
615
    enum ComputeStateOperation { PushState, PushMessage, Accumulate, Project, Store };
#define CSO_TYPE_STR(_c) (_c == PushState ? "PushState" : _c == PushMessage ? "PushMessage" : _c == Accumulate ? "Accumulate" : _c == Project ? "Project" : "Store")
616
    struct compute_state_operation_type {
617
        const graph_type* g;
618
619
620
621
        ComputeStateOperation op_type;
        size_t n;
        edge_type e;
        var_vec v;
622
623
        std::vector<query_operation_type> op;
        size_t n_nei_sub;
624

625
        compute_state_operation_type() : g(NULL), op_type(PushState), n(0), e(), v(), op(), n_nei_sub(0) {}
626
627
        compute_state_operation_type(const graph_type* _g, ComputeStateOperation ot, size_t _n, const edge_type& _e, var_vec&& _v)
            : g(_g), op_type(ot), n(_n), e(_e), v(std::move(_v)), op()
628
629
630
631
632
        {}

        void
            operator () (std::map<edge_type, std::shared_ptr<message_type>>& messages, std::vector<std::shared_ptr<message_type>>& stack) const
            {
633
634
                MSG_DEBUG((*this));
                MSG_QUEUE_FLUSH();
635
                switch (op_type) {
636
637
638
639
                    case PushState:
                        if (g->is_aggregate(n) && !g->is_compound_interface(n)) {
                            std::map<edge_type, std::shared_ptr<message_type>> messages;
                            g->subgraphs[n]->compute_messages(stack.end() - n_nei_sub, stack.end(), messages);
640
                            /*MSG_QUEUE_FLUSH();*/
641
                            stack.resize(stack.size() - n_nei_sub);
642
                            /*MSG_QUEUE_FLUSH();*/
643
644
645
                            g->subgraphs[n]->compute_state(op, messages);
                            stack.emplace_back(std::make_shared<message_type>());
                            for (const auto& msg: g->subgraphs[n]->extract(op)) {
646
                                accumulate(stack.back(), msg, g->domains);
647
                            }
648
                            /*MSG_QUEUE_FLUSH();*/
649
                            /*stack.push_back(g->subgraphs[n]->extract(op));*/
650
                        } else {
651
                            stack.push_back(std::make_shared<message_type>(message_type{g->state[n]}));
652
653
654
655
656
657
                        }
                        break;
                    case PushMessage:
                        stack.push_back(messages[e]);
                        break;
                    case Accumulate:
658
659
660
661
662
663
664
665
666
667
668
669
                        {
                            multiple_product_type mp;
                            auto i = stack.rbegin();
                            auto j = i + n;
                            for (; i != j; ++i) {
                                mp.add(*i);
                                MSG_DEBUG((*i));
                            }
                            auto result = mp.compute(v, g->domains);
                            stack.resize(stack.size() - n);
                            stack.emplace_back(std::make_shared<message_type>(std::move(result)));
                            MSG_DEBUG(" => " << stack.back());
670
                        }
671
672
673
674
675
                        /*for (size_t i = 0; i < n; ++i) {*/
                            /*auto m2 = stack.back();*/
                            /*stack.pop_back();*/
                            /*accumulate(stack.back(), m2, g->domains);*/
                        /*}*/
676
677
678
                        break;
                    case Project:
                        stack.back() %= v;
679
                        normalize(stack.back());
680
681
                        break;
                    case Store:
682
683
684
685
686
                        if (stack.back()) {
                            messages[e] = std::make_shared<message_type>(*stack.back());
                        } else {
                            messages[e] = std::make_shared<message_type>();
                        }
687
688
689
690
                        stack.pop_back();
                        break;
                    default:;
                };
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
                /*MSG_DEBUG("STACK");*/
                /*for (const auto& s: stack) { MSG_DEBUG("  " << s); }*/
                /*MSG_DEBUG("MESSAGES");*/
                /*for (const auto& kv: messages) {*/
                    /*MSG_DEBUG(std::setw(20) << std::left << kv.first << " [" << kv.second << ']');*/
                /*}*/
            }

        void
            file_io_op(ifile& fs, const graph_type* grph)
            {
                size_t size;
                rw_base rw;
                rw(fs, size); op.clear(); op.resize(size);
                for (auto& v: op) {
                    rw(fs, size); v.resize(size);
                    for (auto& o: v) {
                        o.g = grph;
                        o.file_io(fs);
                    }
711
                }
712
713
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
                int ot;
                rw(fs, ot);
                op_type = (ComputeStateOperation) ot;
                g = grph;
            }

        void
            file_io_op(ofile& fs, const graph_type*)
            {
                rw_base rw;
                rw(fs, op.size());
                for (auto& v: op) {
                    rw(fs, v.size());
                    for (auto& o: v) {
                        o.file_io(fs);
                    }
                }
                rw(fs, (int) op_type);
            }

        template <typename STREAM_TYPE>
            void
            file_io(STREAM_TYPE& fs, const graph_type* grph)
            {
                rw_base rw;
                rw(fs, n);
                e.file_io(fs, grph);
                rw(fs, v);
                rw(fs, n_nei_sub);
                file_io_op(fs, grph);
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
            }

        friend
            std::ostream&
            operator << (std::ostream& os, const compute_state_operation_type& cso)
            {
                os << '<';
                switch (cso.op_type) {
                    case PushState:
                        os << "PushState " << (cso.g->is_interface(cso.n) ? 'I' : cso.g->is_aggregate(cso.n) ? 'A' : 'F') << cso.g->variables_of(cso.n);
                        if (cso.g->is_aggregate(cso.n)) {
                            os << " query=" << cso.op;
                        }
                        break;
                    case PushMessage:
                        os << "PushMessage " << cso.e;
                        break;
                    case Accumulate:
760
                        os << "Accumulate " << cso.n << " / " << cso.v;
761
762
763
764
765
766
767
768
769
                        break;
                    case Project:
                        os << "Project " << cso.v;
                        break;
                    case Store:
                        os << "Store " << cso.e;
                        break;
                };
                return os << '>';
770
771
772
            }
    };

773
774
775
776
777
778
779
780
781
782

    node_vec rank;
    node_vec represented_by;
    std::vector<node_type> type;
    std::vector<colour_proxy> colour;
    std::vector<node_vec> neighbours_in;
    std::vector<node_vec> neighbours_out;
    std::vector<node_vec> inner_nodes;
    std::vector<var_vec> rules;
    var_vec variables;
783
    std::map<variable_index_type, VariableIO> io;
784

785
786
    std::map<variable_index_type, node_index_type> interface_to_node;
    std::map<node_index_type, variable_index_type> node_to_interface;
787

788
789
790
791
    std::vector<std::shared_ptr<graph_type>> subgraphs;

    std::vector<std::shared_ptr<message_type>> tables;
    std::vector<message_type> state;
792
    std::map<var_vec, genotype_comb_type> domains;
793
    /* TODO suppress joint_parent_domains and all afferent code */
794
    std::map<var_vec, genotype_comb_type> joint_parent_domains;
795
796
797
798
799
800
801
802
803
    std::map<variable_index_type, char> ancestor_letters;
    std::vector<compute_state_operation_type> compute_state_ops;

    const graph_type* parent;
    node_index_type index_in_parent;

    bool aggregate_cycles;
    bool generate_interfaces;

804
    size_t n_alleles;
805

806
807
808
    /* THIS DOESN'T HAVE  TO BE SAVED/LOADED. TEMPORARY STATE IN ORDER TO COMPUTE THE SEQUENCES OF OPERATIONS. */
    std::vector<var_vec> annotations;

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
    std::vector<bool> is_dh;

    graph_type&
        operator = (graph_type&& other)
        {
            rank = other.rank;
            represented_by = std::move(other.represented_by);
            type = std::move(other.type);
            colour = std::move(other.colour);
            neighbours_in = std::move(other.neighbours_in);
            neighbours_out = std::move(other.neighbours_out);
            inner_nodes = std::move(other.inner_nodes);
            rules = std::move(other.rules);
            variables = std::move(other.variables);
            io = std::move(other.io);
            interface_to_node = std::move(other.interface_to_node);
            node_to_interface = std::move(other.node_to_interface);
            subgraphs = std::move(other.subgraphs);
            tables = std::move(other.tables);
            state = std::move(other.state);
            parent = std::move(other.parent);
            index_in_parent = std::move(other.index_in_parent);
            aggregate_cycles = std::move(other.aggregate_cycles);
            generate_interfaces = std::move(other.generate_interfaces);
            n_alleles = other.n_alleles;
            annotations = std::move(other.annotations);
            is_dh = std::move(other.is_dh);
            domains = std::move(other.domains);
            ancestor_letters = std::move(other.ancestor_letters);
            node_domains = std::move(other.node_domains);
            node_domain_computed = std::move(other.node_domain_computed);
            for (auto& s: subgraphs) {
                if (s) {
                    s->parent = this;
                }
            }
            return *this;
        }

    /* node index 0 is the trash bin */

    graph_type(const graph_type& other)
        : rank(other.rank), represented_by(other.represented_by), type(other.type), colour(other.colour), neighbours_in(other.neighbours_in), neighbours_out(other.neighbours_out), inner_nodes(other.inner_nodes), rules(other.rules), variables(other.variables), io(), interface_to_node(), node_to_interface(), subgraphs(other.subgraphs), tables(other.tables), state(other.state), domains(other.domains), ancestor_letters(other.ancestor_letters), parent(other.parent), index_in_parent(other.index_in_parent),aggregate_cycles(other.aggregate_cycles), generate_interfaces(other.generate_interfaces), n_alleles(other.n_alleles), annotations(other.annotations), is_dh(other.is_dh)
    {
        for (auto& s: subgraphs) {
            if (s) {
                s->parent = this;
            }
        }
    }

860
    graph_type()
861
        : rank(1), represented_by(1), type(1), colour(1), neighbours_in(1), neighbours_out(1), inner_nodes(1), rules(1), variables(1), io(), interface_to_node(), node_to_interface(), subgraphs(1), tables(1), state(1), parent(nullptr), index_in_parent(0),aggregate_cycles(true), generate_interfaces(true), n_alleles(1), annotations(1), is_dh(1, false)
862
863
864
    {}

    graph_type(size_t n_al)
865
        : rank(1), represented_by(1), type(1), colour(1), neighbours_in(1), neighbours_out(1), inner_nodes(1), rules(1), variables(1), io(), interface_to_node(), node_to_interface(), subgraphs(1), tables(1), state(1), parent(nullptr), index_in_parent(0), aggregate_cycles(true), generate_interfaces(true), n_alleles(n_al), annotations(1), is_dh(1, false)
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
972
973
974
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
        io_colours(ofile& fs)
        {
            rw_base rw;
            rw(fs, colour.size());
            for (const auto& c: colour) {
                rw(fs, (ptrdiff_t) &*get_colour_impl(c));
            }
        }

    void
        io_colours(ifile& fs)
        {
            rw_base rw;
            size_t sz;
            rw(fs, sz);
            colour.clear();
            colour.reserve(sz);
            std::map<ptrdiff_t, colour_proxy> dic;
            for (; sz; --sz) {
                ptrdiff_t value;
                rw(fs, value);
                auto it = dic.find(value);
                if (it == dic.end()) {
                    colour.emplace_back(create_colour());
                    dic[value] = colour.back();
                } else {
                    colour.emplace_back(it->second);
                }
            }
        }

    void
        io_type_subgraphs_tables(ofile& fs)
        {
            rw_comb<int, bn_label_type> rw;
            rw(fs, type.size());
            for (auto t: type) {
                rw(fs, (int) t);
            }
            rw(fs, io.size());
            for (const auto& kv: io) {
                rw(fs, kv.first);
                rw(fs, (variable_index_type) kv.second);
            }
            rw(fs, subgraphs.size());
            for (const auto& ptr: subgraphs) {
                rw(fs, !!ptr);
                if (!!ptr) { ptr->file_io(fs); }
            }
            rw(fs, tables.size());
            for (const auto& ptr: tables) {
                rw(fs, !!ptr);
                if (!!ptr) { rw(fs, *ptr); }
            }
            rw(fs, compute_state_ops.size());
            for (auto& cso: compute_state_ops) {
                cso.file_io(fs, this);
            }
        }

    void
        io_type_subgraphs_tables(ifile& fs)
        {
            size_t size;
            bool b;
            rw_comb<int, bn_label_type> rw;
            rw(fs, size); type.clear(); type.resize(size);
            for (auto& t: type) {
                int i;
                rw(fs, i);
                t = (node_type) i;
            }
            rw(fs, size);
            for (; size; --size) {
                variable_index_type k, v;
                rw(fs, k);
                rw(fs, v);
                io[k] = (VariableIO) v;
            }
            rw(fs, size); subgraphs.clear(); subgraphs.resize(size);
            for (auto& ptr: subgraphs) {
                rw(fs, b);
                if (b) {
                    ptr = std::make_shared<graph_type>();
                    ptr->file_io(fs);
                    ptr->parent = this;
                }
            }
            rw(fs, size); tables.clear(); tables.resize(size);
            for (auto& ptr: tables) {
                rw(fs, b);
                if (b) {
                    ptr = std::make_shared<message_type>();
                    rw(fs, *ptr);
                }
            }
            rw(fs, size); compute_state_ops.clear(); compute_state_ops.resize(size);
            for (auto& cso: compute_state_ops) {
                cso.file_io(fs, this);
            }
        }

    template <typename STREAM_TYPE>
        void
        file_io(STREAM_TYPE& fs)
        {
            rw_comb<int, bn_label_type> rw;
            if (rw.fourcc(fs, "GRPH")) { return; }
            rw(fs, rank);
            rw(fs, represented_by);
            /*rw(fs, colour);*/
            rw(fs, neighbours_in);
            rw(fs, neighbours_out);
            rw(fs, inner_nodes);
            rw(fs, rules);
            rw(fs, variables);
            rw(fs, annotations);
            rw(fs, interface_to_node);
            rw(fs, node_to_interface);
            rw(fs, index_in_parent);
            rw(fs, aggregate_cycles);
            rw(fs, generate_interfaces);
            rw(fs, n_alleles);
            rw(fs, state);
            rw(fs, domains);
            rw(fs, ancestor_letters);
            if (rw.fourcc(fs, "TBLS")) { return; }
            io_type_subgraphs_tables(fs);
            if (rw.fourcc(fs, "CLR_")) { return; }
            io_colours(fs);
        }