60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
"""
|
|
this is a test scape for validation.
|
|
"""
|
|
from mathema.actors.actor import Actor
|
|
import math
|
|
import logging
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class XorScape(Actor):
|
|
def __init__(self):
|
|
super().__init__("XorScape")
|
|
self.data = [
|
|
([-1, -1], [-1]),
|
|
([1, -1], [1]),
|
|
([-1, 1], [1]),
|
|
([1, 1], [-1])
|
|
]
|
|
self.index = 0
|
|
self.error_acc = 0.0
|
|
self.last_actuator = None
|
|
|
|
async def run(self):
|
|
while True:
|
|
msg = await self.inbox.get()
|
|
tag = msg[0]
|
|
|
|
if tag == "sense":
|
|
log.debug("SCAPE: got sensed by sensor...")
|
|
_, sid, from_pid = msg
|
|
self.last_actuator = from_pid
|
|
inputs, correct = self.data[self.index]
|
|
log.debug("SENSOR input /correct: ", inputs, correct)
|
|
await from_pid.send(("percept", inputs))
|
|
|
|
elif tag == "action":
|
|
_, output, from_pid = msg
|
|
_, correct = self.data[self.index]
|
|
step_error = sum((o - c) ** 2 for o, c in zip(output, correct))
|
|
step_rmse = math.sqrt(step_error)
|
|
|
|
log.debug(f"XOR PATTERN: Input={self.data[self.index][0]} → Network={output} → Expected={correct}")
|
|
|
|
self.index += 1
|
|
|
|
if self.index >= len(self.data):
|
|
|
|
total_rmse = math.sqrt(self.error_acc + step_rmse)
|
|
fitness = 1.0 / (total_rmse + 1e-5)
|
|
await from_pid.send(("result", fitness, 1))
|
|
self.index = 0
|
|
self.error_acc = 0.0
|
|
else:
|
|
# Continue episode
|
|
self.error_acc += step_rmse
|
|
await from_pid.send(("result", 0.0, 0))
|
|
|
|
elif tag == "terminate":
|
|
return
|