optirs-core 0.3.1

OptiRS core optimization algorithms and utilities
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
// Natural Policy Gradients
//
// This module implements natural policy gradient methods that use the Fisher information
// matrix to precondition policy gradients for more efficient optimization.

#[allow(dead_code)]

use super::{PolicyNetwork, RLOptimizationMetrics, RLOptimizerConfig, TrajectoryBatch};
use crate::error::Result;
use scirs2_core::ndarray::{Array1, Array2, ScalarOperand};
use scirs2_core::numeric::Float;
use std::fmt::Debug;

/// Natural gradient configuration
#[derive(Debug, Clone)]
pub struct NaturalGradientConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Base RL configuration
    pub base_config: RLOptimizerConfig<T>,

    /// Fisher information matrix estimation method
    pub fisher_method: FisherEstimationMethod,

    /// Damping parameter for Fisher matrix regularization
    pub damping: T,

    /// Fisher matrix update frequency
    pub fisher_update_freq: usize,

    /// Use empirical Fisher information matrix
    pub use_empirical_fisher: bool,

    /// Conjugate gradient parameters
    pub cg_iters: usize,
    pub cg_tolerance: T,

    /// Natural gradient scaling factor
    pub natural_grad_scale: T,

    /// Enable Fisher matrix preconditioning
    pub enable_preconditioning: bool,

    /// Diagonal Fisher approximation
    pub diagonal_fisher: bool,

    /// Block diagonal Fisher approximation
    pub block_diagonal_fisher: bool,

    /// Kronecker factored approximation (K-FAC style)
    pub kronecker_factored: bool,
}

/// Fisher information matrix estimation methods
#[derive(Debug, Clone, Copy)]
pub enum FisherEstimationMethod {
    /// Empirical Fisher Information Matrix
    Empirical,

    /// True Fisher Information Matrix (using log-likelihood Hessian)
    True,

    /// Diagonal approximation
    Diagonal,

    /// Block diagonal approximation
    BlockDiagonal,

    /// Kronecker factored approximation
    KroneckerFactored,

    /// Gauss-Newton approximation
    GaussNewton,

    /// BFGS quasi-Newton approximation
    BFGS,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for NaturalGradientConfig<T> {
    fn default() -> Self {
        Self {
            base_config: RLOptimizerConfig::default(),
            fisher_method: FisherEstimationMethod::Empirical,
            damping: T::from(1e-4).unwrap_or_else(|| T::zero()),
            fisher_update_freq: 10,
            use_empirical_fisher: true,
            cg_iters: 10,
            cg_tolerance: T::from(1e-8).unwrap_or_else(|| T::zero()),
            natural_grad_scale: T::from(1.0).unwrap_or_else(|| T::zero()),
            enable_preconditioning: true,
            diagonal_fisher: false,
            block_diagonal_fisher: false,
            kronecker_factored: false,
        }
    }
}

/// Natural Policy Gradient optimizer
pub struct NaturalPolicyGradient<T: Float + Debug, P: PolicyNetwork<T>> {
    /// Configuration
    _config: NaturalGradientConfig<T>,

    /// Policy network
    policy: P,

    /// Fisher Information Matrix
    fisher_matrix: Option<Array2<T>>,

    /// Diagonal Fisher approximation
    fisher_diagonal: Option<Array1<T>>,

    /// Kronecker factors (for K-FAC style approximation)
    kronecker_factors: Option<KroneckerFactors<T>>,

    /// Empirical Fisher accumulator
    empirical_fisher_accumulator: FisherAccumulator<T>,

    /// Natural gradient state
    natural_grad_state: NaturalGradientState<T>,

    /// Update counter
    update_count: usize,

    /// Parameter dimension
    paramdim: usize,
}

/// Kronecker factorization components
#[derive(Debug, Clone)]
pub struct KroneckerFactors<T: Float + Debug + Send + Sync + 'static> {
    /// Input statistics (activation covariances)
    pub input_factors: Vec<Array2<T>>,

    /// Output statistics (gradient covariances)
    pub output_factors: Vec<Array2<T>>,

    /// Layer indices for factor mapping
    pub layer_indices: Vec<usize>,
}

/// Fisher information accumulator for empirical estimation
#[derive(Debug, Clone)]
pub struct FisherAccumulator<T: Float + Debug + Send + Sync + 'static> {
    /// Accumulated Fisher matrix
    pub fisher_sum: Array2<T>,

    /// Number of samples accumulated
    pub sample_count: usize,

    /// Gradient history for empirical Fisher
    pub gradient_history: Vec<Array1<T>>,

    /// Maximum history size
    pub max_history_size: usize,
}

/// Natural gradient optimization state
#[derive(Debug, Clone)]
pub struct NaturalGradientState<T: Float + Debug + Send + Sync + 'static> {
    /// Previous natural gradients
    pub prev_natural_grad: Option<Array1<T>>,

    /// Momentum for natural gradients
    pub momentum: T,

    /// Adaptive scaling factors
    pub adaptive_scales: Option<Array1<T>>,

    /// Trust region radius
    pub trust_radius: T,

    /// KL divergence history
    pub kl_history: Vec<T>,
}

impl<T: Float + Debug + ScalarOperand + std::ops::AddAssign + std::iter::Sum, P: PolicyNetwork<T + Send + Sync>>
    NaturalPolicyGradient<T, P>
{
    /// Create a new natural policy gradient optimizer
    pub fn new(_config: NaturalGradientConfig<T>, policy: P, paramdim: usize) -> Self {
        let fisher_accumulator = FisherAccumulator {
            fisher_sum: Array2::zeros((paramdim, paramdim)),
            sample_count: 0,
            gradient_history: Vec::new(),
            max_history_size: 1000,
        };

        let natural_grad_state = NaturalGradientState {
            prev_natural_grad: None,
            momentum: T::from(0.9).unwrap_or_else(|| T::zero()),
            adaptive_scales: None,
            trust_radius: T::from(1.0).unwrap_or_else(|| T::zero()),
            kl_history: Vec::new(),
        };

        Self {
            _config,
            policy,
            fisher_matrix: None,
            fisher_diagonal: None,
            kronecker_factors: None,
            empirical_fisher_accumulator: fisher_accumulator,
            natural_grad_state,
            update_count: 0,
            paramdim,
        }
    }

    /// Update using trajectory data
    pub fn update(
        &mut self,
        trajectory: TrajectoryBatch<T>,
        gradients: Array1<T>,
    ) -> Result<RLOptimizationMetrics<T>> {
        // Update Fisher information matrix
        if self.update_count % self._config.fisher_update_freq == 0 {
            self.update_fisher_information(&trajectory)?;
        }

        // Compute natural gradients
        let naturalgradients = self.compute_natural_gradients(&gradients)?;

        // Apply natural gradient update
        self.apply_natural_gradient_update(&naturalgradients)?;

        // Update state
        self.natural_grad_state.prev_natural_grad = Some(naturalgradients);
        self.update_count += 1;

        // Compute metrics
        let mut metrics = RLOptimizationMetrics::default();
        metrics.policy_grad_norm = self.vector_norm(&gradients);

        Ok(metrics)
    }

    /// Update Fisher Information Matrix
    fn update_fisher_information(&mut self, trajectory: &TrajectoryBatch<T>) -> Result<()> {
        match self._config.fisher_method {
            FisherEstimationMethod::Empirical => self.update_empirical_fisher(trajectory)?,
            FisherEstimationMethod::True => self.update_true_fisher(trajectory)?,
            FisherEstimationMethod::Diagonal => self.update_diagonal_fisher(trajectory)?,
            FisherEstimationMethod::BlockDiagonal => {
                self.update_block_diagonal_fisher(trajectory)?
            }
            FisherEstimationMethod::KroneckerFactored => {
                self.update_kronecker_factors(trajectory)?
            }
            _ => {
                // Fallback to empirical Fisher
                self.update_empirical_fisher(trajectory)?;
            }
        }

        Ok(())
    }

    /// Update empirical Fisher Information Matrix
    fn update_empirical_fisher(&mut self, trajectory: &TrajectoryBatch<T>) -> Result<()> {
        let batch_size = trajectory.observations.nrows();

        // Collect gradients for each sample
        for i in 0..batch_size {
            let obs = trajectory.observations.row(i).to_owned();
            let action = trajectory.actions.row(i).to_owned();

            // Compute log probability gradients
            let log_prob_grad = self.compute_log_prob_gradients(&obs, &action)?;

            // Add to empirical Fisher accumulator
            self.add_to_empirical_fisher(&log_prob_grad)?;
        }

        // Compute final empirical Fisher matrix
        self.finalize_empirical_fisher()?;

        Ok(())
    }

    /// Update true Fisher Information Matrix
    fn update_true_fisher(&mut self, trajectory: &TrajectoryBatch<T>) -> Result<()> {
        // True Fisher requires computing the Hessian of the log-likelihood
        // This is computationally expensive and often approximated
        self.update_empirical_fisher(trajectory) // Fallback for now
    }

    /// Update diagonal Fisher approximation
    fn update_diagonal_fisher(&mut self, trajectory: &TrajectoryBatch<T>) -> Result<()> {
        let mut diagonal = Array1::zeros(self.paramdim);
        let batch_size = trajectory.observations.nrows();

        for i in 0..batch_size {
            let obs = trajectory.observations.row(i).to_owned();
            let action = trajectory.actions.row(i).to_owned();

            let log_prob_grad = self.compute_log_prob_gradients(&obs, &action)?;
            diagonal = diagonal + log_prob_grad.mapv(|x| x * x);
        }

        diagonal = diagonal / T::from(batch_size).unwrap_or_else(|| T::zero());
        diagonal = diagonal + T::from(self._config.damping).unwrap_or_else(|| T::zero());

        self.fisher_diagonal = Some(diagonal);

        Ok(())
    }

    /// Update block diagonal Fisher approximation
    fn update_block_diagonal_fisher(&mut self, trajectory: &TrajectoryBatch<T>) -> Result<()> {
        // Block diagonal approximation groups parameters into blocks
        // and assumes independence between blocks
        Ok(())
    }

    /// Update Kronecker factorization
    fn update_kronecker_factors(&mut self, trajectory: &TrajectoryBatch<T>) -> Result<()> {
        // Kronecker factorization approximates the Fisher matrix as
        // a Kronecker product of smaller matrices (K-FAC style)
        Ok(())
    }

    /// Compute natural gradients
    fn compute_natural_gradients(&self, gradients: &Array1<T>) -> Result<Array1<T>> {
        if !self._config.enable_preconditioning {
            return Ok(gradients.clone());
        }

        let natural_grad = match self._config.fisher_method {
            FisherEstimationMethod::Diagonal => {
                if let Some(ref diag) = self.fisher_diagonal {
                    gradients / diag
                } else {
                    gradients.clone()
                }
            }
            _ => {
                if let Some(ref fisher) = self.fisher_matrix {
                    // Solve Fisher * natural_grad = gradients
                    self.solve_fisher_system(fisher, gradients)?
                } else {
                    gradients.clone()
                }
            }
        };

        // Apply scaling
        let scaled_natural_grad = natural_grad * self._config.natural_grad_scale;

        Ok(scaled_natural_grad)
    }

    /// Solve Fisher information system using conjugate gradient
    fn solve_fisher_system(&self, fisher: &Array2<T>, rhs: &Array1<T>) -> Result<Array1<T>> {
        let n = rhs.len();
        let mut x = Array1::zeros(n);
        let mut r = rhs.clone();
        let mut p = r.clone();
        let mut rsold = self.dot(&r, &r);

        for _i in 0..self._config.cg_iters {
            let ap = fisher.dot(&p);
            let alpha = rsold / self.dot(&p, &ap);

            x = &x + &(&p * alpha);
            r = &r - &(&ap * alpha);

            let rsnew = self.dot(&r, &r);

            if rsnew.sqrt() < self._config.cg_tolerance {
                break;
            }

            let beta = rsnew / rsold;
            p = &r + &(&p * beta);
            rsold = rsnew;
        }

        Ok(x)
    }

    /// Apply natural gradient update
    fn apply_natural_gradient_update(&mut self, naturalgradients: &Array1<T>) -> Result<()> {
        // In practice, this would update the policy network parameters
        // using the natural _gradients

        // Apply momentum if previous natural _gradients exist
        let update = if let Some(ref prev_ng) = self.natural_grad_state.prev_natural_grad {
            naturalgradients + &(prev_ng * self.natural_grad_state.momentum)
        } else {
            naturalgradients.clone()
        };

        // Apply trust region constraint
        let clipped_update = self.apply_trust_region_constraint(&update)?;

        // Update policy parameters (placeholder)
        self.update_policy_parameters(&clipped_update)?;

        Ok(())
    }

    /// Apply trust region constraint to natural gradient update
    fn apply_trust_region_constraint(&self, update: &Array1<T>) -> Result<Array1<T>> {
        let update_norm = self.vector_norm(update);
        let trust_radius = self.natural_grad_state.trust_radius;

        if update_norm <= trust_radius {
            Ok(update.clone())
        } else {
            Ok(update * (trust_radius / update_norm))
        }
    }

    /// Update policy network parameters
    fn update_policy_parameters(&mut self, update: &Array1<T>) -> Result<()> {
        // Placeholder for actual parameter _update
        // In practice, this would modify the policy network weights
        Ok(())
    }

    /// Compute log probability gradients
    fn compute_log_prob_gradients(
        &self,
        obs: &Array1<T>,
        _action: &Array1<T>,
    ) -> Result<Array1<T>> {
        // Placeholder for computing gradients of log probability
        // This would typically involve backpropagation through the policy network
        Ok(Array1::zeros(self.paramdim))
    }

    /// Add gradient to empirical Fisher accumulator
    fn add_to_empirical_fisher(&mut self, gradient: &Array1<T>) -> Result<()> {
        // Add outer product of gradient to Fisher sum
        for i in 0..self.paramdim {
            for j in 0..self.paramdim {
                self.empirical_fisher_accumulator.fisher_sum[[i, j]] += gradient[i] * gradient[j];
            }
        }

        self.empirical_fisher_accumulator.sample_count += 1;

        // Store gradient in history
        if self.empirical_fisher_accumulator.gradient_history.len()
            >= self.empirical_fisher_accumulator.max_history_size
        {
            self.empirical_fisher_accumulator.gradient_history.remove(0);
        }
        self.empirical_fisher_accumulator
            .gradient_history
            .push(gradient.clone());

        Ok(())
    }

    /// Finalize empirical Fisher matrix computation
    fn finalize_empirical_fisher(&mut self) -> Result<()> {
        if self.empirical_fisher_accumulator.sample_count == 0 {
            return Ok(());
        }

        // Normalize by sample count
        let fisher = &self.empirical_fisher_accumulator.fisher_sum
            / T::from(self.empirical_fisher_accumulator.sample_count).unwrap_or_else(|| T::zero());

        // Add damping for numerical stability
        let mut damped_fisher = fisher;
        for i in 0..self.paramdim {
            damped_fisher[[i, i]] += self._config.damping;
        }

        self.fisher_matrix = Some(damped_fisher);

        // Reset accumulator
        self.empirical_fisher_accumulator.fisher_sum.fill(T::zero());
        self.empirical_fisher_accumulator.sample_count = 0;

        Ok(())
    }

    /// Compute dot product
    fn dot(&self, a: &Array1<T>, b: &Array1<T>) -> T {
        a.iter().zip(b.iter()).map(|(&x, &y)| x * y).sum()
    }

    /// Compute vector norm
    fn vector_norm(&self, v: &Array1<T>) -> T {
        self.dot(v, v).sqrt()
    }

    /// Get current Fisher matrix
    pub fn get_fisher_matrix(&self) -> Option<&Array2<T>> {
        self.fisher_matrix.as_ref()
    }

    /// Get current natural gradient state
    pub fn get_natural_grad_state(&self) -> &NaturalGradientState<T> {
        &self.natural_grad_state
    }
}