swarm-engine-core 0.1.6

Core types and orchestration for SwarmEngine
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
//! Learn Stats - 学習用統計
//!
//! ActionEvent から抽出された学習パターン。
//! Engine (Core) は基本統計のみを持ち、学習パターンはこのモジュールに分離。

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

// ============================================================================
// Type Aliases (clippy::type_complexity)
// ============================================================================

/// 3-tuple key を持つ HashMap の型
type Tuple3Map<V> = HashMap<(String, String, String), V>;

/// 4-tuple key を持つ HashMap の型
type Tuple4Map<V> = HashMap<(String, String, String, String), V>;

// ============================================================================
// LearnStats - 学習用統計の集約
// ============================================================================

/// 学習用統計の集約
///
/// Core の SwarmStats から分離された学習専用統計。
/// ActionEvent を分析して学習パターンを抽出する。
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LearnStats {
    /// エピソード遷移統計(成功/失敗エピソードでの遷移パターン)
    pub episode_transitions: EpisodeTransitions,
    /// N-gram 統計(3-gram, 4-gram パターン)
    pub ngram_stats: NgramStats,
    /// Selection 戦略効果測定
    pub selection_performance: SelectionPerformance,
    /// コンテキスト条件付き統計(prev_action → action の成功/失敗)
    #[serde(
        serialize_with = "serialize_tuple2_map",
        deserialize_with = "deserialize_tuple2_map"
    )]
    pub contextual_stats: HashMap<(String, String), ContextualActionStats>,
}

impl LearnStats {
    /// LearningSnapshot から prior をロード
    pub fn load_prior(&mut self, snapshot: &crate::learn::LearningSnapshot) {
        self.episode_transitions = snapshot.episode_transitions.clone();
        self.ngram_stats = snapshot.ngram_stats.clone();
        self.selection_performance = snapshot.selection_performance.clone();
        // contextual_stats は ActionStats -> ContextualActionStats の変換が必要
        for ((prev, action), stats) in &snapshot.contextual_stats {
            let contextual = ContextualActionStats {
                visits: stats.visits,
                successes: stats.successes,
                failures: stats.failures,
            };
            self.contextual_stats
                .insert((prev.clone(), action.clone()), contextual);
        }
    }
}

/// コンテキスト条件付きアクション統計
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ContextualActionStats {
    pub visits: u32,
    pub successes: u32,
    pub failures: u32,
}

impl ContextualActionStats {
    pub fn success_rate(&self) -> f64 {
        if self.visits == 0 {
            0.5
        } else {
            self.successes as f64 / self.visits as f64
        }
    }
}

// ============================================================================
// EpisodeTransitions
// ============================================================================

/// エピソード遷移統計
///
/// 成功エピソードと失敗エピソードでの遷移パターンを分離して記録。
/// Q-learning風の価値関数近似に活用可能。
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EpisodeTransitions {
    /// 成功エピソードでの遷移カウント (prev_action, action) → count
    #[serde(
        serialize_with = "serialize_tuple2_map",
        deserialize_with = "deserialize_tuple2_map"
    )]
    pub success_transitions: HashMap<(String, String), u32>,
    /// 失敗エピソードでの遷移カウント (prev_action, action) → count
    #[serde(
        serialize_with = "serialize_tuple2_map",
        deserialize_with = "deserialize_tuple2_map"
    )]
    pub failure_transitions: HashMap<(String, String), u32>,
    /// 成功エピソード数
    pub success_episodes: u32,
    /// 失敗エピソード数
    pub failure_episodes: u32,
}

impl EpisodeTransitions {
    /// 成功遷移確率を計算
    pub fn success_transition_rate(&self, from: &str, to: &str) -> f64 {
        let key = (from.to_string(), to.to_string());
        let success_count = self.success_transitions.get(&key).copied().unwrap_or(0);
        let failure_count = self.failure_transitions.get(&key).copied().unwrap_or(0);
        let total = success_count + failure_count;

        if total == 0 {
            0.5
        } else {
            success_count as f64 / total as f64
        }
    }

    /// 遷移の価値スコアを計算 [-1, 1]
    pub fn transition_value(&self, from: &str, to: &str) -> f64 {
        let key = (from.to_string(), to.to_string());
        let success_count = self.success_transitions.get(&key).copied().unwrap_or(0) as f64;
        let failure_count = self.failure_transitions.get(&key).copied().unwrap_or(0) as f64;

        let total_success = self.success_transitions.values().sum::<u32>() as f64;
        let total_failure = self.failure_transitions.values().sum::<u32>() as f64;

        let success_rate = if total_success > 0.0 {
            success_count / total_success
        } else {
            0.0
        };
        let failure_rate = if total_failure > 0.0 {
            failure_count / total_failure
        } else {
            0.0
        };

        success_rate - failure_rate
    }

    /// 特定のアクションからの推奨次アクションを取得
    pub fn recommended_next_actions(&self, from: &str) -> Vec<(String, f64)> {
        let mut candidates: Vec<_> = self
            .success_transitions
            .iter()
            .filter(|((f, _), _)| f == from)
            .map(|((_, to), _)| {
                let value = self.transition_value(from, to);
                (to.clone(), value)
            })
            .collect();

        candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        candidates
    }
}

// ============================================================================
// NgramStats
// ============================================================================

/// N-gram 統計(3-gram, 4-gram パターン学習)
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NgramStats {
    /// 3-gram: (a1, a2, a3) → (success_count, failure_count)
    #[serde(
        serialize_with = "serialize_tuple3_map",
        deserialize_with = "deserialize_tuple3_map"
    )]
    pub trigrams: HashMap<(String, String, String), (u32, u32)>,
    /// 4-gram: (a1, a2, a3, a4) → (success_count, failure_count)
    #[serde(
        serialize_with = "serialize_tuple4_map",
        deserialize_with = "deserialize_tuple4_map"
    )]
    pub quadgrams: HashMap<(String, String, String, String), (u32, u32)>,
}

impl NgramStats {
    /// 3-gram の成功率を計算
    pub fn trigram_success_rate(&self, a1: &str, a2: &str, a3: &str) -> f64 {
        let key = (a1.to_string(), a2.to_string(), a3.to_string());
        match self.trigrams.get(&key) {
            Some(&(success, failure)) => {
                let total = success + failure;
                if total == 0 {
                    0.5
                } else {
                    success as f64 / total as f64
                }
            }
            None => 0.5,
        }
    }

    /// 4-gram の成功率を計算
    pub fn quadgram_success_rate(&self, a1: &str, a2: &str, a3: &str, a4: &str) -> f64 {
        let key = (
            a1.to_string(),
            a2.to_string(),
            a3.to_string(),
            a4.to_string(),
        );
        match self.quadgrams.get(&key) {
            Some(&(success, failure)) => {
                let total = success + failure;
                if total == 0 {
                    0.5
                } else {
                    success as f64 / total as f64
                }
            }
            None => 0.5,
        }
    }

    /// 3-gram の価値スコア [-1, 1]
    pub fn trigram_value(&self, a1: &str, a2: &str, a3: &str) -> f64 {
        let key = (a1.to_string(), a2.to_string(), a3.to_string());
        match self.trigrams.get(&key) {
            Some(&(success, failure)) => {
                let total = success + failure;
                if total == 0 {
                    0.0
                } else {
                    (success as f64 / total as f64) * 2.0 - 1.0
                }
            }
            None => 0.0,
        }
    }

    /// 2つのアクション列の後に推奨されるアクション一覧
    pub fn recommended_after(&self, a1: &str, a2: &str) -> Vec<(String, f64)> {
        let mut candidates: Vec<_> = self
            .trigrams
            .iter()
            .filter(|((x1, x2, _), _)| x1 == a1 && x2 == a2)
            .map(|((_, _, a3), &(success, failure))| {
                let total = success + failure;
                let score = if total == 0 {
                    0.0
                } else {
                    (success as f64 / total as f64) * 2.0 - 1.0
                };
                (a3.clone(), score)
            })
            .collect();

        candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        candidates
    }

    /// 3つのアクション列の後に推奨されるアクション一覧
    pub fn recommended_after_three(&self, a1: &str, a2: &str, a3: &str) -> Vec<(String, f64)> {
        let mut candidates: Vec<_> = self
            .quadgrams
            .iter()
            .filter(|((x1, x2, x3, _), _)| x1 == a1 && x2 == a2 && x3 == a3)
            .map(|((_, _, _, a4), &(success, failure))| {
                let total = success + failure;
                let score = if total == 0 {
                    0.0
                } else {
                    (success as f64 / total as f64) * 2.0 - 1.0
                };
                (a4.clone(), score)
            })
            .collect();

        candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        candidates
    }

    pub fn trigram_count(&self) -> usize {
        self.trigrams.len()
    }

    pub fn quadgram_count(&self) -> usize {
        self.quadgrams.len()
    }
}

// ============================================================================
// SelectionPerformance
// ============================================================================

/// Selection 戦略効果測定(Meta-learning)
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SelectionPerformance {
    /// 戦略ごとの統計
    pub strategy_stats: HashMap<String, StrategyStats>,
    /// 戦略切り替え履歴
    pub switch_history: Vec<StrategySwitchEvent>,
    /// 現在の戦略
    pub current_strategy: Option<String>,
    /// 現在の戦略開始時の visits
    pub strategy_start_visits: u32,
    /// 現在の戦略開始時の success_rate
    pub strategy_start_success_rate: f64,
}

/// 戦略ごとの統計
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StrategyStats {
    pub visits: u32,
    pub successes: u32,
    pub failures: u32,
    pub usage_count: u32,
    pub episodes_success: u32,
    pub episodes_failure: u32,
}

impl StrategyStats {
    pub fn success_rate(&self) -> f64 {
        if self.visits == 0 {
            0.5
        } else {
            self.successes as f64 / self.visits as f64
        }
    }

    pub fn episode_success_rate(&self) -> f64 {
        let total = self.episodes_success + self.episodes_failure;
        if total == 0 {
            0.5
        } else {
            self.episodes_success as f64 / total as f64
        }
    }
}

/// 戦略切り替えイベント
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StrategySwitchEvent {
    pub from: String,
    pub to: String,
    pub visits_at_switch: u32,
    pub success_rate_at_switch: f64,
    pub from_strategy_success_rate: f64,
}

impl SelectionPerformance {
    /// 戦略を記録開始
    pub fn start_strategy(
        &mut self,
        strategy: &str,
        current_visits: u32,
        current_success_rate: f64,
    ) {
        if let Some(ref current) = self.current_strategy {
            if current != strategy {
                let from_stats = self
                    .strategy_stats
                    .get(current)
                    .cloned()
                    .unwrap_or_default();
                self.switch_history.push(StrategySwitchEvent {
                    from: current.clone(),
                    to: strategy.to_string(),
                    visits_at_switch: current_visits,
                    success_rate_at_switch: current_success_rate,
                    from_strategy_success_rate: from_stats.success_rate(),
                });
            }
        }

        self.current_strategy = Some(strategy.to_string());
        self.strategy_start_visits = current_visits;
        self.strategy_start_success_rate = current_success_rate;

        self.strategy_stats
            .entry(strategy.to_string())
            .or_default()
            .usage_count += 1;
    }

    /// アクション結果を記録
    pub fn record_action(&mut self, success: bool) {
        if let Some(ref strategy) = self.current_strategy {
            let stats = self.strategy_stats.entry(strategy.clone()).or_default();
            stats.visits += 1;
            if success {
                stats.successes += 1;
            } else {
                stats.failures += 1;
            }
        }
    }

    /// エピソード終了を記録
    pub fn record_episode_end(&mut self, success: bool) {
        if let Some(ref strategy) = self.current_strategy {
            let stats = self.strategy_stats.entry(strategy.clone()).or_default();
            if success {
                stats.episodes_success += 1;
            } else {
                stats.episodes_failure += 1;
            }
        }
    }

    /// 戦略の効果を取得
    pub fn strategy_effectiveness(&self, strategy: &str) -> Option<f64> {
        self.strategy_stats.get(strategy).map(|s| s.success_rate())
    }

    /// 最も効果的な戦略を取得
    pub fn best_strategy(&self) -> Option<(&str, f64)> {
        self.strategy_stats
            .iter()
            .filter(|(_, stats)| stats.visits >= 10)
            .max_by(|(_, a), (_, b)| {
                a.success_rate()
                    .partial_cmp(&b.success_rate())
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(name, stats)| (name.as_str(), stats.success_rate()))
    }

    /// 状況に応じた推奨戦略を取得
    pub fn recommended_strategy(&self, failure_rate: f64, visits: u32) -> &str {
        let ucb1_score = self.strategy_score_for_context("UCB1", failure_rate, visits);
        let greedy_score = self.strategy_score_for_context("Greedy", failure_rate, visits);
        let thompson_score = self.strategy_score_for_context("Thompson", failure_rate, visits);

        if ucb1_score >= greedy_score && ucb1_score >= thompson_score {
            "UCB1"
        } else if greedy_score >= thompson_score {
            "Greedy"
        } else {
            "Thompson"
        }
    }

    fn strategy_score_for_context(&self, strategy: &str, failure_rate: f64, _visits: u32) -> f64 {
        let base_score = self
            .strategy_stats
            .get(strategy)
            .map(|s| s.success_rate())
            .unwrap_or(0.5);

        match strategy {
            "UCB1" => base_score + failure_rate * 0.2,
            "Greedy" => base_score + (1.0 - failure_rate) * 0.2,
            "Thompson" => {
                let distance_from_middle = (failure_rate - 0.5).abs();
                base_score + (0.5 - distance_from_middle) * 0.2
            }
            _ => base_score,
        }
    }
}

// ============================================================================
// Tuple Key HashMap Serialization
// ============================================================================

fn serialize_tuple2_map<V, S>(
    map: &HashMap<(String, String), V>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    V: Serialize,
    S: serde::Serializer,
{
    use serde::ser::SerializeMap;
    let mut ser_map = serializer.serialize_map(Some(map.len()))?;
    for ((a, b), v) in map {
        ser_map.serialize_entry(&format!("{}:{}", a, b), v)?;
    }
    ser_map.end()
}

fn deserialize_tuple2_map<'de, V, D>(
    deserializer: D,
) -> Result<HashMap<(String, String), V>, D::Error>
where
    V: Deserialize<'de>,
    D: serde::Deserializer<'de>,
{
    use serde::de::Error;
    let string_map: HashMap<String, V> = HashMap::deserialize(deserializer)?;
    let mut result = HashMap::new();
    for (k, v) in string_map {
        let parts: Vec<&str> = k.splitn(2, ':').collect();
        if parts.len() != 2 {
            return Err(D::Error::custom(format!("invalid tuple2 key: {}", k)));
        }
        result.insert((parts[0].to_string(), parts[1].to_string()), v);
    }
    Ok(result)
}

fn serialize_tuple3_map<V, S>(
    map: &HashMap<(String, String, String), V>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    V: Serialize,
    S: serde::Serializer,
{
    use serde::ser::SerializeMap;
    let mut ser_map = serializer.serialize_map(Some(map.len()))?;
    for ((a, b, c), v) in map {
        ser_map.serialize_entry(&format!("{}:{}:{}", a, b, c), v)?;
    }
    ser_map.end()
}

fn deserialize_tuple3_map<'de, V, D>(deserializer: D) -> Result<Tuple3Map<V>, D::Error>
where
    V: Deserialize<'de>,
    D: serde::Deserializer<'de>,
{
    use serde::de::Error;
    let string_map: HashMap<String, V> = HashMap::deserialize(deserializer)?;
    let mut result = HashMap::new();
    for (k, v) in string_map {
        let parts: Vec<&str> = k.splitn(3, ':').collect();
        if parts.len() != 3 {
            return Err(D::Error::custom(format!("invalid tuple3 key: {}", k)));
        }
        result.insert(
            (
                parts[0].to_string(),
                parts[1].to_string(),
                parts[2].to_string(),
            ),
            v,
        );
    }
    Ok(result)
}

fn serialize_tuple4_map<V, S>(
    map: &HashMap<(String, String, String, String), V>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    V: Serialize,
    S: serde::Serializer,
{
    use serde::ser::SerializeMap;
    let mut ser_map = serializer.serialize_map(Some(map.len()))?;
    for ((a, b, c, d), v) in map {
        ser_map.serialize_entry(&format!("{}:{}:{}:{}", a, b, c, d), v)?;
    }
    ser_map.end()
}

fn deserialize_tuple4_map<'de, V, D>(deserializer: D) -> Result<Tuple4Map<V>, D::Error>
where
    V: Deserialize<'de>,
    D: serde::Deserializer<'de>,
{
    use serde::de::Error;
    let string_map: HashMap<String, V> = HashMap::deserialize(deserializer)?;
    let mut result = HashMap::new();
    for (k, v) in string_map {
        let parts: Vec<&str> = k.splitn(4, ':').collect();
        if parts.len() != 4 {
            return Err(D::Error::custom(format!("invalid tuple4 key: {}", k)));
        }
        result.insert(
            (
                parts[0].to_string(),
                parts[1].to_string(),
                parts[2].to_string(),
                parts[3].to_string(),
            ),
            v,
        );
    }
    Ok(result)
}