graphnode.h 131 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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);
                MSG_DEBUG("varset " << kv.second << " assembles " << bins[kv.first].size() << " tables");
            }

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


246
inline
247
message_type
248
product(const message_type& accum, const message_type& msg, const std::map<var_vec, genotype_comb_type>& domains)
249
{
250
251
252
253
254
255
256
257
258
259
260
261
    /*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
262
263
    message_type tmp;

264
    std::unordered_map<variable_index_type, colour_proxy> colours;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

    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) {
288
        if (accum_varsets[i].size() == 0) { continue; }
289
290
291
292
293
294
295
296
297
298
        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) {
299
        if (msg_varsets[i].size() == 0) { continue; }
300
301
302
303
304
305
306
307
308
        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);
            }
        }
    }

309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    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
331
332
333
334
335
336
337
338
    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) {
339
340
341
342
        if (accum_varsets[i].size() == 0) {
            accum_dest.push_back(0);
            continue;
        }
343
344
345
        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) {
346
347
348
349
        if (msg_varsets[i].size() == 0) {
            msg_dest.push_back(0);
            continue;
        }
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
        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];
        }
    }

372
    return tmp;
373
374
#endif
#endif
375
}
376

377
378
inline
message_type&
379
accumulate(message_type& accum, const message_type& msg, const std::map<var_vec, genotype_comb_type>& domains)
380
{
381
382
    /*auto tmp = accum * msg;*/
    auto tmp = product(accum, msg, domains);
383
    accum.swap(tmp);
384
385
386
387
388
    return accum;
}

inline
std::shared_ptr<message_type>
389
accumulate(std::shared_ptr<message_type>& accum, const std::shared_ptr<message_type>& msg, const std::map<var_vec, genotype_comb_type>& domains)
390
{
391
392
393
394
    if (accum && accum->size()) {
        if (msg && msg->size()) {
            /**accum *= *msg;*/
            accumulate(*accum, *msg, domains);
395
396
397
398
        }
    } else {
        accum = msg;
    }
399
400
401
    return accum;
}

402
403
inline
std::shared_ptr<message_type>
404
product(std::shared_ptr<message_type> m1, std::shared_ptr<message_type> m2, const std::map<var_vec, genotype_comb_type>& domains)
405
406
407
408
409
410
{
    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 {
411
412
        /*return std::make_shared<message_type>(*m1 * *m2);*/
        return std::make_shared<message_type>(product(*m1, *m2, domains));
413
414
415
    }
}

416
417
418
419
420
421
422
423
424
425
426
427
428
429

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()) {
430
431
                /*var_vec norm = varset - proj;*/
                var_vec norm;
432
433
434
435
436
437
438
439
440
441
442
443
                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)
{
444
445
446
447
448
    if (msg) {
        return std::make_shared<message_type>(*msg % variables);
    } else {
        return msg;
    }
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
}


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&
464
accumulate(genotype_comb_type& table, const message_type& msg, const std::map<var_vec, genotype_comb_type>& domains)
465
{
466
467
468
469
    joint_variable_product_type jvp;
    jvp.add_table(table);
    for (const auto& mt: msg) {
        jvp.add_table(mt);
470
    }
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    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);*/
        /*}*/
    /*}*/
487
488
489
490
491
492
    return table;
}




493
494
495
496
497
498
499
500
501
502
inline
std::ostream&
operator << (std::ostream& os, std::shared_ptr<message_type> msg)
{
    if (msg) {
        return os << (*msg);
    } else {
        return os << "unif";
    }
}
503
504
505
506




507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
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); }
526
527


528
529
530
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; }
531

532
struct graph_type {
533
534

    struct query_op_atom_type {
535
        const graph_type* g;
536
537
538
539
        var_vec variables;
        node_index_type node;
        size_t n_operands;

540
541
        query_op_atom_type() : g(NULL), variables(), node(0), n_operands(0) {}

542
        query_op_atom_type(const graph_type* _g, const var_vec& vv, node_index_type n, size_t no)
543
544
545
546
547
548
549
            : g(_g), variables(vv), node(n), n_operands(no)
        {}

        friend
            std::ostream&
            operator << (std::ostream& os, const query_op_atom_type& qoa)
            {
550
                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 << '>';
551
552
            }

553
554
555
556
557
558
559
560
561
562
        template <typename STREAM_TYPE>
            void
            file_io(STREAM_TYPE& fs)
            {
                rw_base rw;
                rw(fs, variables);
                rw(fs, node);
                rw(fs, n_operands);
            }

563
564
565
        void
            operator () (std::vector<std::shared_ptr<message_type>>& stack, std::vector<const var_vec*>& var_stack) const
            {
566
567
568
569
570
571
                /*bool itf = g->is_interface(node);*/
                std::shared_ptr<message_type> ret;
                /*if (!itf) {*/
                    ret = std::make_shared<message_type>(g->state[node]);
                /*} else {*/
                /*}*/
572
                multiple_product_type mp;
573
                for (size_t i = 0; i < n_operands; ++i) {
574
575
576
577
578
579
580
581
582
                    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);*/
                /*}*/
583
584
585
586
                /*if (!itf) {*/
                    ret %= variables;
                /*}*/
                stack.emplace_back(ret);
587
588
589
590
591
592
593
                var_stack.push_back(&variables);
            }
    };


    typedef std::vector<query_op_atom_type> query_operation_type;

594
595
    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")
596
    struct compute_state_operation_type {
597
        const graph_type* g;
598
599
600
601
        ComputeStateOperation op_type;
        size_t n;
        edge_type e;
        var_vec v;
602
603
        std::vector<query_operation_type> op;
        size_t n_nei_sub;
604

605
        compute_state_operation_type() : g(NULL), op_type(PushState), n(0), e(), v(), op(), n_nei_sub(0) {}
606
607
        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()
608
609
610
611
612
        {}

        void
            operator () (std::map<edge_type, std::shared_ptr<message_type>>& messages, std::vector<std::shared_ptr<message_type>>& stack) const
            {
613
614
                MSG_DEBUG((*this));
                MSG_QUEUE_FLUSH();
615
                switch (op_type) {
616
617
618
619
                    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);
620
                            /*MSG_QUEUE_FLUSH();*/
621
                            stack.resize(stack.size() - n_nei_sub);
622
                            /*MSG_QUEUE_FLUSH();*/
623
624
625
                            g->subgraphs[n]->compute_state(op, messages);
                            stack.emplace_back(std::make_shared<message_type>());
                            for (const auto& msg: g->subgraphs[n]->extract(op)) {
626
                                accumulate(stack.back(), msg, g->domains);
627
                            }
628
                            /*MSG_QUEUE_FLUSH();*/
629
                            /*stack.push_back(g->subgraphs[n]->extract(op));*/
630
                        } else {
631
                            stack.push_back(std::make_shared<message_type>(message_type{g->state[n]}));
632
633
634
635
636
637
                        }
                        break;
                    case PushMessage:
                        stack.push_back(messages[e]);
                        break;
                    case Accumulate:
638
639
640
641
642
643
644
645
646
647
648
649
                        {
                            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());
650
                        }
651
652
653
654
655
                        /*for (size_t i = 0; i < n; ++i) {*/
                            /*auto m2 = stack.back();*/
                            /*stack.pop_back();*/
                            /*accumulate(stack.back(), m2, g->domains);*/
                        /*}*/
656
657
658
                        break;
                    case Project:
                        stack.back() %= v;
659
                        normalize(stack.back());
660
661
                        break;
                    case Store:
662
663
664
665
666
                        if (stack.back()) {
                            messages[e] = std::make_shared<message_type>(*stack.back());
                        } else {
                            messages[e] = std::make_shared<message_type>();
                        }
667
668
669
670
                        stack.pop_back();
                        break;
                    default:;
                };
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
                /*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);
                    }
691
                }
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
                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);
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
            }

        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:
740
                        os << "Accumulate " << cso.n << " / " << cso.v;
741
742
743
744
745
746
747
748
749
                        break;
                    case Project:
                        os << "Project " << cso.v;
                        break;
                    case Store:
                        os << "Store " << cso.e;
                        break;
                };
                return os << '>';
750
751
752
            }
    };

753
754
755
756
757
758
759
760
761
762

    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;
763
    std::map<variable_index_type, VariableIO> io;
764

765
766
    std::map<variable_index_type, node_index_type> interface_to_node;
    std::map<node_index_type, variable_index_type> node_to_interface;
767

768
769
770
771
    std::vector<std::shared_ptr<graph_type>> subgraphs;

    std::vector<std::shared_ptr<message_type>> tables;
    std::vector<message_type> state;
772
    std::map<var_vec, genotype_comb_type> domains;
773
    std::map<var_vec, genotype_comb_type> joint_parent_domains;
774
775
776
777
778
779
780
781
782
783
784
    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;

    size_t n_alleles = 1;

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

788
    graph_type()
789
        : rank(), represented_by(), type(), colour(), neighbours_in(), neighbours_out(), inner_nodes(), rules(), variables(), io(), interface_to_node(), node_to_interface(), subgraphs(),  parent(nullptr), index_in_parent(0),aggregate_cycles(true), generate_interfaces(true), n_alleles(1), annotations()
790
791
792
    {}

    graph_type(size_t n_al)
793
        : rank(), represented_by(), type(), colour(), neighbours_in(), neighbours_out(), inner_nodes(), rules(), variables(), io(), interface_to_node(), node_to_interface(), subgraphs(), parent(nullptr), index_in_parent(0), aggregate_cycles(true), generate_interfaces(true), n_alleles(n_al), annotations()
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
    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);
        }

    void
        save(const std::string& filename)
        {
            ofile ofs(filename);
            file_io(ofs);
        }

    void
        load(const std::string& filename)
        {
            ifile ifs(filename);
            file_io(ifs);
            parent = NULL;
        }
943

944
    bool is_aggregate(node_index_type node) const { return inner_nodes[node].size() > 1; }
945

946
947
948
949
950
951
952
953
954
955
956
957
958
    bool
        is_compound_interface(node_index_type node) const
        {
            if (is_aggregate(node)) {
                for (node_index_type n: inner_nodes[node]) {
                    if (type[n] != Interface) {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }
959

960
961
962
963
964
965
966
967
968
969
970
971
972
    bool
        is_interface(node_index_type node) const
        {
            if (is_aggregate(node)) {
                for (node_index_type n: inner_nodes[node]) {
                    if (type[n] != Interface) {
                        return false;
                    }
                }
                return true;
            }
            return type[node] == Interface;
        }
973

974
975
976
977
978
    bool
        is_computable(node_index_type node) const
        {
            return !is_interface(node);
        }
979
980
981

    size_t size() const { return rank.size(); }

982
    node_vec
983
984
        active_nodes() const
        {
985
            node_vec ret;
986
            ret.reserve(represented_by.size());
987
            for (node_index_type i = 0; i < represented_by.size(); ++i) {
988
989
990
991
992
993
994
                if (represented_by[i] == i) {
                    ret.push_back(i);
                }
            }
            return ret;
        }

995
996
    node_vec
        resolve_vector(const node_vec& vec) const
997
        {
998
            node_vec ret;
999
            ret.reserve(vec.size());
1000
            for (node_index_type i: vec) {