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

@@ -6,16 +6,10 @@ MorphologyType = Union[str, Callable[[str], List[Dict[str, Any]]]]
def generate_id() -> float:
"""
Zeitbasierte float-ID (ähnlich wie im Buch).
"""
now = time.time()
return 1.0 / now
# ------------------------------------------------------------
# Morphologie-API (duck-typed, wie im Erlang-Original)
# ------------------------------------------------------------
def get_InitSensor(morphology: MorphologyType) -> Dict[str, Any]:
sensors = get_Sensors(morphology)
if not sensors:
@@ -41,13 +35,6 @@ def get_Actuators(morphology: MorphologyType) -> List[Dict[str, Any]]:
def _resolve_morphology(morphology: MorphologyType) -> Callable[[str], List[Dict[str, Any]]]:
"""
Akzeptiert:
- eine Callable Morphologie-Funktion (z.B. xor_mimic)
- einen String (z.B. "xor_mimic") -> per Registry auflösen
- ein Modul-Objekt (z.B. import morphology) -> Standard 'xor_mimic' aus dem Modul
"""
# 1) Bereits eine Callable-Funktion? (z.B. xor_mimic)
if callable(morphology):
return morphology
@@ -55,14 +42,11 @@ def _resolve_morphology(morphology: MorphologyType) -> Callable[[str], List[Dict
if isinstance(morphology, str):
reg = {
"xor_mimic": xor_mimic,
# weitere Morphologien hier registrieren...
}
if morphology in reg:
return reg[morphology]
raise ValueError(f"Unknown morphology name: {morphology}")
# 3) Modul-Objekt: versuche eine Standard-Morphologie-Funktion daraus zu nehmen
# Hier nehmen wir 'xor_mimic' als Default (du kannst das generalisieren).
try:
# Ist es ein Modul mit einer Funktion 'xor_mimic'?
if hasattr(morphology, "xor_mimic") and callable(getattr(morphology, "xor_mimic")):
@@ -73,31 +57,23 @@ def _resolve_morphology(morphology: MorphologyType) -> Callable[[str], List[Dict
raise TypeError("morphology must be a callable, a module with 'xor_mimic', or a registered string key")
# ------------------------------------------------------------
# Beispiel-Morphologie: XOR (wie im Buch)
# ------------------------------------------------------------
def xor_mimic(kind: str) -> List[Dict[str, Any]]:
"""
Liefert je nach 'kind' ('sensors' | 'actuators') die Definitionen.
Felder sind so benannt, dass sie direkt mit unserem Genotype/Phenotype-Code
kompatibel sind (vector_length statt 'vl', etc.).
"""
if kind == "sensors":
return [
{
"id": generate_id(),
"name": "xor_GetInput", # Sensorfunktion (muss in deinem Sensor-Actor implementiert sein)
"name": "xor_GetInput",
"vector_length": 2,
"scape": {"private": "xor_sim"} # optional, falls du Scapes nutzt
"scape": {"private": "xor_sim"}
}
]
elif kind == "actuators":
return [
{
"id": generate_id(),
"name": "xor_SendOutput", # Aktuatorfunktion (muss in deinem Actuator-Actor implementiert sein)
"name": "xor_SendOutput",
"vector_length": 1,
"scape": {"private": "xor_sim"} # optional
"scape": {"private": "xor_sim"}
}
]
else: