aip-sci 0.1.0

Affective Interaction Programming - 情感交互编程
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
use crate::director::core::{
    DirectorError, InteractionStrategyTrainer, Trajectory, TrainingResult,
};
use crate::director::roguelite::core::RogueliteDirector;
use candle_core::{Device, Tensor, DType, Var};
use candle_nn::{AdamW, Optimizer, ParamsAdamW};
use std::collections::HashMap;
use std::path::Path;

const PPO_CLIP_EPS: f32 = 0.2;
const GAE_LAMBDA: f32 = 0.95;
const ENTROPY_COEF: f32 = 0.01;
const LEARNING_RATE: f64 = 1e-4;
const EPOCHS: usize = 10;
const STATE_DIM: usize = 23;
const ACTION_DIM: usize = 7;
const HIDDEN_DIM: usize = 128;

fn relu(x: &Tensor) -> candle_core::Result<Tensor> {
    x.maximum(&x.zeros_like()?)
}

fn sigmoid(x: &Tensor) -> candle_core::Result<Tensor> {
    let clamped = x.clamp(-10.0, 10.0)?;
    (clamped.neg()?.exp()? + 1.0)?.recip()
}

fn xavier_init(rows: usize, cols: usize, device: &Device) -> candle_core::Result<Var> {
    let scale = (2.0 / (rows + cols) as f64).sqrt() as f32;
    let data: Vec<f32> = (0..rows * cols)
        .map(|i| ((i as f32 * 0.01) % 1.0 - 0.5) * 2.0 * scale)
        .collect();
    let tensor = Tensor::from_vec(data, (rows, cols), device)?;
    Var::from_tensor(&tensor)
}

pub struct RogueliteDirectorTrainer {
    device: Device,
    actor_fc1_weight: Var,
    actor_fc1_bias: Var,
    actor_fc2_weight: Var,
    actor_fc2_bias: Var,
    actor_fc3_weight: Var,
    actor_fc3_bias: Var,
    critic_fc1_weight: Var,
    critic_fc1_bias: Var,
    critic_fc2_weight: Var,
    critic_fc2_bias: Var,
    critic_fc3_weight: Var,
    critic_fc3_bias: Var,
}

impl RogueliteDirectorTrainer {
    pub fn new(device: Device) -> Result<Self, DirectorError> {
        let actor_fc1_weight = xavier_init(HIDDEN_DIM, STATE_DIM, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        let actor_fc1_bias = Var::zeros(HIDDEN_DIM, DType::F32, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        let actor_fc2_weight = xavier_init(HIDDEN_DIM, HIDDEN_DIM, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        let actor_fc2_bias = Var::zeros(HIDDEN_DIM, DType::F32, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        let actor_fc3_weight = xavier_init(ACTION_DIM, HIDDEN_DIM, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        let actor_fc3_bias = Var::zeros(ACTION_DIM, DType::F32, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        let critic_fc1_weight = xavier_init(HIDDEN_DIM, STATE_DIM, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        let critic_fc1_bias = Var::zeros(HIDDEN_DIM, DType::F32, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        let critic_fc2_weight = xavier_init(HIDDEN_DIM, HIDDEN_DIM, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        let critic_fc2_bias = Var::zeros(HIDDEN_DIM, DType::F32, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        let critic_fc3_weight = xavier_init(1, HIDDEN_DIM, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        let critic_fc3_bias = Var::zeros(1, DType::F32, &device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        Ok(Self {
            device,
            actor_fc1_weight,
            actor_fc1_bias,
            actor_fc2_weight,
            actor_fc2_bias,
            actor_fc3_weight,
            actor_fc3_bias,
            critic_fc1_weight,
            critic_fc1_bias,
            critic_fc2_weight,
            critic_fc2_bias,
            critic_fc3_weight,
            critic_fc3_bias,
        })
    }
    
    pub fn compute_gae(rewards: &[f32], values: &[f32], gamma: f32) -> Vec<f32> {
        let n = rewards.len();
        if n == 0 {
            return Vec::new();
        }
        
        let mut advantages = Vec::with_capacity(n);
        let mut gae = 0.0;
        
        for t in (0..n).rev() {
            let next_value = if t + 1 < values.len() { values[t + 1] } else { 0.0 };
            let delta = rewards[t] + gamma * next_value - values[t];
            gae = delta + gamma * GAE_LAMBDA * gae;
            advantages.insert(0, gae);
        }
        
        advantages
    }
    
    pub fn compute_reward(
        progress: f32,
        emotion_improvement: f32,
        retention: bool,
    ) -> f32 {
        let retention_reward = if retention { 1.0 } else { 0.0 };
        0.4 * progress + 0.4 * emotion_improvement + 0.2 * retention_reward
    }
    
    pub fn ppo_clip(ratio: f32, advantage: f32) -> f32 {
        let clipped = ratio.clamp(1.0 - PPO_CLIP_EPS, 1.0 + PPO_CLIP_EPS);
        -(ratio * advantage).min(clipped * advantage)
    }
    
    fn actor_forward(&self, state_tensor: &Tensor) -> candle_core::Result<Tensor> {
        let x = state_tensor.matmul(&self.actor_fc1_weight.t()?)?;
        let x = x.broadcast_add(&self.actor_fc1_bias)?;
        let x = relu(&x)?;
        
        let x = x.matmul(&self.actor_fc2_weight.t()?)?;
        let x = x.broadcast_add(&self.actor_fc2_bias)?;
        let x = relu(&x)?;
        
        let x = x.matmul(&self.actor_fc3_weight.t()?)?;
        let x = x.broadcast_add(&self.actor_fc3_bias)?;
        sigmoid(&x)
    }
    
    fn critic_forward(&self, state_tensor: &Tensor) -> candle_core::Result<Tensor> {
        let x = state_tensor.matmul(&self.critic_fc1_weight.t()?)?;
        let x = x.broadcast_add(&self.critic_fc1_bias)?;
        let x = relu(&x)?;
        
        let x = x.matmul(&self.critic_fc2_weight.t()?)?;
        let x = x.broadcast_add(&self.critic_fc2_bias)?;
        let x = relu(&x)?;
        
        let x = x.matmul(&self.critic_fc3_weight.t()?)?;
        x.broadcast_add(&self.critic_fc3_bias)
    }
    
    fn compute_entropy(action_probs: &Tensor) -> candle_core::Result<Tensor> {
        let log_probs = action_probs.log()?;
        let entropy = (action_probs * log_probs)?.neg()?.sum_all()?;
        Ok(entropy)
    }
    
    pub fn train_epoch(&mut self, trajectories: &[Trajectory]) -> Result<f32, DirectorError> {
        let params = ParamsAdamW {
            lr: LEARNING_RATE,
            ..Default::default()
        };
        let mut optimizer = AdamW::new(
            vec![
                self.actor_fc1_weight.clone(), self.actor_fc1_bias.clone(),
                self.actor_fc2_weight.clone(), self.actor_fc2_bias.clone(),
                self.actor_fc3_weight.clone(), self.actor_fc3_bias.clone(),
                self.critic_fc1_weight.clone(), self.critic_fc1_bias.clone(),
                self.critic_fc2_weight.clone(), self.critic_fc2_bias.clone(),
                self.critic_fc3_weight.clone(), self.critic_fc3_bias.clone(),
            ],
            params,
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        let mut total_loss = 0.0f32;
        let mut num_samples = 0;
        
        for trajectory in trajectories {
            if trajectory.steps.is_empty() {
                continue;
            }
            
            let states: Vec<f32> = trajectory.steps.iter()
                .flat_map(|step| {
                    let mut v = Vec::new();
                    for i in 0..8 {
                        v.push(*step.state.user_traits.get(&(i as u32)).unwrap_or(&0.5));
                    }
                    for i in 0..6 {
                        v.push(*step.state.env_state.get(&(i as u32)).unwrap_or(&0.5));
                    }
                    v.push(step.state.emotion.valence);
                    v.push(step.state.emotion.arousal);
                    v.push(step.state.emotion.dominance);
                    v
                })
                .collect();
            
            if states.is_empty() {
                continue;
            }
            
            let state_tensor = Tensor::from_vec(
                states.clone(),
                (trajectory.steps.len(), STATE_DIM),
                &self.device,
            ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let values = self.critic_forward(&state_tensor)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            let values_flat = values.flatten_all()
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            let values_vec: Vec<f32> = values_flat.to_vec1()
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let rewards: Vec<f32> = trajectory.steps.iter()
                .map(|s| s.reward)
                .collect();
            
            let advantages = Self::compute_gae(&rewards, &values_vec, 0.99);
            
            if advantages.is_empty() {
                continue;
            }
            
            let action_probs = self.actor_forward(&state_tensor)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let old_action_probs = action_probs.clone();
            
            let ratio = (&action_probs / &old_action_probs)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let ratio_flat = ratio.flatten_all()
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            let ratio_vals: Vec<f32> = ratio_flat.to_vec1()
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let mut clip_loss_sum = 0.0f32;
            for (i, &r) in ratio_vals.iter().enumerate() {
                let adv_idx = i / ACTION_DIM;
                let adv = if adv_idx < advantages.len() { advantages[adv_idx] } else { 0.0 };
                let clipped = Self::ppo_clip(r, adv);
                clip_loss_sum += clipped;
            }
            let clip_loss = Tensor::new(clip_loss_sum / ratio_vals.len() as f32, &self.device)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let entropy = Self::compute_entropy(&action_probs)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let value_targets: Vec<f32> = advantages.iter()
                .zip(values_vec.iter())
                .map(|(a, v)| a + v)
                .collect();
            
            let value_targets_tensor = Tensor::from_vec(
                value_targets.clone(),
                value_targets.len(),
                &self.device,
            ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let values_flat = values.flatten_all()
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            let value_diff = (values_flat - value_targets_tensor)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            let value_loss = value_diff
                .sqr().map_err(|e| DirectorError::ModelError(e.to_string()))?
                .mean_all().map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let entropy_coef_tensor = Tensor::new(ENTROPY_COEF, &self.device)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            let entropy_scaled = (&entropy * entropy_coef_tensor)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            let loss = (clip_loss + value_loss - entropy_scaled)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            let loss_val = loss.to_scalar::<f32>()
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
            
            if loss_val.is_nan() || loss_val.is_infinite() {
                continue;
            }
            
            total_loss += loss_val;
            num_samples += 1;
            
            optimizer.backward_step(&loss)
                .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        }
        
        Ok(total_loss / num_samples.max(1) as f32)
    }
    
    pub fn to_model(&self) -> RogueliteDirector {
        RogueliteDirector {
            device: self.device.clone(),
            actor_fc1_weight: self.actor_fc1_weight.as_tensor().clone(),
            actor_fc1_bias: self.actor_fc1_bias.as_tensor().clone(),
            actor_fc2_weight: self.actor_fc2_weight.as_tensor().clone(),
            actor_fc2_bias: self.actor_fc2_bias.as_tensor().clone(),
            actor_fc3_weight: self.actor_fc3_weight.as_tensor().clone(),
            actor_fc3_bias: self.actor_fc3_bias.as_tensor().clone(),
            critic_fc1_weight: self.critic_fc1_weight.as_tensor().clone(),
            critic_fc1_bias: self.critic_fc1_bias.as_tensor().clone(),
            critic_fc2_weight: self.critic_fc2_weight.as_tensor().clone(),
            critic_fc2_bias: self.critic_fc2_bias.as_tensor().clone(),
            critic_fc3_weight: self.critic_fc3_weight.as_tensor().clone(),
            critic_fc3_bias: self.critic_fc3_bias.as_tensor().clone(),
        }
    }
}

impl InteractionStrategyTrainer for RogueliteDirectorTrainer {
    fn train(&mut self, trajectories: &[Trajectory]) -> Result<TrainingResult, DirectorError> {
        let mut best_loss = f32::MAX;
        
        for epoch in 0..EPOCHS {
            let loss = self.train_epoch(trajectories)?;
            
            if loss < best_loss {
                best_loss = loss;
            }
            
            if epoch % 5 == 0 {
                eprintln!("Epoch {}: loss = {:.4}", epoch, loss);
            }
        }
        
        Ok(TrainingResult {
            mean_reward: -best_loss,
            episodes: trajectories.len(),
        })
    }
    
    fn save(&self, path: &Path) -> Result<(), DirectorError> {
        let weights = HashMap::from([
            ("actor_fc1_weight", self.actor_fc1_weight.as_tensor().clone()),
            ("actor_fc1_bias", self.actor_fc1_bias.as_tensor().clone()),
            ("actor_fc2_weight", self.actor_fc2_weight.as_tensor().clone()),
            ("actor_fc2_bias", self.actor_fc2_bias.as_tensor().clone()),
            ("actor_fc3_weight", self.actor_fc3_weight.as_tensor().clone()),
            ("actor_fc3_bias", self.actor_fc3_bias.as_tensor().clone()),
            ("critic_fc1_weight", self.critic_fc1_weight.as_tensor().clone()),
            ("critic_fc1_bias", self.critic_fc1_bias.as_tensor().clone()),
            ("critic_fc2_weight", self.critic_fc2_weight.as_tensor().clone()),
            ("critic_fc2_bias", self.critic_fc2_bias.as_tensor().clone()),
            ("critic_fc3_weight", self.critic_fc3_weight.as_tensor().clone()),
            ("critic_fc3_bias", self.critic_fc3_bias.as_tensor().clone()),
        ]);
        
        candle_core::safetensors::save(&weights, path)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        Ok(())
    }
    
    fn load(&mut self, path: &Path) -> Result<(), DirectorError> {
        let weights = candle_core::safetensors::load(path, &self.device)
            .map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.actor_fc1_weight = Var::from_tensor(
            weights.get("actor_fc1_weight")
                .ok_or_else(|| DirectorError::ModelError("actor_fc1_weight not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.actor_fc1_bias = Var::from_tensor(
            weights.get("actor_fc1_bias")
                .ok_or_else(|| DirectorError::ModelError("actor_fc1_bias not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.actor_fc2_weight = Var::from_tensor(
            weights.get("actor_fc2_weight")
                .ok_or_else(|| DirectorError::ModelError("actor_fc2_weight not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.actor_fc2_bias = Var::from_tensor(
            weights.get("actor_fc2_bias")
                .ok_or_else(|| DirectorError::ModelError("actor_fc2_bias not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.actor_fc3_weight = Var::from_tensor(
            weights.get("actor_fc3_weight")
                .ok_or_else(|| DirectorError::ModelError("actor_fc3_weight not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.actor_fc3_bias = Var::from_tensor(
            weights.get("actor_fc3_bias")
                .ok_or_else(|| DirectorError::ModelError("actor_fc3_bias not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.critic_fc1_weight = Var::from_tensor(
            weights.get("critic_fc1_weight")
                .ok_or_else(|| DirectorError::ModelError("critic_fc1_weight not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.critic_fc1_bias = Var::from_tensor(
            weights.get("critic_fc1_bias")
                .ok_or_else(|| DirectorError::ModelError("critic_fc1_bias not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.critic_fc2_weight = Var::from_tensor(
            weights.get("critic_fc2_weight")
                .ok_or_else(|| DirectorError::ModelError("critic_fc2_weight not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.critic_fc2_bias = Var::from_tensor(
            weights.get("critic_fc2_bias")
                .ok_or_else(|| DirectorError::ModelError("critic_fc2_bias not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.critic_fc3_weight = Var::from_tensor(
            weights.get("critic_fc3_weight")
                .ok_or_else(|| DirectorError::ModelError("critic_fc3_weight not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        self.critic_fc3_bias = Var::from_tensor(
            weights.get("critic_fc3_bias")
                .ok_or_else(|| DirectorError::ModelError("critic_fc3_bias not found".into()))?
        ).map_err(|e| DirectorError::ModelError(e.to_string()))?;
        
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_trainer_new() {
        let device = Device::Cpu;
        let trainer = RogueliteDirectorTrainer::new(device);
        assert!(trainer.is_ok());
    }
    
    #[test]
    fn test_compute_gae() {
        let rewards = vec![1.0, 0.5, 0.3, 0.8];
        let values = vec![0.5, 0.4, 0.3, 0.6];
        let advantages = RogueliteDirectorTrainer::compute_gae(&rewards, &values, 0.99);
        assert!(!advantages.is_empty());
    }
    
    #[test]
    fn test_compute_reward() {
        let reward = RogueliteDirectorTrainer::compute_reward(0.5, 0.3, true);
        assert!(reward > 0.0);
        
        let reward2 = RogueliteDirectorTrainer::compute_reward(0.5, 0.3, false);
        assert!(reward > reward2);
    }
    
    #[test]
    fn test_ppo_clip() {
        let loss = RogueliteDirectorTrainer::ppo_clip(1.0, 1.0);
        assert!(loss <= 0.0);
        
        let loss2 = RogueliteDirectorTrainer::ppo_clip(1.5, 1.0);
        assert!(loss2 <= 0.0);
    }
}