final implementation of shc

This commit is contained in:
2025-09-26 14:18:00 +02:00
parent 0ace1cec3c
commit 90e67652ab
12 changed files with 186 additions and 462 deletions

View File

@@ -1,4 +1,3 @@
# actors/cortex.py
import time
from actor import Actor
@@ -12,12 +11,12 @@ class Cortex(Actor):
self.actuators = actuator_pids
self.exoself_pid = exoself_pid
self.awaiting_sync = set() # AIDs, von denen wir im aktuellen Schritt noch Sync erwarten
self.fitness_acc = 0.0 # akkumulierte Fitness über die Episode
self.ef_acc = 0 # akkumulierte EndFlags im aktuellen Schritt/Episode
self.cycle_acc = 0 # Anzahl abgeschlossener Sense-Think-Act Zyklen
self.active = False # aktiv/inaktiv (wartet auf reactivate)
self._t0 = None # Startzeit der Episode
self.awaiting_sync = set()
self.fitness_acc = 0.0
self.ef_acc = 0
self.cycle_acc = 0
self.active = False
self._t0 = None
async def _kick_sensors(self):
for s in self.sensors:
@@ -35,7 +34,6 @@ class Cortex(Actor):
self.active = True
async def run(self):
# Initialisierung: falls Actuatoren schon gesetzt sind, Episode starten
if self.actuators:
self._reset_for_new_episode()
await self._kick_sensors()
@@ -44,10 +42,8 @@ class Cortex(Actor):
msg = await self.inbox.get()
tag = msg[0]
# Optional: Exoself kann AIDs registrieren, bevor wir starten
if tag == "register_actuators":
_, aids = msg
# Map aids auf echte Actuatoren falls nötig; hier übernehmen wir sie direkt
self.awaiting_sync = set(aids)
if not self.active:
self._reset_for_new_episode()
@@ -56,7 +52,6 @@ class Cortex(Actor):
if tag == "sync" and self.active:
print("CORTEX: got sync message: ", msg)
# Aktuator meldet Schrittabschluss
_t, aid, fitness, halt_flag = msg
print("----------------")
@@ -66,36 +61,28 @@ class Cortex(Actor):
print("halt_flag:", halt_flag)
print("----------------")
# akkumulieren
self.fitness_acc += float(fitness)
self.ef_acc += int(halt_flag)
# diesen Aktuator als "eingetroffen" markieren
if aid in self.awaiting_sync:
self.awaiting_sync.remove(aid)
print("CORTEX: awaiting sync: ", self.awaiting_sync)
# Wenn alle Aktuatoren gemeldet haben, ist ein Zyklus fertig
if not self.awaiting_sync:
print("CORTEX: cycle completed.")
self.cycle_acc += 1
# Episodenende, wenn irgendein Aktuator EndFlag setzt
if self.ef_acc > 0:
elapsed = time.perf_counter() - self._t0
# An Exoself berichten
await self.exoself_pid.send((
"evaluation_completed",
self.fitness_acc,
self.cycle_acc,
elapsed
))
# inaktiv werden warten auf reactivate
self.active = False
# Flags für nächste Episode werden erst bei reactivate gesetzt
else:
# Nächsten Zyklus starten
self.ef_acc = 0
self._reset_for_new_cycle()
await self._kick_sensors()
@@ -103,17 +90,14 @@ class Cortex(Actor):
continue
if tag == "reactivate":
# neue Episode starten
self._reset_for_new_episode()
await self._kick_sensors()
continue
if tag == "terminate":
# geordnet alle Kinder beenden
for a in (self.sensors + self.actuators + self.neurons):
await a.send(("terminate",))
return
elif tag == "backup_from_neuron":
# vom Neuron an Cortex -> an Exoself weiterreichen
await self.exoself_pid.send(msg)