Files
neuroevolution/mathema/population_monitor_test.py
2025-12-13 14:12:35 +01:00

50 lines
1.3 KiB
Python

# tests/test_population_monitor_integration_lite.py
import asyncio
import random
import mathema.core.population_monitor as pm
class FakeExoself:
def __init__(self, agent_id, monitor):
self.agent_id = agent_id
self.monitor = monitor
self._task = asyncio.create_task(self._run())
async def _run(self):
try:
await asyncio.sleep(0.01)
seed = sum(ord(c) for c in str(self.agent_id)) % 1000
random.seed(seed)
fitness = 0.5 + random.random()
await self.monitor.cast(("terminated", self.agent_id, float(fitness), 4, 4, 10))
except asyncio.CancelledError:
pass
async def cast(self, msg):
if msg and msg[0] == "terminate":
if self._task:
self._task.cancel()
async def fake_exoself_start(agent_id, monitor):
return FakeExoself(agent_id, monitor)
pm.EXOSELF_START = fake_exoself_start # 👉 sauberer DI-Hook
async def main():
monitor = await pm.init_population(("pop_iso", pm.INIT_CONSTRAINTS, "gt", "competition"))
G = 3
for _ in range(G):
await monitor.gen_ended.wait()
# await monitor.gen_ended.wait() # gezielt auf Generationsende warten
await monitor.stop("normal")
if __name__ == "__main__":
asyncio.run(main())