Skip to content

Reference

This part of the project documentation focuses on an information-oriented approach. Use it as a reference for the technical implementation of the calculator project code.

VRP Base Components Module.

This module allows the suer to make basic operations with the base elements form a VRP problem.

Examples:

>>> import numpy as np
>>> from hypy.problems.vrp.base_components import Location
>>> Location(np.array([2, 3, 4])) - Location(np.array([1, 2, 3]))
array([1, 1, 1])
>>> Location(np.array([5, 23, 7])) - Location(np.array([2, 20, 1]))
array([3, 3, 6])

BaseElement

VRP Base Element Class.

Source code in hypy/problems/vrp/base_components.py
 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
class BaseElement:
    """VRP Base Element Class."""

    def __init__(self, location: Location) -> None:
        """Class Constructor.

        Args:
            location: _description_

        Raises:
            TypeError: _description_
        """
        if isinstance(location, Location):
            self.location = location
        else:
            raise TypeError(
                f"Location must be of type {Location}, not {type(location)}"
            )

    def compute_distance(self, element: BaseElement) -> np.float_:
        """Computes distance between current object an another VRP element.

        Args:
            element: _description_

        Returns:
            _description_
        """
        # TODO: Check Location types are the same, same dimensions, etc.
        return self.distance_op(element.location)

    def distance_op(self, location: Location) -> np.float_:
        """Defines the distance operation. Defaults to euclidean distance.

        Args:
            location: _description_

        Returns:
            _description_
        """
        return np.linalg.norm(self.location - location, ord=2)

__init__(location)

Class Constructor.

Parameters:

Name Type Description Default
location Location

description

required

Raises:

Type Description
TypeError

description

Source code in hypy/problems/vrp/base_components.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def __init__(self, location: Location) -> None:
    """Class Constructor.

    Args:
        location: _description_

    Raises:
        TypeError: _description_
    """
    if isinstance(location, Location):
        self.location = location
    else:
        raise TypeError(
            f"Location must be of type {Location}, not {type(location)}"
        )

compute_distance(element)

Computes distance between current object an another VRP element.

Parameters:

Name Type Description Default
element BaseElement

description

required

Returns:

Type Description
np.float_

description

Source code in hypy/problems/vrp/base_components.py
 99
100
101
102
103
104
105
106
107
108
109
def compute_distance(self, element: BaseElement) -> np.float_:
    """Computes distance between current object an another VRP element.

    Args:
        element: _description_

    Returns:
        _description_
    """
    # TODO: Check Location types are the same, same dimensions, etc.
    return self.distance_op(element.location)

distance_op(location)

Defines the distance operation. Defaults to euclidean distance.

Parameters:

Name Type Description Default
location Location

description

required

Returns:

Type Description
np.float_

description

Source code in hypy/problems/vrp/base_components.py
111
112
113
114
115
116
117
118
119
120
def distance_op(self, location: Location) -> np.float_:
    """Defines the distance operation. Defaults to euclidean distance.

    Args:
        location: _description_

    Returns:
        _description_
    """
    return np.linalg.norm(self.location - location, ord=2)

Customer

Bases: BaseElement

VRP Customer Class.

Source code in hypy/problems/vrp/base_components.py
123
124
125
126
127
128
129
130
131
132
133
134
class Customer(BaseElement):
    """VRP Customer Class."""

    def __init__(self, location: Location, demand: float | int) -> None:
        """Class Constructor.

        Args:
            location: _description_
            demand: _description_
        """
        super().__init__(location)
        self.demand = demand

__init__(location, demand)

Class Constructor.

Parameters:

Name Type Description Default
location Location

description

required
demand float | int

description

required
Source code in hypy/problems/vrp/base_components.py
126
127
128
129
130
131
132
133
134
def __init__(self, location: Location, demand: float | int) -> None:
    """Class Constructor.

    Args:
        location: _description_
        demand: _description_
    """
    super().__init__(location)
    self.demand = demand

Depot

Bases: BaseElement

VRP Depot Class.

Source code in hypy/problems/vrp/base_components.py
151
152
153
154
155
156
157
158
159
160
class Depot(BaseElement):
    """VRP Depot Class."""

    def __init__(self, location: Location) -> None:
        """Class Constructor.

        Args:
            location: _description_
        """
        super().__init__(location)

__init__(location)

Class Constructor.

Parameters:

Name Type Description Default
location Location

description

required
Source code in hypy/problems/vrp/base_components.py
154
155
156
157
158
159
160
def __init__(self, location: Location) -> None:
    """Class Constructor.

    Args:
        location: _description_
    """
    super().__init__(location)

Location

VRP Element Location Class.

Source code in hypy/problems/vrp/base_components.py
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
class Location:
    """VRP Element Location Class."""

    def __init__(self, coordinates: npt.NDArray[np.float_]) -> None:
        """Class Constructor.

        Args:
            coordinates: _description_

        Raises:
            LocationCoordinatesError: _description_
            LocationCoordinatesError: _description_
            LocationCoordinatesError: _description_
        """
        if not isinstance(coordinates, np.ndarray):
            raise LocationCoordinatesError(coordinates_type=type(coordinates))

        if len(coordinates.shape) > 1:
            raise LocationCoordinatesError(shape=coordinates.shape[1])  # type: ignore  # noqa: E501

        if coordinates.size == 0:
            raise LocationCoordinatesError(size=coordinates.size)

        self.coordinates = coordinates

    def __sub__(self, location: Location) -> np.float_ | np.int_:
        """Subtraction Method.

        Examples:
            >>> import numpy as np
            >>> Location(np.array([2, 3, 4])) - Location(np.array([1, 2, 3]))
            array([1, 1, 1])
            >>> Location(np.array([5, 23, 7])) - Location(np.array([2, 20, 1]))
            array([3, 3, 6])

        Args:
            location: _description_

        Returns:
            _description_
        """
        return self.coordinates - location.coordinates  # type: ignore

    def __add__(self, location: Location) -> np.float_ | np.int_:
        """Addition Method.

        Args:
            location: _description_

        Returns:
            _description_
        """
        return self.coordinates + location.coordinates  # type: ignore

__add__(location)

Addition Method.

Parameters:

Name Type Description Default
location Location

description

required

Returns:

Type Description
np.float_ | np.int_

description

Source code in hypy/problems/vrp/base_components.py
68
69
70
71
72
73
74
75
76
77
def __add__(self, location: Location) -> np.float_ | np.int_:
    """Addition Method.

    Args:
        location: _description_

    Returns:
        _description_
    """
    return self.coordinates + location.coordinates  # type: ignore

__init__(coordinates)

Class Constructor.

Parameters:

Name Type Description Default
coordinates npt.NDArray[np.float_]

description

required

Raises:

Type Description
LocationCoordinatesError

description

LocationCoordinatesError

description

LocationCoordinatesError

description

Source code in hypy/problems/vrp/base_components.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def __init__(self, coordinates: npt.NDArray[np.float_]) -> None:
    """Class Constructor.

    Args:
        coordinates: _description_

    Raises:
        LocationCoordinatesError: _description_
        LocationCoordinatesError: _description_
        LocationCoordinatesError: _description_
    """
    if not isinstance(coordinates, np.ndarray):
        raise LocationCoordinatesError(coordinates_type=type(coordinates))

    if len(coordinates.shape) > 1:
        raise LocationCoordinatesError(shape=coordinates.shape[1])  # type: ignore  # noqa: E501

    if coordinates.size == 0:
        raise LocationCoordinatesError(size=coordinates.size)

    self.coordinates = coordinates

__sub__(location)

Subtraction Method.

Examples:

>>> import numpy as np
>>> Location(np.array([2, 3, 4])) - Location(np.array([1, 2, 3]))
array([1, 1, 1])
>>> Location(np.array([5, 23, 7])) - Location(np.array([2, 20, 1]))
array([3, 3, 6])

Parameters:

Name Type Description Default
location Location

description

required

Returns:

Type Description
np.float_ | np.int_

description

Source code in hypy/problems/vrp/base_components.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __sub__(self, location: Location) -> np.float_ | np.int_:
    """Subtraction Method.

    Examples:
        >>> import numpy as np
        >>> Location(np.array([2, 3, 4])) - Location(np.array([1, 2, 3]))
        array([1, 1, 1])
        >>> Location(np.array([5, 23, 7])) - Location(np.array([2, 20, 1]))
        array([3, 3, 6])

    Args:
        location: _description_

    Returns:
        _description_
    """
    return self.coordinates - location.coordinates  # type: ignore

Route

VRP Route Class.

Source code in hypy/problems/vrp/base_components.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
class Route:
    """VRP Route Class."""

    def __init__(self, vehicle: Vehicle, route: list[Customer]) -> None:
        """Class Constructor.

        Args:
            vehicle: _description_
            route: _description_
        """
        self.vehicle = vehicle
        self.route = route

    def __len__(self):
        """Length ob object method.

        Returns:
            _description_
        """
        return len(self.route)

__init__(vehicle, route)

Class Constructor.

Parameters:

Name Type Description Default
vehicle Vehicle

description

required
route list[Customer]

description

required
Source code in hypy/problems/vrp/base_components.py
166
167
168
169
170
171
172
173
174
def __init__(self, vehicle: Vehicle, route: list[Customer]) -> None:
    """Class Constructor.

    Args:
        vehicle: _description_
        route: _description_
    """
    self.vehicle = vehicle
    self.route = route

__len__()

Length ob object method.

Returns:

Type Description

description

Source code in hypy/problems/vrp/base_components.py
176
177
178
179
180
181
182
def __len__(self):
    """Length ob object method.

    Returns:
        _description_
    """
    return len(self.route)

Solution

Bases: BaseSolution

VRP Solution Class.

Source code in hypy/problems/vrp/base_components.py
185
186
187
188
189
190
191
192
193
194
class Solution(BaseSolution):
    """VRP Solution Class."""

    def __init__(self, routes: list[Route]) -> None:
        """Class Constructor.

        Args:
            routes: _description_
        """
        self.routes = routes

__init__(routes)

Class Constructor.

Parameters:

Name Type Description Default
routes list[Route]

description

required
Source code in hypy/problems/vrp/base_components.py
188
189
190
191
192
193
194
def __init__(self, routes: list[Route]) -> None:
    """Class Constructor.

    Args:
        routes: _description_
    """
    self.routes = routes

VRPHeuristic

Bases: BaseHeuristic

VRP Heuristic Class.

Source code in hypy/problems/vrp/base_components.py
219
220
221
222
223
224
class VRPHeuristic(BaseHeuristic):
    """VRP Heuristic Class."""

    def __init__(self) -> None:
        """Class Constructor."""
        super().__init__()

__init__()

Class Constructor.

Source code in hypy/problems/vrp/base_components.py
222
223
224
def __init__(self) -> None:
    """Class Constructor."""
    super().__init__()

VRPProblem

Bases: BaseProblem

VRP Problem Class.

Source code in hypy/problems/vrp/base_components.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
class VRPProblem(BaseProblem):
    """VRP Problem Class."""

    def __init__(
        self,
        depot: list[Depot],
        customers: list[Customer],
        vehicles: list[Vehicle],
    ) -> None:
        """Class Constructor.

        Args:
            depot: _description_
            customers: _description_
            vehicles: _description_
        """
        super().__init__()
        self.depot = depot
        self.customers = customers
        self.vehicles = vehicles

__init__(depot, customers, vehicles)

Class Constructor.

Parameters:

Name Type Description Default
depot list[Depot]

description

required
customers list[Customer]

description

required
vehicles list[Vehicle]

description

required
Source code in hypy/problems/vrp/base_components.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
def __init__(
    self,
    depot: list[Depot],
    customers: list[Customer],
    vehicles: list[Vehicle],
) -> None:
    """Class Constructor.

    Args:
        depot: _description_
        customers: _description_
        vehicles: _description_
    """
    super().__init__()
    self.depot = depot
    self.customers = customers
    self.vehicles = vehicles

Vehicle

Bases: BaseElement

VRP Vehicle Class.

Source code in hypy/problems/vrp/base_components.py
137
138
139
140
141
142
143
144
145
146
147
148
class Vehicle(BaseElement):
    """VRP Vehicle Class."""

    def __init__(self, location: Location, capacity: float | int) -> None:
        """Class Constructor.

        Args:
            location: _description_
            capacity: _description_
        """
        super().__init__(location)
        self.capacity = capacity

__init__(location, capacity)

Class Constructor.

Parameters:

Name Type Description Default
location Location

description

required
capacity float | int

description

required
Source code in hypy/problems/vrp/base_components.py
140
141
142
143
144
145
146
147
148
def __init__(self, location: Location, capacity: float | int) -> None:
    """Class Constructor.

    Args:
        location: _description_
        capacity: _description_
    """
    super().__init__(location)
    self.capacity = capacity