Bird.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import math
  2. import os
  3. from typing import NamedTuple
  4. from pypdevs.infinity import INFINITY
  5. if os.getenv("PYPDEVS_MINIMAL") == "1":
  6. from pypdevs.minimal import AtomicDEVS
  7. else:
  8. from pypdevs.DEVS import AtomicDEVS
  9. class Phase:
  10. IDLE = 0
  11. COMPUTING = 1
  12. PHASES = ["IDLE", "COMPUTING"]
  13. class BirdState(NamedTuple):
  14. phase: int # "IDLE" | "COMPUTING"
  15. pos: tuple[float, float]
  16. vel: tuple[float, float]
  17. def __repr__(self):
  18. return f"Bird | {PHASES[self.phase]}, p = {self.pos}, v = {self.vel})"
  19. class Bird(AtomicDEVS):
  20. def __init__(self, bid: int, extent: tuple[float, float], pos: tuple[float, float],
  21. vel: tuple[float, float], speed: float, separation: float,
  22. visual_distance: float, cohere_factor: float, match_factor: float,
  23. separate_factor: float):
  24. super().__init__(f"bird_{bid}")
  25. self.bid = bid
  26. # Space bounds for torus wrapping
  27. self.w, self.h = extent
  28. self.w_half = self.w / 2.0
  29. self.h_half = self.h / 2.0
  30. # Flocking Parameters
  31. self.speed = speed
  32. self.separation_sq = separation ** 2
  33. self.visual_distance = visual_distance
  34. self.cohere_factor = cohere_factor
  35. self.match_factor = match_factor
  36. self.separate_factor = separate_factor
  37. # The state is now completely flattened and stripped of empty arrays
  38. self.state = BirdState(Phase.IDLE, pos, vel)
  39. # DEVS Ports
  40. self.in_neighbours = self.addInPort("in_neighbours")
  41. self.out_position = self.addOutPort("out_position")
  42. def extTransition(self, inputs):
  43. if self.in_neighbours in inputs: # Neighbours Update
  44. neighbours = inputs[self.in_neighbours][0]
  45. new_pos, new_vel = self._flock(self.state.pos, self.state.vel, neighbours)
  46. return BirdState(Phase.COMPUTING, new_pos, new_vel)
  47. return self.state
  48. def intTransition(self):
  49. if self.state.phase == Phase.COMPUTING:
  50. return BirdState(Phase.IDLE, self.state.pos, self.state.vel)
  51. return self.state
  52. def timeAdvance(self):
  53. if self.state.phase == Phase.COMPUTING:
  54. return 0.0
  55. return INFINITY
  56. def outputFnc(self):
  57. if self.state.phase == Phase.COMPUTING:
  58. return {self.out_position: [(self.bid, self.state.pos, self.state.vel)]}
  59. return {}
  60. def _heading(self, my_pos, n_pos):
  61. hx = n_pos[0] - my_pos[0]
  62. hy = n_pos[1] - my_pos[1]
  63. if hx > self.w_half:
  64. hx -= self.w
  65. elif hx < -self.w_half:
  66. hx += self.w
  67. if hy > self.h_half:
  68. hy -= self.h
  69. elif hy < -self.h_half:
  70. hy += self.h
  71. return hx, hy
  72. def _flock(self, pos, vel, neighbours):
  73. N = len(neighbours)
  74. # No neighbours? => maintain trajectory
  75. if N == 0:
  76. raw_x = (pos[0] + vel[0] * self.speed) % self.w
  77. raw_y = (pos[1] + vel[1] * self.speed) % self.h
  78. return (raw_x, raw_y), vel
  79. cohere_x, cohere_y = 0.0, 0.0
  80. separate_x, separate_y = 0.0, 0.0
  81. match_x, match_y = 0.0, 0.0
  82. for n_id, n_pos, n_vel in neighbours:
  83. hx, hy = self._heading(pos, n_pos)
  84. # Cohere: Vector towards neighbour
  85. cohere_x += hx
  86. cohere_y += hy
  87. # Separate: Vector away from neighbour
  88. if (hx ** 2 + hy ** 2) < self.separation_sq:
  89. separate_x -= hx
  90. separate_y -= hy
  91. # Match: Neighbour's velocity
  92. match_x += n_vel[0]
  93. match_y += n_vel[1]
  94. # Apply factors based on number of neighbours
  95. N = max(N, 1)
  96. cohere_x = (cohere_x / N) * self.cohere_factor
  97. cohere_y = (cohere_y / N) * self.cohere_factor
  98. separate_x = (separate_x / N) * self.separate_factor
  99. separate_y = (separate_y / N) * self.separate_factor
  100. match_x = (match_x / N) * self.match_factor
  101. match_y = (match_y / N) * self.match_factor
  102. # Update Velocity (Average of vectors)
  103. new_vx = (vel[0] + cohere_x + separate_x + match_x)
  104. new_vy = (vel[1] + cohere_y + separate_y + match_y)
  105. # Normalise Velocity to unit vector
  106. norm = math.sqrt(new_vx ** 2 + new_vy ** 2)
  107. if norm > 0:
  108. new_vx /= norm
  109. new_vy /= norm
  110. # Update Position&Wrap
  111. new_px = (pos[0] + new_vx * self.speed) % self.w
  112. new_py = (pos[1] + new_vy * self.speed) % self.h
  113. return (new_px, new_py), (new_vx, new_vy)