cost-matrix 0.1.0__tar.gz → 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cost_matrix-0.1.0 → cost_matrix-0.1.1}/PKG-INFO +2 -1
- {cost_matrix-0.1.0 → cost_matrix-0.1.1}/README.md +1 -0
- cost_matrix-0.1.1/cost_matrix/euclidean_matrix.py +28 -0
- cost_matrix-0.1.1/cost_matrix/manhattan_matrix.py +29 -0
- {cost_matrix-0.1.0 → cost_matrix-0.1.1}/cost_matrix/osrm_matrix.py +22 -1
- {cost_matrix-0.1.0 → cost_matrix-0.1.1}/cost_matrix/spherical_matrix.py +15 -1
- {cost_matrix-0.1.0 → cost_matrix-0.1.1}/pyproject.toml +1 -1
- cost_matrix-0.1.0/cost_matrix/euclidean_matrix.py +0 -5
- cost_matrix-0.1.0/cost_matrix/manhattan_matrix.py +0 -5
- {cost_matrix-0.1.0 → cost_matrix-0.1.1}/cost_matrix/__init__.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: cost-matrix
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.1
|
4
4
|
Summary: Designed to simplify the creation of cost matrices for optimization problems.
|
5
5
|
Author: Luan
|
6
6
|
Author-email: llvdmoraes@gmail.com
|
@@ -69,4 +69,5 @@ osrm_duration_matrix = cost_matrix.osrm(
|
|
69
69
|
batch_size=250
|
70
70
|
)
|
71
71
|
print(osrm_duration_matrix)
|
72
|
+
```
|
72
73
|
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import numpy as np
|
2
|
+
|
3
|
+
|
4
|
+
def euclidean(sources, destinations):
|
5
|
+
"""
|
6
|
+
Calculate the Euclidean distance matrix between source and destination points.
|
7
|
+
|
8
|
+
Parameters
|
9
|
+
----------
|
10
|
+
sources : np.ndarray
|
11
|
+
Array of shape (n, d) containing the source points.
|
12
|
+
destinations : np.ndarray
|
13
|
+
Array of shape (m, d) containing the destination points.
|
14
|
+
|
15
|
+
Returns
|
16
|
+
-------
|
17
|
+
np.ndarray
|
18
|
+
Euclidean distance matrix of shape (n, m).
|
19
|
+
"""
|
20
|
+
# Expand dimensions to enable broadcasting
|
21
|
+
sources_expanded = sources[:, np.newaxis, :]
|
22
|
+
destinations_expanded = destinations[np.newaxis, :, :]
|
23
|
+
|
24
|
+
# Compute differences along the last dimension
|
25
|
+
differences = sources_expanded - destinations_expanded
|
26
|
+
|
27
|
+
# Compute Euclidean distances using norm along the last dimension
|
28
|
+
return np.linalg.norm(differences, axis=2)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import numpy as np
|
2
|
+
|
3
|
+
|
4
|
+
def manhattan(sources: np.ndarray, destinations: np.ndarray) -> np.ndarray:
|
5
|
+
"""
|
6
|
+
Compute the Manhattan distance matrix between source and destination points.
|
7
|
+
|
8
|
+
Parameters
|
9
|
+
----------
|
10
|
+
sources : np.ndarray
|
11
|
+
Array of shape (n, d) containing the source points.
|
12
|
+
destinations : np.ndarray
|
13
|
+
Array of shape (m, d) containing the destination points.
|
14
|
+
|
15
|
+
Returns
|
16
|
+
-------
|
17
|
+
np.ndarray
|
18
|
+
Manhattan distance matrix of shape (n, m).
|
19
|
+
"""
|
20
|
+
# Expand dimensions to enable broadcasting
|
21
|
+
sources_expanded = sources[:, np.newaxis, :]
|
22
|
+
destinations_expanded = destinations[np.newaxis, :, :]
|
23
|
+
|
24
|
+
# Compute absolute differences along the last dimension
|
25
|
+
differences = np.abs(sources_expanded - destinations_expanded)
|
26
|
+
|
27
|
+
# Sum absolute differences along the last dimension to get
|
28
|
+
# Manhattan distance
|
29
|
+
return np.sum(differences, axis=2)
|
@@ -11,7 +11,28 @@ def osrm(
|
|
11
11
|
batch_size: int = 150,
|
12
12
|
cost_type: str = "distances",
|
13
13
|
) -> np.ndarray:
|
14
|
-
"""
|
14
|
+
"""
|
15
|
+
Compute the OSRM cost matrix between sources and destinations
|
16
|
+
|
17
|
+
Parameters
|
18
|
+
----------
|
19
|
+
sources : np.ndarray
|
20
|
+
Array of shape (n, d) containing the source points.
|
21
|
+
destinations : np.ndarray
|
22
|
+
Array of shape (m, d) containing the destination points.
|
23
|
+
server_address : str
|
24
|
+
Address of the OSRM server. Default is "http://router.project-osrm.org".
|
25
|
+
batch_size : int
|
26
|
+
Number of points to send in each request. Default is 150.
|
27
|
+
cost_type : str
|
28
|
+
Type of cost to be computed. Default is "distances". Options are
|
29
|
+
"distances" and "durations".
|
30
|
+
|
31
|
+
Returns
|
32
|
+
-------
|
33
|
+
np.ndarray
|
34
|
+
OSRM cost matrix of shape (n, m).
|
35
|
+
"""
|
15
36
|
|
16
37
|
num_sources = sources.shape[0]
|
17
38
|
num_destinations = destinations.shape[0]
|
@@ -4,7 +4,21 @@ EARTH_RADIUS_METERS = 6371000
|
|
4
4
|
|
5
5
|
|
6
6
|
def spherical(sources: np.ndarray, destinations: np.ndarray) -> np.ndarray:
|
7
|
-
"""
|
7
|
+
"""
|
8
|
+
Compute the distance matrix using the Spherical distance]
|
9
|
+
|
10
|
+
Parameters
|
11
|
+
----------
|
12
|
+
sources : np.ndarray
|
13
|
+
Array of shape (n, d) containing the source points.
|
14
|
+
destinations : np.ndarray
|
15
|
+
Array of shape (m, d) containing the destination points.
|
16
|
+
|
17
|
+
Returns
|
18
|
+
-------
|
19
|
+
np.ndarray
|
20
|
+
Spherical distance matrix of shape (n, m).
|
21
|
+
"""
|
8
22
|
|
9
23
|
sources_rad = np.radians(sources)
|
10
24
|
destinations_rad = np.radians(destinations)
|
File without changes
|