scirs2-optimize 0.4.2

Optimization module for SciRS2 (scirs2-optimize)
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
//! Advanced MDP planning algorithms.
//!
//! Includes:
//! - RTDP (Real-Time Dynamic Programming)
//! - Prioritized Sweeping
//! - Stochastic Shortest Path
//! - State-action occupancy measure
//! - Maximum Entropy Inverse Reinforcement Learning

use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashSet};
use scirs2_core::ndarray::Array2;
use crate::error::OptimizeError;
use crate::mdp::tabular::{
    Mdp, MdpSolution, evaluate_policy, compute_bellman_residual,
    lcg_next, lcg_index, sample_next_state,
};

// ─────────────────────────────────────────────────────────────────────────────
// Priority queue entry (max-heap by priority)
// ─────────────────────────────────────────────────────────────────────────────

/// Entry in the priority queue for Prioritized Sweeping.
#[derive(Debug, Clone, PartialEq)]
struct PriorityEntry {
    priority: f64,
    state: usize,
}

impl PartialOrd for PriorityEntry {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.priority.partial_cmp(&other.priority)
    }
}

impl Ord for PriorityEntry {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap_or(Ordering::Equal)
    }
}

impl Eq for PriorityEntry {}

// ─────────────────────────────────────────────────────────────────────────────
// RTDP
// ─────────────────────────────────────────────────────────────────────────────

/// Real-Time Dynamic Programming (RTDP).
///
/// Focuses Bellman updates on states reachable from `initial_state` by
/// simulating greedy trajectories.  Each trial follows the current greedy
/// policy and applies a Bellman backup at every visited state.
///
/// Suitable for large MDPs where many states are unreachable.
pub fn rtdp(
    mdp: &Mdp,
    initial_state: usize,
    n_trials: usize,
    max_trial_steps: usize,
    tol: f64,
) -> Result<MdpSolution, OptimizeError> {
    if initial_state >= mdp.n_states {
        return Err(OptimizeError::ValueError(format!(
            "initial_state {} >= n_states {}",
            initial_state, mdp.n_states
        )));
    }
    if tol <= 0.0 {
        return Err(OptimizeError::ValueError(
            "tol must be positive".to_string(),
        ));
    }

    let r = mdp.expected_reward();
    let mut v = vec![0.0_f64; mdp.n_states];
    let terminal_set: HashSet<usize> = mdp.terminal_states.iter().copied().collect();
    let mut rng = 0xdeadbeef_u64;
    let mut total_updates = 0usize;

    for _ in 0..n_trials {
        let mut state = initial_state;
        for _ in 0..max_trial_steps {
            if terminal_set.contains(&state) {
                break;
            }
            // Bellman backup at current state
            let best_q = (0..mdp.n_actions)
                .map(|a| {
                    let future: f64 = (0..mdp.n_states)
                        .map(|sp| mdp.transition[[state, a, sp]] * v[sp])
                        .sum();
                    r[[state, a]] + mdp.gamma * future
                })
                .fold(f64::NEG_INFINITY, f64::max);
            v[state] = best_q;
            total_updates += 1;

            // Greedy action
            let best_a = (0..mdp.n_actions)
                .max_by(|&a1, &a2| {
                    let q1: f64 = {
                        let fut: f64 = (0..mdp.n_states)
                            .map(|sp| mdp.transition[[state, a1, sp]] * v[sp])
                            .sum();
                        r[[state, a1]] + mdp.gamma * fut
                    };
                    let q2: f64 = {
                        let fut: f64 = (0..mdp.n_states)
                            .map(|sp| mdp.transition[[state, a2, sp]] * v[sp])
                            .sum();
                        r[[state, a2]] + mdp.gamma * fut
                    };
                    q1.partial_cmp(&q2).unwrap_or(Ordering::Equal)
                })
                .unwrap_or(0);

            state = sample_next_state(mdp, state, best_a, rng);
            rng = lcg_next(rng);
        }
    }

    // Extract greedy policy from converged values
    let policy: Vec<usize> = (0..mdp.n_states)
        .map(|s| {
            (0..mdp.n_actions)
                .max_by(|&a1, &a2| {
                    let q = |a: usize| -> f64 {
                        let fut: f64 = (0..mdp.n_states)
                            .map(|sp| mdp.transition[[s, a, sp]] * v[sp])
                            .sum();
                        r[[s, a]] + mdp.gamma * fut
                    };
                    q(a1).partial_cmp(&q(a2)).unwrap_or(Ordering::Equal)
                })
                .unwrap_or(0)
        })
        .collect();

    let max_diff = compute_bellman_residual(mdp, &v, &r);
    let converged = max_diff < tol;

    Ok(MdpSolution {
        value_function: v,
        policy,
        n_iterations: n_trials,
        converged,
        max_diff,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// Prioritized Sweeping
// ─────────────────────────────────────────────────────────────────────────────

/// Prioritized Sweeping.
///
/// Maintains a max-priority queue of states ordered by Bellman error and
/// updates the highest-error state first.  After each update, predecessors are
/// inserted (or upgraded) in the queue.
///
/// Parameters:
/// - `n_sweeps` – maximum number of priority updates.
/// - `theta` – minimum priority to insert into the queue.
/// - `tol` – convergence tolerance (‖TV − V‖_∞).
pub fn prioritized_sweeping(
    mdp: &Mdp,
    n_sweeps: usize,
    theta: f64,
    tol: f64,
) -> Result<MdpSolution, OptimizeError> {
    if tol <= 0.0 {
        return Err(OptimizeError::ValueError(
            "tol must be positive".to_string(),
        ));
    }
    if theta < 0.0 {
        return Err(OptimizeError::ValueError(
            "theta must be >= 0".to_string(),
        ));
    }

    let r = mdp.expected_reward();
    let mut v = vec![0.0_f64; mdp.n_states];

    // Build predecessor lists: pred[s'] = set of (s, a) pairs with T(s,a,s') > 0
    let mut predecessors: Vec<Vec<(usize, usize)>> = vec![Vec::new(); mdp.n_states];
    for s in 0..mdp.n_states {
        for a in 0..mdp.n_actions {
            for sp in 0..mdp.n_states {
                if mdp.transition[[s, a, sp]] > 1e-12 {
                    predecessors[sp].push((s, a));
                }
            }
        }
    }

    // Initialise priority queue with all states
    let mut heap: BinaryHeap<PriorityEntry> = BinaryHeap::new();
    let mut in_queue: Vec<bool> = vec![false; mdp.n_states];

    for s in 0..mdp.n_states {
        let bellman_err = bellman_error_at(mdp, &v, &r, s);
        if bellman_err > theta {
            heap.push(PriorityEntry {
                priority: bellman_err,
                state: s,
            });
            in_queue[s] = true;
        }
    }

    for _ in 0..n_sweeps {
        let entry = match heap.pop() {
            Some(e) => e,
            None => break,
        };
        let s = entry.state;
        in_queue[s] = false;

        // Bellman backup
        let new_v = (0..mdp.n_actions)
            .map(|a| {
                let fut: f64 = (0..mdp.n_states)
                    .map(|sp| mdp.transition[[s, a, sp]] * v[sp])
                    .sum();
                r[[s, a]] + mdp.gamma * fut
            })
            .fold(f64::NEG_INFINITY, f64::max);
        v[s] = new_v;

        // Update predecessors
        for &(pred_s, _pred_a) in &predecessors[s] {
            let pred_err = bellman_error_at(mdp, &v, &r, pred_s);
            if pred_err > theta && !in_queue[pred_s] {
                heap.push(PriorityEntry {
                    priority: pred_err,
                    state: pred_s,
                });
                in_queue[pred_s] = true;
            }
        }
    }

    // Extract policy
    let policy: Vec<usize> = (0..mdp.n_states)
        .map(|s| {
            (0..mdp.n_actions)
                .max_by(|&a1, &a2| {
                    let q = |a: usize| {
                        let fut: f64 = (0..mdp.n_states)
                            .map(|sp| mdp.transition[[s, a, sp]] * v[sp])
                            .sum();
                        r[[s, a]] + mdp.gamma * fut
                    };
                    q(a1).partial_cmp(&q(a2)).unwrap_or(Ordering::Equal)
                })
                .unwrap_or(0)
        })
        .collect();

    let max_diff = compute_bellman_residual(mdp, &v, &r);
    Ok(MdpSolution {
        value_function: v,
        policy,
        n_iterations: n_sweeps,
        converged: max_diff < tol,
        max_diff,
    })
}

/// Compute the Bellman error at a single state: |max_a Q(s,a) - V(s)|.
fn bellman_error_at(mdp: &Mdp, v: &[f64], r: &Array2<f64>, s: usize) -> f64 {
    let best_q = (0..mdp.n_actions)
        .map(|a| {
            let fut: f64 = (0..mdp.n_states)
                .map(|sp| mdp.transition[[s, a, sp]] * v[sp])
                .sum();
            r[[s, a]] + mdp.gamma * fut
        })
        .fold(f64::NEG_INFINITY, f64::max);
    (best_q - v[s]).abs()
}

// ─────────────────────────────────────────────────────────────────────────────
// Stochastic Shortest Path
// ─────────────────────────────────────────────────────────────────────────────

/// Stochastic Shortest Path (SSP).
///
/// Solves the SSP problem with a designated `goal_state`.  The goal state is
/// absorbing with zero cost.  All other states optimise expected cost-to-goal.
///
/// Uses value iteration with the goal state value fixed at 0 and
/// `gamma = 1.0` (undiscounted), requiring negative rewards for convergence.
/// For proper convergence the MDP should have all policies reach `goal_state`
/// with probability 1; this implementation also supports γ < 1 as a proxy.
pub fn stochastic_shortest_path(
    mdp: &Mdp,
    goal_state: usize,
    max_iter: usize,
    tol: f64,
) -> Result<MdpSolution, OptimizeError> {
    if goal_state >= mdp.n_states {
        return Err(OptimizeError::ValueError(format!(
            "goal_state {} >= n_states {}",
            goal_state, mdp.n_states
        )));
    }
    if tol <= 0.0 {
        return Err(OptimizeError::ValueError(
            "tol must be positive".to_string(),
        ));
    }

    let r = mdp.expected_reward();
    let mut v = vec![0.0_f64; mdp.n_states];
    v[goal_state] = 0.0;
    let mut policy = vec![0usize; mdp.n_states];
    let mut max_diff = f64::INFINITY;

    for iter in 0..max_iter {
        max_diff = 0.0_f64;
        for s in 0..mdp.n_states {
            if s == goal_state {
                v[s] = 0.0;
                continue;
            }
            let best_a = (0..mdp.n_actions)
                .max_by(|&a1, &a2| {
                    let q = |a: usize| {
                        let fut: f64 = (0..mdp.n_states)
                            .map(|sp| mdp.transition[[s, a, sp]] * v[sp])
                            .sum();
                        r[[s, a]] + mdp.gamma * fut
                    };
                    q(a1).partial_cmp(&q(a2)).unwrap_or(Ordering::Equal)
                })
                .unwrap_or(0);
            let new_v = {
                let fut: f64 = (0..mdp.n_states)
                    .map(|sp| mdp.transition[[s, best_a, sp]] * v[sp])
                    .sum();
                r[[s, best_a]] + mdp.gamma * fut
            };
            let diff = (new_v - v[s]).abs();
            if diff > max_diff {
                max_diff = diff;
            }
            v[s] = new_v;
            policy[s] = best_a;
        }
        if max_diff < tol {
            return Ok(MdpSolution {
                value_function: v,
                policy,
                n_iterations: iter + 1,
                converged: true,
                max_diff,
            });
        }
    }

    Ok(MdpSolution {
        value_function: v,
        policy,
        n_iterations: max_iter,
        converged: false,
        max_diff,
    })
}

// ─────────────────────────────────────────────────────────────────────────────
// State-action occupancy measure
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the discounted state-action occupancy measure under a deterministic policy.
///
/// `d(s, a) = Σ_{t=0}^∞ γ^t P(S_t = s, A_t = a | π, μ_0)`
///
/// where `μ_0 = initial_distribution`.  Computed by iterating the Bellman
/// flow equations until convergence.
///
/// Returns a matrix of shape `(n_states, n_actions)`.
pub fn state_action_occupancy(
    mdp: &Mdp,
    policy: &[usize],
    initial_distribution: &[f64],
) -> Array2<f64> {
    let n = mdp.n_states;
    let a = mdp.n_actions;
    // Validate lengths (silently cap to valid range)
    let init_len = initial_distribution.len().min(n);

    // State occupancy: ρ(s) = μ_0(s) + γ Σ_{s',a'} T(s',a',s) π(a'|s') ρ(s')
    // For deterministic policy π(a|s) = 1[a = policy[s]]
    // We iterate the state occupancy until convergence.
    let mut rho = vec![0.0_f64; n];
    for s in 0..init_len {
        rho[s] = initial_distribution[s];
    }

    let max_iter = 10_000;
    let tol = 1e-9;
    for _ in 0..max_iter {
        let mut rho_new = vec![0.0_f64; n];
        // Initial contribution
        for s in 0..init_len {
            rho_new[s] += initial_distribution[s];
        }
        // Flow from previous occupancy
        for s_prev in 0..n {
            let act = if s_prev < policy.len() {
                policy[s_prev].min(a - 1)
            } else {
                0
            };
            for s_next in 0..n {
                let flow = mdp.gamma * mdp.transition[[s_prev, act, s_next]] * rho[s_prev];
                rho_new[s_next] += flow;
            }
        }
        let max_diff: f64 = rho_new
            .iter()
            .zip(rho.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0_f64, f64::max);
        rho = rho_new;
        if max_diff < tol {
            break;
        }
    }

    // d(s, a) = ρ(s) * 1[a = policy[s]]
    let mut d = Array2::<f64>::zeros((n, a));
    for s in 0..n {
        let act = if s < policy.len() {
            policy[s].min(a - 1)
        } else {
            0
        };
        d[[s, act]] = rho[s];
    }
    d
}

// ─────────────────────────────────────────────────────────────────────────────
// Maximum Entropy IRL
// ─────────────────────────────────────────────────────────────────────────────

/// Maximum Entropy Inverse Reinforcement Learning (Ziebart et al., 2008).
///
/// Infers a reward function `R(s, a)` that maximises the likelihood of the
/// observed demonstrations under the maximum-entropy policy distribution.
///
/// The algorithm alternates:
/// 1. Soft value iteration to compute soft policy.
/// 2. Computing expected feature counts under soft policy.
/// 3. Gradient step on reward parameters.
///
/// Here the "features" are one-hot state-action indicators, so the learned
/// reward is a per-state-action scalar.
///
/// Parameters:
/// - `demonstrations` – each element is a `(state, action)` sequence.
/// - `learning_rate` – gradient ascent step size.
/// - `n_iter` – number of gradient ascent iterations.
///
/// Returns the inferred reward matrix of shape `(n_states, n_actions)`.
pub fn max_entropy_irl(
    mdp: &Mdp,
    demonstrations: &[Vec<(usize, usize)>],
    learning_rate: f64,
    n_iter: usize,
) -> Result<Array2<f64>, OptimizeError> {
    if demonstrations.is_empty() {
        return Err(OptimizeError::ValueError(
            "demonstrations must be non-empty".to_string(),
        ));
    }
    if learning_rate <= 0.0 {
        return Err(OptimizeError::ValueError(
            "learning_rate must be positive".to_string(),
        ));
    }
    if n_iter == 0 {
        return Err(OptimizeError::ValueError(
            "n_iter must be > 0".to_string(),
        ));
    }

    let ns = mdp.n_states;
    let na = mdp.n_actions;

    // Empirical state-action feature counts from demonstrations
    let mut empirical_counts = Array2::<f64>::zeros((ns, na));
    let mut total_transitions = 0usize;
    for demo in demonstrations {
        for &(s, a) in demo {
            if s < ns && a < na {
                empirical_counts[[s, a]] += 1.0;
                total_transitions += 1;
            }
        }
    }
    if total_transitions > 0 {
        let norm = 1.0 / total_transitions as f64;
        for v in empirical_counts.iter_mut() {
            *v *= norm;
        }
    }

    // Reward parameters θ (one per state-action pair), initialised to zero
    let mut theta = Array2::<f64>::zeros((ns, na));

    for _ in 0..n_iter {
        // Soft value iteration with reward = theta
        let soft_v = soft_value_iteration(mdp, &theta, 1e-6, 1000);

        // Soft policy: π(a|s) ∝ exp(Q_soft(s,a))
        // Q_soft(s,a) = θ(s,a) + γ Σ_{s'} T(s,a,s') V_soft(s')
        let mut soft_policy = Array2::<f64>::zeros((ns, na));
        for s in 0..ns {
            let log_probs: Vec<f64> = (0..na)
                .map(|a| {
                    let fut: f64 = (0..ns)
                        .map(|sp| mdp.transition[[s, a, sp]] * soft_v[sp])
                        .sum();
                    theta[[s, a]] + mdp.gamma * fut
                })
                .collect();
            let max_lp = log_probs.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
            let exps: Vec<f64> = log_probs.iter().map(|&lp| (lp - max_lp).exp()).collect();
            let sum_exp: f64 = exps.iter().sum();
            for a in 0..na {
                soft_policy[[s, a]] = if sum_exp > 1e-300 {
                    exps[a] / sum_exp
                } else {
                    1.0 / na as f64
                };
            }
        }

        // Expected state-action counts under soft policy (using uniform start distribution)
        let init_dist: Vec<f64> = vec![1.0 / ns as f64; ns];
        let expected_counts =
            state_action_occupancy_soft(mdp, &soft_policy, &init_dist);

        // Gradient: empirical_counts - expected_counts
        for s in 0..ns {
            for a in 0..na {
                theta[[s, a]] +=
                    learning_rate * (empirical_counts[[s, a]] - expected_counts[[s, a]]);
            }
        }
    }

    Ok(theta)
}

/// Soft value iteration for MaxEnt IRL.
///
/// Computes `V_soft(s) = log Σ_a exp(θ(s,a) + γ Σ_{s'} T(s,a,s') V_soft(s'))`.
fn soft_value_iteration(
    mdp: &Mdp,
    theta: &Array2<f64>,
    tol: f64,
    max_iter: usize,
) -> Vec<f64> {
    let ns = mdp.n_states;
    let na = mdp.n_actions;
    let mut v = vec![0.0_f64; ns];

    for _ in 0..max_iter {
        let mut max_diff = 0.0_f64;
        for s in 0..ns {
            let log_sum_exp = {
                let qs: Vec<f64> = (0..na)
                    .map(|a| {
                        let fut: f64 = (0..ns)
                            .map(|sp| mdp.transition[[s, a, sp]] * v[sp])
                            .sum();
                        theta[[s, a]] + mdp.gamma * fut
                    })
                    .collect();
                let max_q = qs.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
                let sum_exp: f64 = qs.iter().map(|&q| (q - max_q).exp()).sum();
                max_q + sum_exp.ln()
            };
            let diff = (log_sum_exp - v[s]).abs();
            if diff > max_diff {
                max_diff = diff;
            }
            v[s] = log_sum_exp;
        }
        if max_diff < tol {
            break;
        }
    }
    v
}

/// Compute state-action occupancy with a soft (stochastic) policy.
fn state_action_occupancy_soft(
    mdp: &Mdp,
    soft_policy: &Array2<f64>,
    initial_distribution: &[f64],
) -> Array2<f64> {
    let ns = mdp.n_states;
    let na = mdp.n_actions;
    let init_len = initial_distribution.len().min(ns);

    let mut rho = vec![0.0_f64; ns];
    for s in 0..init_len {
        rho[s] = initial_distribution[s];
    }

    let tol = 1e-9;
    for _ in 0..10_000 {
        let mut rho_new = vec![0.0_f64; ns];
        for s in 0..init_len {
            rho_new[s] += initial_distribution[s];
        }
        for s_prev in 0..ns {
            for a in 0..na {
                let pi_sa = soft_policy[[s_prev, a]];
                if pi_sa < 1e-15 {
                    continue;
                }
                for s_next in 0..ns {
                    rho_new[s_next] +=
                        mdp.gamma * mdp.transition[[s_prev, a, s_next]] * pi_sa * rho[s_prev];
                }
            }
        }
        let max_diff: f64 = rho_new
            .iter()
            .zip(rho.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0_f64, f64::max);
        rho = rho_new;
        if max_diff < tol {
            break;
        }
    }

    let mut d = Array2::<f64>::zeros((ns, na));
    for s in 0..ns {
        for a in 0..na {
            d[[s, a]] = rho[s] * soft_policy[[s, a]];
        }
    }
    d
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{Array2, Array3};
    use crate::mdp::tabular::{value_iteration, simulate};

    fn three_state_mdp() -> Mdp {
        let n = 3;
        let a = 2;
        let mut t = Array3::<f64>::zeros((n, a, n));
        t[[0, 0, 1]] = 1.0;
        t[[1, 0, 2]] = 1.0;
        t[[2, 0, 2]] = 1.0;
        t[[0, 1, 0]] = 1.0;
        t[[1, 1, 1]] = 1.0;
        t[[2, 1, 2]] = 1.0;
        let mut r = Array3::<f64>::zeros((n, a, n));
        r[[1, 0, 2]] = 1.0;
        Mdp::new(n, a, t, r, 0.9).expect("unexpected None or Err")
    }

    fn stochastic_mdp() -> Mdp {
        let n = 3;
        let a = 2;
        let mut t = Array3::<f64>::zeros((n, a, n));
        t[[0, 0, 1]] = 0.7;
        t[[0, 0, 2]] = 0.3;
        t[[0, 1, 0]] = 1.0;
        t[[1, 0, 2]] = 1.0;
        t[[1, 1, 2]] = 1.0;
        t[[2, 0, 2]] = 1.0;
        t[[2, 1, 2]] = 1.0;
        let mut r = Array3::<f64>::zeros((n, a, n));
        r[[0, 0, 1]] = 0.5;
        r[[0, 0, 2]] = 1.0;
        r[[1, 0, 2]] = 2.0;
        r[[1, 1, 2]] = 2.0;
        Mdp::new(n, a, t, r, 0.9).expect("unexpected None or Err")
    }

    // ── RTDP ──────────────────────────────────────────────────────────────────

    #[test]
    fn test_rtdp_converges() {
        let mdp = three_state_mdp();
        let sol = rtdp(&mdp, 0, 500, 50, 1e-4).expect("failed to create sol");
        // RTDP might not fully converge in few trials but should run without error
        assert!(sol.n_iterations == 500);
    }

    #[test]
    fn test_rtdp_bad_initial_state() {
        let mdp = three_state_mdp();
        assert!(rtdp(&mdp, 99, 10, 10, 1e-4).is_err());
    }

    #[test]
    fn test_rtdp_value_nonnegative() {
        let mdp = three_state_mdp();
        let sol = rtdp(&mdp, 0, 1000, 100, 1e-4).expect("failed to create sol");
        // All values should be >= 0 since all rewards are >= 0
        for &v in &sol.value_function {
            assert!(v >= -1e-9, "value {} < 0", v);
        }
    }

    #[test]
    fn test_rtdp_stochastic_mdp() {
        let mdp = stochastic_mdp();
        let sol = rtdp(&mdp, 0, 2000, 50, 1e-3).expect("failed to create sol");
        assert_eq!(sol.value_function.len(), 3);
        assert_eq!(sol.policy.len(), 3);
    }

    // ── Prioritized Sweeping ───────────────────────────────────────────────────

    #[test]
    fn test_prioritized_sweeping_value_close_to_vi() {
        let mdp = three_state_mdp();
        let vi = value_iteration(&mdp, 1e-9, 10_000).expect("failed to create vi");
        let ps = prioritized_sweeping(&mdp, 10_000, 1e-10, 1e-6).expect("failed to create ps");
        for s in 0..mdp.n_states {
            assert!(
                (ps.value_function[s] - vi.value_function[s]).abs() < 1e-3,
                "state {}: ps={} vi={}",
                s,
                ps.value_function[s],
                vi.value_function[s]
            );
        }
    }

    #[test]
    fn test_prioritized_sweeping_policy_shape() {
        let mdp = three_state_mdp();
        let sol = prioritized_sweeping(&mdp, 1000, 1e-6, 1e-6).expect("failed to create sol");
        assert_eq!(sol.policy.len(), mdp.n_states);
        for &a in &sol.policy {
            assert!(a < mdp.n_actions);
        }
    }

    #[test]
    fn test_prioritized_sweeping_bad_theta() {
        let mdp = three_state_mdp();
        assert!(prioritized_sweeping(&mdp, 100, -0.1, 1e-6).is_err());
    }

    // ── Stochastic Shortest Path ───────────────────────────────────────────────

    #[test]
    fn test_ssp_goal_value_zero() {
        let mdp = three_state_mdp();
        let sol = stochastic_shortest_path(&mdp, 2, 10_000, 1e-9).expect("failed to create sol");
        assert!(sol.value_function[2].abs() < 1e-9, "goal value = {}", sol.value_function[2]);
    }

    #[test]
    fn test_ssp_bad_goal_state() {
        let mdp = three_state_mdp();
        assert!(stochastic_shortest_path(&mdp, 99, 100, 1e-9).is_err());
    }

    #[test]
    fn test_ssp_convergence() {
        let mdp = three_state_mdp();
        let sol = stochastic_shortest_path(&mdp, 2, 10_000, 1e-9).expect("failed to create sol");
        assert!(sol.converged, "SSP did not converge");
    }

    #[test]
    fn test_ssp_values_consistent_with_vi() {
        // With gamma < 1, SSP and VI with goal terminal should agree on V(0) order.
        let mdp = three_state_mdp();
        let vi = value_iteration(&mdp, 1e-9, 10_000).expect("failed to create vi");
        let ssp = stochastic_shortest_path(&mdp, 2, 10_000, 1e-9).expect("failed to create ssp");
        // Both should agree that state 0 has lower value than state 1 (further from goal)
        // or the ordering makes sense for the problem
        assert!(vi.value_function[0] <= vi.value_function[1] + 1e-6
            || ssp.value_function[0] <= ssp.value_function[1] + 1e-6);
    }

    // ── Occupancy measure ──────────────────────────────────────────────────────

    #[test]
    fn test_occupancy_shape() {
        let mdp = three_state_mdp();
        let policy = vec![0usize, 0, 0];
        let init = vec![1.0, 0.0, 0.0];
        let d = state_action_occupancy(&mdp, &policy, &init);
        assert_eq!(d.shape(), [3, 2]);
    }

    #[test]
    fn test_occupancy_nonnegative() {
        let mdp = three_state_mdp();
        let policy = vec![0usize, 0, 0];
        let init = vec![1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0];
        let d = state_action_occupancy(&mdp, &policy, &init);
        for &v in d.iter() {
            assert!(v >= -1e-9, "negative occupancy {}", v);
        }
    }

    #[test]
    fn test_occupancy_zero_for_non_policy_actions() {
        // With deterministic policy π(s)=0 for all s, d(s,1) should be 0.
        let mdp = three_state_mdp();
        let policy = vec![0usize, 0, 0];
        let init = vec![1.0, 0.0, 0.0];
        let d = state_action_occupancy(&mdp, &policy, &init);
        // Action 1 never taken under this policy
        for s in 0..3 {
            assert!(d[[s, 1]].abs() < 1e-6, "d[{},1] = {} should be 0", s, d[[s, 1]]);
        }
    }

    // ── MaxEnt IRL ──────────────────────────────────────────────────────────────

    #[test]
    fn test_maxent_irl_output_shape() {
        let mdp = three_state_mdp();
        // Demonstrate optimal policy (always move right)
        let demos: Vec<Vec<(usize, usize)>> = vec![
            vec![(0, 0), (1, 0)],
            vec![(0, 0), (1, 0)],
        ];
        let reward = max_entropy_irl(&mdp, &demos, 0.01, 10).expect("failed to create reward");
        assert_eq!(reward.shape(), [3, 2]);
    }

    #[test]
    fn test_maxent_irl_empty_demos_error() {
        let mdp = three_state_mdp();
        let demos: Vec<Vec<(usize, usize)>> = vec![];
        assert!(max_entropy_irl(&mdp, &demos, 0.01, 10).is_err());
    }

    #[test]
    fn test_maxent_irl_bad_learning_rate() {
        let mdp = three_state_mdp();
        let demos = vec![vec![(0usize, 0usize)]];
        assert!(max_entropy_irl(&mdp, &demos, -0.1, 10).is_err());
    }

    #[test]
    fn test_maxent_irl_prefers_demonstrated_actions() {
        let mdp = three_state_mdp();
        // Always demonstrate action 0 from state 0
        let demos: Vec<Vec<(usize, usize)>> = (0..20)
            .map(|_| vec![(0usize, 0usize), (1usize, 0usize)])
            .collect();
        let reward = max_entropy_irl(&mdp, &demos, 0.1, 50).expect("failed to create reward");
        // After IRL, reward for demonstrated action 0 at state 0 should be
        // at least as high as action 1 at state 0 (or close to it).
        // This is a weak check since we only do a few iterations.
        assert!(reward[[0, 0]] >= reward[[0, 1]] - 0.5);
    }
}