| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import random
- import math
- import os
- from Space import Space
- from Bird import Bird
- if os.getenv("PYPDEVS_MINIMAL") == "1":
- from pypdevs.minimal import CoupledDEVS
- else:
- from pypdevs.DEVS import CoupledDEVS
- class Flocking(CoupledDEVS):
- def __init__(self, seed: int, extent: tuple[int, int], num_birds: int, visual_distance: float):
- super().__init__("Flocking")
- self.rng = random.Random(seed)
- space_width, space_depth = extent
- speed = 1.0
- cohere_factor = 0.03
- separation = 1.0
- separate_factor = 0.015
- match_factor = 0.05
- initial_positions = {}
- for bird_id in range(num_birds):
- start_x = self.rng.uniform(0, space_width)
- start_y = self.rng.uniform(0, space_depth)
- angle = self.rng.uniform(0, 2 * math.pi)
- vel_x = math.cos(angle)
- vel_y = math.sin(angle)
- initial_positions[bird_id] = ((start_x, start_y), (vel_x, vel_y))
- # test for onestep comparison with Agents.JL
- # num_birds = 3
- # initial_positions = {
- # 0: ((10.0, 10.0), (1.0, 0.0)),
- # 1: ((12.0, 11.0), (0.0, 1.0)),
- # 2: ((9.0, 9.5), (-1.0, 0.0))
- # }
- self.space = self.addSubModel(Space(
- extent=extent,
- visual_distance=visual_distance,
- initial_positions=initial_positions,
- rng=self.rng
- ))
- for bird_id in range(num_birds):
- pos, vel = initial_positions[bird_id]
- bird = self.addSubModel(Bird(
- bid=bird_id,
- extent=extent,
- pos=pos,
- vel=vel,
- speed=speed,
- separation=separation,
- visual_distance=visual_distance,
- cohere_factor=cohere_factor,
- match_factor=match_factor,
- separate_factor=separate_factor
- ))
- self.connectPorts(bird.out_position, self.space.in_bird_pos)
- self.connectPorts(self.space.out_neighbours[bird_id], bird.in_neighbours)
|