| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import math
- import os
- from typing import NamedTuple
- from pypdevs.infinity import INFINITY
- if os.getenv("PYPDEVS_MINIMAL") == "1":
- from pypdevs.minimal import AtomicDEVS
- else:
- from pypdevs.DEVS import AtomicDEVS
- class Phase:
- IDLE = 0
- COMPUTING = 1
- PHASES = ["IDLE", "COMPUTING"]
- class BirdState(NamedTuple):
- phase: int # "IDLE" | "COMPUTING"
- pos: tuple[float, float]
- vel: tuple[float, float]
- def __repr__(self):
- return f"Bird | {PHASES[self.phase]}, p = {self.pos}, v = {self.vel})"
- class Bird(AtomicDEVS):
- def __init__(self, bid: int, extent: tuple[float, float], pos: tuple[float, float],
- vel: tuple[float, float], speed: float, separation: float,
- visual_distance: float, cohere_factor: float, match_factor: float,
- separate_factor: float):
- super().__init__(f"bird_{bid}")
- self.bid = bid
- # Space bounds for torus wrapping
- self.w, self.h = extent
- self.w_half = self.w / 2.0
- self.h_half = self.h / 2.0
- # Flocking Parameters
- self.speed = speed
- self.separation_sq = separation ** 2
- self.visual_distance = visual_distance
- self.cohere_factor = cohere_factor
- self.match_factor = match_factor
- self.separate_factor = separate_factor
- # The state is now completely flattened and stripped of empty arrays
- self.state = BirdState(Phase.IDLE, pos, vel)
- # DEVS Ports
- self.in_neighbours = self.addInPort("in_neighbours")
- self.out_position = self.addOutPort("out_position")
- def extTransition(self, inputs):
- if self.in_neighbours in inputs: # Neighbours Update
- neighbours = inputs[self.in_neighbours][0]
- new_pos, new_vel = self._flock(self.state.pos, self.state.vel, neighbours)
- return BirdState(Phase.COMPUTING, new_pos, new_vel)
- return self.state
- def intTransition(self):
- if self.state.phase == Phase.COMPUTING:
- return BirdState(Phase.IDLE, self.state.pos, self.state.vel)
- return self.state
- def timeAdvance(self):
- if self.state.phase == Phase.COMPUTING:
- return 0.0
- return INFINITY
- def outputFnc(self):
- if self.state.phase == Phase.COMPUTING:
- return {self.out_position: [(self.bid, self.state.pos, self.state.vel)]}
- return {}
- def _heading(self, my_pos, n_pos):
- hx = n_pos[0] - my_pos[0]
- hy = n_pos[1] - my_pos[1]
- if hx > self.w_half:
- hx -= self.w
- elif hx < -self.w_half:
- hx += self.w
- if hy > self.h_half:
- hy -= self.h
- elif hy < -self.h_half:
- hy += self.h
- return hx, hy
- def _flock(self, pos, vel, neighbours):
- N = len(neighbours)
- # No neighbours? => maintain trajectory
- if N == 0:
- raw_x = (pos[0] + vel[0] * self.speed) % self.w
- raw_y = (pos[1] + vel[1] * self.speed) % self.h
- return (raw_x, raw_y), vel
- cohere_x, cohere_y = 0.0, 0.0
- separate_x, separate_y = 0.0, 0.0
- match_x, match_y = 0.0, 0.0
- for n_id, n_pos, n_vel in neighbours:
- hx, hy = self._heading(pos, n_pos)
- # Cohere: Vector towards neighbour
- cohere_x += hx
- cohere_y += hy
- # Separate: Vector away from neighbour
- if (hx ** 2 + hy ** 2) < self.separation_sq:
- separate_x -= hx
- separate_y -= hy
- # Match: Neighbour's velocity
- match_x += n_vel[0]
- match_y += n_vel[1]
- # Apply factors based on number of neighbours
- N = max(N, 1)
- cohere_x = (cohere_x / N) * self.cohere_factor
- cohere_y = (cohere_y / N) * self.cohere_factor
- separate_x = (separate_x / N) * self.separate_factor
- separate_y = (separate_y / N) * self.separate_factor
- match_x = (match_x / N) * self.match_factor
- match_y = (match_y / N) * self.match_factor
- # Update Velocity (Average of vectors)
- new_vx = (vel[0] + cohere_x + separate_x + match_x)
- new_vy = (vel[1] + cohere_y + separate_y + match_y)
- # Normalise Velocity to unit vector
- norm = math.sqrt(new_vx ** 2 + new_vy ** 2)
- if norm > 0:
- new_vx /= norm
- new_vy /= norm
- # Update Position&Wrap
- new_px = (pos[0] + new_vx * self.speed) % self.w
- new_py = (pos[1] + new_vy * self.speed) % self.h
- return (new_px, new_py), (new_vx, new_vy)
|