| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- from typing import NamedTuple
- from pypdevs.infinity import INFINITY
- import os
- if os.getenv("PYPDEVS_MINIMAL") == "1":
- from pypdevs.minimal import AtomicDEVS
- else:
- from pypdevs.DEVS import AtomicDEVS
- class SpaceState(NamedTuple):
- grid: dict[tuple[int, int], tuple[int, int]] # (x,y): (aid, group) find neighbours
- empty: list[tuple[int, int]] # (x,y) move to empty space
- action: str
- class Space(AtomicDEVS):
- def __init__(self, width, height, radius: int, threshold: int, grid: dict[tuple[int, int], tuple[int, int]], empty: list[tuple[int, int]], rng) -> None:
- AtomicDEVS.__init__(self, "Space")
- self.width = width
- self.height = height
- self.offsets = [
- (dx, dy)
- for dx in range(-radius, radius + 1)
- for dy in range(-radius, radius + 1)
- if not (dx == 0 and dy == 0)
- ]
- self.threshold = threshold
- self.rng = rng
- self.state = SpaceState(grid, empty, "Idle")
- self.neighbourCheck = self.addInPort("neighbour check")
- self.changeConditionPorts = {} # dict with output ports to update an agents emotional condition
- self.newPositionsPorts = {} # dict with output ports to update an agents position
- for aid, group in grid.values():
- self.changeConditionPorts[aid] = self.addOutPort(f"change condition {aid}")
- self.newPositionsPorts[aid] = self.addOutPort(f"new position {aid}")
- self.happy: list[int] = [] # (aid) list of agents which will become happy
- self.move: list[tuple[int, tuple[int, int]]] = [] # (aid, (x,y)) list of agents which will move in an attempt to become happy
- def isHappy(self, position: tuple[int, int], group) -> int:
- x, y = position
- similar = 0
- grid = self.state.grid
- threshold = self.threshold
- remaining_checks = len(self.offsets)
- for dx, dy in self.offsets:
- # threshold unreachable
- if similar + remaining_checks < threshold:
- return False
- neighbor = grid.get((x + dx, y + dy))
- if neighbor is not None and neighbor[1] == group:
- similar += 1
- if similar == threshold:
- return True
- remaining_checks -= 1
- return False
- def extTransition(self, inputs):
- grid, empty, action = self.state
- if self.neighbourCheck in inputs:
- self.happy = []
- self.move = []
- queries = inputs[self.neighbourCheck]
- self.rng.shuffle(queries)
- for aid, pos, group in queries:
- if self.isHappy(pos, group):
- # print(f"Agent {aid} at {pos} is HAPPY ({similar} similar).")
- self.happy.append(aid)
- continue
- idx = self.rng.randrange(len(empty))
- new_pos = empty[idx]
- empty[idx] = pos
- del grid[pos]
- grid[new_pos] = (aid, group)
- # print(f"Agent {aid} at {pos} is UNHAPPY ({similar} similar). Moving to {new_pos}...")
- self.move.append((aid, new_pos))
- return SpaceState(grid, empty, "Send")
- return self.state
- def intTransition(self):
- grid, empty, action = self.state
- if action == "Send":
- return SpaceState(grid, empty, "Idle")
- return self.state
- def outputFnc(self):
- grid, empty, action = self.state
- if action == "Send":
- output = {}
- for aid in self.happy:
- output[self.changeConditionPorts[aid]] = [True]
- for aid, pos in self.move:
- output[self.newPositionsPorts[aid]] = [pos]
- return output
- return {}
- def timeAdvance(self):
- grid, empty, action = self.state
- if action == "Send":
- return 0.0
- return INFINITY
|