Agent.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from typing import NamedTuple
  2. from pypdevs.infinity import INFINITY
  3. import os
  4. if os.getenv("PYPDEVS_MINIMAL") == "1":
  5. from pypdevs.minimal import AtomicDEVS
  6. else:
  7. from pypdevs.DEVS import AtomicDEVS
  8. class AgentState(NamedTuple):
  9. position: tuple[int, int]
  10. happy: bool
  11. action: str
  12. class Agent(AtomicDEVS):
  13. def __init__(self, aid: int, group: int, position: tuple[int, int]) -> None:
  14. AtomicDEVS.__init__(self, f"Agent_{aid}_{group}")
  15. self.aid = aid
  16. self.group = group
  17. self.state = AgentState(position, False, "Idle")
  18. self.updateCondition = self.addInPort("update condition")
  19. self.move = self.addInPort("move")
  20. self.queryNeighbours = self.addOutPort("query neighbours")
  21. def extTransition(self, inputs):
  22. pos, happy, action = self.state
  23. if self.updateCondition in inputs:
  24. return AgentState(pos, True, "Idle")
  25. if self.move in inputs:
  26. new_pos = inputs[self.move][0]
  27. return AgentState(new_pos, False, "Idle")
  28. return self.state
  29. def intTransition(self):
  30. pos, happy, action = self.state
  31. if action == "Idle":
  32. return AgentState(pos, happy, "Query")
  33. elif action == "Query":
  34. return AgentState(pos, happy, "Waiting")
  35. return self.state
  36. def outputFnc(self):
  37. pos, happy, action = self.state
  38. if action == "Query":
  39. return {self.queryNeighbours: [(self.aid, pos, self.group)]}
  40. return {}
  41. def timeAdvance(self):
  42. pos, happy, action = self.state
  43. if action == "Idle":
  44. return 1.0 # needs to be investigated further
  45. elif action == "Query":
  46. return 0
  47. return INFINITY # for actions Waiting and Finished