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
//! The local searches driven by a trained network: NeuralPrior, which decodes the logits of the
//! network, and NeuralPriorBP, which combines them with belief propagation over the constraints.
//!
//! # Meaning
//!
//! The network reads the current assignments of a batch and returns a logit per value of every
//! variable: a prior over what each freed variable should become. [`NeuralPriorSearch`] takes
//! the most likely value (or samples one) from it. [`NeuralPriorBpSearch`] treats it as the prior
//! of belief propagation over the decomposed MDDs of the constraints, so the values it chooses
//! agree with the constraints.
//!
//! Both are generic over the network and the [`Batch`] it reads, so any architecture of
//! [`crate::neural`] can drive them. What problems a batch can hold together is the batch type's
//! requirement ([`Batch::check_problems`]): setup asks it, and nothing else here knows it.
//!
//! # How it works
//!
//! The engine keeps the assignments on the CPU. A step destroys the pending rows there, copies the
//! assignments into a `[rows, n]` tensor, evaluates the network on the whole batch, and turns its
//! logits into the next assignments. The two searches share everything but that last part.
//!
//! # Design notes
//!
//! - **The logits are the whole interface to the network.** Nothing here depends on how they are
//!   computed, which is what lets another network be plugged in.

use std::marker::PhantomData;
use std::path::Path;
use std::sync::Arc;

use burn::tensor::activation::softmax;
use burn::tensor::backend::Backend;
use burn::tensor::{Distribution, Int, Tensor};

use rand::rngs::StdRng;

use crate::destroy::{DestroyKind, DestroyOperator};
use crate::mdd::arena::MddCompilationConfig;
use crate::modelling::Problem;
use crate::modelling::Solution;
use crate::neural::consformer::{ConsFormer, ConsFormerBatch, ConsFormerConfig};
use crate::neural::{Batch, Network, load_network};
use crate::sampling::DecodeMode;
use crate::utils::tensor::{rows_to_tensor, to_rows};

use super::repair::{BatchRepair, MddRepair};
use super::{
    LocalSearch, SearchArchitecture, SearchConfig, SearchState, SolveConfig, destroy_rows,
    row_seeds, run_batched,
};

/// The settings of the NeuralPrior local search, which decodes the logits of the network.
#[derive(Debug, Clone)]
pub struct NeuralPriorSearchConfig {
    /// Destroy operator choosing the variables to free at each iteration.
    pub destroy_kind: DestroyKind,
    /// Fraction of the variables the destroy operator frees at each iteration, in `[0, 1]`.
    pub destroy_fraction: f64,
    /// Sample values from the softmax of the logits instead of taking the most likely one.
    pub stochastic: bool,
    /// Softmax temperature when sampling; unused by greedy decoding.
    pub temperature: f64,
}

impl Default for NeuralPriorSearchConfig {
    fn default() -> Self {
        Self {
            destroy_kind: DestroyKind::default(),
            destroy_fraction: 1.0,
            stochastic: false,
            temperature: 1.0,
        }
    }
}

/// The settings of the NeuralPriorBP local search, which uses the probabilities of the network as
/// the prior of belief propagation over the decomposed MDDs of the constraints.
#[derive(Debug, Clone)]
pub struct NeuralPriorBpSearchConfig {
    /// Destroy operator choosing the variables to free at each iteration.
    pub destroy_kind: DestroyKind,
    /// Fraction of the variables the destroy operator frees at each iteration, in `[0, 1]`.
    pub destroy_fraction: f64,
    /// Sample values from the refined marginals instead of taking the most likely one.
    pub stochastic: bool,
    /// Number of belief-propagation rounds per iteration.
    pub bp_iterations: usize,
}

impl Default for NeuralPriorBpSearchConfig {
    fn default() -> Self {
        Self {
            destroy_kind: DestroyKind::default(),
            destroy_fraction: 1.0,
            stochastic: false,
            bp_iterations: 1,
        }
    }
}

/// The network and the destroy operator, which the two searches share.
struct Core<B: Backend, Ba, N> {
    /// The trained network evaluated at each step.
    network: N,
    /// Heuristic choosing the variables to free.
    destroy_op: Box<dyn DestroyOperator>,
    /// Device used (cpu or gpu).
    device: B::Device,
    /// The kind of batch the network reads.
    batch: PhantomData<fn() -> Ba>,
}

/// What a step learns before it chooses the new values.
struct Proposal<B: Backend> {
    /// The variables each row frees.
    destroyed: Vec<Vec<usize>>,
    /// The assignments as the network read them, `[rows, n]`.
    current: Tensor<B, 2, Int>,
    /// 1 for the freed positions, `[rows, n]`.
    destroy_mask: Tensor<B, 2, Int>,
    /// The logits of the network, `[rows, n, domain_size]`.
    logits: Tensor<B, 3>,
}

impl<B, Ba, N> Core<B, Ba, N>
where
    B: Backend,
    Ba: Batch<B>,
    N: Network<B, Ba>,
{
    /// Destroys the pending rows and evaluates the network on the batch.
    fn propose(
        &self,
        problems: &[Arc<Problem>],
        assignments: &[Vec<isize>],
        pending: &[bool],
        rng: &mut StdRng,
    ) -> Proposal<B> {
        let rows = problems.len();
        // The batch type has accepted the problems at setup, so they share their variables.
        let n = problems[0].number_variables();
        let destroyed = destroy_rows(
            self.destroy_op.as_ref(),
            problems,
            assignments,
            pending,
            rng,
        );

        // The positions the network must re-predict, which it applies in its architecture.
        let mut mask_data = vec![0i64; rows * n];
        for (row, free) in destroyed.iter().enumerate() {
            for &var in free {
                mask_data[row * n + var] = 1;
            }
        }
        let destroy_mask: Tensor<B, 2, Int> =
            Tensor::<B, 1, Int>::from_data(mask_data.as_slice(), &self.device).reshape([rows, n]);

        let current = rows_to_tensor(assignments, n, &self.device);
        let batch = Ba::for_assignments(
            problems,
            current.clone(),
            destroy_mask.clone(),
            &self.device,
        );
        let logits = self.network.forward(&batch);
        Proposal {
            destroyed,
            current,
            destroy_mask,
            logits,
        }
    }
}

/// How the logits of the network become values directly: the highest logit, or a sample of the
/// softmax of the logits at a temperature.
enum Decoder {
    /// Greedy / MAP decoding: takes the most likely value per variable.
    Argmax,
    /// Stochastic decoding: samples a value per variable from `softmax(logits / temperature)`.
    Sampling {
        /// Temperature of the softmax.
        temperature: f64,
    },
}

impl Decoder {
    /// `logits` has shape `[rows, variables, domain_size]`. Only the positions flagged in
    /// `destroy_mask` change; everywhere else `current` is kept, whatever the logits say.
    fn decode<B: Backend>(
        &self,
        logits: Tensor<B, 3>,
        destroy_mask: Tensor<B, 2, Int>,
        current: Tensor<B, 2, Int>,
    ) -> Tensor<B, 2, Int> {
        let proposed: Tensor<B, 2, Int> = match self {
            Decoder::Argmax => logits.argmax(2).squeeze_dim(2),
            Decoder::Sampling { temperature } => {
                let device = logits.device();
                let u = Tensor::<B, 3>::random(
                    logits.dims(),
                    Distribution::Uniform(1e-20, 1.0),
                    &device,
                );
                let neg_log_u = -u.log(); // -ln(u), > 0 since u in (0, 1)
                let gumbel = -neg_log_u.log(); // Gumbel(0, 1) noise: -ln(-ln(u))
                let scaled = logits.div_scalar(*temperature) + gumbel;
                scaled.argmax(2).squeeze_dim(2)
            }
        };
        current.mask_where(destroy_mask.equal_elem(1), proposed)
    }
}

/// The NeuralPrior local search: a trained network whose logits are decoded directly.
pub struct NeuralPriorSearch<B: Backend, Ba, N> {
    /// The network and destroy operator.
    core: Core<B, Ba, N>,
    /// How the logits become values.
    decoder: Decoder,
}

impl<B, Ba, N> NeuralPriorSearch<B, Ba, N>
where
    B: Backend,
    Ba: Batch<B>,
    N: Network<B, Ba>,
{
    /// Builds the search from its network and settings. Nothing depends on the problems being
    /// solved, so it can be reused across batches.
    pub fn new(network: N, config: &NeuralPriorSearchConfig, device: B::Device) -> Self {
        Self {
            core: Core {
                network,
                destroy_op: config.destroy_kind.build(config.destroy_fraction),
                device,
                batch: PhantomData,
            },
            decoder: if config.stochastic {
                Decoder::Sampling {
                    temperature: config.temperature,
                }
            } else {
                Decoder::Argmax
            },
        }
    }
}

impl<B, Ba, N> SearchArchitecture for NeuralPriorSearch<B, Ba, N>
where
    B: Backend,
    Ba: Batch<B>,
    N: Network<B, Ba>,
{
    fn setup<'a>(&'a self, problems: &[Arc<Problem>]) -> Result<Box<dyn SearchState + 'a>, String> {
        Ba::check_problems(problems)?;
        Ok(Box::new(NeuralPriorState { search: self }))
    }
}

/// The state of a NeuralPrior search over a batch: it holds nothing besides the engine's
/// assignments.
struct NeuralPriorState<'a, B: Backend, Ba, N> {
    /// The components of the search.
    search: &'a NeuralPriorSearch<B, Ba, N>,
}

impl<B, Ba, N> SearchState for NeuralPriorState<'_, B, Ba, N>
where
    B: Backend,
    Ba: Batch<B>,
    N: Network<B, Ba>,
{
    fn step(
        &mut self,
        problems: &[Arc<Problem>],
        assignments: &mut [Vec<isize>],
        pending: &[bool],
        rng: &mut StdRng,
    ) {
        let proposal = self
            .search
            .core
            .propose(problems, assignments, pending, rng);
        let next =
            self.search
                .decoder
                .decode(proposal.logits, proposal.destroy_mask, proposal.current);
        let n = assignments.first().map_or(0, Vec::len);
        for (row, values) in assignments
            .iter_mut()
            .zip(to_rows(&next, problems.len(), n))
        {
            *row = values;
        }
    }
}

/// The NeuralPriorBP local search: a trained network whose probabilities are the prior of belief
/// propagation over the MDDs of the constraints.
pub struct NeuralPriorBpSearch<B: Backend, Ba, N> {
    /// The network and destroy operator.
    core: Core<B, Ba, N>,
    /// Number of belief-propagation rounds per iteration.
    bp_iterations: usize,
    /// How a value is taken from a marginal.
    mode: DecodeMode,
}

impl<B, Ba, N> NeuralPriorBpSearch<B, Ba, N>
where
    B: Backend,
    Ba: Batch<B>,
    N: Network<B, Ba>,
{
    /// Builds the search from its network and settings. Nothing depends on the problems being
    /// solved, so it can be reused across batches.
    pub fn new(network: N, config: &NeuralPriorBpSearchConfig, device: B::Device) -> Self {
        Self {
            core: Core {
                network,
                destroy_op: config.destroy_kind.build(config.destroy_fraction),
                device,
                batch: PhantomData,
            },
            bp_iterations: config.bp_iterations,
            mode: if config.stochastic {
                DecodeMode::Sample
            } else {
                DecodeMode::Greedy
            },
        }
    }
}

impl<B, Ba, N> SearchArchitecture for NeuralPriorBpSearch<B, Ba, N>
where
    B: Backend,
    Ba: Batch<B>,
    N: Network<B, Ba>,
{
    /// Checks the problems before compiling their constraints, which is the costly part.
    fn setup<'a>(&'a self, problems: &[Arc<Problem>]) -> Result<Box<dyn SearchState + 'a>, String> {
        Ba::check_problems(problems)?;
        let repair = MddRepair::new(
            MddCompilationConfig::default(),
            MddRepair::domain_size_covering(problems),
            self.mode,
            self.bp_iterations,
        );
        Ok(Box::new(NeuralPriorBpState {
            search: self,
            repair: BatchRepair::new(repair, problems),
        }))
    }
}

/// The state of a NeuralPriorBP search over a batch.
struct NeuralPriorBpState<'a, B: Backend, Ba, N> {
    /// The components of the search.
    search: &'a NeuralPriorBpSearch<B, Ba, N>,
    /// Repairs the freed variables of the rows, with the alphabet of this batch.
    repair: BatchRepair,
}

impl<B, Ba, N> SearchState for NeuralPriorBpState<'_, B, Ba, N>
where
    B: Backend,
    Ba: Batch<B>,
    N: Network<B, Ba>,
{
    fn step(
        &mut self,
        problems: &[Arc<Problem>],
        assignments: &mut [Vec<isize>],
        pending: &[bool],
        rng: &mut StdRng,
    ) {
        let proposal = self
            .search
            .core
            .propose(problems, assignments, pending, rng);
        let seeds = row_seeds(problems.len(), rng);
        let [_, n, domain_width] = proposal.logits.dims();
        let probs: Vec<f32> = softmax(proposal.logits, 2)
            .into_data()
            .to_vec::<f32>()
            .expect("softmax output should be f32-convertible");
        self.repair.repair_rows(
            problems,
            assignments,
            &proposal.destroyed,
            &seeds,
            |row, variable, value| {
                // The network may predict fewer values than the batch's alphabet.
                if value < domain_width {
                    probs[row * n * domain_width + variable * domain_width + value] as f64
                } else {
                    0.0
                }
            },
        );
    }

    fn solved(&mut self, solved: &[bool]) {
        self.repair.solved(solved);
    }

    fn retain(&mut self, keep: &[bool]) {
        self.repair.retain(keep);
    }
}

/// Solves `problems` with the neural search of `config` and the ConsFormer network of the
/// checkpoint, on the best device found. See [`super::solve`].
pub(super) fn solve_with_consformer(
    problems: &[Arc<Problem>],
    config: &SolveConfig,
    checkpoint_dir: Option<&Path>,
    seed: u64,
) -> Result<Vec<Solution>, String> {
    let dir = checkpoint_dir.ok_or("a neural search needs a checkpoint_dir")?;
    crate::neural::dispatch_backend!(
        inference,
        solve_with_checkpoint,
        problems,
        config,
        dir,
        seed
    )
}

/// Loads the ConsFormer of the checkpoint on `device` and runs the neural search of `config`.
fn solve_with_checkpoint<B: Backend>(
    device: B::Device,
    problems: &[Arc<Problem>],
    config: &SolveConfig,
    dir: &Path,
    seed: u64,
) -> Result<Vec<Solution>, String> {
    // Building the network reads the variables of the problems, so they are checked first.
    ConsFormerBatch::<B>::check_problems(problems)?;
    let (_, network) =
        load_network::<B, ConsFormerConfig>(dir, problems, &device).map_err(|e| {
            format!(
                "failed to load network from checkpoint {}: {e}",
                dir.display()
            )
        })?;
    solve_with_network::<B, ConsFormerBatch<B>, ConsFormer<B>>(
        network, device, problems, config, seed,
    )
}

/// Solves `problems` with the neural search of `config` driven by `network` on `device`, in
/// batches (see [`super::run_batched`]). The network reads batches of type `Ba`, which decides
/// what problems may share a batch.
///
/// # Errors
///
/// When `config` is not a neural search, or when the network cannot search these problems
/// together.
pub fn solve_with_network<B, Ba, N>(
    network: N,
    device: B::Device,
    problems: &[Arc<Problem>],
    config: &SolveConfig,
    seed: u64,
) -> Result<Vec<Solution>, String>
where
    B: Backend,
    Ba: Batch<B>,
    N: Network<B, Ba> + 'static,
{
    let engine = match &config.search {
        SearchConfig::NeuralPrior(c) => LocalSearch::new(Box::new(
            NeuralPriorSearch::<B, Ba, N>::new(network, c, device),
        )),
        SearchConfig::NeuralPriorBp(c) => LocalSearch::new(Box::new(
            NeuralPriorBpSearch::<B, Ba, N>::new(network, c, device),
        )),
        SearchConfig::BeliefPropagation(_) => {
            return Err("the belief-propagation search uses no network".to_string());
        }
    };
    run_batched(&engine, problems, config.batch_size, config.budget, seed)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::constraints::all_different;
    use crate::local_search::{Budget, LocalSearch};
    use crate::neural::NetworkConfig;
    use crate::neural::consformer::{ConsFormer, ConsFormerBatch, ConsFormerConfig};
    use burn::backend::NdArray;
    use burn::backend::ndarray::NdArrayDevice;

    type Net = ConsFormer<NdArray>;
    type Batch = ConsFormerBatch<NdArray>;

    fn clique(size: usize, colours: usize) -> Arc<Problem> {
        let mut problem = Problem::default();
        let vars = problem.add_variables(size, (0..colours as isize).collect());
        all_different(&mut problem, vars);
        Arc::new(problem)
    }

    /// An untrained network for `problems`.
    fn network(problems: &[Arc<Problem>]) -> Net {
        let config = ConsFormerConfig::new(3, 4, 4, 1, 4, 0, 0.5, 1.0).with_num_layers(1);
        NetworkConfig::<NdArray>::init(&config, problems, &NdArrayDevice::default())
    }

    fn engines(problems: &[Arc<Problem>]) -> Vec<LocalSearch> {
        let device = NdArrayDevice::default();
        let prior = NeuralPriorSearch::<NdArray, Batch, Net>::new(
            network(problems),
            &NeuralPriorSearchConfig::default(),
            device,
        );
        let with_bp = NeuralPriorBpSearch::<NdArray, Batch, Net>::new(
            network(problems),
            &NeuralPriorBpSearchConfig::default(),
            device,
        );
        vec![
            LocalSearch::new(Box::new(prior)),
            LocalSearch::new(Box::new(with_bp)),
        ]
    }

    #[test]
    fn both_refuse_problems_with_different_numbers_of_variables() {
        let problems = vec![clique(3, 3), clique(4, 3)];
        for engine in engines(&problems[..1]) {
            let error = engine
                .run(&problems, Budget::default(), 0, None)
                .expect_err("the problems cannot be searched together");
            assert!(error.contains("same number of variables"), "{error}");
        }
    }

    #[test]
    fn both_stay_in_the_domain_over_a_batch() {
        let problems = vec![clique(3, 3), clique(3, 3)];
        for engine in engines(&problems) {
            let budget = Budget {
                iteration_limit: 30,
                ..Budget::default()
            };
            let solutions = engine.run(&problems, budget, 0, None).unwrap();
            assert_eq!(solutions.len(), 2);
            for solution in solutions {
                if let Some(values) = solution.assignment() {
                    assert!(values.iter().all(|v| (0..3).contains(v)));
                }
            }
        }
    }

    #[test]
    fn decoding_only_changes_the_flagged_positions() {
        let device = NdArrayDevice::default();
        // Every variable's logits favour value 2.
        let logits = Tensor::<NdArray, 3>::from_data(
            burn::tensor::TensorData::new([0.0f32, 0.0, 1.0].repeat(3), [1, 3, 3]),
            &device,
        );
        let mask = Tensor::<NdArray, 2, Int>::from_data([[1, 0, 1]], &device);
        let current = Tensor::<NdArray, 2, Int>::from_data([[0, 1, 0]], &device);
        let next = Decoder::Argmax.decode(logits, mask, current);
        let values: Vec<i64> = next.into_data().to_vec().unwrap();
        assert_eq!(values, [2, 1, 2]);
    }
}