lp-ip-craft-native 0.34.0

Native Rust LP/MIP engine: dual revised simplex on a sparse LU factorization, presolve, cuts and branch-and-bound
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
//! Opt-in primal reductions and optimum-preserving objective fixes.
use crate::{certified_cuts, dyadic::Dy, rational, *};
#[derive(Default)]
pub(crate) struct Stats {
    pub rounds: u64,
    pub bounds: u64,
    pub redundant: u64,
    pub forcing: u64,
    pub coefficients: u64,
    pub dual_fixed: u64,
    pub work: u64,
    pub conflicts: u64,
    pub probed: u64,
    pub probe_fixed: u64,
    pub probe_tightened: u64,
}
fn dy(v: f64) -> Option<Dy> {
    Dy::from_f64(v)
}
/// Exact i128 fast path; every emitted integer is exactly representable in f64.
fn tighten_integral(model: &Model, i: usize, sign: f64) -> Option<certified_cuts::Cut> {
    let a = model.matrix();
    let rhs = if sign > 0. {
        model.rows().upper[i]
    } else {
        -model.rows().lower[i]
    };
    let exact = |v: f64| v.is_finite() && v.fract() == 0. && v.abs() <= 2_147_483_648.;
    if !exact(rhs) {
        return None;
    }
    let mut terms = Vec::new();
    let mut maximum = 0i128;
    for k in a.pattern.indptr[i]..a.pattern.indptr[i + 1] {
        let j = a.pattern.indices[k];
        let c = sign * a.values[k];
        let bound = if c > 0. {
            model.variables().upper[j]
        } else {
            model.variables().lower[j]
        };
        if !exact(c) || !exact(bound) {
            return None;
        }
        let c = c as i128;
        maximum = maximum.checked_add(c.checked_mul(bound as i128)?)?;
        terms.push((j, c));
    }
    let mut bound = rhs as i128;
    let delta = maximum.checked_sub(bound)?;
    if delta <= 0 {
        return None;
    }
    let mut changed = false;
    for (j, c) in &mut terms {
        if model.kinds()[*j].is_integral()
            && model.variables().lower[*j] == 0.
            && model.variables().upper[*j] == 1.
            && c.abs() > delta
        {
            if *c > 0 {
                bound = maximum.checked_sub(*c)?;
                maximum = bound.checked_add(delta)?;
                *c = delta;
            } else {
                *c = -delta;
            }
            changed = true;
        }
    }
    if !changed
        || bound.abs() > 9_007_199_254_740_991
        || terms.iter().any(|t| t.1.abs() > 9_007_199_254_740_991)
    {
        return None;
    }
    Some(certified_cuts::Cut {
        terms: terms
            .into_iter()
            .filter(|t| t.1 != 0)
            .map(|(j, c)| (j, c as f64))
            .collect(),
        rhs: bound as f64,
        family: "tightening",
    })
}
fn tighten_row(model: &Model, i: usize, sign: f64) -> Option<certified_cuts::Cut> {
    if let Some(cut) = tighten_integral(model, i, sign) {
        return Some(cut);
    }
    // Exact dyadic fallback: the maximum activity, its excess over the side
    // and the replacement coefficient/RHS are sums and products of binary64
    // data, so no division is needed.
    let a = model.matrix();
    let bound = if sign > 0. {
        model.rows().upper[i]
    } else {
        -model.rows().lower[i]
    };
    let mut maximum = Dy::zero();
    let mut row = Vec::new();
    for k in a.pattern.indptr[i]..a.pattern.indptr[i + 1] {
        let j = a.pattern.indices[k];
        let c = sign * a.values[k];
        let b = if c > 0. {
            model.variables().upper[j]
        } else {
            model.variables().lower[j]
        };
        maximum = maximum.add(&dy(c)?.mul(&dy(b)?)?)?;
        row.push((j, c));
    }
    row.sort_by_key(|t| t.0);
    let delta = maximum.sub(&dy(bound)?)?;
    if !delta.is_positive() {
        return None;
    }
    for &(j, c) in &row {
        if !model.kinds()[j].is_integral()
            || model.variables().lower[j] != 0.
            || model.variables().upper[j] != 1.
            || dy(c.abs())?.cmp(&delta)? != std::cmp::Ordering::Greater
        {
            continue;
        }
        let (replacement, rhs) = if c > 0. {
            (delta.clone(), maximum.sub(&dy(c)?)?)
        } else {
            (delta.neg(), dy(bound)?)
        };
        let coefficients = row
            .iter()
            .map(|&(k, v)| Some((k, if k == j { replacement.clone() } else { dy(v)? })))
            .collect::<Option<Vec<_>>>()?;
        return certified_cuts::enclose(model, coefficients, &rhs, "tightening");
    }
    None
}
/// Binaries probed per presolve call, at most.
const PROBES: usize = 2000;
/// Probing on binaries with exact integer propagation. For each unfixed
/// binary, both values are propagated on a copy of the bounds. A value that
/// propagates to a conflict cannot occur in any feasible point, so the
/// binary is fixed to the other one. Otherwise every feasible point lies in
/// one of the two propagated boxes, so each column's bounds may shrink to
/// the hull of its two propagated intervals. Returns the new bounds, or
/// `None` when the model's own bounds conflict (left to the checked LP).
fn probe(
    model: &Model,
    feasibility: f64,
    budget: &Budget<'_>,
    stats: &mut Stats,
) -> Option<Bounds> {
    let mut base = model.variables().clone();
    let mut domain = propagate::Domain::new(model, true, feasibility);
    let mut changed = Vec::new();
    if !domain.apply(&mut base, &mut changed, budget) {
        return None;
    }
    let binaries: Vec<usize> = (0..model.n())
        .filter(|&j| model.kinds()[j].is_integral() && base.lower[j] == 0. && base.upper[j] == 1.)
        .take(PROBES)
        .collect();
    // Trials run in place on `base` and are undone from `kept`, a copy of
    // `base` between probes, over the columns they changed only; a probe
    // costs its propagation, not a copy of every bound.
    let mut kept = base.clone();
    // Reuse the direct index when two probe trails are large enough to make
    // repeated scans expensive. Each probed binary is visited once.
    let mut last = Vec::<[(usize, f64, f64); 2]>::new();
    for j in binaries {
        if budget.stop().is_some() {
            break;
        }
        if base.lower[j] == base.upper[j] {
            continue;
        }
        stats.probed += 1;
        let mut outcome: [Option<Vec<(usize, f64, f64)>>; 2] = [None, None];
        for (side, value) in [0., 1.].into_iter().enumerate() {
            base.lower[j] = value;
            base.upper[j] = value;
            let mut columns = vec![j];
            let feasible = domain.apply(&mut base, &mut columns, budget);
            if feasible {
                outcome[side] = Some(
                    columns
                        .iter()
                        .map(|&k| (k, base.lower[k], base.upper[k]))
                        .collect(),
                );
            }
            for &k in &columns {
                base.lower[k] = kept.lower[k];
                base.upper[k] = kept.upper[k];
            }
            domain.reset(&base, &columns);
        }
        let mut tightened = Vec::new();
        match outcome {
            [None, None] => return None,
            [Some(_), None] | [None, Some(_)] => {
                let value = if outcome[0].is_some() { 0. } else { 1. };
                base.lower[j] = value;
                base.upper[j] = value;
                tightened.push(j);
                stats.probe_fixed += 1;
            }
            [Some(zero), Some(one)] => {
                // A column one trial did not change keeps its bounds there.
                let indexed = zero.len() + one.len() >= 32;
                let stamp = j + 1;
                if indexed {
                    last.resize(model.n(), [(0, 0., 0.); 2]);
                    for (side, trial) in [&zero, &one].into_iter().enumerate() {
                        for &(k, lo, hi) in trial {
                            last[k][side] = (stamp, lo, hi);
                        }
                    }
                }
                let at = |trial: &[(usize, f64, f64)], side: usize, k: usize| {
                    if indexed {
                        let (seen, lo, hi) = last[k][side];
                        if seen == stamp {
                            (lo, hi)
                        } else {
                            (kept.lower[k], kept.upper[k])
                        }
                    } else {
                        trial
                            .iter()
                            .rev()
                            .find(|t| t.0 == k)
                            .map_or((kept.lower[k], kept.upper[k]), |t| (t.1, t.2))
                    }
                };
                for &(k, _, _) in zero.iter().chain(&one) {
                    let (l0, u0) = at(&zero, 0, k);
                    let (l1, u1) = at(&one, 1, k);
                    let (lo, hi) = (l0.min(l1), u0.max(u1));
                    if lo > base.lower[k] || hi < base.upper[k] {
                        base.lower[k] = base.lower[k].max(lo);
                        base.upper[k] = base.upper[k].min(hi);
                        tightened.push(k);
                        stats.probe_tightened += 1;
                    }
                }
            }
        }
        if !tightened.is_empty() && !domain.apply(&mut base, &mut tightened, budget) {
            return None;
        }
        for &k in &tightened {
            kept.lower[k] = base.lower[k];
            kept.upper[k] = base.upper[k];
        }
    }
    Some(base)
}
pub(crate) fn run(
    model: &mut Model,
    settings: &NativeOptions,
    options: &Options,
    cancel: &CancellationToken,
    began: Instant,
) -> Result<Stats> {
    let mut stats = Stats::default();
    rational::reset_work();
    let budget = Budget {
        start: began,
        settings,
        cancel,
        pivots: 0,
    };
    for _ in 0..settings.mip_presolve_rounds {
        if budget.stop().is_some() {
            break;
        }
        stats.rounds += 1;
        let before = model.clone();
        if settings.mip_presolve {
            let mut b = model.variables().clone();
            let mut changed = Vec::new();
            let mut exact = propagate::Domain::with_equalities(
                model,
                true,
                options.tolerances.feasibility,
                settings
                    .integer_equality_presolve
                    .then_some(&options.tolerances),
            );
            let mut mixed = mixed::Domain::new(model, true, options.tolerances.feasibility);
            for _ in 0..4 {
                let prev = changed.len();
                if !exact.apply(&mut b, &mut changed, &budget)
                    || !mixed.apply(model, &mut b, &mut changed, &budget)
                {
                    stats.conflicts += 1;
                    break;
                }
                if prev == changed.len() {
                    break;
                }
            }
            if stats.rounds == 1 && (0..model.n()).all(|j| b.lower[j] <= b.upper[j]) {
                let mut d = model.data().clone();
                d.variables = b.clone();
                if let Some(p) = probe(
                    &Model::new(d)?,
                    options.tolerances.feasibility,
                    &budget,
                    &mut stats,
                ) {
                    b = p;
                }
            }
            if (0..model.n()).all(|j| b.lower[j] <= b.upper[j]) {
                stats.bounds += (0..model.n())
                    .filter(|&j| {
                        b.lower[j] != model.variables().lower[j]
                            || b.upper[j] != model.variables().upper[j]
                    })
                    .count() as u64;
                let mut d = model.data().clone();
                d.variables = b;
                *model = Model::new(d)?;
            }
            let a = model.matrix();
            let mut d = model.data().clone();
            let active = presolve::active_rows(model, model.variables(), true);
            for (i, &keep) in active.iter().enumerate() {
                if !keep && (d.rows.lower[i].is_finite() || d.rows.upper[i].is_finite()) {
                    d.rows.lower[i] = f64::NEG_INFINITY;
                    d.rows.upper[i] = f64::INFINITY;
                    stats.redundant += 1;
                }
                let terms = (a.pattern.indptr[i]..a.pattern.indptr[i + 1]).map(|k| {
                    let j = a.pattern.indices[k];
                    let c = a.values[k];
                    (
                        c,
                        if c > 0. {
                            model.variables().upper[j]
                        } else {
                            model.variables().lower[j]
                        },
                    )
                });
                if let Some((l, u)) = incumbent::dot(terms, 0.) {
                    if l == u && l == model.rows().lower[i] {
                        stats.forcing += 1;
                    }
                }
            }
            *model = Model::new(d)?;
        }
        if settings.coefficient_tightening {
            // Each rewrite depends only on its own row and the column bounds,
            // so one pass collects them and rebuilds the matrix once.
            let mut rewrites = Vec::new();
            for i in 0..model.m() {
                if budget.stop().is_some() || rational::work() >= settings.cut_exact_work {
                    break;
                }
                let sign = if model.rows().lower[i] == f64::NEG_INFINITY
                    && model.rows().upper[i].is_finite()
                {
                    1.
                } else if model.rows().upper[i] == f64::INFINITY
                    && model.rows().lower[i].is_finite()
                {
                    -1.
                } else {
                    continue;
                };
                if let Some(cut) = tighten_row(model, i, sign) {
                    rewrites.push((i, sign, cut));
                }
            }
            if !rewrites.is_empty() {
                let mut data = model.data().clone();
                let a = model.matrix();
                let mut indices = Vec::new();
                let mut values = Vec::new();
                let mut ptr = vec![0];
                let mut next = rewrites.iter().peekable();
                for row in 0..model.m() {
                    match next.next_if(|r| r.0 == row) {
                        Some((_, sign, cut)) => {
                            for &(j, v) in &cut.terms {
                                indices.push(j);
                                values.push(sign * v);
                            }
                            if *sign > 0. {
                                data.rows.upper[row] = cut.rhs;
                            } else {
                                data.rows.lower[row] = -cut.rhs;
                            }
                        }
                        None => {
                            let range = a.pattern.indptr[row]..a.pattern.indptr[row + 1];
                            indices.extend_from_slice(&a.pattern.indices[range.clone()]);
                            values.extend_from_slice(&a.values[range]);
                        }
                    }
                    ptr.push(values.len());
                }
                data.matrix.pattern.indptr = ptr;
                data.matrix.pattern.indices = indices;
                data.matrix.values = values;
                *model = Model::new(data)?;
                stats.coefficients += rewrites.len() as u64;
            }
        }
        if settings.dual_presolve {
            let mut count = vec![0; model.n()];
            let mut only = vec![(0, 0.); model.n()];
            let a = model.matrix();
            for i in 0..model.m() {
                if !model.rows().lower[i].is_finite() && !model.rows().upper[i].is_finite() {
                    continue;
                }
                for k in a.pattern.indptr[i]..a.pattern.indptr[i + 1] {
                    if a.values[k] != 0. {
                        let j = a.pattern.indices[k];
                        count[j] += 1;
                        only[j] = (i, a.values[k]);
                    }
                }
            }
            let mut d = model.data().clone();
            for j in 0..model.n() {
                if count[j] > 1 || d.variables.lower[j] == d.variables.upper[j] {
                    continue;
                }
                let cost = oriented(model, model.objective()[j]);
                let down = count[j] == 0 || {
                    let (i, c) = only[j];
                    if c > 0. {
                        model.rows().lower[i] == f64::NEG_INFINITY
                    } else {
                        model.rows().upper[i] == f64::INFINITY
                    }
                };
                let up = count[j] == 0 || {
                    let (i, c) = only[j];
                    if c > 0. {
                        model.rows().upper[i] == f64::INFINITY
                    } else {
                        model.rows().lower[i] == f64::NEG_INFINITY
                    }
                };
                let target = if cost >= 0. && down {
                    d.variables.lower[j]
                } else if cost <= 0. && up {
                    d.variables.upper[j]
                } else {
                    continue;
                };
                let target = if model.kinds()[j].is_integral() {
                    if cost >= 0. && down {
                        target.ceil()
                    } else {
                        target.floor()
                    }
                } else {
                    target
                };
                if target.is_finite()
                    && target >= d.variables.lower[j]
                    && target <= d.variables.upper[j]
                {
                    d.variables.lower[j] = target;
                    d.variables.upper[j] = target;
                    stats.dual_fixed += 1;
                }
            }
            *model = Model::new(d)?;
        }
        if *model == before {
            break;
        }
    }
    stats.work = rational::work();
    let _ = options;
    Ok(stats)
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::certified_cuts::tests::model;
    use lp_ip_craft::VariableKind;
    #[test]
    fn binary_big_m_tightening_is_exactly_integer_equivalent_in_mixed_rows() {
        for sign in [-1., 1.] {
            let m = model(
                vec![10., 1.],
                vec![VariableKind::Binary, VariableKind::Continuous],
                vec![0., 0.],
                vec![1., 10.],
                vec![100. * sign, sign],
                vec![if sign > 0. { 5. } else { f64::NEG_INFINITY }],
                vec![if sign < 0. { -5. } else { f64::INFINITY }],
            );
            let mut reduced = m.clone();
            let s = NativeOptions {
                coefficient_tightening: true,
                ..Default::default()
            };
            let stats = run(
                &mut reduced,
                &s,
                &Options::default(),
                &CancellationToken::default(),
                Instant::now(),
            )
            .unwrap();
            assert!(stats.coefficients > 0);
            assert!(reduced.matrix().values[0].abs() < 100.);
            for binary in [0., 1.] {
                for k in 0..=40 {
                    let x = [binary, k as f64 / 4.];
                    assert_eq!(
                        check::evaluate(&m, &x, &Options::default().tolerances)
                            .unwrap()
                            .feasible_candidate,
                        check::evaluate(&reduced, &x, &Options::default().tolerances)
                            .unwrap()
                            .feasible_candidate
                    );
                }
            }
        }
    }
    #[test]
    fn one_pass_rewrites_every_big_m_row_and_keeps_the_integer_set() {
        // x0 binary, x1 x2 continuous in [0, 10]; one upper and one lower
        // big-M row, plus an untouched row between them.
        let m = model(
            vec![1., 1., 1.],
            vec![
                VariableKind::Binary,
                VariableKind::Continuous,
                VariableKind::Continuous,
            ],
            vec![0., 0., 0.],
            vec![1., 10., 10.],
            vec![-100., 1., 0., 1., 1., 1., 100., 0., -1.],
            vec![f64::NEG_INFINITY, f64::NEG_INFINITY, -5.],
            vec![2., 15., f64::INFINITY],
        );
        let mut reduced = m.clone();
        let s = NativeOptions {
            coefficient_tightening: true,
            ..Default::default()
        };
        let stats = run(
            &mut reduced,
            &s,
            &Options::default(),
            &CancellationToken::default(),
            Instant::now(),
        )
        .unwrap();
        assert!(stats.coefficients >= 2, "{}", stats.coefficients);
        let a = reduced.matrix();
        assert!(a.values[a.pattern.indptr[0]].abs() < 100.);
        assert!(a.values[a.pattern.indptr[2]].abs() < 100.);
        for b in [0., 1.] {
            for p in 0..=20 {
                for q in 0..=20 {
                    let x = [b, p as f64 / 2., q as f64 / 2.];
                    let f = |m: &Model| {
                        check::evaluate(m, &x, &Options::default().tolerances)
                            .unwrap()
                            .feasible_candidate
                    };
                    assert_eq!(f(&m), f(&reduced), "{x:?}");
                }
            }
        }
    }
    // One probe trail holds a binary and the columns it moves. Two trails of
    // at least 32 entries use the stamped index, shorter ones the scan. The
    // second binary moves half of the columns on one side only, so its other
    // side must read the kept bound and not the entry of the first probe.
    #[test]
    fn long_probe_trails_give_the_bounds_of_short_ones() {
        for k in [8usize, 64] {
            let n = k + 2;
            let (mut a, mut ru) = (Vec::new(), Vec::new());
            let mut row = |terms: &[(usize, f64)], upper: f64| {
                let mut dense = vec![0.; n];
                for &(j, v) in terms {
                    dense[j] = v;
                }
                a.extend(dense);
                ru.push(upper);
            };
            for i in 0..k {
                row(&[(2 + i, 1.), (0, -3.)], 5.);
                row(&[(2 + i, 1.), (0, 1.)], 7.);
                if i % 2 == 0 {
                    row(&[(2 + i, 1.), (1, 2.)], 7.);
                }
            }
            let mut kinds = vec![VariableKind::Integer; n];
            kinds[0] = VariableKind::Binary;
            kinds[1] = VariableKind::Binary;
            let mut up = vec![8.; n];
            up[0] = 1.;
            up[1] = 1.;
            let rl = vec![f64::NEG_INFINITY; ru.len()];
            let m = model(vec![1.; n], kinds, vec![0.; n], up, a, rl, ru);
            let s = NativeOptions::default();
            let cancel = CancellationToken::default();
            let budget = Budget {
                start: Instant::now(),
                settings: &s,
                cancel: &cancel,
                pivots: 0,
            };
            let mut stats = Stats::default();
            let b = probe(&m, 1e-7, &budget, &mut stats).unwrap();
            assert_eq!((stats.probed, stats.probe_tightened), (2, k as u64));
            // x <= 5 + 3 b and x <= 7 - b: 5 at b = 0 and 6 at b = 1. The
            // second binary allows 6 at 0 and 5 at 1, which removes nothing.
            for j in 2..n {
                assert_eq!((b.lower[j], b.upper[j]), (0., 6.), "k = {k}, column {j}");
            }
            assert_eq!(
                (&b.lower[..2], &b.upper[..2]),
                (&[0., 0.][..], &[1., 1.][..])
            );
        }
    }
    #[test]
    fn probing_keeps_every_feasible_binary_point_and_finds_fixings() {
        let mut rng = 0x2545_F491_4F6C_DD1Du64;
        let mut next = || {
            rng ^= rng << 13;
            rng ^= rng >> 7;
            rng ^= rng << 17;
            rng
        };
        let (mut fixed, mut tightened) = (0, 0);
        for _ in 0..300 {
            let n = 6;
            let a: Vec<f64> = (0..3 * n).map(|_| (next() % 9) as f64 - 4.).collect();
            let rl: Vec<f64> = (0..3).map(|_| -((next() % 6) as f64)).collect();
            let ru: Vec<f64> = (0..3).map(|_| (next() % 5) as f64).collect();
            let kinds = (0..n)
                .map(|j| {
                    if j < 5 {
                        VariableKind::Binary
                    } else {
                        VariableKind::Integer
                    }
                })
                .collect();
            let mut up = vec![1.; n];
            up[5] = 3.;
            let m = model(vec![1.; n], kinds, vec![0.; n], up, a, rl, ru);
            let s = NativeOptions::default();
            let cancel = CancellationToken::default();
            let budget = Budget {
                start: Instant::now(),
                settings: &s,
                cancel: &cancel,
                pivots: 0,
            };
            let mut stats = Stats::default();
            let Some(b) = probe(&m, 1e-7, &budget, &mut stats) else {
                continue;
            };
            fixed += stats.probe_fixed;
            tightened += stats.probe_tightened;
            for mask in 0..(32 * 4) {
                let x: Vec<f64> = (0..5)
                    .map(|j| ((mask >> j) & 1) as f64)
                    .chain([(mask >> 5) as f64])
                    .collect();
                if check::evaluate(&m, &x, &Options::default().tolerances)
                    .unwrap()
                    .feasible_candidate
                {
                    for j in 0..n {
                        assert!(b.lower[j] <= x[j] && x[j] <= b.upper[j], "{x:?} {b:?}");
                    }
                }
            }
        }
        assert!(fixed > 0 && tightened > 0, "{fixed} {tightened}");
    }
    #[test]
    fn singleton_dual_fixing_is_sense_aware_and_general_integer_safe() {
        let mut m = model(
            vec![-1., 1.],
            vec![VariableKind::Integer; 2],
            vec![0.; 2],
            vec![3.; 2],
            vec![1., 0.],
            vec![f64::NEG_INFINITY],
            vec![5.],
        );
        let s = NativeOptions {
            dual_presolve: true,
            ..Default::default()
        };
        run(
            &mut m,
            &s,
            &Options::default(),
            &CancellationToken::default(),
            Instant::now(),
        )
        .unwrap();
        assert_eq!(m.variables().lower[0], 0.);
        assert_eq!(m.variables().upper[0], 0.);
        assert_eq!(m.variables().lower[1], 3.);
    }
}