aicad 1.0.0-rc.1

A library to solve Constraint satisfaction problems with Multi-valued decision diagrams and, optionnaly, neural networks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//! Weighted model counting on an MDD: the forward and backward passes, the weighted count of
//! solutions and its gradient.
//!
//! # Purpose
//!
//! The weighted model count of a solution space given a probability distribution is the weighted
//! sum of each solution's probability.
//! Currently, we assume that the probability distribution on the solution space (i.e., an MDD) is
//! factorised as the product of each variable's own probability distribution over its domain.
//! Hence, the weighted model count can be computed in a single forward-pass on the MDD.
//! Similarly, the gradient (when probabilities come from a parametrised model such as a neural network) can be computed with,
//! in addition, a single backward pass.
//!
//! # How it works
//!
//! Two dynamic programs run over the MDD structure in $\mathcal{O}(|N| + |E|)$ with $N$ the set of
//! nodes and $E$ the set of edges of the MDD.
//! The weight of a path is the product of the weights of its edges. We define by $\alpha(u)$ the
//! sum of the weights of the paths from the root to $u$ and by $\beta(u)$ the sum of the weights
//! of the paths from $u$ to the sink.
//! It follows that $\alpha(s)$, with $s$ the sink, gives the weighted model count of the MDD,
//! since each path is a full assignment encoded in the MDD.
//! Given that an MDD is a directed acyclic graph, these two values are easily computed by a
//! layer-wise forward/backward pass, starting from $\alpha(\text{root}) = 1$ and
//! $\beta(s) = 1$ (the empty path has weight $1$).
//!
//! Now, let us compute the gradient of the weighted model count with respect to the parameters
//! (i.e., the edges' weights).
//! Let $u$ be a node of layer $\ell$ and $E_u$ its outgoing edges.
//! For an edge $e$, we denote $w_e$ its weight, $\mathrm{from}(e)$ its source node, and
//! $\mathrm{to}(e)$ its destination node. The weight of the paths that leave $u$ can be computed as
//!
//! $$\beta(u) = \sum_{e \in E_u} w_e \times \beta(\mathrm{to}(e)).$$
//!
//! Since every path in the MDD crosses exactly one node in layer $\ell$, then the weighted
//! model count can be computed as
//!
//! $$\mathrm{WMC} = \sum_{u \in \ell} \alpha(u) \times \beta(u) = \sum_{u \in \ell} \sum_{e \in
//! E_u} \alpha(u) \times w_e \times \beta(\mathrm{to}(e)).$$
//!
//! Notice that $\alpha(u)$ only depends on the weights of the layers before $\ell$ and
//! $\beta(\mathrm{to}(e))$ on those after it: neither involves $w_e$. Hence, the gradient of the
//! $\mathrm{WMC}$ w.r.t. the parameter $w_e$ of edge $e \in E_u$ is given by
//!
//! $$ \frac{\partial\ \mathrm{WMC}}{\partial w_e} = \alpha(u) \times \beta(\mathrm{to}(e)).$$
//!
//! More generally, a parameter $w$ can be shared among a set of edges $E_w$ (here, the edges of
//! layer $\ell$ with the same value $v$ share the weight $w_{\ell,v}$). By the chain rule, the
//! gradient is given by
//!
//! $$ \frac{\partial\ \mathrm{WMC}}{\partial w} = \sum_{e \in E_w}
//! \alpha(\mathrm{from}(e)) \times \beta(\mathrm{to}(e)).$$
//!
//! ## Scaling
//!
//! The products of many weights underflow: a path of 400 weights of $10^{-3}$ is worth
//! $10^{-1200}$, which `f64` rounds to zero. The passes therefore rescale layer by layer, as is
//! done to decode long sequences with hidden Markov models. Let $c_\ell$ be a scale factor per
//! layer, with $c_0 = 1$, and let
//!
//! $$A_\ell = \prod_{k \le \ell} c_k, \qquad B_\ell = \prod_{k > \ell} c_k, \qquad
//! \hat\alpha(u) = \frac{\alpha(u)}{A_\ell}, \qquad \hat\beta(u) = \frac{\beta(u)}{B_\ell}
//! \quad (u \in \ell).$$
//!
//! *Forward pass.* Base case: the root is alone in layer $0$, $\alpha(\text{root}) = 1$ and
//! $A_0 = 1$, so $\hat\alpha(\text{root}) = 1$. For a layer $\ell + 1$, compute for every node $v$
//! of that layer
//!
//! $$\tilde\alpha(v) = \sum_{e : \mathrm{to}(e) = v} \hat\alpha(\mathrm{from}(e)) \times w_e,
//! \qquad c_{\ell+1} = \sum_{v \in \ell+1} \tilde\alpha(v), \qquad
//! \hat\alpha(v) = \frac{\tilde\alpha(v)}{c_{\ell+1}}.$$
//!
//! Then $\alpha(v) = A_\ell \times \tilde\alpha(v) = A_{\ell+1} \times \hat\alpha(v)$, so
//! $\hat\alpha$ is indeed the rescaled $\alpha$, and every layer sums to one. The sink $s$ is
//! alone in layer $L$: $\hat\alpha(s) = 1$ and
//!
//! $$\mathrm{WMC} = \alpha(s) = A_L = \prod_{\ell=1}^{L} c_\ell, \qquad
//! \ln \mathrm{WMC} = \sum_{\ell=1}^{L} \ln c_\ell.$$
//!
//! *Backward pass, with the same $c_\ell$.* Base case: $\beta(s) = 1$ and $B_L = 1$ (an empty
//! product), so $\hat\beta(s) = 1$. For a node $u$ of a layer $\ell < L$, since
//! $B_\ell = c_{\ell+1} B_{\ell+1}$,
//!
//! $$\hat\beta(u) = \frac{1}{c_{\ell+1}} \sum_{e \in E_u} w_e \times \hat\beta(\mathrm{to}(e)).$$
//!
//! The two passes meet: $A_\ell B_\ell = \prod_{k=0}^{L} c_k = \mathrm{WMC}$ for every layer,
//! hence $\sum_{u \in \ell} \hat\alpha(u) \times \hat\beta(u) = 1$. At layer $L$ this is
//! $\hat\alpha(s)\hat\beta(s) = 1$, and at layer $0$ it says that the backward pass ends with
//! $\hat\beta(\text{root}) = 1$.
//!
//! *Gradient.* With $\alpha(\mathrm{from}(e)) = A_\ell \times \hat\alpha(\mathrm{from}(e))$ and
//! $\beta(\mathrm{to}(e)) = B_{\ell+1} \times \hat\beta(\mathrm{to}(e))$, and $A_\ell B_\ell =
//! \mathrm{WMC}$,
//!
//! $$\frac{\partial \ln \mathrm{WMC}}{\partial w} = \frac{1}{\mathrm{WMC}}
//! \sum_{e \in E_w} \alpha(\mathrm{from}(e)) \times \beta(\mathrm{to}(e))
//! = \frac{1}{c_{\ell+1}} \sum_{e \in E_w} \hat\alpha(\mathrm{from}(e)) \times
//! \hat\beta(\mathrm{to}(e)).$$
//!
//! In these formulas no number leaves the range of ordinary floating point values.
//!
//! *Degenerate case.* If no weight reaches layer $\ell + 1$ then $c_{\ell+1} = 0$: $\hat\alpha$
//! and $\hat\beta$ are taken to be $0$ from there on (to avoid a division by zero),
//! $\mathrm{WMC} = 0$, $\ln \mathrm{WMC} = -\infty$ and the gradient is zero.
//!
//! # Design notes
//!
//! - **The functions work on logarithms.** [`log_wmc`] and [`log_wmc_and_gradient`] return
//!   $\ln \mathrm{WMC}$ and the gradient of $\ln \mathrm{WMC}$, which is what a loss of the form
//!   $-\ln \mathrm{WMC}$ needs; [`wmc`] exponentiates for the callers that want the count itself
//!   and accept that it may underflow. A diagram without any weighted path has $\ln \mathrm{WMC}
//!   = -\infty$ and a zero gradient.
//! - **Weights are indexed by layer** of the diagram (`weights[layer][value]`, one row per layer
//!   but the sink's), so the functions do not need to know which variable a layer stands for.
//! - **Shape only.** Every function takes an [`Mdd`] and reads its layers, nodes and edges, so it
//!   runs on a freshly compiled diagram as well as on one shared through the arena, whose
//!   variables are not those of the constraint using it.
//! - **The diagram must be well formed.** The passes assume what `Compiler::new`, `Compiler::refine` and the
//!   diagrams made from them guarantee: a single root and a single sink, which are node `0` of
//!   their layer, and only active edges, each going from a layer to the next. Debug builds check
//!   it on every call.
//! - **Weights are plain numbers.** They need not be normalised or even positive.
//!
//! # Example
//!
//! The probability that two variables with uniform distributions over `{0, 1, 2}` take
//! different values is `6 / 9`, and the gradient says how much each weight contributes to it.
//!
//! ```
//! use aicad::constraints::not_equals;
//! use aicad::mdd::heuristics::{
//!     CompilationHeuristic, MergeHeuristic, OrderingHeuristic, SelectHeuristic,
//! };
//! use aicad::mdd::wmc::{log_wmc_and_gradient, wmc};
//! use aicad::mdd::Compiler;
//! use aicad::modelling::Problem;
//! use std::sync::Arc;
//!
//! let mut problem = Problem::default();
//! let x = problem.add_variable(vec![0, 1, 2]);
//! let y = problem.add_variable(vec![0, 1, 2]);
//! let constraint = not_equals(&mut problem, x, y);
//!
//! let mut compiler = Compiler::new(
//!     Arc::new(problem),
//!     CompilationHeuristic {
//!         ordering: OrderingHeuristic::MinDomMaxLinked,
//!         merge: MergeHeuristic::LessRelaxed,
//!         selection: SelectHeuristic::Greedy,
//!     },
//!     &[constraint],
//! );
//! compiler.refine(usize::MAX);
//! let mdd = compiler.into_mdd();
//!
//! // One row of weights per layer: here the first layer is `x`, the second `y`.
//! let uniform = vec![vec![1.0 / 3.0; 3]; 2];
//! assert!((wmc(&mdd, &uniform) - 6.0 / 9.0).abs() < 1e-12);
//!
//! // d(ln WMC) / d(weight of x = 1) = (weight of the values of `y` other than 1) / WMC = 1.
//! let (ln_count, gradient) = log_wmc_and_gradient(&mdd, &uniform);
//! assert!((ln_count.exp() - 6.0 / 9.0).abs() < 1e-12);
//! assert!((gradient[0][1] - 1.0).abs() < 1e-12);
//! ```

use super::{Mdd, NodeIndex};

/// What the passes of this module compute: the rescaled `alpha` and `beta` of the module
/// documentation and the constants that rescaled them.
struct Scaled {
    /// `alpha[layer][node]` is $\hat\alpha$; every layer sums to one, or to zero if no weight
    /// reaches it.
    alpha: Vec<Vec<f64>>,
    /// `beta[layer][node]` is $\hat\beta$.
    beta: Vec<Vec<f64>>,
    /// `scale[layer]` is $c_{layer}$, the factor of the forward pass in that layer (`1.0` for the
    /// root's layer). One of them is zero exactly when the diagram has no weighted path.
    scale: Vec<f64>,
}

impl Scaled {
    /// $\ln \mathrm{WMC}$, `-inf` if the diagram has no weighted path.
    fn log_wmc(&self) -> f64 {
        self.scale.iter().map(|c| c.ln()).sum()
    }
}

/// Panics, with a message naming the problem, if the diagram does not have the shape that the
/// passes rely on (see the module documentation). Walks the whole diagram, so it only runs in
/// debug builds.
fn assert_well_formed(mdd: &Mdd) {
    let last_layer = mdd.sink().0;
    assert_eq!(
        last_layer + 1,
        mdd.number_layers(),
        "the sink is in layer {last_layer} but the diagram has {} layers: the sink must be in the \
         last one",
        mdd.number_layers()
    );
    assert_eq!(
        mdd.number_nodes_in_layer(0),
        1,
        "the root layer must have exactly one node"
    );
    assert_eq!(
        mdd.number_nodes_in_layer(last_layer),
        1,
        "the sink layer must have exactly one node"
    );
    assert_eq!(
        mdd.root(),
        NodeIndex(0, 0),
        "the root must be node 0 of layer 0"
    );
    assert_eq!(
        mdd.sink(),
        NodeIndex(last_layer, 0),
        "the sink must be node 0 of the last layer"
    );
    for layer in 0..last_layer {
        for node in mdd.nodes_in_layer(layer) {
            for edge in mdd[node].iter_children() {
                assert!(
                    mdd[edge].is_active(),
                    "node {node:?} lists the deactivated edge {edge:?}: the diagram was not cleaned"
                );
                assert_eq!(
                    mdd[edge].to().0,
                    layer + 1,
                    "edge {edge:?} leaves layer {layer} but does not enter layer {}",
                    layer + 1
                );
            }
        }
    }
}

/// Runs both passes. Panics if `weights` does not have one row per layer but the sink's.
fn scaled_forward_backward(mdd: &Mdd, weights: &[Vec<f64>]) -> Scaled {
    if cfg!(debug_assertions) {
        assert_well_formed(mdd);
    }
    let last_layer = mdd.sink().0;
    if last_layer != weights.len() {
        panic!(
            "Calling the passes on an MDD structure with {} layers and {} layer's weights",
            last_layer,
            weights.len()
        );
    }

    // Forward. The root is the only node of layer 0 and carries all the weight, 1.
    let mut alpha: Vec<Vec<f64>> = Vec::with_capacity(last_layer + 1);
    let mut scale = vec![1.0];
    alpha.push(vec![1.0]);
    for layer in 0..last_layer {
        let mut next_alpha = vec![0.0; mdd.number_nodes_in_layer(layer + 1)];
        for node in mdd.nodes_in_layer(layer) {
            let mass = alpha[layer][node.1];
            // No mass can go through this node.
            if mass == 0.0 {
                continue;
            }
            for edge in mdd[node].iter_children() {
                let value = mdd[edge].assignment();
                let weight = weights[layer][value.0];
                next_alpha[mdd[edge].to().1] += mass * weight;
            }
        }
        let total: f64 = next_alpha.iter().sum();
        if total > 0.0 {
            for mass in &mut next_alpha {
                *mass /= total;
            }
        }
        scale.push(total);
        alpha.push(next_alpha);
    }

    // Backward, with the constants of the forward pass.
    let mut beta: Vec<Vec<f64>> = vec![Vec::new(); last_layer + 1];
    beta[last_layer] = vec![1.0];
    for layer in (0..last_layer).rev() {
        let mut layer_beta = vec![0.0; mdd.number_nodes_in_layer(layer)];
        if scale[layer + 1] > 0.0 {
            for node in mdd.nodes_in_layer(layer) {
                let mut mass = 0.0;
                for edge in mdd[node].iter_children() {
                    let value = mdd[edge].assignment();
                    let weight = weights[layer][value.0];
                    mass += weight * beta[layer + 1][mdd[edge].to().1];
                }
                layer_beta[node.1] = mass / scale[layer + 1];
            }
        }
        beta[layer] = layer_beta;
    }

    Scaled { alpha, beta, scale }
}

/// The logarithm of the weighted model count of `mdd`: the total weight of its root-to-sink
/// paths, where an edge of value `v` in `layer` weighs `weights[layer][v]`. It is `-inf` if
/// no path has a positive weight, in particular if `mdd` is unsatisfiable.
///
/// `weights` is indexed by layer of `mdd`, one row per layer but the sink's (see the
/// [module documentation](self)). The row of a layer needs an entry for every value of the
/// variable it branches on.
///
/// # Panics
///
/// Panics if `weights` does not have one row per layer but the sink's, and, when running, if a
/// row is shorter than the largest value of its layer. Debug builds also panic if the diagram is
/// not well formed.
pub fn log_wmc(mdd: &Mdd, weights: &[Vec<f64>]) -> f64 {
    if mdd.is_unsat() {
        return f64::NEG_INFINITY;
    }
    scaled_forward_backward(mdd, weights).log_wmc()
}

/// The weighted model count of `mdd`, see [`log_wmc`]. It underflows to zero when the count is
/// below the smallest `f64`: prefer [`log_wmc`] for long diagrams.
pub fn wmc(mdd: &Mdd, weights: &[Vec<f64>]) -> f64 {
    log_wmc(mdd, weights).exp()
}

/// The logarithm of the weighted model count and its gradient: `gradient[layer][value]` is
/// $\partial \ln \mathrm{WMC} / \partial w_{layer,value}$, the derivative of the logarithm
/// of the count with respect to `weights[layer][value]`. See the [module documentation](self) for
/// the derivation.
///
/// Multiplied by the weight, the gradient is the probability that the variable takes the value
/// given that the constraint holds. Divided by its sum over the values of the layer it is the
/// belief of the constraint, which does not depend on the weights of the layer itself.
///
/// The gradient has the shape of `weights`. If no path has a positive weight (in particular if
/// `mdd` is unsatisfiable) the count is `-inf` and the gradient is zero.
///
/// # Panics
///
/// Under the same conditions as [`log_wmc`].
pub fn log_wmc_and_gradient(mdd: &Mdd, weights: &[Vec<f64>]) -> (f64, Vec<Vec<f64>>) {
    let mut gradient: Vec<Vec<f64>> = weights.iter().map(|row| vec![0.0; row.len()]).collect();
    if mdd.is_unsat() {
        return (f64::NEG_INFINITY, gradient);
    }
    let scaled = scaled_forward_backward(mdd, weights);
    let log_count = scaled.log_wmc();
    let last_layer = mdd.sink().0;
    if log_count == f64::NEG_INFINITY {
        return (log_count, gradient);
    }

    for (layer, row) in gradient.iter_mut().enumerate().take(last_layer) {
        let factor = 1.0 / scaled.scale[layer + 1];
        for node in mdd.nodes_in_layer(layer) {
            let alpha = scaled.alpha[layer][node.1];
            if alpha == 0.0 {
                continue;
            }
            for edge in mdd[node].iter_children() {
                let value = mdd[edge].assignment();
                row[value.0] += factor * alpha * scaled.beta[layer + 1][mdd[edge].to().1];
            }
        }
    }
    (log_count, gradient)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::constraints::{all_different, not_equals, sum};
    use crate::mdd::arena::{MddArena, compile_constraint, nominal_domain};
    use crate::mdd::heuristics::{
        CompilationHeuristic, MergeHeuristic, OrderingHeuristic, SelectHeuristic,
    };
    use crate::mdd::{Compiler, Mdd};
    use crate::mdd::{Edge, EdgeIndex, Node};
    use crate::modelling::{ConstraintIndex, Problem, ValueIndex};
    use std::sync::Arc;

    /// The exact diagram of `constraints`.
    fn build_mdd(problem: Arc<Problem>, constraints: &[ConstraintIndex]) -> Mdd {
        let mut compiler = Compiler::new(
            problem,
            CompilationHeuristic {
                ordering: OrderingHeuristic::MinDomMaxLinked,
                merge: MergeHeuristic::LessRelaxed,
                selection: SelectHeuristic::Greedy,
            },
            constraints,
        );
        compiler.refine(usize::MAX);
        compiler.into_mdd()
    }

    /// The weighted count by enumeration: the sum, over the assignments of the problem (whose
    /// domains are `0..d`) that satisfy every constraint and agree with `decided` on the values
    /// of `assignment`, of the product of the weights of their values. `weights` is indexed by
    /// variable.
    fn brute_force(
        problem: &Problem,
        weights: &[Vec<f64>],
        decided: &[bool],
        assignment: &[ValueIndex],
    ) -> f64 {
        let mut total = 0.0;
        let mut tuple = vec![0isize; problem.number_variables()];
        loop {
            let agrees =
                (0..tuple.len()).all(|v| !decided[v] || assignment[v].0 as isize == tuple[v]);
            if agrees && problem.is_solution(&tuple) {
                total += (0..tuple.len())
                    .map(|v| weights[v][tuple[v] as usize])
                    .product::<f64>();
            }
            // Next tuple, odometer style.
            let mut position = 0;
            loop {
                if position == tuple.len() {
                    return total;
                }
                tuple[position] += 1;
                if (tuple[position] as usize) < weights[position].len() {
                    break;
                }
                tuple[position] = 0;
                position += 1;
            }
        }
    }

    /// The weights of the layers of `mdd`, from weights indexed by variable.
    fn by_layer(mdd: &Mdd, by_variable: &[Vec<f64>]) -> Vec<Vec<f64>> {
        (0..mdd.number_layers() - 1)
            .map(|layer| by_variable[mdd.decision_at_layer(layer).0].clone())
            .collect()
    }

    /// Arbitrary, distinct weights for `variables` variables over `domain` values.
    fn some_weights(variables: usize, domain: usize) -> Vec<Vec<f64>> {
        (0..variables)
            .map(|v| {
                (0..domain)
                    .map(|d| 0.1 + 0.07 * ((3 * v + 5 * d) % 11) as f64)
                    .collect()
            })
            .collect()
    }

    fn brute_force_not_equals(probs: &[f64], domain_size: usize) -> f64 {
        let mut brute = 0.0;
        for a in 0..domain_size {
            for b in 0..domain_size {
                if a != b {
                    brute += probs[a] * probs[domain_size + b];
                }
            }
        }
        brute
    }

    /// `layers` layers of one node each, plus the sink's, with an edge for every value of
    /// `0..values` between two consecutive nodes: a diagram built by hand, to feed the passes
    /// diagrams that the compiler never produces.
    fn chain(layers: usize, values: usize) -> Mdd {
        let nodes = (0..=layers)
            .map(|layer| vec![Node::new(layer, 0, false)])
            .collect();
        let mut diagram = Mdd::from_parts(nodes, vec![Vec::new(); layers]);
        for layer in 0..layers {
            for value in 0..values {
                add_edge(
                    &mut diagram,
                    layer,
                    NodeIndex(layer, 0),
                    NodeIndex(layer + 1, 0),
                    value,
                );
            }
        }
        diagram
    }

    /// Adds an edge leaving `layer` and lists it among the children of `from`.
    fn add_edge(diagram: &mut Mdd, layer: usize, from: NodeIndex, to: NodeIndex, value: usize) {
        let index = EdgeIndex(layer, diagram.edges[layer].len());
        diagram.edges[layer].push(Edge::new(from, to, ValueIndex(value)));
        diagram.nodes[from.0][from.1].add_child_edge(index);
    }

    /// A second node in `layer`.
    fn add_node(diagram: &mut Mdd, layer: usize) {
        let index = diagram.nodes[layer].len();
        diagram.nodes[layer].push(Node::new(layer, index, false));
    }

    #[test]
    fn wmc_matches_brute_force_for_not_equals() {
        let mut problem = Problem::default();
        let x = problem.add_variable(vec![0, 1, 2]);
        let y = problem.add_variable(vec![0, 1, 2]);
        not_equals(&mut problem, x, y);
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(problem, &constraints);

        let weights = vec![vec![0.2, 0.5, 0.3], vec![0.1, 0.3, 0.6]];
        let expected = brute_force_not_equals(&weights.concat(), 3);
        assert!((wmc(&mdd, &weights) - expected).abs() < 1e-9);
        assert!((log_wmc(&mdd, &weights) - expected.ln()).abs() < 1e-9);
    }

    #[test]
    fn unit_weights_count_the_solutions() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(4, vec![0, 1, 2, 3]);
        all_different(&mut problem, vars);
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(problem, &constraints);

        assert!((wmc(&mdd, &vec![vec![1.0; 4]; 4]) - 24.0).abs() < 1e-9);
        assert_eq!(mdd.count_solutions().to_string(), "24");
    }

    #[test]
    fn wmc_matches_brute_force_on_several_constraints() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(4, vec![0, 1, 2]);
        all_different(&mut problem, vars[..3].to_vec());
        not_equals(&mut problem, vars[2], vars[3]);
        sum(&mut problem, vars.clone(), 4);
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(Arc::clone(&problem), &constraints);

        let weights = some_weights(4, 3);
        let expected = brute_force(&problem, &weights, &[false; 4], &[ValueIndex(0); 4]);
        assert!(expected > 0.0);
        assert!((wmc(&mdd, &by_layer(&mdd, &weights)) - expected).abs() < 1e-9);
    }

    #[test]
    fn an_unsatisfiable_diagram_has_no_weight_and_no_gradient() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(3, vec![0, 1]);
        all_different(&mut problem, vars);
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(problem, &constraints);
        assert!(mdd.is_unsat());

        let weights = vec![vec![0.5, 0.5]; 3];
        let (log_count, gradient) = log_wmc_and_gradient(&mdd, &weights);
        assert_eq!(log_count, f64::NEG_INFINITY);
        assert_eq!(wmc(&mdd, &weights), 0.0);
        assert_eq!(gradient, vec![vec![0.0; 2]; 3]);
    }

    #[test]
    fn weights_that_forbid_every_solution_give_a_zero_count() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(2, vec![0, 1]);
        not_equals(&mut problem, vars[0], vars[1]);
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(Arc::clone(&problem), &constraints);

        // Both variables are forced to 0, which `not_equals` rules out.
        let by_variable = vec![vec![1.0, 0.0], vec![1.0, 0.0]];
        let weights = by_layer(&mdd, &by_variable);
        let (log_count, gradient) = log_wmc_and_gradient(&mdd, &weights);
        assert_eq!(log_count, f64::NEG_INFINITY);
        assert_eq!(gradient, vec![vec![0.0; 2]; 2]);
    }

    #[test]
    fn the_scaled_passes_meet_at_every_layer() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(4, vec![0, 1, 2]);
        all_different(&mut problem, vars[..3].to_vec());
        sum(&mut problem, vars.clone(), 4);
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(problem, &constraints);

        let weights = by_layer(&mdd, &some_weights(4, 3));
        let scaled = scaled_forward_backward(&mdd, &weights);
        assert!(scaled.log_wmc() > f64::NEG_INFINITY);
        // Every path crosses each layer exactly once: the paths through the nodes of a layer add
        // up to the whole count, which is 1 once both passes are scaled.
        for layer in 0..mdd.number_layers() {
            let through: f64 = scaled.alpha[layer]
                .iter()
                .zip(&scaled.beta[layer])
                .map(|(a, b)| a * b)
                .sum();
            assert!((through - 1.0).abs() < 1e-9, "layer {layer}: {through}");
            assert!((scaled.alpha[layer].iter().sum::<f64>() - 1.0).abs() < 1e-9);
        }
    }

    #[test]
    fn gradient_is_the_derivative_of_the_log_count() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(3, vec![0, 1, 2]);
        all_different(&mut problem, vars.clone());
        not_equals(&mut problem, vars[0], vars[1]);
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(problem, &constraints);

        let weights = vec![
            vec![0.2, 0.5, 0.3],
            vec![0.1, 0.3, 0.6],
            vec![0.4, 0.4, 0.2],
        ];
        let (log_count, grad) = log_wmc_and_gradient(&mdd, &weights);
        assert!((log_wmc(&mdd, &weights) - log_count).abs() < 1e-12);
        assert_eq!(grad.len(), weights.len());

        let eps = 1e-6;
        for layer in 0..weights.len() {
            for value in 0..weights[layer].len() {
                let mut bumped = weights.clone();
                bumped[layer][value] += eps;
                let finite_diff = (log_wmc(&mdd, &bumped) - log_count) / eps;
                assert!(
                    (finite_diff - grad[layer][value]).abs() < 1e-4,
                    "layer={layer} value={value} analytic={} finite_diff={finite_diff}",
                    grad[layer][value],
                );
            }
        }
    }

    #[test]
    fn gradient_times_the_weight_is_the_probability_of_the_value() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(3, vec![0, 1, 2]);
        all_different(&mut problem, vars.clone());
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(Arc::clone(&problem), &constraints);

        let by_variable = some_weights(3, 3);
        let weights = by_layer(&mdd, &by_variable);
        let (_, grad) = log_wmc_and_gradient(&mdd, &weights);
        let total = brute_force(&problem, &by_variable, &[false; 3], &[ValueIndex(0); 3]);
        for layer in 0..3 {
            let variable = mdd.decision_at_layer(layer).0;
            let mut layer_sum = 0.0;
            for value in 0..3 {
                let mut decided = [false; 3];
                decided[variable] = true;
                let mut assignment = [ValueIndex(0); 3];
                assignment[variable] = ValueIndex(value);
                let joint = brute_force(&problem, &by_variable, &decided, &assignment);
                let probability = weights[layer][value] * grad[layer][value];
                assert!((probability - joint / total).abs() < 1e-9);
                layer_sum += probability;
            }
            assert!((layer_sum - 1.0).abs() < 1e-9);
        }
    }

    #[test]
    fn a_structure_shared_through_the_arena_counts_with_its_own_order() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(4, vec![0, 1, 2]);
        // The scope is given in a different order than the one the diagram will branch in.
        let constraint = sum(&mut problem, vec![vars[3], vars[1], vars[0], vars[2]], 5);
        let problem = Arc::new(problem);
        let compiled = compile_constraint(
            &MddArena::new(),
            &problem,
            constraint,
            &OrderingHeuristic::MinDomMaxLinked,
            &nominal_domain(3),
            usize::MAX,
        );

        let weights = some_weights(4, 3);
        let expected = brute_force(&problem, &weights, &[false; 4], &[ValueIndex(0); 4]);
        assert!(expected > 0.0);
        let by_layer: Vec<Vec<f64>> = (0..compiled.order.len())
            .map(|layer| weights[compiled.order[layer].0].clone())
            .collect();
        assert!((wmc(&compiled, &by_layer) - expected).abs() < 1e-9);
    }

    #[test]
    fn a_count_below_the_smallest_float_is_still_known_through_its_logarithm() {
        // 400 layers whose two values weigh 0.001 each: WMC = 0.002^400 = 10^-1079.
        let diagram = chain(400, 2);
        let weights = vec![vec![1e-3, 1e-3]; 400];

        let (log_count, gradient) = log_wmc_and_gradient(&diagram, &weights);
        assert!((log_count - 400.0 * 0.002f64.ln()).abs() < 1e-9);
        assert_eq!(wmc(&diagram, &weights), 0.0);
        // d(ln WMC) / d(weight) = 1 / (sum of the two weights of the layer).
        for row in &gradient {
            for &g in row {
                assert!((g - 500.0).abs() < 1e-6, "{g}");
            }
        }
    }

    #[test]
    #[should_panic(expected = "layer's weights")]
    fn too_few_rows_of_weights_are_reported() {
        let mut problem = Problem::default();
        let vars = problem.add_variables(3, vec![0, 1, 2]);
        all_different(&mut problem, vars);
        let problem = Arc::new(problem);
        let constraints: Vec<ConstraintIndex> = problem.iter_constraints().collect();
        let mdd = build_mdd(problem, &constraints);
        wmc(&mdd, &[vec![1.0; 3], vec![1.0; 3]]);
    }

    #[test]
    fn a_hand_built_chain_is_well_formed() {
        let diagram = chain(3, 2);
        assert_well_formed(&diagram);
        assert!((wmc(&diagram, &vec![vec![1.0, 1.0]; 3]) - 8.0).abs() < 1e-9);
    }

    #[test]
    #[cfg(debug_assertions)]
    #[should_panic(expected = "root layer must have exactly one node")]
    fn a_second_root_is_reported() {
        let mut diagram = chain(2, 2);
        add_node(&mut diagram, 0);
        assert_well_formed(&diagram);
    }

    #[test]
    #[cfg(debug_assertions)]
    #[should_panic(expected = "sink layer must have exactly one node")]
    fn a_second_sink_is_reported() {
        let mut diagram = chain(2, 2);
        add_node(&mut diagram, 2);
        assert_well_formed(&diagram);
    }

    #[test]
    #[cfg(debug_assertions)]
    #[should_panic(expected = "deactivated edge")]
    fn a_listed_deactivated_edge_is_reported() {
        let mut diagram = chain(2, 2);
        diagram.edges[0][1].deactivate();
        assert_well_formed(&diagram);
    }

    #[test]
    #[cfg(debug_assertions)]
    #[should_panic(expected = "does not enter layer")]
    fn an_edge_that_skips_a_layer_is_reported() {
        let mut diagram = chain(3, 1);
        add_edge(&mut diagram, 0, NodeIndex(0, 0), NodeIndex(2, 0), 1);
        assert_well_formed(&diagram);
    }

    #[test]
    #[cfg(debug_assertions)]
    #[should_panic(expected = "root layer must have exactly one node")]
    fn the_passes_check_the_diagram_in_debug_builds() {
        let mut diagram = chain(2, 2);
        add_node(&mut diagram, 0);
        wmc(&diagram, &vec![vec![1.0, 1.0]; 2]);
    }
}