|
| 1 | +import numpy |
| 2 | +import geopandas |
| 3 | +import pandas |
| 4 | +import pulp |
| 5 | +from shapely.geometry import Point |
| 6 | + |
| 7 | +from spopt.locate.p_median import KNearestPMedian |
| 8 | +import os |
| 9 | +import pickle |
| 10 | +import platform |
| 11 | +import pytest |
| 12 | +import warnings |
| 13 | + |
| 14 | + |
| 15 | +class TestKNearestPMedian: |
| 16 | + def setup_method(self) -> None: |
| 17 | + # Create the test data |
| 18 | + k = numpy.array([1, 1]) |
| 19 | + self.demand_data = { |
| 20 | + "ID": [1, 2], |
| 21 | + "geometry": [Point(0.5, 1), Point(1.5, 1)], |
| 22 | + "demand": [1, 1], |
| 23 | + } |
| 24 | + self.facility_data = { |
| 25 | + "ID": [101, 102, 103], |
| 26 | + "geometry": [Point(1, 1), Point(0, 2), Point(2, 0)], |
| 27 | + "capacity": [1, 1, 1], |
| 28 | + } |
| 29 | + gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326") |
| 30 | + gdf_fac = geopandas.GeoDataFrame(self.facility_data, crs="EPSG:4326") |
| 31 | + self.k_nearest_pmedian = KNearestPMedian.from_geodataframe( |
| 32 | + gdf_demand, |
| 33 | + gdf_fac, |
| 34 | + "geometry", |
| 35 | + "geometry", |
| 36 | + "demand", |
| 37 | + p_facilities=2, |
| 38 | + facility_capacity_col="capacity", |
| 39 | + k_array=k, |
| 40 | + ) |
| 41 | + |
| 42 | + def test_knearest_p_median_from_geodataframe(self): |
| 43 | + result = self.k_nearest_pmedian.solve(pulp.PULP_CBC_CMD(msg=False)) |
| 44 | + assert isinstance(result, KNearestPMedian) |
| 45 | + |
| 46 | + def test_knearest_p_median_from_geodataframe_no_results(self): |
| 47 | + result = self.k_nearest_pmedian.solve( |
| 48 | + pulp.PULP_CBC_CMD(msg=False), results=False |
| 49 | + ) |
| 50 | + assert isinstance(result, KNearestPMedian) |
| 51 | + |
| 52 | + with pytest.raises(AttributeError): |
| 53 | + result.cli2fac |
| 54 | + with pytest.raises(AttributeError): |
| 55 | + result.fac2cli |
| 56 | + with pytest.raises(AttributeError): |
| 57 | + result.mean_dist |
| 58 | + |
| 59 | + def test_solve(self): |
| 60 | + solver = pulp.PULP_CBC_CMD(msg=False) |
| 61 | + self.k_nearest_pmedian.solve(solver) |
| 62 | + assert self.k_nearest_pmedian.problem.status == pulp.LpStatusOptimal |
| 63 | + |
| 64 | + fac2cli_known = [[1], [0], []] |
| 65 | + cli2fac_known = [[1], [0]] |
| 66 | + mean_dist_known = 0.8090169943749475 |
| 67 | + assert self.k_nearest_pmedian.fac2cli == fac2cli_known |
| 68 | + assert self.k_nearest_pmedian.cli2fac == cli2fac_known |
| 69 | + assert self.k_nearest_pmedian.mean_dist == mean_dist_known |
| 70 | + |
| 71 | + def test_error_k_array_non_numpy_array(self): |
| 72 | + gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326") |
| 73 | + gdf_fac = geopandas.GeoDataFrame(self.facility_data, crs="EPSG:4326") |
| 74 | + k = [1, 1] |
| 75 | + with pytest.raises(TypeError): |
| 76 | + KNearestPMedian.from_geodataframe( |
| 77 | + gdf_demand, |
| 78 | + gdf_fac, |
| 79 | + "geometry", |
| 80 | + "geometry", |
| 81 | + "demand", |
| 82 | + p_facilities=2, |
| 83 | + facility_capacity_col="capacity", |
| 84 | + k_array=k, |
| 85 | + ) |
| 86 | + |
| 87 | + def test_error_k_array_invalid_value(self): |
| 88 | + gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326") |
| 89 | + gdf_fac = geopandas.GeoDataFrame(self.facility_data, crs="EPSG:4326") |
| 90 | + |
| 91 | + k = numpy.array([1, 4]) |
| 92 | + with pytest.raises(ValueError): |
| 93 | + KNearestPMedian.from_geodataframe( |
| 94 | + gdf_demand, |
| 95 | + gdf_fac, |
| 96 | + "geometry", |
| 97 | + "geometry", |
| 98 | + "demand", |
| 99 | + p_facilities=2, |
| 100 | + facility_capacity_col="capacity", |
| 101 | + k_array=k, |
| 102 | + ) |
| 103 | + |
| 104 | + def test_error_geodataframe_crs_mismatch(self): |
| 105 | + gdf_demand = geopandas.GeoDataFrame(self.demand_data, crs="EPSG:4326") |
| 106 | + gdf_fac = geopandas.GeoDataFrame( |
| 107 | + self.facility_data, crs="EPSG:3857" |
| 108 | + ) # Different CRS |
| 109 | + |
| 110 | + k = numpy.array([1, 1]) |
| 111 | + with pytest.raises(ValueError): |
| 112 | + KNearestPMedian.from_geodataframe( |
| 113 | + gdf_demand, |
| 114 | + gdf_fac, |
| 115 | + "geometry", |
| 116 | + "geometry", |
| 117 | + "demand", |
| 118 | + p_facilities=2, |
| 119 | + facility_capacity_col="capacity", |
| 120 | + k_array=k, |
| 121 | + ) |
0 commit comments