scirs2-neural 0.4.3

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
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
//! Actor-Critic reinforcement learning algorithms

use crate::error::Result;
use crate::reinforcement::policy::PolicyNetwork;
use crate::reinforcement::value::ValueNetwork;
use crate::reinforcement::{ExperienceBatch, LossInfo};
use scirs2_core::ndarray::prelude::*;
use std::sync::Arc;

/// Base Actor-Critic structure combining a policy (actor) and value function (critic)
pub struct ActorCritic {
    actor: PolicyNetwork,
    critic: ValueNetwork,
    actor_lr: f32,
    critic_lr: f32,
    discount_factor: f32,
}

impl ActorCritic {
    /// Create a new Actor-Critic
    pub fn new(
        state_dim: usize,
        action_dim: usize,
        hidden_sizes: Vec<usize>,
        continuous: bool,
        actor_lr: f32,
        critic_lr: f32,
        discount_factor: f32,
    ) -> Result<Self> {
        let actor = PolicyNetwork::new(state_dim, action_dim, hidden_sizes.clone(), continuous)?;
        let critic = ValueNetwork::new(state_dim, 1, hidden_sizes)?;
        Ok(Self {
            actor,
            critic,
            actor_lr,
            critic_lr,
            discount_factor,
        })
    }

    /// Sample an action from the actor
    pub fn get_action(&self, state: &ArrayView1<f32>) -> Result<Array1<f32>> {
        self.actor.sample_action(state)
    }

    /// Estimate V(s) from the critic
    pub fn get_value(&self, state: &ArrayView1<f32>) -> Result<f32> {
        self.critic.predict(state)
    }

    /// Compute one-step TD advantages
    pub fn calculate_advantages(
        &self,
        rewards: &[f32],
        values: &[f32],
        next_value: f32,
        dones: &[bool],
    ) -> Vec<f32> {
        let mut advantages = Vec::with_capacity(rewards.len());
        for i in 0..rewards.len() {
            let next_val = if i + 1 < values.len() {
                values[i + 1]
            } else {
                next_value
            };
            let td_error = rewards[i]
                + if dones[i] {
                    0.0
                } else {
                    self.discount_factor * next_val
                }
                - values[i];
            advantages.push(td_error);
        }
        advantages
    }

    /// Learning rates
    pub fn learning_rates(&self) -> (f32, f32) {
        (self.actor_lr, self.critic_lr)
    }
}

// ── A2C ──────────────────────────────────────────────────────────────────────

/// Advantage Actor-Critic (A2C)
pub struct A2C {
    actor_critic: ActorCritic,
    entropy_coef: f32,
    value_loss_coef: f32,
}

impl A2C {
    /// Create a new A2C agent
    pub fn new(
        state_dim: usize,
        action_dim: usize,
        hidden_sizes: Vec<usize>,
        continuous: bool,
        actor_lr: f32,
        critic_lr: f32,
        discount_factor: f32,
        entropy_coef: f32,
        value_loss_coef: f32,
    ) -> Result<Self> {
        let actor_critic = ActorCritic::new(
            state_dim,
            action_dim,
            hidden_sizes,
            continuous,
            actor_lr,
            critic_lr,
            discount_factor,
        )?;
        Ok(Self {
            actor_critic,
            entropy_coef,
            value_loss_coef,
        })
    }

    /// Update the actor and critic from a trajectory
    ///
    /// Returns `(actor_loss, value_loss, entropy_bonus)`
    pub fn update(
        &mut self,
        states: &[Array1<f32>],
        actions: &[Array1<f32>],
        rewards: &[f32],
        dones: &[bool],
        next_state: &ArrayView1<f32>,
    ) -> Result<(f32, f32, f32)> {
        let n = states.len();
        if n == 0 {
            return Ok((0.0, 0.0, 0.0));
        }

        // Critic predictions
        let values: Vec<f32> = states
            .iter()
            .map(|s| self.actor_critic.get_value(&s.view()))
            .collect::<Result<Vec<_>>>()?;
        let next_value = self.actor_critic.get_value(next_state)?;

        // Advantages
        let advantages = self
            .actor_critic
            .calculate_advantages(rewards, &values, next_value, dones);

        // Policy loss: -log_prob * advantage
        let mut actor_loss = 0.0f32;
        let mut entropy = 0.0f32;
        for (i, s) in states.iter().enumerate() {
            let lp = self
                .actor_critic
                .actor
                .log_prob(&s.view(), &actions[i].view())?;
            actor_loss -= lp * advantages[i];
            entropy -= lp; // approximate entropy via -log_prob
        }
        actor_loss /= n as f32;
        entropy /= n as f32;

        // Value loss: MSE
        let next_val = if dones.last().copied().unwrap_or(false) {
            0.0
        } else {
            next_value
        };
        let mut returns = vec![0.0f32; n];
        returns[n - 1] = rewards[n - 1]
            + if dones[n - 1] {
                0.0
            } else {
                self.actor_critic.discount_factor * next_val
            };
        for i in (0..n - 1).rev() {
            returns[i] = rewards[i]
                + if dones[i] {
                    0.0
                } else {
                    self.actor_critic.discount_factor * returns[i + 1]
                };
        }
        let value_loss = values
            .iter()
            .zip(returns.iter())
            .map(|(v, r)| (v - r).powi(2))
            .sum::<f32>()
            / n as f32;

        Ok((
            actor_loss,
            value_loss * self.value_loss_coef,
            entropy * self.entropy_coef,
        ))
    }
}

// ── A3C ──────────────────────────────────────────────────────────────────────

/// Asynchronous Advantage Actor-Critic (A3C) wrapper
///
/// A3C uses multiple worker threads sharing a global network. This struct holds
/// the shared global actor-critic; workers are created externally and communicate
/// gradients back.
pub struct A3C {
    global: Arc<std::sync::Mutex<ActorCritic>>,
    n_workers: usize,
}

impl A3C {
    /// Create a new A3C with `n_workers` async worker slots
    pub fn new(
        state_dim: usize,
        action_dim: usize,
        hidden_sizes: Vec<usize>,
        continuous: bool,
        actor_lr: f32,
        critic_lr: f32,
        discount_factor: f32,
        n_workers: usize,
    ) -> Result<Self> {
        let ac = ActorCritic::new(
            state_dim,
            action_dim,
            hidden_sizes,
            continuous,
            actor_lr,
            critic_lr,
            discount_factor,
        )?;
        Ok(Self {
            global: Arc::new(std::sync::Mutex::new(ac)),
            n_workers,
        })
    }

    /// Get action from the global network
    pub fn get_action(&self, state: &ArrayView1<f32>) -> Result<Array1<f32>> {
        self.global
            .lock()
            .map_err(|_| {
                crate::error::NeuralError::InvalidArgument("A3C lock poisoned".to_string())
            })?
            .get_action(state)
    }

    /// Number of worker slots
    pub fn n_workers(&self) -> usize {
        self.n_workers
    }
}

// ── PPO ──────────────────────────────────────────────────────────────────────

/// Proximal Policy Optimization (PPO) with clipped surrogate
pub struct PPO {
    actor_critic: ActorCritic,
    clip_epsilon: f32,
    entropy_coef: f32,
    value_loss_coef: f32,
}

impl PPO {
    /// Create a new PPO agent
    pub fn new(
        state_dim: usize,
        action_dim: usize,
        hidden_sizes: Vec<usize>,
        continuous: bool,
        actor_lr: f32,
        critic_lr: f32,
        discount_factor: f32,
        clip_epsilon: f32,
        entropy_coef: f32,
        value_loss_coef: f32,
    ) -> Result<Self> {
        let actor_critic = ActorCritic::new(
            state_dim,
            action_dim,
            hidden_sizes,
            continuous,
            actor_lr,
            critic_lr,
            discount_factor,
        )?;
        Ok(Self {
            actor_critic,
            clip_epsilon,
            entropy_coef,
            value_loss_coef,
        })
    }

    /// Act: sample from the policy
    pub fn act(&self, state: &ArrayView1<f32>) -> Result<Array1<f32>> {
        self.actor_critic.get_action(state)
    }

    /// Compute the PPO clipped objective losses
    ///
    /// Returns `(policy_loss, value_loss, entropy_loss)`
    pub fn train_batch(
        &mut self,
        states: &ArrayView2<f32>,
        actions: &ArrayView2<f32>,
        rewards: &ArrayView1<f32>,
        next_states: &ArrayView2<f32>,
        dones: &ArrayView1<bool>,
    ) -> Result<(f32, f32, f32)> {
        let n = states.nrows();
        if n == 0 {
            return Ok((0.0, 0.0, 0.0));
        }
        let mut policy_loss = 0.0f32;
        let mut value_loss = 0.0f32;
        let mut entropy = 0.0f32;

        for i in 0..n {
            let s = states.row(i);
            let a = actions.row(i);
            let ns = next_states.row(i);

            let v = self.actor_critic.critic.predict(&s)?;
            let nv = self.actor_critic.critic.predict(&ns)?;
            let advantage = rewards[i]
                + if dones[i] {
                    0.0
                } else {
                    self.actor_critic.discount_factor * nv
                }
                - v;

            let log_prob = self.actor_critic.actor.log_prob(&s, &a)?;
            let ratio = log_prob.exp(); // simplified: ratio = exp(lp_new - lp_old) ≈ exp(lp_new)
            let clipped = ratio.clamp(1.0 - self.clip_epsilon, 1.0 + self.clip_epsilon);
            policy_loss -= (ratio * advantage).min(clipped * advantage);
            value_loss += (v
                - (rewards[i]
                    + if dones[i] {
                        0.0
                    } else {
                        self.actor_critic.discount_factor * nv
                    }))
            .powi(2);
            entropy -= log_prob;
        }
        policy_loss /= n as f32;
        value_loss = value_loss / n as f32 * self.value_loss_coef;
        entropy = entropy / n as f32 * self.entropy_coef;
        Ok((policy_loss, value_loss, entropy))
    }

    /// Clip epsilon accessor
    pub fn clip_epsilon(&self) -> f32 {
        self.clip_epsilon
    }

    /// Save to disk (stub — no serialization implemented)
    pub fn save(&self, _path: &str) -> Result<()> {
        Ok(())
    }

    /// Load from disk (stub)
    pub fn load(&mut self, _path: &str) -> Result<()> {
        Ok(())
    }
}

// ── SAC ──────────────────────────────────────────────────────────────────────

/// Configuration for Soft Actor-Critic
#[derive(Debug, Clone)]
pub struct SACConfig {
    pub state_dim: usize,
    pub action_dim: usize,
    pub hidden_sizes: Vec<usize>,
    pub actor_lr: f32,
    pub critic_lr: f32,
    pub alpha: f32,
    pub gamma: f32,
    pub tau: f32,
}

impl Default for SACConfig {
    fn default() -> Self {
        Self {
            state_dim: 4,
            action_dim: 2,
            hidden_sizes: vec![64, 64],
            actor_lr: 3e-4,
            critic_lr: 3e-4,
            alpha: 0.2,
            gamma: 0.99,
            tau: 5e-3,
        }
    }
}

/// Soft Actor-Critic (maximum-entropy RL, Haarnoja et al. 2018)
pub struct SAC {
    actor: PolicyNetwork,
    q1: ValueNetwork,
    q2: ValueNetwork,
    config: SACConfig,
}

impl SAC {
    /// Create a new SAC agent
    pub fn new(config: SACConfig) -> Result<Self> {
        // State + action concatenated as Q-network input
        let q_input_dim = config.state_dim + config.action_dim;
        let actor = PolicyNetwork::new(
            config.state_dim,
            config.action_dim,
            config.hidden_sizes.clone(),
            true,
        )?;
        let q1 = ValueNetwork::new(q_input_dim, 1, config.hidden_sizes.clone())?;
        let q2 = ValueNetwork::new(q_input_dim, 1, config.hidden_sizes.clone())?;
        Ok(Self {
            actor,
            q1,
            q2,
            config,
        })
    }

    /// Select an action
    pub fn act(&self, state: &ArrayView1<f32>) -> Result<Array1<f32>> {
        self.actor.sample_action(state)
    }

    /// Update from a batch of experiences
    pub fn update(&mut self, batch: &ExperienceBatch) -> Result<LossInfo> {
        let n = batch.states.nrows();
        if n == 0 {
            return Ok(LossInfo {
                policy_loss: Some(0.0),
                value_loss: Some(0.0),
                entropy_loss: Some(0.0),
                total_loss: 0.0,
                metrics: std::collections::HashMap::new(),
            });
        }
        // Simplified SAC update: compute soft value and policy losses
        let mut actor_loss = 0.0f32;
        let mut critic_loss = 0.0f32;
        let mut entropy = 0.0f32;

        for i in 0..n {
            let s = batch.states.row(i);
            let a = batch.actions.row(i);
            // Actor loss: -Q(s, π(s)) + α * log π(s)
            let log_prob = self.actor.log_prob(&s, &a)?;
            // Compute Q-value estimate (simplified: use V-net as proxy)
            let sa_dim = s.len() + a.len();
            let sa: Array1<f32> = Array1::from_iter(s.iter().chain(a.iter()).cloned());
            let sa_batch = sa.insert_axis(Axis(0));
            if sa_batch.shape()[1] == self.q1.output_dim() + sa_dim {
                // Proper Q evaluation skipped (dimension mismatch would be a bug)
            }
            actor_loss += -log_prob; // simplified: maximize log prob
            entropy -= log_prob;
            critic_loss += (batch.rewards[i]).powi(2); // placeholder
        }
        actor_loss /= n as f32;
        critic_loss /= n as f32;
        entropy /= n as f32;

        let total = actor_loss + critic_loss + self.config.alpha * entropy;
        let mut metrics = std::collections::HashMap::new();
        metrics.insert("entropy".to_string(), entropy);

        Ok(LossInfo {
            policy_loss: Some(actor_loss),
            value_loss: Some(critic_loss),
            entropy_loss: Some(entropy),
            total_loss: total,
            metrics,
        })
    }

    /// Configuration accessor
    pub fn config(&self) -> &SACConfig {
        &self.config
    }
}

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

    #[test]
    fn test_actor_critic_get_action() {
        let ac = ActorCritic::new(4, 2, vec![8], false, 1e-3, 1e-3, 0.99).expect("create ok");
        let state = Array1::zeros(4);
        let action = ac.get_action(&state.view()).expect("get_action ok");
        assert_eq!(action.len(), 2);
    }

    #[test]
    fn test_actor_critic_get_value() {
        let ac = ActorCritic::new(4, 2, vec![8], false, 1e-3, 1e-3, 0.99).expect("create ok");
        let state = Array1::zeros(4);
        let val = ac.get_value(&state.view()).expect("get_value ok");
        assert!(val.is_finite());
    }

    #[test]
    fn test_actor_critic_advantages() {
        let ac = ActorCritic::new(4, 2, vec![8], false, 1e-3, 1e-3, 0.99).expect("create ok");
        let rewards = vec![1.0, 1.0, 1.0];
        let values = vec![0.5, 0.5, 0.5];
        let dones = vec![false, false, true];
        let advs = ac.calculate_advantages(&rewards, &values, 0.0, &dones);
        assert_eq!(advs.len(), 3);
        assert!((advs[2] - 0.5).abs() < 1e-5, "terminal advantage");
    }

    #[test]
    fn test_a2c_create_and_update() {
        let mut a2c =
            A2C::new(4, 2, vec![8], false, 1e-3, 1e-3, 0.99, 0.01, 0.5).expect("create ok");
        let states = vec![Array1::zeros(4); 4];
        let actions: Vec<Array1<f32>> = (0..4).map(|_| Array1::from_vec(vec![1.0, 0.0])).collect();
        let rewards = vec![1.0f32; 4];
        let dones = vec![false; 4];
        let next_state = Array1::zeros(4);
        let (pl, vl, el) = a2c
            .update(&states, &actions, &rewards, &dones, &next_state.view())
            .expect("update ok");
        assert!(pl.is_finite());
        assert!(vl.is_finite());
        assert!(el.is_finite());
    }

    #[test]
    fn test_a3c_create() {
        let a3c = A3C::new(4, 2, vec![8], false, 1e-3, 1e-3, 0.99, 4).expect("create ok");
        assert_eq!(a3c.n_workers(), 4);
        let state = Array1::zeros(4);
        let action = a3c.get_action(&state.view()).expect("action ok");
        assert_eq!(action.len(), 2);
    }

    #[test]
    fn test_ppo_create_and_act() {
        let ppo =
            PPO::new(4, 2, vec![8], false, 1e-3, 1e-3, 0.99, 0.2, 0.01, 0.5).expect("create ok");
        let state = Array1::zeros(4);
        let action = ppo.act(&state.view()).expect("act ok");
        assert_eq!(action.len(), 2);
    }

    #[test]
    fn test_ppo_train_batch() {
        let mut ppo =
            PPO::new(4, 2, vec![8], false, 1e-3, 1e-3, 0.99, 0.2, 0.01, 0.5).expect("create ok");
        let states = Array2::zeros((4, 4));
        let actions = Array2::from_shape_fn((4, 2), |(i, j)| if j == i % 2 { 1.0 } else { 0.0 });
        let rewards = Array1::ones(4);
        let next_states = Array2::zeros((4, 4));
        let dones = Array1::from_elem(4, false);
        let (pl, vl, el) = ppo
            .train_batch(
                &states.view(),
                &actions.view(),
                &rewards.view(),
                &next_states.view(),
                &dones.view(),
            )
            .expect("train_batch ok");
        assert!(pl.is_finite());
        assert!(vl.is_finite());
        assert!(el.is_finite());
    }

    #[test]
    fn test_sac_create_and_act() {
        let config = SACConfig {
            state_dim: 4,
            action_dim: 2,
            hidden_sizes: vec![8],
            ..SACConfig::default()
        };
        let sac = SAC::new(config).expect("create ok");
        let state = Array1::zeros(4);
        let action = sac.act(&state.view()).expect("act ok");
        assert_eq!(action.len(), 2);
    }
}