def test_lscp(self):
     merged_dict = covering.merge_coverages([self.binary_coverage_point, self.binary_coverage_point2])
     merged_dict = covering.update_serviceable_demand(merged_dict, self.serviceable_demand_point)
     lscp = covering.create_lscp_model(merged_dict)
     lscp_i = covering.create_lscp_model(self.binary_coverage_point)
     lscp.solve(pulp.GUROBI())
     lscp_i.solve(pulp.GUROBI())
     ids = utilities.get_ids(lscp, "facility_service_areas")
     ids2 = utilities.get_ids(lscp, "facility2_service_areas")
     self.assertEqual(['1', '3', '4', '5', '6', '7'], ids)
     self.assertEqual(
         ['0', '1', '11', '12', '13', '14', '15', '16', '17', '19', '2', '20', '22', '4', '5', '6', '7', '9'], ids2)
     self.assertEqual(lscp_i.status, pulp.constants.LpStatusInfeasible)
 def test_lscp(self):
     merged_dict = covering.merge_coverages(
         [self.binary_coverage_point, self.binary_coverage_point2])
     merged_dict = covering.update_serviceable_demand(
         merged_dict, self.serviceable_demand_point)
     lscp = covering.create_lscp_model(merged_dict)
     lscp_i = covering.create_lscp_model(self.binary_coverage_point2)
     lscp.solve(pulp.GLPK())
     lscp_i.solve(pulp.GLPK())
     ids = utilities.get_ids(lscp, "facility_service_areas")
     ids2 = utilities.get_ids(lscp, "facility2_service_areas")
     self.assertEqual(['3', '4', '5', '6', '7'], ids)
     self.assertEqual([
         '0', '1', '11', '12', '13', '14', '15', '16', '17', '18', '19',
         '2', '20', '21', '22', '4', '5', '6', '9'
     ], ids2)
     self.assertEqual(lscp_i.status, pulp.constants.LpStatusInfeasible)
 def test_lscp(self):
     merged_dict = covering.merge_coverages(
         [self.binary_coverage_point, self.binary_coverage_point2])
     merged_dict = covering.update_serviceable_demand(
         merged_dict, self.serviceable_demand_point)
     lscp = covering.create_lscp_model(merged_dict, "lscp.lp")
     lscp.solve(pulp.GUROBI())
     ids = utilities.get_ids(lscp, "facility_service_areas")
     ids2 = utilities.get_ids(lscp, "facility2_service_areas")
     self.assertEqual(['1', '3', '4', '5', '6', '7'], ids)
     self.assertEqual([
         '0', '1', '11', '12', '13', '14', '15', '16', '17', '19', '2',
         '20', '22', '4', '5', '6', '7', '9'
     ], ids2)
def lscp_solver_coverage_dict(dict_coverage,
                              env_path,
                              demand_point,
                              facility_service_area,
                              attr_demand,
                              id_demand_point,
                              id_facility,
                              id_facility_as_string=True):
    """
    Solve a LSCP using the given inputs and parameters
    :param dict_coverage: (dictionary) Dictionay of coverage
    :param env_path: (string) Path of the env
    :param demand_point: (string) File name of the demand point layer
    :param facility_service_area: (string) File name of the facility service areas
    :param attr_demand: (string) Field name of demand weight. Not used now
    :param id_facility_as_string: (boolean) whether the ID attribute of facilities is string
    :return: (A dict of objects) [demand_coverage, n_facility, list_id_facility]
    """
    # demand layer
    demand_polygon_fl = arcpy.MakeFeatureLayer_management(
        os.path.join(env_path, demand_point)).getOutput(0)
    # service layer
    facility_service_areas_fl = arcpy.MakeFeatureLayer_management(
        os.path.join(env_path, facility_service_area)).getOutput(0)
    # Create binary coverage (polygon) dictionary structure
    # Use "demand" of each polygon as demand,
    # Use "id" as the unique field
    # Use "object_id" as the unique id for the facilities

    binary_coverage_polygon = dict_coverage

    # Create the mclp model
    # Maximize the total coverage (binary polygon) using at most 5 out of 8 facilities
    logger.info("Creating LSCP model...")

    lscp = covering.create_lscp_model(binary_coverage_polygon)
    # Solve the model using GLPK
    # logger.info("Solving MCLP...")
    lscp.solve(pulp.GLPK())

    # get rid of the file postfix: .shp
    # example: york_facility_sample_buffer_100
    facility_layer_name = os.path.splitext(facility_service_area)[0]
    # print(facility_layer_name)
    ids = utilities.get_ids(lscp, facility_layer_name)
    # print "List of IDs: ", ids

    # select_query = arcpy_analysis.generate_query(ids, unique_field_name=id_facility, wrap_values_in_quotes=id_facility_as_string)
    # # logger.info("Output query to use to generate maps is: {}".format(select_query))
    # # Determine how much demand is covered by the results
    # facility_service_areas_fl.definitionQuery = select_query
    # total_coverage = arcpy_analysis.get_covered_demand(demand_polygon_fl, attr_demand, "binary",
    #                                                    facility_service_areas_fl)

    # # logger.info("{0:.2f}% of demand is covered".format((100 * total_coverage) / binary_coverage_polygon["totalDemand"]))
    result = {}
    print "Total demand is: ", binary_coverage_polygon["totalDemand"]

    # result["demand_coverage"] = (total_coverage) / binary_coverage_polygon["totalDemand"]

    result["demand_coverage"] = 1.0
    # number of facilities used
    result["n_facility"] = len(ids)

    result["list_id_facility"] = " ".join(str(x) for x in ids)

    return result
Example #5
0
    # Use GEOID as the unique field
    # Ue ORIG_ID as the unique id for the facilities
    binary_coverage_point1 = arcpy_analysis.generate_binary_coverage(
        demand_point_fl, facility_service_areas_fl, "Population", "GEOID10",
        "ORIG_ID")
    binary_coverage_point2 = arcpy_analysis.generate_binary_coverage(
        demand_point_fl, facility2_service_areas_fl, "Population", "GEOID10",
        "ORIG_ID")
    # Merge the binary coverages together, serviceable area is auto-updated for binary coverage
    total_binary_coverage = covering.merge_coverages(
        [binary_coverage_point1, binary_coverage_point2])

    # Create the mclp model
    # Maximize the total coverage (binary polygon) using at most 5 out of 8 facilities
    logger.info("Creating MCLP model...")
    lscp = covering.create_lscp_model(total_binary_coverage, "lscp.lp")
    # Solve the model using GLPK
    logger.info("Solving LSCP...")
    lscp.solve(pulp.GLPK())
    # Get the unique ids of the facilities chosen
    logger.info("Extracting results")
    ids = utilities.get_ids(lscp, "facility_service_areas")
    ids2 = utilities.get_ids(lscp, "facility2_service_areas")
    # Generate a query that could be used as a definition query or selection in arcpy
    select_query = arcpy_analysis.generate_query(ids,
                                                 unique_field_name="ORIG_ID")
    # Generate a second query for the other layer
    select_query2 = arcpy_analysis.generate_query(ids2,
                                                  unique_field_name="ORIG_ID")
    logger.info(
        "Output query to use to generate maps is: {}".format(select_query))
    # we need to use both sets of facilities
    # Use population of each polygon as demand,
    # Use GEOID as the unique field
    # Ue ORIG_ID as the unique id for the facilities
    binary_coverage_point1 = arcpy_analysis.generate_binary_coverage(demand_point_fl, facility_service_areas_fl,
                                                                     "Population", "GEOID10", "ORIG_ID")
    binary_coverage_point2 = arcpy_analysis.generate_binary_coverage(demand_point_fl, facility2_service_areas_fl,
                                                                     "Population",
                                                                     "GEOID10", "ORIG_ID")
    # Merge the binary coverages together, serviceable area is auto-updated for binary coverage
    total_binary_coverage = covering.merge_coverages([binary_coverage_point1, binary_coverage_point2])

    # Create the mclp model
    # Maximize the total coverage (binary polygon) using at most 5 out of 8 facilities
    logger.info("Creating MCLP model...")
    lscp = covering.create_lscp_model(total_binary_coverage)
    # Solve the model using GLPK
    logger.info("Solving LSCP...")
    lscp.solve(pulp.GLPK())
    # Get the unique ids of the facilities chosen
    logger.info("Extracting results")
    ids = utilities.get_ids(lscp, "facility_service_areas")
    ids2 = utilities.get_ids(lscp, "facility2_service_areas")
    # Generate a query that could be used as a definition query or selection in arcpy
    select_query = arcpy_analysis.generate_query(ids, unique_field_name="ORIG_ID")
    # Generate a second query for the other layer
    select_query2 = arcpy_analysis.generate_query(ids2, unique_field_name="ORIG_ID")
    logger.info("Output query to use to generate maps is: {}".format(select_query))
    logger.info("Output query to use to generate maps is: {}".format(select_query2))
    # Determine how much demand is covered by the results
    facility_service_areas_fl.definitionQuery = select_query