Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/swarmsim/sensors/AbstractSensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
class AbstractSensor:
config_vars = ['static_position', 'n_possible_states', 'show']

def __init__(self, agent=None, parent=None, static_position=None, n_possible_states=0, draw=True, **kwargs):
def __init__(self, agent=None, parent=None, static_position=None, n_possible_states=0,
draw=True, seed=None, **kwargs):
"""Sensor class for the agent.

Sensors should typically have a parent that is assigned to them that must be of subclass 'Agent'
Expand All @@ -31,6 +32,7 @@ def __init__(self, agent=None, parent=None, static_position=None, n_possible_sta
self.current_state = 0
self.detection_id = 0
self.goal_detected = False
self.set_seed(seed)

def step(self, world):
if self.agent is None and self.static_position is None:
Expand Down Expand Up @@ -62,3 +64,17 @@ def set_agent(self, agent, parent=None):
self.parent = parent
elif self.parent is None or parent is ...:
self.parent = agent

def set_seed(self, seed):
if seed is None:
if hasattr(self.parent, 'rng'):
self.seed = self.parent.rng.integers(0, 2**31)
elif hasattr(self.agent, 'rng'):
self.seed = self.agent.rng.integers(0, 2**31)
else:
temp_rng = np.random.default_rng(None)
self.seed = int(temp_rng.integers(0, 2**31))
else:
self.seed = seed
self.rng = np.random.default_rng(self.seed)
return self.seed
10 changes: 3 additions & 7 deletions src/swarmsim/sensors/BinaryFOVSensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(
target_team=None,
**kwargs
):
super().__init__(agent=agent, parent=parent)
super().__init__(agent=agent, parent=parent, seed=seed, **kwargs)
self.angle = 0.0
self.theta = theta
self.bias = bias
Expand Down Expand Up @@ -99,10 +99,6 @@ def __init__(

self.r = distance

self.seed = seed
if self.seed is not None:
np.random.seed(self.seed)

def checkForLOSCollisions(self, world: RectangularWorld) -> None:
# Mathematics obtained from Sundaram Ramaswamy
# https://legends2k.github.io/2d-fov/design.html
Expand Down Expand Up @@ -342,7 +338,7 @@ def determineState(self, real_value, agent, world=None):
invert = self.invert
if real_value:
# Consider Reporting False Negative
if np.random.random_sample() < self.fn:
if self.rng.random() < self.fn:
self.agent_in_sight = None
self.current_state = 1 if invert else 0
self.detection_id = 0
Expand All @@ -354,7 +350,7 @@ def determineState(self, real_value, agent, world=None):

else:
# Consider Reporting False Positive
if np.random.random_sample() < self.fp:
if self.rng.random() < self.fp:
self.agent_in_sight = None
self.detection_id = 0
self.current_state = 0 if invert else 1
Expand Down
Loading