| 123456789101112131415161718192021222324252627282930313233343536373839 |
- import random
- import os
- if os.getenv("PYPDEVS_MINIMAL") == "1":
- from pypdevs.minimal import CoupledDEVS
- else:
- from pypdevs.DEVS import CoupledDEVS
- from Space import Space
- from Agent import Agent
- class Schelling(CoupledDEVS):
- def __init__(self, dim: int, num_agents: int, radius: int, happy_threshold: int, seed: int) -> None:
- CoupledDEVS.__init__(self, "Schelling")
- self.rng = random.Random(seed)
- self.agents = []
- grid = {}
- empty = []
- for x in range(dim):
- for y in range(dim):
- empty.append((x, y))
- aid = 0
- for i in range(num_agents):
- pos = self.rng.choice(empty)
- empty.remove(pos)
- group = i % 2
- grid[pos] = (aid, group)
- agent = self.addSubModel(Agent(aid, group, pos))
- self.agents.append(agent)
- aid += 1
- self.space = self.addSubModel(Space(dim, dim, radius, happy_threshold, grid, empty, self.rng))
- for a in self.agents:
- self.connectPorts(a.queryNeighbours, self.space.neighbourCheck)
- self.connectPorts(self.space.newPositionsPorts[a.aid], a.move)
- self.connectPorts(self.space.changeConditionPorts[a.aid], a.updateCondition)
|