# Source code for jupedsim.distributions

```# Copyright © 2012-2023 Forschungszentrum Jülich GmbH
import numpy as np
import shapely

from jupedsim.internal.grid import Grid

[docs]class AgentNumberError(Exception):
def __init__(self, message):
self.message = message

[docs]class IncorrectParameterError(Exception):
def __init__(self, message):
self.message = message

[docs]class OverlappingCirclesError(Exception):
def __init__(self, message):
self.message = message

[docs]class NegativeValueError(Exception):
def __init__(self, message):
self.message = message

"""returns the intersecting area of circle and polygon"""
# creates a point
point = shapely.Point(mid_point)
# creates a layer with the size of the radius all around this point
# returns the size of the intersecting area
return polygon.intersection(circle).area

def __is_inside_circle(point, mid, min_r, max_r):
"""checks if a point is inside a Circle segment reaching from minimum radius to maximum radius"""
dif_x = point - mid
dif_y = point - mid
circle_equation = dif_x**2 + dif_y**2
return min_r**2 <= circle_equation <= max_r**2

def __get_bounding_box(polygon):
"""returns an Axis Aligned Bounding Box containing the minimal/maximal x and y values
formatted like : [(min(x_values), min(y_values)), (max(x_values), max(y_values))]
polygon needs to be a shapely polygon
"""
corner_points = list(polygon.exterior.coords)
x_values, y_values = [], []
for point in corner_points:
x_values.append(point)
y_values.append(point)

return [(min(x_values), min(y_values)), ((max(x_values)), max(y_values))]

def __min_distance_to_polygon(pt, polygon):
"""returns the minimal distance between a point and every line segment of a polygon"""
pt = shapely.Point(pt)
min_dist = polygon.exterior.distance(pt)
for hole in polygon.interiors:
candidate_dist = hole.distance(pt)
min_dist = min(min_dist, candidate_dist)
return min_dist

[docs]def distribute_by_number(
*,
polygon: shapely.Polygon,
number_of_agents: int,
distance_to_agents: float,
distance_to_polygon: float,
seed: int | None = None,
max_iterations: int = 10000,
) -> list[tuple[float, float]]:
"""Generates specified number of randomized 2D coordiantes.

This function will generate the speficied number of 2D coordiantes where
all coordiantes are inside the specified geometry and generated coordinates
are constraint by distance_to_agents and distance_to_polygon. This function
may not always by able to generate the requested coordinate because it
cannot do so without violating the constraints. In this case the function
will stop after max_iterations and raise an Exception.

Arguments:
polygon: polygon where the agents shall be placed
number_of_agents: number of agents to be distributed
distance_to_agents: minimal distance between the centers of agents
distance_to_polygon: minimal distance between the center of agents
and the polygon edges
seed: Will be used to seed the random number generator.
max_iterations: Up to max_iterations are attempts are made to
place a random point without conastraint violation, default is 10_000

Returns:
2D coordiantes

Raises:
:class:`AgentNumberError`: if not all agents could be placed.
:class:`IncorrectParameterError`: if polygon is not of type
:class:`~shapely.Polygon`

"""
if not isinstance(polygon, shapely.Polygon):
raise IncorrectParameterError(
f"Polygon is expected to be a shapely Polygon"
)
box = __get_bounding_box(polygon)

np.random.seed(seed)

grid = Grid(box, distance_to_agents)
created_points = 0
iterations = 0
while created_points < number_of_agents:
if iterations > max_iterations:
msg = (
f"Only {created_points} of {number_of_agents}  could be placed."
f" density: {round(created_points / polygon.area, 2)} p/m²"
)
raise AgentNumberError(msg)
temp_point = (
np.random.uniform(box, box),
np.random.uniform(box, box),
)
if __check_distance_constraints(
temp_point, distance_to_polygon, grid, polygon
):
grid.append_point(temp_point)

iterations = 0
created_points += 1
else:
iterations += 1

return grid.get_samples()

[docs]def distribute_by_density(
*,
polygon: shapely.Polygon,
density: float,
distance_to_agents: float,
distance_to_polygon: float,
seed: int | None = None,
max_iterations: int = 10000,
) -> list[tuple[float, float]]:
"""Generates randomized 2D coordinates based on a desired agent density per
square meter.

This function will generate as many 2D coordinates as required to reach the
desired density. Essentially this function tries to place area * density
many agents while adhering to the distance_to_polygon and
distance_to_agents constraints. This function may not always by able to
generate the requested coordinate because it cannot do so without violating
the constraints. In this case the function will stop after max_iterations
and raise an Exception.

Arguments:
polygon: Area where to generate 2D coordinates in.
density: desired density in agents per square meter
distance_to_agents: minimal distance between the centers of agents
distance_to_polygon: minimal distance between the center of agents
and the polygon edges
seed: Will be used to seed the random number generator.
max_iterations: Up to max_iterations are attempts are made to
place a random point without constraint violation, default is 10_000

Returns:
2D coordiantes

Raises:
:class:`AgentNumberError`: if not all agents could be placed.
:class:`IncorrectParameterError`: if polygon is not of type
:class:`~shapely.Polygon`

"""

if not isinstance(polygon, shapely.Polygon):
raise IncorrectParameterError(
f"Polygon is expected to be a shapely Polygon"
)
area = polygon.area
number = round(density * area)
return distribute_by_number(
polygon=polygon,
number_of_agents=number,
distance_to_agents=distance_to_agents,
distance_to_polygon=distance_to_polygon,
seed=seed,
max_iterations=max_iterations,
)

def __catch_wrong_inputs(
):
"""checks if an input parameter is incorrect and raises an Exception"""

if not isinstance(polygon, shapely.Polygon):
raise IncorrectParameterError(
f"Polygon is expected to be a shapely Polygon"
)
try:
if len(center_point) != 2:
raise IncorrectParameterError(
f"Center_point expected a tuple of 2 numbers, {len(center_point)} were given"
)
except TypeError:
# center point is no tuple or list
raise IncorrectParameterError(
f"Center_point expected a tuple of 2 numbers, given Type: {type(center_point)}"
)
raise IncorrectParameterError(
f"the number of circle segments does not match the number of fill parameters.\n"
f"fill parameter given for {len(fill_parameters)} circle segments"
)
raise NegativeValueError(
)
raise OverlappingCirclesError(
)
j = 0
while j < i:
if (
):
j = j + 1
continue
else:
raise OverlappingCirclesError(
)

[docs]def distribute_in_circles_by_number(
*,
polygon: shapely.Polygon,
distance_to_agents: float,
distance_to_polygon: float,
center_point: tuple[float, float],
numbers_of_agents: list[int],
seed=None,
max_iterations=10_000,
) -> list[tuple[float, float]]:
"""Generates randomized 2D coordiantes in a user defined number of rings.

This function will generate 2D coordinates in the intersection of the
polygon and the rings specified by the centerpoint and the min/max radii of
each ring. `number_of_agents` is expected to contain the number of agents
to be placed for each ring. This function may not always by able to
generate the requested coordinate because it cannot do so without violating
the constraints. In this case the function will stop after max_iterations
and raise an Exception.

Arguments:
polygon: polygon where agents can be placed.
distance_to_agents: minimal distance between the centers of agents
distance_to_polygon: minimal distance between the center of agents
and the polygon edges
center_point: Center point of the rings.
number_of_agents: agents to be placed per ring
seed: Will be used to seed the random number generator.
max_iterations: Up to max_iterations are attempts are made to
place a random point without conastraint violation, default is 10_000

Returns:
2D coordiantes

Raises:
:class:`AgentNumberError`: if not all agents could be placed.
:class:`IncorrectParameterError`: if polygon is not of type
:class:`~shapely.Polygon`
overlapp
"""

# catch wrong inputs
__catch_wrong_inputs(
polygon=polygon,
center_point=center_point,
fill_parameters=numbers_of_agents,
)
np.random.seed(seed)
box = __get_bounding_box(polygon)
grid = Grid(box, distance_to_agents)

for circle_segment, number in zip(circle_segment_radii, numbers_of_agents):
big_circle_area = __intersecting_area_polygon_circle(
)
small_circle_area = __intersecting_area_polygon_circle(
)
placeable_area = big_circle_area - small_circle_area

# checking whether to place points
# inside the circle segment or
# inside the bounding box of the intersection of polygon and Circle Segment

# determine the entire area of the circle segment
# determine the area where a point might be placed around the polygon
dif_x, dif_y = (
sec_box - sec_box,
sec_box - sec_box,
)
bounding_box_area = dif_x * dif_y

if entire_circle_area < bounding_box_area:
# inside the circle it is more likely to find a random point that is inside the polygon
for placed_count in range(number):
i = 0
while i < max_iterations:
i += 1
# determines a random radius within the circle segment
rho = np.sqrt(
)
# determines a random degree
theta = np.random.uniform(0, 2 * np.pi)
pt = center_point + rho * np.cos(theta), center_point[
1
] + rho * np.sin(theta)
if __check_distance_constraints(
pt, distance_to_polygon, grid, polygon
):
grid.append_point(pt)
break

if i >= max_iterations and placed_count != number:
message = (
f"the desired amount of agents in the Circle segment from"
f"\nOnly {placed_count} of {number}  could be placed."
f"\nactual density: {round(placed_count / placeable_area, 2)} p/m²"
)
raise AgentNumberError(message)
else:
# placing point inside the bounding box is more likely to find a random point that is inside the circle
placed_count = 0
iterations = 0
while placed_count < number:
if iterations > max_iterations:
message = (
f"the desired amount of agents in the Circle segment from"
f"\nOnly {placed_count} of {number}  could be placed."
f"\nactual density: {round(placed_count / placeable_area, 2)} p/m²"
)
raise AgentNumberError(message)
temp_point = (
np.random.uniform(sec_box, sec_box),
np.random.uniform(sec_box, sec_box),
)
if __is_inside_circle(
) and __check_distance_constraints(
temp_point, distance_to_polygon, grid, polygon
):
grid.append_point(temp_point)
iterations = 0
placed_count += 1
else:
iterations += 1
return grid.get_samples()

[docs]def distribute_in_circles_by_density(
*,
polygon: shapely.Polygon,
distance_to_agents: float,
distance_to_polygon: float,
center_point: tuple[float, float],
densities: list[float],
seed: int | None = None,
max_iterations: int = 10_000,
) -> list[tuple[float, float]]:
"""Generates randomized 2D coordiantes in a user defined number of rings
with defined density.

This function will generate 2D coordinates in the intersection of the
polygon and the rings specified by the centerpoint and the min/max radii of
each ring. The number of positions generated is defined by the desired
density and available space of each ring. This function may not always by
able to generate the requested coordinate because it cannot do so without
violating the constraints. In this case the function will stop after
max_iterations and raise an Exception.

Arguments:
polygon: polygon where agents can be placed.
distance_to_agents: minimal distance between the centers of agents
distance_to_polygon: minimal distance between the center of agents
and the polygon edges
center_point: Center point of the rings.
desnities: density in positionsper square meter for each ring
seed: Will be used to seed the random number generator.
max_iterations: Up to max_iterations are attempts are made to place a
random point without conastraint violation, default is 10_000

Returns:
2D coordiantes

Raises:
:class:`AgentNumberError`: if not all agents could be placed.
:class:`IncorrectParameterError`: if polygon is not of type
:class:`~shapely.Polygon`
overlapp
"""

__catch_wrong_inputs(
polygon=polygon,
center_point=center_point,
fill_parameters=densities,
)
number_of_agents = []
for circle_segment, density in zip(circle_segment_radii, densities):
big_circle_area = __intersecting_area_polygon_circle(
center_point, circle_segment, polygon
)
small_circle_area = __intersecting_area_polygon_circle(
center_point, circle_segment, polygon
)
placeable_area = big_circle_area - small_circle_area
number_of_agents.append(int(density * placeable_area))

return distribute_in_circles_by_number(
polygon=polygon,
distance_to_agents=distance_to_agents,
distance_to_polygon=distance_to_polygon,
center_point=center_point,
numbers_of_agents=number_of_agents,
seed=seed,
max_iterations=max_iterations,
)

[docs]def distribute_until_filled(
*,
polygon: shapely.Polygon,
distance_to_agents: float,
distance_to_polygon: float,
seed: int | None = None,
max_iterations: int = 10_000,
k: int = 30,
) -> list[tuple[float, float]]:
"""Generates randomized 2D coordiantes that fill the specified area.

This function will generate 2D coordinates in the specified area. The
number of positions generated depends on the ability to place aditional
points. This function may not always by able to generate the requested
coordinate because it cannot do so without violating the constraints. In
this case the function will stop after max_iterations and raise an
Exception.

Arguments:
polygon: polygon where agents can be placed.
distance_to_agents: minimal distance between the centers of agents
distance_to_polygon: minimal distance between the center of agents
and the polygon edges
seed: Will be used to seed the random number generator.
max_iterations: Up to max_iterations are attempts are made to
place a random point without conastraint violation, default is 10_000
k: maximum number of attempts to place neighbors to already inserted
points. A higher value will result in a higher density but will greatly
increase runtim.

Returns:
2D coordiantes

Raises:
:class:`AgentNumberError`: if not all agents could be placed.
:class:`IncorrectParameterError`: if polygon is not of type
:class:`~shapely.Polygon`
"""
if not isinstance(polygon, shapely.Polygon):
raise IncorrectParameterError(
f"Polygon is expected to be a shapely Polygon"
)
box = __get_bounding_box(polygon)

np.random.seed(seed)
# initialises a list for active Points and a Grid administering all created points
active = []
grid = Grid(box, distance_to_agents)
# initialisation of the first point
iteration = 0
while iteration < max_iterations:
first_point = (
np.random.uniform(box, box),
np.random.uniform(box, box),
)
if __check_distance_constraints(
first_point, distance_to_polygon, grid, polygon
):
grid.append_point(first_point)
active.append(first_point)
break
iteration = iteration + 1
if iteration >= max_iterations:
raise IncorrectParameterError(
"The first point could not be placed inside the polygon."
" Check if there is enough space for agents provided inside the polygon"
)

# Uses https://www.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf
# "Fast Poisson Disk Sampling in Arbitrary Dimensions"
# while points are active a random reference point is selected
while active:
ref_point = active[np.random.randint(0, len(active))]
iteration = 0
# tries to find a point around the reference Point
while iteration < k:
# determines a random radius within a circle segment
# with radius from distance_to_agents to distance_to_agents * 2
rho = np.sqrt(
np.random.uniform(
distance_to_agents**2, 4 * distance_to_agents**2
)
)
# determines a random degree
theta = np.random.uniform(0, 2 * np.pi)
pt = ref_point + rho * np.cos(theta), ref_point[
1
] + rho * np.sin(theta)
if __check_distance_constraints(
pt, distance_to_polygon, grid, polygon
):
grid.append_point(pt)
active.append(pt)
break
iteration = iteration + 1

# if there was no point found around the reference point it is considered inactive
if iteration >= k:
active.remove(ref_point)

return grid.get_samples()

[docs]def distribute_by_percentage(
*,
polygon: shapely.Polygon,
percent: float,
distance_to_agents: float,
distance_to_polygon: float,
seed: int | None = None,
max_iterations: int = 10000,
k: int = 30,
):
"""Generates randomized 2D coordiantes that fill the specified area to a
percentage of a possible maximum.

This function will generate 2D coordinates in the specified area. The
number of positions generated depends on the ability to place aditional
points. This function may not always by able to generate the requested
coordinate because it cannot do so without violating the constraints. In
this case the function will stop after max_iterations and raise an
Exception.

Arguments:
polygon: polygon where agents can be placed.
percent: percent value of occupancy to generate. needs to be in
the intervall (0, 100]
distance_to_agents: minimal distance between the centers of agents
distance_to_polygon: minimal distance between the center of agents
and the polygon edges
seed: Will be used to seed the random number generator.
max_iterations: Up to max_iterations are attempts are made to
place a random point without conastraint violation, default is 10_000
k: maximum number of attempts to place neighbors to already inserted
points. A higher value will result in a higher density but will greatly
increase runtim.

Returns:
2D coordiantes

Raises:
:class:`AgentNumberError`: if not all agents could be placed.
:class:`IncorrectParameterError`: if polygon is not of type
:class:`~shapely.Polygon`
"""
samples = distribute_until_filled(
polygon=polygon,
distance_to_agents=distance_to_agents,
distance_to_polygon=distance_to_polygon,
seed=seed,
max_iterations=max_iterations,
k=k,
)
sample_amount = len(samples)
needed_amount = round(sample_amount * (percent / 100))
np.random.seed(seed)
np.random.shuffle(samples)

return samples[:needed_amount]

def __check_distance_constraints(pt, wall_distance, grid, polygon):
"""Determines if a point has enough distance to other points and to the walls
Uses a Grid to determine neighbours
:param grid: the grid of the polygon
:param pt: point that is being checked
:param wall_distance: minimal distance between point and the polygon
:param polygon: shapely Polygon in which the points must lie
:return:True or False"""
if not polygon.contains(shapely.Point(pt)):
return False
if __min_distance_to_polygon(pt, polygon) < wall_distance:
return False
return grid.no_neighbours_in_distance(pt)

"""returns an Axis Aligned Bounding Box containing the intersection of a Circle and the polygon
@:param polygon is a shapely Polygon
@:param center_point is the Center point of the Circle