def test_load_and_trim_time_matrix_rows(sectors):
    '''
    Test trimmed time matrix rows are as expected
    '''
    matrix = io.load_travel_time()
    trimmed = io.trim_matrix(matrix, sectors)
    assert sectors == list(trimmed.index.values)
def test_load_and_trim_time_matrix_cols(sectors):
    '''
    Test trimmed time matrix columns are as expected
    '''
    matrix = io.load_travel_time()
    trimmed = io.trim_matrix(matrix, sectors)
    assert sectors == list(trimmed.columns)
def test_load_and_trim_time_matrix(sectors, expected_shape):
    '''
    Test trimmed time matrix shape is as expected
    '''
    matrix = io.load_travel_time()
    trimmed = io.trim_matrix(matrix, sectors)
    assert expected_shape == trimmed.shape
Example #4
0
def test_capacity_one_cost(warehouse, selected_sectors, expected_cost):
    '''
    Test single occupancy cost (warehouse->city_i, city_i<-warehouse)
    '''
    matrix = io.trim_matrix(io.load_travel_distance(), selected_sectors)
    cost = cn.single_capacity_cost(warehouse, matrix)
    #allowing for minor floating point difference
    assert pytest.approx(cost) == expected_cost
    def single_replication(self):
        '''
        runs a single replication of the model.

        sample covid positive patients that need transport
        constructs routes to transport each patient to the hospital 
        for different capacities.

        '''
        #step 1: sample n patients that need transport
        patients = self._sample_patients(self._params.n_patients)

        #step 2: sample which patients need transport
        transport_patients = self._sample_transport(self._params.p_transport,
                                                    self._params.n_patients)

        #step 3: sample covid-19 positive patients
        covid_patients = self._sample_covid(self._params.p_positive,
                                            self._params.n_patients)

        #step 4: covid_positive patients that need transport
        covid_patient_mask = transport_patients * covid_patients
        covid_patient_sectors = patients[covid_patient_mask.astype(bool)]

        #step 5: unit demand (simplification at this point)
        demand = demand_by_sector(self._params.warehouse,
                                  covid_patient_sectors)

        #step 6: trim the matrix to help with debug and efficiency
        unique_sectors = list(demand.keys())
        if self._params.warehouse not in unique_sectors:
            unique_sectors.append(self._params.warehouse)

        #6.1 create smaller matrix using unique sectors.
        sub_matrix = io.trim_matrix(self._params.cost_matrix, unique_sectors)

        #6.2 clone areas with multiple demands
        #This because sector demand may exceed vehicle_capacity.
        #This workaround allows multiple trips to same postcode sector
        demand, sub_matrix = clone_sectors_with_multiple_demands(
            demand, sub_matrix, intra_sector_travel_cost=5.0)

        #6.3 sort columns and index (incase enforcing symmetry)
        sub_matrix = sort_cost_matrix_lexographically(sub_matrix)

        #step 7: for each capacity, create routes and record total cost.
        costs = {}
        costs['capacity_1'] = c.single_capacity_cost(self._params.warehouse,
                                                     sub_matrix)
        for capacity in self._params.vehicle_capacities:
            costs[f'capacity_{capacity}'] = []
            journeys = self._solver.solve(vehicle_capacity=capacity,
                                          matrix=sub_matrix,
                                          demand=demand)

            costs[f'capacity_{capacity}'] = self.total_routing_cost(
                journeys, sub_matrix)
        return costs
def test_travel_distance_cost(city_1, city_2, expected_cost):
    '''
    Test costs are symmetric.
    '''
    df = io.load_travel_distance()
    sectors = ['L1', 'L10', 'L100','L101', 'L102']
    trimmed = io.trim_matrix(df, sectors)
    print(trimmed)
    cost = io.travel_cost(city_1, city_2, trimmed)
    assert expected_cost == cost