from typing import NamedTuple from pypdevs.infinity import INFINITY import os if os.getenv("PYPDEVS_MINIMAL") == "1": from pypdevs.minimal import AtomicDEVS else: from pypdevs.DEVS import AtomicDEVS class AgentState(NamedTuple): position: tuple[int, int] happy: bool action: str class Agent(AtomicDEVS): def __init__(self, aid: int, group: int, position: tuple[int, int]) -> None: AtomicDEVS.__init__(self, f"Agent_{aid}_{group}") self.aid = aid self.group = group self.state = AgentState(position, False, "Idle") self.updateCondition = self.addInPort("update condition") self.move = self.addInPort("move") self.queryNeighbours = self.addOutPort("query neighbours") def extTransition(self, inputs): pos, happy, action = self.state if self.updateCondition in inputs: return AgentState(pos, True, "Idle") if self.move in inputs: new_pos = inputs[self.move][0] return AgentState(new_pos, False, "Idle") return self.state def intTransition(self): pos, happy, action = self.state if action == "Idle": return AgentState(pos, happy, "Query") elif action == "Query": return AgentState(pos, happy, "Waiting") return self.state def outputFnc(self): pos, happy, action = self.state if action == "Query": return {self.queryNeighbours: [(self.aid, pos, self.group)]} return {} def timeAdvance(self): pos, happy, action = self.state if action == "Idle": return 1.0 # needs to be investigated further elif action == "Query": return 0 return INFINITY # for actions Waiting and Finished