Skip to content

Location Dataclass

Module containing location representation for the AMR Hub ABM simulation.

Location dataclass

Representation of a location in the AMR Hub ABM simulation.

Parameters:

Name Type Description Default
x float

The x-coordinate of the location.

required
y float

The y-coordinate of the location.

required
floor int

The floor number of the location.

required
building str | None

The building name of the location. Defaults to None, which indicates that the location is not associated with a specific building.

None
Source code in src/amr_hub_abm/spatial/location.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@dataclass(frozen=True)
class Location:
    """
    Representation of a location in the AMR Hub ABM simulation.

    Parameters
    ----------
    x : float
        The x-coordinate of the location.
    y : float
        The y-coordinate of the location.
    floor : int
        The floor number of the location.
    building : str | None, optional
        The building name of the location. Defaults to None, which indicates that the
        location is not associated with a specific building.

    """

    x: float
    y: float
    floor: int
    building: str | None = None

    def distance_to(self, other: Location) -> float:
        """
        Calculate the Euclidean distance to another location.

        Parameters
        ----------
        other : Location
            The other location to calculate the distance to.

        Returns
        -------
        float
            The Euclidean distance between the two locations.

        Raises
        ------
        InvalidDistanceError
            If the two locations are in different buildings or on different floors.

        """
        if self.building != other.building:
            raise InvalidDistanceError((self.building, other.building), building=True)
        if self.floor != other.floor:
            raise InvalidDistanceError((self.floor, other.floor), building=False)

        return math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)

    def __repr__(self) -> str:
        """Return a string representation of the location."""
        return (
            f"Location(x={self.x:.2f}, y={self.y:.2f}, {self.floor}, {self.building})"
        )

    def check_line_of_sight(self, other: Location, walls: list[Wall]) -> bool:
        """
        Check if there is a line of sight to another location, considering walls.

        Parameters
        ----------
        other : Location
            The other location to check line of sight to.
        walls : list[Wall]
            The list of walls to consider.

        Returns
        -------
        bool
            True if there is a line of sight, False otherwise.

        """
        if self.building != other.building:
            return False

        if self.floor != other.floor:
            return False

        line_of_sight = shapely.geometry.LineString(
            [(self.x, self.y), (other.x, other.y)]
        )

        return not any(line_of_sight.crosses(wall.line) for wall in walls)

    @staticmethod
    def check_intersection_with_walls(
        loc_x: float, loc_y: float, interaction_radius: float, walls: list[Wall]
    ) -> bool:
        """
        Check if the agent intersects with any walls.

        Parameters
        ----------
        loc_x : float
            The x-coordinate of the agent's location.
        loc_y : float
            The y-coordinate of the agent's location.
        interaction_radius : float
            The radius within which the agent is considered to interact with walls.
        walls : list[Wall]
            The list of walls to consider.

        Returns
        -------
        bool
            True if the agent intersects with any walls, False otherwise.

        """
        for wall in walls:
            if (
                wall.polygon.distance(shapely.geometry.Point(loc_x, loc_y))
                < interaction_radius
            ):
                return True
        return False

__repr__()

Return a string representation of the location.

Source code in src/amr_hub_abm/spatial/location.py
68
69
70
71
72
def __repr__(self) -> str:
    """Return a string representation of the location."""
    return (
        f"Location(x={self.x:.2f}, y={self.y:.2f}, {self.floor}, {self.building})"
    )

check_intersection_with_walls(loc_x, loc_y, interaction_radius, walls) staticmethod

Check if the agent intersects with any walls.

Parameters:

Name Type Description Default
loc_x float

The x-coordinate of the agent's location.

required
loc_y float

The y-coordinate of the agent's location.

required
interaction_radius float

The radius within which the agent is considered to interact with walls.

required
walls list[Wall]

The list of walls to consider.

required

Returns:

Type Description
bool

True if the agent intersects with any walls, False otherwise.

Source code in src/amr_hub_abm/spatial/location.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@staticmethod
def check_intersection_with_walls(
    loc_x: float, loc_y: float, interaction_radius: float, walls: list[Wall]
) -> bool:
    """
    Check if the agent intersects with any walls.

    Parameters
    ----------
    loc_x : float
        The x-coordinate of the agent's location.
    loc_y : float
        The y-coordinate of the agent's location.
    interaction_radius : float
        The radius within which the agent is considered to interact with walls.
    walls : list[Wall]
        The list of walls to consider.

    Returns
    -------
    bool
        True if the agent intersects with any walls, False otherwise.

    """
    for wall in walls:
        if (
            wall.polygon.distance(shapely.geometry.Point(loc_x, loc_y))
            < interaction_radius
        ):
            return True
    return False

check_line_of_sight(other, walls)

Check if there is a line of sight to another location, considering walls.

Parameters:

Name Type Description Default
other Location

The other location to check line of sight to.

required
walls list[Wall]

The list of walls to consider.

required

Returns:

Type Description
bool

True if there is a line of sight, False otherwise.

Source code in src/amr_hub_abm/spatial/location.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def check_line_of_sight(self, other: Location, walls: list[Wall]) -> bool:
    """
    Check if there is a line of sight to another location, considering walls.

    Parameters
    ----------
    other : Location
        The other location to check line of sight to.
    walls : list[Wall]
        The list of walls to consider.

    Returns
    -------
    bool
        True if there is a line of sight, False otherwise.

    """
    if self.building != other.building:
        return False

    if self.floor != other.floor:
        return False

    line_of_sight = shapely.geometry.LineString(
        [(self.x, self.y), (other.x, other.y)]
    )

    return not any(line_of_sight.crosses(wall.line) for wall in walls)

distance_to(other)

Calculate the Euclidean distance to another location.

Parameters:

Name Type Description Default
other Location

The other location to calculate the distance to.

required

Returns:

Type Description
float

The Euclidean distance between the two locations.

Raises:

Type Description
InvalidDistanceError

If the two locations are in different buildings or on different floors.

Source code in src/amr_hub_abm/spatial/location.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def distance_to(self, other: Location) -> float:
    """
    Calculate the Euclidean distance to another location.

    Parameters
    ----------
    other : Location
        The other location to calculate the distance to.

    Returns
    -------
    float
        The Euclidean distance between the two locations.

    Raises
    ------
    InvalidDistanceError
        If the two locations are in different buildings or on different floors.

    """
    if self.building != other.building:
        raise InvalidDistanceError((self.building, other.building), building=True)
    if self.floor != other.floor:
        raise InvalidDistanceError((self.floor, other.floor), building=False)

    return math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)