RockPaperScissors.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import random
  2. from pypdevs.infinity import INFINITY
  3. from pypdevs.DEVS import AtomicDEVS, CoupledDEVS
  4. from Agents import Agents, Rock, Paper, Scissors
  5. from Space import Space
  6. class RockPaperScissors(CoupledDEVS):
  7. def __init__(self, seed, width, height, stats):
  8. CoupledDEVS.__init__(self, "Rock Paper Scissors")
  9. self.rng = random.Random(seed)
  10. self.width = width
  11. self.height = height
  12. self.space = self.addSubModel(Space(width, height, self.rng, stats))
  13. self.active_agents = {}
  14. # self.init_agent(Agents.Rock.value, 0, 0)
  15. # self.init_agent(Agents.Paper.value, 2, 0)
  16. # self.init_agent(Agents.Scissors.value, 2, 2)
  17. index = 0
  18. for h in range(height):
  19. for w in range(width):
  20. # print(f"Agent {index} {list(Agents)[index % 3].value} at {h}:{w}")
  21. agent_type = list(Agents)[index % 3].value
  22. index += 1
  23. self.init_agent(agent_type, w, h)
  24. def _create_agent(self, aid, variant):
  25. name = f"{variant}_Agent_{aid}"
  26. match variant:
  27. case Agents.Rock.value:
  28. return Rock(name, aid, self.rng)
  29. case Agents.Paper.value:
  30. return Paper(name, aid, self.rng)
  31. case Agents.Scissors.value:
  32. return Scissors(name, aid, self.rng)
  33. case _:
  34. raise ValueError("Unknown agent type")
  35. def connectAgent(self, agent):
  36. self.connectPorts(agent.requestVictim, self.space.requestVictim)
  37. self.connectPorts(agent.move, self.space.move)
  38. self.connectPorts(agent.kill, self.space.kill)
  39. self.connectPorts(agent.reproduce, self.space.reproduce)
  40. def init_agent(self, variant, x, y):
  41. """
  42. initialisation helper,
  43. accesses Space Model's state
  44. """
  45. # print(f"Initialising agent {aid}")
  46. aid, space_port = self.space.init_agent(variant, x, y)
  47. agent = self._create_agent(aid, variant)
  48. new_agent = self.addSubModel(agent)
  49. self.active_agents[aid] = new_agent
  50. self.connectAgent(new_agent)
  51. self.connectPorts(space_port, new_agent.receiveVictim)
  52. def spawn_agent(self, aid, variant):
  53. """
  54. dynamically add new agent, at sim time
  55. """
  56. agent = self._create_agent(aid, variant)
  57. new_agent = self.addSubModel(agent)
  58. self.active_agents[aid] = new_agent
  59. space_port = self.space.agent_out_ports[aid]
  60. self.connectAgent(new_agent)
  61. self.connectPorts(space_port, new_agent.receiveVictim)
  62. def modelTransition(self, state):
  63. if "remove" in state:
  64. for victim_aid in state["remove"]:
  65. dead_agent_model = self.active_agents.pop(victim_aid, None)
  66. if dead_agent_model:
  67. self.removeSubModel(dead_agent_model)
  68. if "mitosis" in state:
  69. for variant, aid in state["mitosis"]:
  70. self.spawn_agent(aid, variant)
  71. return False