graphnode.h 165 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
    std::weak_ptr<colour_proxy_impl> cache;
84
85
86
87
88

    friend
        colour_proxy
        get_colour_impl(colour_proxy col)
        {
89
90
            while (col->cache.lock()->proxy) { col->cache = col->cache.lock()->proxy; }
            return col->cache.lock();
91
92
93
94
95
96
        }

    friend
        colour_proxy
        assign_colour_impl(colour_proxy old_col, colour_proxy new_col)
        {
97
98
99
100
101
            auto oc = get_colour_impl(old_col);
            auto nc = get_colour_impl(new_col);
            if (oc != nc) {
                oc->proxy = nc;
            }
102
103
104
105
106
107
108
109
110
111
112
113
114
            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
115
create_colour() { auto ret = std::make_shared<colour_proxy_impl>(); ret->cache = ret; return ret; }
116
117


118
template <typename V>
119
void
120
sort_and_unique(std::vector<V>& v)
121
122
123
124
125
126
{
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());
}


127
enum node_type { Factor, Interface, Aggregate };
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
158
159
160
161

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;


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
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);
234
235
                /*MSG_DEBUG("varset " << kv.second << " assembles " << bins[kv.first].size() << " tables ");*/
                /*for (auto p: bins[kv.first]) { MSG_DEBUG(" * " << (*p)); }*/
236
237
238
239
240
241
            }

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


258
inline
259
message_type
260
product(const message_type& accum, const message_type& msg, const std::map<var_vec, genotype_comb_type>& domains)
261
{
262
263
264
265
266
267
268
269
270
271
272
273
    /*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
274
275
    message_type tmp;

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

    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) {
300
        if (accum_varsets[i].size() == 0) { continue; }
301
302
303
304
305
306
307
308
309
310
        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) {
311
        if (msg_varsets[i].size() == 0) { continue; }
312
313
314
315
316
317
318
319
320
        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);
            }
        }
    }

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    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
343
344
345
346
347
348
349
350
    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) {
351
352
353
354
        if (accum_varsets[i].size() == 0) {
            accum_dest.push_back(0);
            continue;
        }
355
356
357
        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) {
358
359
360
361
        if (msg_varsets[i].size() == 0) {
            msg_dest.push_back(0);
            continue;
        }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
        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];
        }
    }

384
    return tmp;
385
386
#endif
#endif
387
}
388

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

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

414
415
inline
std::shared_ptr<message_type>
416
product(std::shared_ptr<message_type> m1, std::shared_ptr<message_type> m2, const std::map<var_vec, genotype_comb_type>& domains)
417
418
419
420
421
422
{
    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 {
423
424
        /*return std::make_shared<message_type>(*m1 * *m2);*/
        return std::make_shared<message_type>(product(*m1, *m2, domains));
425
426
427
    }
}

428
429
430
431
432
433
434
435
436
437
438
439
440
441

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


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


474
475
476
477
478
479
480
481
482
483
484
485
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&
486
accumulate(genotype_comb_type& table, const message_type& msg, const std::map<var_vec, genotype_comb_type>& domains)
487
{
488
489
490
491
    joint_variable_product_type jvp;
    jvp.add_table(table);
    for (const auto& mt: msg) {
        jvp.add_table(mt);
492
    }
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
    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);*/
        /*}*/
    /*}*/
509
510
511
512
513
514
    return table;
}




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




529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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); }
548
549


550
551
552
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; }
553
inline bool operator & (VariableIO v1, VariableIO v2) { return !!(((int) v1) & ((int) v2)); }
554

555
struct graph_type {
556

557
#if 0
558
    struct query_op_atom_type {
559
        const graph_type* g;
560
561
562
563
        var_vec variables;
        node_index_type node;
        size_t n_operands;

564
565
        query_op_atom_type() : g(NULL), variables(), node(0), n_operands(0) {}

566
        query_op_atom_type(const graph_type* _g, const var_vec& vv, node_index_type n, size_t no)
567
568
569
570
571
572
573
            : g(_g), variables(vv), node(n), n_operands(no)
        {}

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

577
578
579
580
581
582
583
584
585
586
        template <typename STREAM_TYPE>
            void
            file_io(STREAM_TYPE& fs)
            {
                rw_base rw;
                rw(fs, variables);
                rw(fs, node);
                rw(fs, n_operands);
            }

587
588
589
        void
            operator () (std::vector<std::shared_ptr<message_type>>& stack, std::vector<const var_vec*>& var_stack) const
            {
590
591
592
593
594
595
                /*bool itf = g->is_interface(node);*/
                std::shared_ptr<message_type> ret;
                /*if (!itf) {*/
                    ret = std::make_shared<message_type>(g->state[node]);
                /*} else {*/
                /*}*/
596
                multiple_product_type mp;
597
                for (size_t i = 0; i < n_operands; ++i) {
598
599
600
601
602
603
604
605
606
                    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);*/
                /*}*/
607
608
609
610
                /*if (!itf) {*/
                    ret %= variables;
                /*}*/
                stack.emplace_back(ret);
611
612
613
614
615
616
617
                var_stack.push_back(&variables);
            }
    };


    typedef std::vector<query_op_atom_type> query_operation_type;

618
619
    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")
620
    struct compute_state_operation_type {
621
        const graph_type* g;
622
623
624
625
        ComputeStateOperation op_type;
        size_t n;
        edge_type e;
        var_vec v;
626
627
        std::vector<query_operation_type> op;
        size_t n_nei_sub;
628

629
        compute_state_operation_type() : g(NULL), op_type(PushState), n(0), e(), v(), op(), n_nei_sub(0) {}
630
631
        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()
632
633
634
635
636
        {}

        void
            operator () (std::map<edge_type, std::shared_ptr<message_type>>& messages, std::vector<std::shared_ptr<message_type>>& stack) const
            {
637
638
                /*MSG_DEBUG((*this));*/
                /*MSG_QUEUE_FLUSH();*/
639
                switch (op_type) {
640
641
642
643
                    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);
644
                            /*MSG_QUEUE_FLUSH();*/
645
                            stack.resize(stack.size() - n_nei_sub);
646
                            /*MSG_QUEUE_FLUSH();*/
647
648
649
                            g->subgraphs[n]->compute_state(op, messages);
                            stack.emplace_back(std::make_shared<message_type>());
                            for (const auto& msg: g->subgraphs[n]->extract(op)) {
650
                                accumulate(stack.back(), msg, g->domains);
651
                            }
652
                            /*MSG_QUEUE_FLUSH();*/
653
                            /*stack.push_back(g->subgraphs[n]->extract(op));*/
654
                        } else {
655
                            stack.push_back(std::make_shared<message_type>(message_type{g->state[n]}));
656
657
658
659
660
661
                        }
                        break;
                    case PushMessage:
                        stack.push_back(messages[e]);
                        break;
                    case Accumulate:
662
663
664
665
666
667
                        {
                            multiple_product_type mp;
                            auto i = stack.rbegin();
                            auto j = i + n;
                            for (; i != j; ++i) {
                                mp.add(*i);
668
                                /*MSG_DEBUG((*i));*/
669
670
671
672
                            }
                            auto result = mp.compute(v, g->domains);
                            stack.resize(stack.size() - n);
                            stack.emplace_back(std::make_shared<message_type>(std::move(result)));
673
                            /*MSG_DEBUG(" => " << stack.back());*/
674
                        }
675
676
677
678
679
                        /*for (size_t i = 0; i < n; ++i) {*/
                            /*auto m2 = stack.back();*/
                            /*stack.pop_back();*/
                            /*accumulate(stack.back(), m2, g->domains);*/
                        /*}*/
680
681
682
                        break;
                    case Project:
                        stack.back() %= v;
683
                        normalize(stack.back());
684
685
                        break;
                    case Store:
686
687
688
689
690
                        if (stack.back()) {
                            messages[e] = std::make_shared<message_type>(*stack.back());
                        } else {
                            messages[e] = std::make_shared<message_type>();
                        }
691
692
693
694
                        stack.pop_back();
                        break;
                    default:;
                };
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
                /*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);
                    }
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
                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);
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
            }

        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:
764
                        os << "Accumulate " << cso.n << " / " << cso.v;
765
766
767
768
769
770
771
772
773
                        break;
                    case Project:
                        os << "Project " << cso.v;
                        break;
                    case Store:
                        os << "Store " << cso.e;
                        break;
                };
                return os << '>';
774
775
            }
    };
776
#endif
777
778
779
780
781
782
783
784
785
786

    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;
787
    std::vector<var_vec> node_variables;
788
    std::map<variable_index_type, VariableIO> io;
789

790
791
    std::map<variable_index_type, node_index_type> interface_to_node;
    std::map<node_index_type, variable_index_type> node_to_interface;
792

793
794
795
    std::vector<std::shared_ptr<graph_type>> subgraphs;

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

    const graph_type* parent;
    node_index_type index_in_parent;

    bool aggregate_cycles;
    bool generate_interfaces;

809
    size_t n_alleles;
810

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

814
815
    std::vector<bool> is_dh;

816
817
    graph_type& operator = (graph_type&& other) = delete;
#if 0
818
819
820
821
822
823
824
825
826
827
        {
            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);
828
            node_variables = std::move(other.node_variables);
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
            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;
        }
853
#endif
854
855
856

    /* node index 0 is the trash bin */

857
858
859
    graph_type(const graph_type& other) = delete;
#if 0
        : 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), node_variables(other.node_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)
860
861
862
863
864
865
866
    {
        for (auto& s: subgraphs) {
            if (s) {
                s->parent = this;
            }
        }
    }
867
#endif
868

869
    graph_type()
870
        : rank(1), represented_by(1), type(1), colour(1), neighbours_in(1), neighbours_out(1), inner_nodes(1), rules(1), variables(1), node_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)
871
872
873
    {}

    graph_type(size_t n_al)
874
        : rank(1), represented_by(1), type(1), colour(1), neighbours_in(1), neighbours_out(1), inner_nodes(1), rules(1), variables(1), node_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)
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
    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());
918
            for (auto& kv: io) {
919
920
921
922
                rw(fs, kv.first);
                rw(fs, (variable_index_type) kv.second);
            }
            rw(fs, subgraphs.size());
923
            for (auto& ptr: subgraphs) {
924
925
926
927
                rw(fs, !!ptr);
                if (!!ptr) { ptr->file_io(fs); }
            }
            rw(fs, tables.size());
928
            for (auto& ptr: tables) {
929
930
931
                rw(fs, !!ptr);
                if (!!ptr) { rw(fs, *ptr); }
            }
932
933
934
935
            /*rw(fs, compute_state_ops.size());*/
            /*for (auto& cso: compute_state_ops) {*/
                /*cso.file_io(fs, this);*/
            /*}*/
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
        }

    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);
                }
            }
974
975
976
977
            /*rw(fs, size); compute_state_ops.clear(); compute_state_ops.resize(size);*/
            /*for (auto& cso: compute_state_ops) {*/
                /*cso.file_io(fs, this);*/
            /*}*/
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        }

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