swarm-engine-eval 0.1.6

Evaluation framework 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
//! 組み込みシナリオ定義
//!
//! 評価フレームワークに組み込みで提供されるシナリオ。

use super::conditions::{CompareOp, Condition, EvalConditions, TimeoutBehavior};
use super::milestone::{Milestone, PartialConfig};
use super::types::*;

/// 全ての builtin シナリオを取得
pub fn builtin_scenarios() -> Vec<EvalScenario> {
    vec![
        resource_gathering(),
        task_queue_processing(),
        coordination_stress(),
    ]
}

/// リソース収集シナリオ
///
/// 複数のエージェントが協調してリソースを収集するタスク。
/// 基本的な協調能力の評価に使用。
pub fn resource_gathering() -> EvalScenario {
    EvalScenario {
        meta: ScenarioMeta {
            name: "Resource Gathering".to_string(),
            version: "1.0.0".to_string(),
            id: ScenarioId::new("builtin:resource_gathering:v1"),
            description: "Multiple agents coordinate to collect resources in a grid world"
                .to_string(),
            tags: vec![
                "coordination".to_string(),
                "basic".to_string(),
                "resource".to_string(),
            ],
        },
        task: TaskConfig::default(),
        llm: LlmConfig::default(),
        manager: ManagerConfig::default(),
        batch_processor: BatchProcessorConfig::default(),
        dependency_graph: None,
        actions: ScenarioActions::default(),
        app_config: AppConfigTemplate {
            tick_duration_ms: 10,
            max_ticks: 500,
            management_strategy: ManagementStrategyConfig::IntervalBased { max_interval: 20 },
            enable_exploration: false,
        },
        environment: EnvironmentConfig {
            env_type: "grid_world".to_string(),
            params: serde_json::json!({
                "width": 10,
                "height": 10,
                "resource_count": 5,
                "obstacle_density": 0.1
            }),
            initial_state: Some(InitialStateConfig::SeededRandom {}),
        },
        agents: AgentsConfig {
            workers: vec![WorkerTemplate {
                id_pattern: "gatherer_{i}".to_string(),
                count: 4,
                role: "gatherer".to_string(),
                config: serde_json::json!({
                    "speed": 1.0,
                    "capacity": 2
                }),
            }],
            managers: vec![ManagerTemplate {
                id: Some("coordinator".to_string()),
                id_pattern: None,
                count: 1,
                role: "coordinator".to_string(),
                activation: ManagerActivationConfig::Hybrid {
                    interval: 10,
                    triggers: vec!["resource_found".to_string(), "collision".to_string()],
                },
                config: serde_json::Value::Null,
            }],
        },
        conditions: EvalConditions {
            success: vec![
                Condition::new(
                    "all_resources_collected",
                    "environment.resources_collected",
                    CompareOp::Gte,
                    5,
                ),
                Condition::new("within_time_limit", "tick", CompareOp::Lte, 400),
            ],
            failure: vec![Condition::new(
                "deadlock_detected",
                "coordination.deadlock_count",
                CompareOp::Gte,
                3,
            )],
            on_timeout: TimeoutBehavior::MilestoneScore,
        },
        milestones: vec![
            Milestone::new(
                "first_collection",
                Condition::new(
                    "first",
                    "environment.resources_collected",
                    CompareOp::Gte,
                    1,
                ),
                0.15,
            )
            .with_description("Collect the first resource"),
            Milestone::new(
                "half_collected",
                Condition::new("half", "environment.resources_collected", CompareOp::Gte, 3),
                0.25,
            )
            .with_description("Collect half of the resources"),
            Milestone::new(
                "efficiency_bonus",
                Condition::new("efficiency", "tick", CompareOp::Lte, 300),
                0.20,
            )
            .with_description("Complete within 300 ticks")
            .with_partial(PartialConfig::Linear {
                min: Some(300.0),
                max: Some(400.0),
                descending: true, // tick が小さいほどスコアが高い
            }),
            Milestone::new(
                "all_collected",
                Condition::new("all", "environment.resources_collected", CompareOp::Gte, 5),
                0.40,
            )
            .with_description("Collect all resources"),
        ],
        variants: Vec::new(),
    }
}

/// タスクキュー処理シナリオ
///
/// タスクキューから順次タスクを取り出して処理する。
/// スループットとレイテンシの評価に使用。
pub fn task_queue_processing() -> EvalScenario {
    EvalScenario {
        meta: ScenarioMeta {
            name: "Task Queue Processing".to_string(),
            version: "1.0.0".to_string(),
            id: ScenarioId::new("builtin:task_queue:v1"),
            description: "Process tasks from a queue with varying complexity".to_string(),
            tags: vec![
                "throughput".to_string(),
                "latency".to_string(),
                "basic".to_string(),
            ],
        },
        task: TaskConfig::default(),
        llm: LlmConfig::default(),
        manager: ManagerConfig::default(),
        batch_processor: BatchProcessorConfig::default(),
        dependency_graph: None,
        actions: ScenarioActions::default(),
        app_config: AppConfigTemplate {
            tick_duration_ms: 10,
            max_ticks: 1000,
            management_strategy: ManagementStrategyConfig::EventDriven {
                triggers: vec!["task_completed".to_string(), "queue_empty".to_string()],
            },
            enable_exploration: false,
        },
        environment: EnvironmentConfig {
            env_type: "task_queue".to_string(),
            params: serde_json::json!({
                "initial_tasks": 100,
                "task_complexity": {
                    "min": 1,
                    "max": 10
                },
                "arrival_rate": 0.0  // バッチ処理
            }),
            initial_state: Some(InitialStateConfig::SeededRandom {}),
        },
        agents: AgentsConfig {
            workers: vec![WorkerTemplate {
                id_pattern: "processor_{i}".to_string(),
                count: 4,
                role: "processor".to_string(),
                config: serde_json::json!({
                    "processing_speed": 1.0
                }),
            }],
            managers: vec![ManagerTemplate {
                id: Some("scheduler".to_string()),
                id_pattern: None,
                count: 1,
                role: "scheduler".to_string(),
                activation: ManagerActivationConfig::Event {
                    triggers: vec!["task_completed".to_string()],
                },
                config: serde_json::Value::Null,
            }],
        },
        conditions: EvalConditions {
            success: vec![Condition::new(
                "all_tasks_processed",
                "task.completed_count",
                CompareOp::Gte,
                100,
            )],
            failure: vec![Condition::new(
                "task_timeout",
                "task.timeout_count",
                CompareOp::Gte,
                10,
            )],
            on_timeout: TimeoutBehavior::PartialSuccess,
        },
        milestones: vec![
            Milestone::new(
                "quarter_processed",
                Condition::new("quarter", "task.completed_count", CompareOp::Gte, 25),
                0.10,
            ),
            Milestone::new(
                "half_processed",
                Condition::new("half", "task.completed_count", CompareOp::Gte, 50),
                0.20,
            ),
            Milestone::new(
                "three_quarter_processed",
                Condition::new("three_quarter", "task.completed_count", CompareOp::Gte, 75),
                0.30,
            ),
            Milestone::new(
                "all_processed",
                Condition::new("all", "task.completed_count", CompareOp::Gte, 100),
                0.40,
            ),
        ],
        variants: Vec::new(),
    }
}

/// 協調ストレステストシナリオ
///
/// 多数のエージェントと頻繁なイベントによる高負荷シナリオ。
/// スケーラビリティとロバスト性の評価に使用。
pub fn coordination_stress() -> EvalScenario {
    EvalScenario {
        meta: ScenarioMeta {
            name: "Coordination Stress Test".to_string(),
            version: "1.0.0".to_string(),
            id: ScenarioId::new("builtin:coordination_stress:v1"),
            description: "High-load scenario with many agents and frequent events".to_string(),
            tags: vec![
                "stress".to_string(),
                "scalability".to_string(),
                "advanced".to_string(),
            ],
        },
        task: TaskConfig::default(),
        llm: LlmConfig::default(),
        manager: ManagerConfig::default(),
        batch_processor: BatchProcessorConfig::default(),
        dependency_graph: None,
        actions: ScenarioActions::default(),
        app_config: AppConfigTemplate {
            tick_duration_ms: 5, // より短いインターバル
            max_ticks: 2000,
            management_strategy: ManagementStrategyConfig::Hybrid {
                max_interval: 10,
                triggers: vec!["conflict".to_string(), "resource_contention".to_string()],
            },
            enable_exploration: false,
        },
        environment: EnvironmentConfig {
            env_type: "multi_resource".to_string(),
            params: serde_json::json!({
                "width": 20,
                "height": 20,
                "resource_types": 3,
                "resources_per_type": 10,
                "dynamic_spawning": true,
                "spawn_interval": 50
            }),
            initial_state: Some(InitialStateConfig::SeededRandom {}),
        },
        agents: AgentsConfig {
            workers: vec![
                WorkerTemplate {
                    id_pattern: "collector_a_{i}".to_string(),
                    count: 4,
                    role: "collector_type_a".to_string(),
                    config: serde_json::json!({
                        "target_resource": "type_a",
                        "speed": 1.2
                    }),
                },
                WorkerTemplate {
                    id_pattern: "collector_b_{i}".to_string(),
                    count: 4,
                    role: "collector_type_b".to_string(),
                    config: serde_json::json!({
                        "target_resource": "type_b",
                        "speed": 1.0
                    }),
                },
                WorkerTemplate {
                    id_pattern: "collector_c_{i}".to_string(),
                    count: 4,
                    role: "collector_type_c".to_string(),
                    config: serde_json::json!({
                        "target_resource": "type_c",
                        "speed": 0.8
                    }),
                },
            ],
            managers: vec![
                ManagerTemplate {
                    id: Some("global_coordinator".to_string()),
                    id_pattern: None,
                    count: 1,
                    role: "coordinator".to_string(),
                    activation: ManagerActivationConfig::Interval { interval: 20 },
                    config: serde_json::json!({
                        "strategy": "load_balancing"
                    }),
                },
                ManagerTemplate {
                    id: Some("conflict_resolver".to_string()),
                    id_pattern: None,
                    count: 1,
                    role: "resolver".to_string(),
                    activation: ManagerActivationConfig::Event {
                        triggers: vec!["conflict".to_string()],
                    },
                    config: serde_json::json!({
                        "strategy": "priority_based"
                    }),
                },
            ],
        },
        conditions: EvalConditions {
            success: vec![
                Condition::new(
                    "total_collected",
                    "environment.total_collected",
                    CompareOp::Gte,
                    50,
                ),
                Condition::new(
                    "low_conflict_rate",
                    "coordination.conflict_rate",
                    CompareOp::Lte,
                    0.1,
                ),
            ],
            failure: vec![
                Condition::new(
                    "system_overload",
                    "performance.tick_miss_rate",
                    CompareOp::Gte,
                    0.2,
                ),
                Condition::new(
                    "excessive_conflicts",
                    "coordination.conflict_count",
                    CompareOp::Gte,
                    100,
                ),
            ],
            on_timeout: TimeoutBehavior::MilestoneScore,
        },
        milestones: vec![
            Milestone::new(
                "system_stable",
                Condition::new("stable", "performance.tick_miss_rate", CompareOp::Lte, 0.05),
                0.20,
            )
            .with_description("System remains stable under load"),
            Milestone::new(
                "efficient_coordination",
                Condition::new(
                    "efficient",
                    "coordination.conflict_rate",
                    CompareOp::Lte,
                    0.05,
                ),
                0.25,
            )
            .with_description("Low conflict rate maintained"),
            Milestone::new(
                "good_throughput",
                Condition::new(
                    "throughput",
                    "environment.total_collected",
                    CompareOp::Gte,
                    30,
                ),
                0.25,
            )
            .with_description("Achieve good throughput"),
            Milestone::new(
                "excellent_throughput",
                Condition::new(
                    "excellent",
                    "environment.total_collected",
                    CompareOp::Gte,
                    50,
                ),
                0.30,
            )
            .with_description("Achieve excellent throughput"),
        ],
        variants: Vec::new(),
    }
}

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

    #[test]
    fn test_builtin_scenarios_exist() {
        let scenarios = builtin_scenarios();
        assert_eq!(scenarios.len(), 3);
    }

    #[test]
    fn test_resource_gathering_scenario() {
        let scenario = resource_gathering();
        assert_eq!(scenario.meta.name, "Resource Gathering");
        assert_eq!(scenario.agents.workers.len(), 1);
        assert_eq!(scenario.agents.workers[0].count, 4);
        assert_eq!(scenario.milestones.len(), 4);
    }

    #[test]
    fn test_scenario_serialization_json() {
        // JSON でのラウンドトリップをテスト
        let scenario = resource_gathering();
        let json_str = serde_json::to_string_pretty(&scenario).unwrap();

        // 再度デシリアライズできることを確認
        let deserialized: EvalScenario = serde_json::from_str(&json_str).unwrap();
        assert_eq!(deserialized.meta.name, scenario.meta.name);
        assert_eq!(deserialized.milestones.len(), scenario.milestones.len());
    }

    #[test]
    fn test_scenario_ids_unique() {
        let scenarios = builtin_scenarios();
        let ids: std::collections::HashSet<_> = scenarios.iter().map(|s| &s.meta.id).collect();
        assert_eq!(ids.len(), scenarios.len(), "Scenario IDs must be unique");
    }
}