ec80af9f26
Task 5 del piano Phase 2.5: nuovo modulo src/multi_swarm/metrics/diversity.py con population_prompt_diversity(prompts) che ritorna la diversità media 1 - SequenceMatcher.ratio() su tutte le coppie distinte. 0.0 identici, fino a ~0.9 totalmente diversi (SequenceMatcher considera spazi/lunghezza). 5 test: edge case empty/single, identici, diversi, intermediate, simmetria. Piano aggiornato a stato "IMPLEMENTATO 2026-05-11": checkbox task 1-5 spuntate, task 6 (cost attribution per call_kind) deferito con motivazione. Header preambolo aggiornato con trigger verificati e decisione collaterale rollback tier C. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from multi_swarm.metrics.diversity import population_prompt_diversity
|
|
|
|
|
|
def test_empty_or_single_prompt_zero_diversity() -> None:
|
|
assert population_prompt_diversity([]) == 0.0
|
|
assert population_prompt_diversity(["solo prompt"]) == 0.0
|
|
|
|
|
|
def test_identical_prompts_zero_diversity() -> None:
|
|
prompts = ["Strategia RSI < 30 long"] * 5
|
|
assert population_prompt_diversity(prompts) == 0.0
|
|
|
|
|
|
def test_completely_different_prompts_high_diversity() -> None:
|
|
prompts = [
|
|
"AAAAAA AAAA AAAAA",
|
|
"BBBBBB BBBB BBBBB",
|
|
"CCCCCC CCCC CCCCC",
|
|
"DDDDDD DDDD DDDDD",
|
|
]
|
|
d = population_prompt_diversity(prompts)
|
|
# SequenceMatcher considera spazi e lunghezza simili → similarity > 0
|
|
# anche su stringhe completamente "diverse". Soglia realistica: 0.8.
|
|
assert d > 0.8
|
|
|
|
|
|
def test_partial_overlap_intermediate_diversity() -> None:
|
|
prompts = [
|
|
"Strategia momentum 1h con RSI",
|
|
"Strategia momentum 1h con SMA",
|
|
"Strategia momentum 4h con RSI",
|
|
]
|
|
d = population_prompt_diversity(prompts)
|
|
assert 0.05 < d < 0.5
|
|
|
|
|
|
def test_diversity_symmetric() -> None:
|
|
prompts_a = ["x", "yy", "zzz"]
|
|
prompts_b = ["zzz", "x", "yy"]
|
|
assert (
|
|
abs(population_prompt_diversity(prompts_a)
|
|
- population_prompt_diversity(prompts_b)) < 1e-9
|
|
)
|