Files
Multi_Swarm_Coevolutive/src/multi_swarm/ga/loop.py
T
Adriano c38311e5fa feat(phase-2.5): mutate_prompt_llm operator + weighted dispatcher + GA wiring
Implementazione completa Phase 2.5 (LLM prompt mutator) seguendo il piano in
docs/superpowers/plans/2026-05-11-mutate-prompt-llm-phase-2-5.md.

Nuovo modulo src/multi_swarm/genome/mutation_prompt_llm.py:
- 6 mutation instructions (tighten_threshold, swap_comparator, add_condition,
  remove_condition, change_timeframe, add_temporal_gate)
- mutate_prompt_llm(g, llm, rng, mutator_tier=ModelTier.B): clona genome con
  tier B per la call mutator, costruisce system+user prompt con istruzione
  scelta random, estrae prompt da tag <prompt>...</prompt>, valida
- is_valid_prompt(): 3 check (lunghezza >= 50, keyword tecnica, diff > 5%
  Levenshtein-like via difflib.SequenceMatcher)
- Fallback random_mutate su qualsiasi validation fail O LLM exception

Esteso src/multi_swarm/genome/mutation.py con weighted_random_mutate dispatcher:
con probabilità prompt_mutation_weight invoca mutate_prompt_llm, altrimenti
random_mutate. Backward compat: llm=None oppure weight=0 → solo scalare.

Integrazione GA loop + RunConfig:
- GAConfig.prompt_mutation_weight: float = 0.0 (default off)
- next_generation(llm=...) propagato all'invocazione mutator
- RunConfig.prompt_mutation_weight con stesso default
- run_phase1: passa llm a next_generation solo se weight > 0
- scripts/run_phase1.py: flag CLI --prompt-mutation-weight

Tests (+18, 175 totale):
- tests/unit/test_mutation_prompt_llm.py (12): extract_prompt, is_valid_prompt
  3 check, operator success + fallback su 3 modi (invalid/identical/exception),
  tier B per LLM call, istruzione scelta dal pool
- tests/unit/test_mutation_dispatcher.py (4): weight 0/1/None + distribuzione
  30/70 su 1000 estrazioni con tolleranza ±5%
- tests/integration/test_ga_loop_with_prompt_mutator.py (2): loop con
  weight=1.0 produce prompt evoluti; backward compat weight=0 invariato

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 23:49:46 +02:00

52 lines
1.7 KiB
Python

from __future__ import annotations
import random
from dataclasses import dataclass
from typing import Any
from ..genome.crossover import uniform_crossover
from ..genome.hypothesis import HypothesisAgentGenome
from ..genome.mutation import weighted_random_mutate
from .selection import elite_select, tournament_select
@dataclass(frozen=True)
class GAConfig:
population_size: int
elite_k: int
tournament_k: int
p_crossover: float
prompt_mutation_weight: float = 0.0 # Phase 2.5: opt-in LLM mutator
def next_generation(
population: list[HypothesisAgentGenome],
fitnesses: dict[str, float],
cfg: GAConfig,
rng: random.Random,
llm: Any | None = None,
) -> list[HypothesisAgentGenome]:
"""Costruisce la prossima generazione: elitismo + tournament + crossover/mutate.
Quando ``cfg.prompt_mutation_weight > 0`` e ``llm`` è fornito, la mutazione
invoca ``weighted_random_mutate`` che con quella probabilità usa
``mutate_prompt_llm`` (Phase 2.5).
"""
new_pop: list[HypothesisAgentGenome] = list(
elite_select(population, fitnesses, cfg.elite_k)
)
while len(new_pop) < cfg.population_size:
if rng.random() < cfg.p_crossover and len(population) >= 2:
p1 = tournament_select(population, fitnesses, cfg.tournament_k, rng)
p2 = tournament_select(population, fitnesses, cfg.tournament_k, rng)
child = uniform_crossover(p1, p2, rng)
else:
parent = tournament_select(population, fitnesses, cfg.tournament_k, rng)
child = weighted_random_mutate(
parent, rng, llm=llm, prompt_mutation_weight=cfg.prompt_mutation_weight
)
new_pop.append(child)
return new_pop[: cfg.population_size]