coopgame 0.3.1

Compute and verify the nucleolus, prenucleolus, kernel, and prekernel of TU cooperative games
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
//! カーネル・プレカーネル全体 (説明は [`super::kernel_set`])。

use microlp::Variable;

use crate::Domain;
use crate::error::{Error, Result};
use crate::game::allocation::lower_bounds;
use crate::game::{Coalition, ExplicitGame, PlayerSet, default_tolerance};
use crate::linalg::{Span, for_each_combination, solve_square};
use crate::lp::{self, Cmp, Counter};

/// [`kernel_set`] が扱うプレイヤー数の上限。
pub const MAX_KERNEL_SET_PLAYERS: usize = 6;

/// 頂点の総当たりで調べる行の組み合わせ数の上限。これを超える多面体は頂点を返さない。
const MAX_VERTEX_COMBINATIONS: usize = 2_000_000;

/// 一次式の行 `coefficients . x (<= または =) rhs`。
#[derive(Clone, Debug, PartialEq)]
pub struct Row {
    pub coefficients: Vec<f64>,
    pub rhs: f64,
}

impl Row {
    pub fn lhs(&self, x: &[f64]) -> f64 {
        self.coefficients.iter().zip(x).map(|(a, b)| a * b).sum()
    }
}

/// カーネルを構成する多面体の 1 つ。
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct KernelPiece {
    /// アフィン包の次元。
    pub dimension: usize,
    /// 多面体に属する点の 1 つ。
    pub point: Vec<f64>,
    /// 頂点。非有界な場合と、行の組み合わせが多すぎて列挙できない場合は `None`。
    pub vertices: Option<Vec<Vec<f64>>>,
    pub equalities: Vec<Row>,
    pub inequalities: Vec<Row>,
}

impl KernelPiece {
    /// 多面体を `factor` 倍する (正規化したゲームで求めた多面体を元の大きさに戻す)。
    fn scale(&mut self, factor: f64) {
        let points = std::iter::once(&mut self.point).chain(self.vertices.iter_mut().flatten());
        for value in points.flatten() {
            *value *= factor;
        }
        for row in self.equalities.iter_mut().chain(&mut self.inequalities) {
            row.rhs *= factor;
        }
    }

    pub fn contains(&self, x: &[f64], tolerance: f64) -> bool {
        self.equalities
            .iter()
            .all(|row| (row.lhs(x) - row.rhs).abs() <= tolerance)
            && self
                .inequalities
                .iter()
                .all(|row| row.lhs(x) <= row.rhs + tolerance)
    }
}

#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct KernelSet {
    /// 他の多面体に含まれるものを除いた多面体の一覧(次元の降順)。
    pub pieces: Vec<KernelPiece>,
    /// 探索した節点の数。
    pub nodes: usize,
    pub lp_solves: usize,
}

impl KernelSet {
    pub fn contains(&self, x: &[f64], tolerance: f64) -> bool {
        self.pieces.iter().any(|piece| piece.contains(x, tolerance))
    }

    /// 同じ直線上にあり、重なるか端点を共有する線分 (頂点が 2 つの 1 次元の多面体) を 1 本にまとめる。
    ///
    /// 探索は最大余剰を与える提携の場合分けごとに多面体を作るので、1 本の線分が
    /// 場合分けの境界で複数の多面体に分かれることがある。まとめた線分は、元の線分の等式
    /// (アフィン包) と、直線の向き `d` に沿った 2 本の不等式 `t_min <= d . x <= t_max` で表す。
    /// 他の形の多面体はそのまま残す。
    pub fn merge_collinear_segments(&self, tolerance: f64) -> KernelSet {
        let mut segments: Vec<Segment> = Vec::new();
        let mut others: Vec<KernelPiece> = Vec::new();
        for piece in &self.pieces {
            match Segment::from_piece(piece, tolerance) {
                Some(segment) => segments.push(segment),
                None => others.push(piece.clone()),
            }
        }
        // 1 回の合併で他の線分とつながることがあるので、変化がなくなるまで繰り返す。
        let mut merged = true;
        while merged {
            merged = false;
            'search: for a in 0..segments.len() {
                for b in a + 1..segments.len() {
                    if let Some(union) = segments[a].union(&segments[b], tolerance) {
                        segments[a] = union;
                        segments.remove(b);
                        merged = true;
                        break 'search;
                    }
                }
            }
        }
        let mut pieces: Vec<KernelPiece> = segments.into_iter().map(Segment::into_piece).collect();
        pieces.extend(others);
        pieces.sort_by_key(|piece| std::cmp::Reverse(piece.dimension));
        KernelSet {
            pieces,
            nodes: self.nodes,
            lp_solves: self.lp_solves,
        }
    }
}

/// [`KernelSet::merge_collinear_segments`] で扱う線分。`d . x` が `low..=high` を動く。
struct Segment {
    equalities: Vec<Row>,
    /// 単位ベクトル。
    direction: Vec<f64>,
    /// `d . x = low` と `d . x = high` の端点。
    ends: [Vec<f64>; 2],
    low: f64,
    high: f64,
}

impl Segment {
    fn from_piece(piece: &KernelPiece, tolerance: f64) -> Option<Segment> {
        let vertices = piece.vertices.as_ref()?;
        if piece.dimension != 1 || vertices.len() != 2 {
            return None;
        }
        let delta: Vec<f64> = vertices[1]
            .iter()
            .zip(&vertices[0])
            .map(|(a, b)| a - b)
            .collect();
        let length = dot(&delta, &delta).sqrt();
        if length <= tolerance {
            return None;
        }
        let direction: Vec<f64> = delta.iter().map(|v| v / length).collect();
        Some(Segment {
            equalities: piece.equalities.clone(),
            low: dot(&direction, &vertices[0]),
            high: dot(&direction, &vertices[1]),
            direction,
            ends: [vertices[0].clone(), vertices[1].clone()],
        })
    }

    /// `point` がこの線分を含む直線上にあるか。
    fn on_line(&self, point: &[f64], tolerance: f64) -> bool {
        let t = dot(&self.direction, point);
        let projected: Vec<f64> = self.ends[0]
            .iter()
            .zip(&self.direction)
            .map(|(p, d)| p + (t - self.low) * d)
            .collect();
        projected
            .iter()
            .zip(point)
            .all(|(a, b)| (a - b).abs() <= tolerance)
    }

    /// 同じ直線上で重なるか接していれば、合わせた線分を返す。
    fn union(&self, other: &Segment, tolerance: f64) -> Option<Segment> {
        if !other.ends.iter().all(|end| self.on_line(end, tolerance)) {
            return None;
        }
        let ts = other.ends.iter().map(|end| dot(&self.direction, end));
        let (other_low, other_high) = ts.fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), t| {
            (lo.min(t), hi.max(t))
        });
        if other_low > self.high + tolerance || other_high < self.low - tolerance {
            return None;
        }
        let pick = |t: f64| -> Vec<f64> {
            if (t - self.low).abs() <= tolerance {
                self.ends[0].clone()
            } else if (t - self.high).abs() <= tolerance {
                self.ends[1].clone()
            } else {
                other
                    .ends
                    .iter()
                    .find(|end| (dot(&self.direction, end) - t).abs() <= tolerance)
                    .expect("端点のどちらか")
                    .clone()
            }
        };
        let low = self.low.min(other_low);
        let high = self.high.max(other_high);
        Some(Segment {
            equalities: self.equalities.clone(),
            direction: self.direction.clone(),
            ends: [pick(low), pick(high)],
            low,
            high,
        })
    }

    fn into_piece(self) -> KernelPiece {
        let point = self.ends[0]
            .iter()
            .zip(&self.ends[1])
            .map(|(a, b)| (a + b) / 2.0)
            .collect();
        let inequalities = vec![
            Row {
                coefficients: self.direction.clone(),
                rhs: self.high,
            },
            Row {
                coefficients: self.direction.iter().map(|d| -d).collect(),
                rhs: -self.low,
            },
        ];
        let [first, second] = self.ends;
        KernelPiece {
            dimension: 1,
            point,
            vertices: Some(vec![first, second]),
            equalities: self.equalities,
            inequalities,
        }
    }
}

fn dot(a: &[f64], b: &[f64]) -> f64 {
    a.iter().zip(b).map(|(x, y)| x * y).sum()
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct SetOptions {
    /// 等号・不等号の判定に使う許容誤差 (ゲームの値と同じ単位)。既定は [`default_tolerance`]。
    ///
    /// 配分集合の扱いは [`crate::nucleolus::Options::tolerance`] と同じで、[`crate::kernel::kernel_point`] とも
    /// 同じ式 (`sum v({i}) <= v(N) + tolerance`) で空かどうかを判定する。ただし既定の許容誤差は
    /// `kernel_point` ([`crate::kernel::TransferOptions::for_game`]) の方が 100 倍小さいので、
    /// 差が `0.01 * default_tolerance` を超えて `default_tolerance` 以下のゲームでは、既定値のままだと
    /// `kernel_set` は解を返し `kernel_point` は [`crate::Error::EmptyImputationSet`] を返す。
    /// 判定をそろえるには同じ許容誤差を渡す。
    pub tolerance: f64,
    /// 探索する節点の数の上限。超えたらエラーを返す。
    pub max_nodes: usize,
}

impl SetOptions {
    pub fn for_game(game: &ExplicitGame) -> SetOptions {
        SetOptions {
            tolerance: default_tolerance(game),
            max_nodes: 200_000,
        }
    }
}

/// 小さいゲームのカーネル (`Domain::Imputation`)・プレカーネル (`Domain::Preimputation`) 全体を、
/// 多面体の和集合として求める。
///
/// 最大余剰 `s_ij(x)` は区分線形なので、各組 `(i, j)` で `s_ij` を与える提携
/// `S_ij`(`i in S, j not in S` で超過が最大の提携)を固定すると、その領域では
/// `s_ij(x) = e(S_ij, x)` が一次式になる。全ての組について `S_ij` と `S_ji` を選び、
///
/// - プレカーネル: `e(S_ij) = e(S_ji)`
/// - カーネル: 上に加えて、`e(S_ij) >= e(S_ji)` かつ `x_j = v({j})`、
///   または `e(S_ji) >= e(S_ij)` かつ `x_i = v({i})`
///
/// のいずれかを課すと、条件は全て一次の等式・不等式になる。
/// この選び方を組ごとに深さ優先で探索し、LP で実行不可能な枝を刈る。
/// 葉に残った多面体の和集合がカーネル(プレカーネル)に一致する。
///
/// 同点の提携による分岐の重複を避けるため、各節点で多面体のアフィン包
/// (明示的な等式と、LP で見つけた暗黙の等式)を求め、その上で超過が恒等的に
/// 等しくなる提携は同じ候補として 1 つだけ展開する。
///
/// 計算量は組の数と提携の数に対して指数的なので、プレイヤー数は [`MAX_KERNEL_SET_PLAYERS`] 以下に限る。
///
/// LP は絶対誤差で判定するので、ゲームの値を 2 のべき乗で割って最大絶対値を `[1, 2)` にそろえてから求め、
/// 多面体を元の大きさに戻す (値を `c` 倍すると、多面体も `c` 倍になる)。
pub fn kernel_set(game: &ExplicitGame, domain: Domain, options: SetOptions) -> Result<KernelSet> {
    let n = game.players();
    if n > MAX_KERNEL_SET_PLAYERS {
        return Err(Error::TooManyPlayers {
            players: n,
            max: MAX_KERNEL_SET_PLAYERS,
        });
    }
    let (normalized, divisor) = game.normalized();
    let options = SetOptions {
        tolerance: options.tolerance / divisor,
        ..options
    };
    let mut set = normalized_kernel_set(&normalized, domain, options)?;
    for piece in &mut set.pieces {
        piece.scale(divisor);
    }
    Ok(set)
}

fn normalized_kernel_set(
    game: &ExplicitGame,
    domain: Domain,
    options: SetOptions,
) -> Result<KernelSet> {
    let n = game.players();
    let grand = PlayerSet::grand(n);
    let lower = lower_bounds(game, &[grand], domain, options.tolerance)?;
    let mut search = Search {
        game,
        domain,
        // 準配分では場合分けに使わない。
        lower: lower.clone().unwrap_or_else(|| game.singleton_values()),
        tolerance: options.tolerance,
        max_nodes: options.max_nodes,
        n,
        pairs: (0..n)
            .flat_map(|i| (i + 1..n).map(move |j| (i, j)))
            .collect(),
        counter: Counter::default(),
        nodes: 0,
        leaves: Vec::new(),
    };

    let mut root = Polyhedron::new(n);
    let grand = game.grand();
    root.add_equality(indicator(grand, n), game.value(grand));
    for (i, bound) in lower.iter().flatten().enumerate() {
        let mut coefficients = vec![0.0; n];
        coefficients[i] = -1.0;
        root.inequalities.push(Row {
            coefficients,
            rhs: -bound,
        });
    }
    // 根は配分集合 (準配分集合)。空でないことは確かめてあるので、LP が実行不可能なら
    // 許容誤差の範囲を超える丸めが起きたとして、空の結果ではなくエラーを返す。
    let Some(point) = search.feasible(&root)? else {
        return Err(match domain {
            Domain::Imputation => Error::EmptyImputationSet,
            Domain::Preimputation => Error::Numerical("準配分集合の LP が実行不可能".into()),
        });
    };
    search.explore(0, root, point)?;

    let mut pieces = Vec::with_capacity(search.leaves.len());
    for (polyhedron, point) in std::mem::take(&mut search.leaves) {
        pieces.push(search.piece(polyhedron, point)?);
    }
    Ok(KernelSet {
        pieces: remove_contained(pieces, 10.0 * options.tolerance),
        nodes: search.nodes,
        lp_solves: search.counter.solves,
    })
}

#[derive(Clone, Debug)]
struct Polyhedron {
    equalities: Vec<Row>,
    inequalities: Vec<Row>,
    /// 等式の係数ベクトルが張る空間。アフィン包の方向はこの直交補空間。
    span: Span,
}

impl Polyhedron {
    fn new(n: usize) -> Polyhedron {
        Polyhedron {
            equalities: Vec::new(),
            inequalities: Vec::new(),
            span: Span::new(n),
        }
    }

    /// 等式を加える。係数が既存の等式に一次従属でも、定数項が矛盾しうるので行は残す。
    fn add_equality(&mut self, coefficients: Vec<f64>, rhs: f64) {
        self.span.insert(&coefficients);
        self.equalities.push(Row { coefficients, rhs });
    }

    /// 実行可能な多面体で見つけた暗黙の等式を加える。
    /// 矛盾は起こりえないので、一次従属な行は捨てる。
    fn add_implied_equality(&mut self, coefficients: Vec<f64>, rhs: f64) {
        if self.span.insert(&coefficients) {
            self.equalities.push(Row { coefficients, rhs });
        }
    }
}

enum Case {
    /// 条件を付けない(`x_i = v({i})` と `x_j = v({j})` が既に成り立つ場合)。
    Free,
    Equal,
    /// `s_ij >= s_ji` かつ `x_j = v({j})`。
    FirstGreater,
    /// `s_ji >= s_ij` かつ `x_i = v({i})`。
    SecondGreater,
}

struct Search<'a> {
    game: &'a ExplicitGame,
    domain: Domain,
    /// 各プレイヤーの下限 (`Domain::Imputation` で使う)。
    lower: Vec<f64>,
    tolerance: f64,
    max_nodes: usize,
    n: usize,
    pairs: Vec<(usize, usize)>,
    counter: Counter,
    nodes: usize,
    leaves: Vec<(Polyhedron, Vec<f64>)>,
}

impl Search<'_> {
    fn explore(&mut self, depth: usize, mut polyhedron: Polyhedron, point: Vec<f64>) -> Result<()> {
        self.nodes += 1;
        if self.nodes > self.max_nodes {
            return Err(Error::LimitExceeded(format!(
                "探索した節点が上限 {} を超えた",
                self.max_nodes
            )));
        }
        if depth == self.pairs.len() {
            self.leaves.push((polyhedron, point));
            return Ok(());
        }
        self.refresh_affine_hull(&mut polyhedron)?;
        let (i, j) = self.pairs[depth];
        // x_k = v({k}) が多面体上で恒等的に成り立つか。
        let at_lower_bound = |k: usize| {
            polyhedron.span.contains(&unit(k, self.n))
                && (point[k] - self.lower[k]).abs() <= self.tolerance
        };
        // 重なる場合分けは、他方に含まれるものを省く。
        let cases: &[Case] = match (self.domain, at_lower_bound(i), at_lower_bound(j)) {
            (Domain::Preimputation, _, _) => &[Case::Equal],
            // 両方が下限に張り付いていれば、組 (i, j) には条件が付かない。
            (Domain::Imputation, true, true) => &[Case::Free],
            // x_j = v({j}) なら「等しい」は「s_ij >= s_ji」に含まれる。
            (Domain::Imputation, false, true) => &[Case::FirstGreater, Case::SecondGreater],
            (Domain::Imputation, true, false) => &[Case::SecondGreater, Case::FirstGreater],
            (Domain::Imputation, false, false) => {
                &[Case::Equal, Case::FirstGreater, Case::SecondGreater]
            }
        };
        for (first, with_first) in self.argmax_candidates(i, j, &polyhedron)? {
            for (second, with_both) in self.argmax_candidates(j, i, &with_first)? {
                for case in cases {
                    let mut next = with_both.clone();
                    let difference = difference(first, second, self.n);
                    let gap = self.game.value(first) - self.game.value(second);
                    match case {
                        Case::Free => {}
                        Case::Equal => next.add_equality(difference, gap),
                        Case::FirstGreater => {
                            next.inequalities.push(Row {
                                coefficients: difference,
                                rhs: gap,
                            });
                            next.add_equality(unit(j, self.n), self.lower[j]);
                        }
                        Case::SecondGreater => {
                            next.inequalities.push(Row {
                                coefficients: difference.iter().map(|a| -a).collect(),
                                rhs: -gap,
                            });
                            next.add_equality(unit(i, self.n), self.lower[i]);
                        }
                    }
                    if let Some(point) = self.feasible(&next)? {
                        self.explore(depth + 1, next, point)?;
                    }
                }
            }
        }
        Ok(())
    }

    /// `i in S, j not in S` の提携 `S` のうち、多面体の中で超過が最大になりうるものと、
    /// 「`S` が最大」という条件を加えた多面体の組を返す。
    /// アフィン包の上で超過が恒等的に等しい提携は 1 つにまとめる。
    fn argmax_candidates(
        &mut self,
        i: usize,
        j: usize,
        polyhedron: &Polyhedron,
    ) -> Result<Vec<(Coalition, Polyhedron)>> {
        let group: Vec<Coalition> = (1..(1u64 << self.n))
            .map(Coalition)
            .filter(|s| s.contains(i) && !s.contains(j))
            .collect();
        let mut candidates: Vec<(Coalition, Polyhedron)> = Vec::new();
        for &coalition in &group {
            let duplicate = candidates.iter().any(|(chosen, _)| {
                polyhedron
                    .span
                    .contains(&difference(coalition, *chosen, self.n))
            });
            if duplicate {
                continue;
            }
            let mut next = polyhedron.clone();
            for &other in &group {
                if other == coalition {
                    continue;
                }
                // e(S) >= e(S') <=> x(S) - x(S') <= v(S) - v(S')
                // 方向が等式の張る空間に入る行もアフィン包上の定数条件として残す。
                next.inequalities.push(Row {
                    coefficients: difference(coalition, other, self.n),
                    rhs: self.game.value(coalition) - self.game.value(other),
                });
            }
            if self.feasible(&next)?.is_some() {
                candidates.push((coalition, next));
            }
        }
        Ok(candidates)
    }

    fn feasible(&mut self, polyhedron: &Polyhedron) -> Result<Option<Vec<f64>>> {
        let mut problem = lp::minimize();
        let x: Vec<Variable> = (0..self.n)
            .map(|_| problem.add_var(0.0, (f64::NEG_INFINITY, f64::INFINITY)))
            .collect();
        add_rows(&mut problem, &x, polyhedron, &[]);
        Ok(self
            .counter
            .solve(&problem)?
            .map(|solution| x.iter().map(|&var| solution[var]).collect()))
    }

    /// 暗黙の等式(多面体上で常に等号が成り立つ不等式)を等式に移す。
    ///
    /// 残りの不等式にスラック `s_k in [0, 1]` を付けて和を最大化し、
    /// 正のスラックを取れた不等式は暗黙の等式ではないとして外す。
    /// 最大値が 0 なら、残った不等式は全て暗黙の等式である。
    fn refresh_affine_hull(&mut self, polyhedron: &mut Polyhedron) -> Result<()> {
        let mut candidates: Vec<usize> = (0..polyhedron.inequalities.len()).collect();
        while !candidates.is_empty() {
            let mut problem = lp::maximize();
            let x: Vec<Variable> = (0..self.n)
                .map(|_| problem.add_var(0.0, (f64::NEG_INFINITY, f64::INFINITY)))
                .collect();
            let slacks: Vec<Variable> = candidates
                .iter()
                .map(|_| problem.add_var(1.0, (0.0, 1.0)))
                .collect();
            add_rows(
                &mut problem,
                &x,
                polyhedron,
                &candidates
                    .iter()
                    .copied()
                    .zip(slacks.iter().copied())
                    .collect::<Vec<_>>(),
            );
            let solution = self
                .counter
                .solve(&problem)?
                .ok_or_else(|| Error::Numerical("アフィン包の計算で多面体が空になった".into()))?;
            let before = candidates.len();
            let mut kept = Vec::new();
            for (&index, &slack) in candidates.iter().zip(&slacks) {
                if solution[slack] <= self.tolerance {
                    kept.push(index);
                }
            }
            candidates = kept;
            if candidates.len() == before {
                break;
            }
        }
        if candidates.is_empty() {
            return Ok(());
        }
        // 暗黙の等式を等式に移し、不等式からは外す。
        let mut implicit = vec![false; polyhedron.inequalities.len()];
        for &index in &candidates {
            implicit[index] = true;
        }
        let inequalities = std::mem::take(&mut polyhedron.inequalities);
        for (row, is_implicit) in inequalities.into_iter().zip(implicit) {
            if is_implicit {
                polyhedron.add_implied_equality(row.coefficients, row.rhs);
            } else {
                polyhedron.inequalities.push(row);
            }
        }
        Ok(())
    }

    fn piece(&mut self, mut polyhedron: Polyhedron, point: Vec<f64>) -> Result<KernelPiece> {
        self.refresh_affine_hull(&mut polyhedron)?;
        let basis = polyhedron.span.null_space();
        let dimension = basis.len();
        let vertices = self.vertices(&polyhedron, &point, &basis)?;
        Ok(KernelPiece {
            dimension,
            point,
            vertices,
            equalities: polyhedron.equalities,
            inequalities: polyhedron.inequalities,
        })
    }

    /// アフィン包を `x = point + sum z_l basis_l` と表し、`z` の空間で頂点を総当たりで求める。
    fn vertices(
        &mut self,
        polyhedron: &Polyhedron,
        point: &[f64],
        basis: &[Vec<f64>],
    ) -> Result<Option<Vec<Vec<f64>>>> {
        let dimension = basis.len();
        if dimension == 0 {
            return Ok(Some(vec![point.to_vec()]));
        }
        // z の空間での行 c . z <= r。正規化して重複を除く。
        let mut rows: Vec<(Vec<f64>, f64)> = Vec::new();
        for row in &polyhedron.inequalities {
            let c: Vec<f64> = basis.iter().map(|direction| row.lhs(direction)).collect();
            let norm = c.iter().map(|v| v * v).sum::<f64>().sqrt();
            if norm <= 1e-9 {
                continue;
            }
            let c: Vec<f64> = c.iter().map(|v| v / norm).collect();
            let r = (row.rhs - row.lhs(point)) / norm;
            match rows
                .iter_mut()
                .find(|(existing, _)| existing.iter().zip(&c).all(|(a, b)| (a - b).abs() <= 1e-9))
            {
                Some(existing) => existing.1 = existing.1.min(r),
                None => rows.push((c, r)),
            }
        }
        if !self.is_bounded(&rows, dimension)? {
            return Ok(None);
        }
        if combinations_count(rows.len(), dimension) > MAX_VERTEX_COMBINATIONS {
            return Ok(None);
        }
        let mut vertices: Vec<Vec<f64>> = Vec::new();
        for_each_combination(rows.len(), dimension, &mut |chosen| {
            let a = chosen.iter().map(|&k| rows[k].0.clone()).collect();
            let b = chosen.iter().map(|&k| rows[k].1).collect();
            let Some(z) = solve_square(a, b, 1e-9) else {
                return;
            };
            let feasible = rows.iter().all(|(c, r)| {
                c.iter().zip(&z).map(|(a, b)| a * b).sum::<f64>() <= r + self.tolerance
            });
            if !feasible {
                return;
            }
            let x: Vec<f64> = (0..point.len())
                .map(|i| point[i] + basis.iter().zip(&z).map(|(d, zl)| d[i] * zl).sum::<f64>())
                .collect();
            let known = vertices.iter().any(|v| {
                v.iter()
                    .zip(&x)
                    .all(|(a, b)| (a - b).abs() <= 10.0 * self.tolerance)
            });
            if !known {
                vertices.push(x);
            }
        });
        Ok(Some(vertices))
    }

    fn is_bounded(&mut self, rows: &[(Vec<f64>, f64)], dimension: usize) -> Result<bool> {
        for axis in 0..dimension {
            for sign in [1.0, -1.0] {
                let mut problem = lp::maximize();
                let z: Vec<Variable> = (0..dimension)
                    .map(|l| {
                        let objective = if l == axis { sign } else { 0.0 };
                        problem.add_var(objective, (f64::NEG_INFINITY, f64::INFINITY))
                    })
                    .collect();
                for (c, r) in rows {
                    let terms = z.iter().zip(c).map(|(&var, &coef)| (var, coef)).collect();
                    lp::add(&mut problem, terms, Cmp::Le, *r);
                }
                if self.counter.solve_bounded(&problem)?.is_none() {
                    return Ok(false);
                }
            }
        }
        Ok(true)
    }
}

/// 多面体の行を LP に入れる。`slacks` に含まれる不等式にはスラック変数を足す。
fn add_rows(
    problem: &mut microlp::Problem,
    x: &[Variable],
    polyhedron: &Polyhedron,
    slacks: &[(usize, Variable)],
) {
    let terms = |row: &Row| -> Vec<(Variable, f64)> {
        x.iter()
            .zip(&row.coefficients)
            .filter(|(_, coef)| **coef != 0.0)
            .map(|(&var, &coef)| (var, coef))
            .collect()
    };
    for row in &polyhedron.equalities {
        lp::add(problem, terms(row), Cmp::Eq, row.rhs);
    }
    for (index, row) in polyhedron.inequalities.iter().enumerate() {
        let mut row_terms = terms(row);
        if let Some((_, slack)) = slacks.iter().find(|(k, _)| *k == index) {
            row_terms.push((*slack, 1.0));
        }
        lp::add(problem, row_terms, Cmp::Le, row.rhs);
    }
}

/// 次元の大きい順に並べ、頂点が全て既出の多面体に含まれるものを除く。
fn remove_contained(mut pieces: Vec<KernelPiece>, tolerance: f64) -> Vec<KernelPiece> {
    pieces.sort_by_key(|piece| std::cmp::Reverse(piece.dimension));
    let mut kept: Vec<KernelPiece> = Vec::new();
    for piece in pieces {
        let contained = match &piece.vertices {
            Some(vertices) => kept.iter().any(|other| {
                vertices
                    .iter()
                    .all(|vertex| other.contains(vertex, tolerance))
            }),
            None => false,
        };
        if !contained {
            kept.push(piece);
        }
    }
    kept
}

fn indicator(coalition: Coalition, n: usize) -> Vec<f64> {
    crate::linalg::indicator(coalition, n)
}

fn unit(i: usize, n: usize) -> Vec<f64> {
    let mut vector = vec![0.0; n];
    vector[i] = 1.0;
    vector
}

/// `1_S - 1_T`。
fn difference(s: Coalition, t: Coalition, n: usize) -> Vec<f64> {
    (0..n)
        .map(|i| f64::from(u8::from(s.contains(i))) - f64::from(u8::from(t.contains(i))))
        .collect()
}

fn combinations_count(n: usize, k: usize) -> usize {
    let mut count: usize = 1;
    for i in 0..k {
        count = count.saturating_mul(n - i) / (i + 1);
    }
    count
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn counts_combinations() {
        assert_eq!(combinations_count(4, 2), 6);
    }

    #[test]
    fn pair_game_kernel_is_single_point() {
        let game = ExplicitGame::from_lex(&[0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 4.0]).unwrap();
        let set = kernel_set(&game, Domain::Imputation, SetOptions::for_game(&game)).unwrap();
        assert_eq!(set.pieces.len(), 1, "{set:?}");
        let piece = &set.pieces[0];
        assert_eq!(piece.dimension, 0);
        let vertex = &piece.vertices.as_ref().unwrap()[0];
        for (a, e) in vertex.iter().zip([2.0, 2.0, 0.0]) {
            assert!((a - e).abs() < 1e-6, "{vertex:?}");
        }
    }

    #[test]
    fn two_player_prekernel() {
        // s_12 = v1 - x1, s_21 = v2 - x2 から x1 - x2 = v1 - v2。
        let game = ExplicitGame::from_binary(&[1.0, 3.0, 10.0]).unwrap();
        let set = kernel_set(&game, Domain::Preimputation, SetOptions::for_game(&game)).unwrap();
        assert_eq!(set.pieces.len(), 1);
        let vertex = &set.pieces[0].vertices.as_ref().unwrap()[0];
        assert!((vertex[0] - 4.0).abs() < 1e-9 && (vertex[1] - 6.0).abs() < 1e-9);
    }

    fn segment(a: [f64; 3], b: [f64; 3]) -> KernelPiece {
        // x1 + x2 + x3 = 3 の平面上の線分 (等式はアフィン包を表す 2 本)。
        let delta: Vec<f64> = b.iter().zip(&a).map(|(p, q)| p - q).collect();
        // delta と (1, 1, 1) に直交する法線
        let normal = vec![
            delta[1] - delta[2],
            delta[2] - delta[0],
            delta[0] - delta[1],
        ];
        KernelPiece {
            dimension: 1,
            point: a.iter().zip(&b).map(|(p, q)| (p + q) / 2.0).collect(),
            vertices: Some(vec![a.to_vec(), b.to_vec()]),
            equalities: vec![
                Row {
                    coefficients: vec![1.0; 3],
                    rhs: 3.0,
                },
                Row {
                    rhs: dot(&normal, &a),
                    coefficients: normal,
                },
            ],
            inequalities: Vec::new(),
        }
    }

    #[test]
    fn merges_touching_and_overlapping_collinear_segments() {
        let set = KernelSet {
            pieces: vec![
                segment([1.0, 1.0, 1.0], [2.0, 0.5, 0.5]),
                // 端点を共有する (向きは逆)
                segment([3.0, 0.0, 0.0], [2.0, 0.5, 0.5]),
                // 重なる
                segment([1.5, 0.75, 0.75], [0.0, 1.5, 1.5]),
                // 端点を共有するが別の直線
                segment([1.0, 1.0, 1.0], [1.0, 2.0, 0.0]),
                // 同じ直線だが離れている
                segment([-1.0, 2.0, 2.0], [-2.0, 2.5, 2.5]),
            ],
            nodes: 0,
            lp_solves: 0,
        };
        let merged = set.merge_collinear_segments(1e-9);
        assert_eq!(merged.pieces.len(), 3, "{merged:?}");
        let long = merged
            .pieces
            .iter()
            .find(|piece| piece.contains(&[3.0, 0.0, 0.0], 1e-9))
            .expect("合わせた線分");
        let mut ends = long.vertices.clone().unwrap();
        ends.sort_by(|a, b| a[0].total_cmp(&b[0]));
        assert_eq!(ends, vec![vec![0.0, 1.5, 1.5], vec![3.0, 0.0, 0.0]]);
        for x in [[0.0, 1.5, 1.5], [1.2, 0.9, 0.9], [3.0, 0.0, 0.0]] {
            assert!(long.contains(&x, 1e-9));
        }
        assert!(!long.contains(&[3.2, -0.1, -0.1], 1e-9));
        assert!(!long.contains(&[1.0, 2.0, 0.0], 1e-9));
        // 元の和集合に含まれる点は、まとめた後も含まれる
        for piece in &set.pieces {
            for vertex in piece.vertices.as_ref().unwrap() {
                assert!(merged.contains(vertex, 1e-9));
            }
        }
    }

    #[test]
    fn enumerates_vertices_of_four_dimensional_simplex() {
        // 配分集合 {x >= 0, sum x = 1} (5 人) は 4 次元の単体で、頂点は単位ベクトル。
        let game = ExplicitGame::from_binary(&[0.0; 31]).unwrap();
        let mut search = Search {
            game: &game,
            domain: Domain::Imputation,
            lower: vec![0.0; 5],
            tolerance: 1e-9,
            max_nodes: 1,
            n: 5,
            pairs: Vec::new(),
            counter: Counter::default(),
            nodes: 0,
            leaves: Vec::new(),
        };
        let mut polyhedron = Polyhedron::new(5);
        polyhedron.add_equality(vec![1.0; 5], 1.0);
        for i in 0..5 {
            let mut coefficients = vec![0.0; 5];
            coefficients[i] = -1.0;
            polyhedron.inequalities.push(Row {
                coefficients,
                rhs: 0.0,
            });
        }
        let piece = search.piece(polyhedron, vec![0.2; 5]).unwrap();
        assert_eq!(piece.dimension, 4);
        let mut vertices = piece.vertices.expect("有界なので頂点を返す");
        vertices.sort_by(|a, b| {
            b.iter()
                .zip(a)
                .map(|(p, q)| p.total_cmp(q))
                .find(|o| o.is_ne())
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        assert_eq!(vertices.len(), 5);
        for (i, vertex) in vertices.iter().enumerate() {
            for (k, value) in vertex.iter().enumerate() {
                let expected = if k == i { 1.0 } else { 0.0 };
                assert!((value - expected).abs() < 1e-9, "{vertices:?}");
            }
        }
    }

    #[test]
    fn rejects_large_games() {
        let game = crate::generators::bnf(2, MAX_KERNEL_SET_PLAYERS + 1, 0).unwrap();
        let err = kernel_set(&game, Domain::Imputation, SetOptions::for_game(&game)).unwrap_err();
        assert!(matches!(err, Error::TooManyPlayers { .. }));
    }
}