VehicleRoutingProblem

概要

An ArcPy class for performing a vehicle routing problem analysis.

ディスカッション

A vehicle routing problem analysis allows you to calculate the best routes for a fleet of vehicles.

構文

VehicleRoutingProblem (in_network)
パラメーター説明データ タイプ
in_network

The network dataset or service that will be used for the network analysis. The argument can be specified using the catalog path to the network dataset, a network dataset layer object, the string name of the network dataset layer, or a portal URL for a network analysis service. The network must have at least one travel mode.

To use a portal URL, you must be signed in to the portal with an account that has routing privileges.

Solving an analysis will consume credits when the object uses ArcGIS Online as the in_network. For more information, see Service Credits Overview

String

プロパティ

プロパティ説明データ タイプ
allowSaveLayerFile
(読み書き)

Specifies whether to permit saving the analysis result as a layer file using the saveAsLayerFile method on the result object. A value of True means that you can save a layer file. A value of False means that you cannot. The default is True if the analysis references a network dataset and False if it references a portal service. Allowing a layer file to be saved may slow down the analysis when the analysis references a portal service.

Boolean
allowSaveRouteData
(読み書き)

Specifies whether to permit saving the analysis result to a ZIP file using the saveRouteData method on the result object. A value of True means that you can save the route data. A value of False means that you cannot. The default is True if the analysis references a network dataset and False if it references a portal service. Allowing the route data to be saved may slow down the analysis when the analysis references a portal service.

Boolean
defaultDate
(読み書き)

The default date for time field values that specify a time of day without including a date. The default is None, which indicates that all date time fields include date and time values.

DateTime
directionsDistanceUnits
(読み書き)

The units to use when reporting travel distance in the output turn-by-turn directions. The property is returned and set as a member of the DistanceUnits enumeration and is applicable only when the returnDirections property is True. The default is the units of the distance attribute in the travel mode used for the analysis.

Object
directionsLanguage
(読み書き)

The language in which the output turn-by-turn directions text will appear. The property is returned and set as a string using one of the two- or five-character language codes representing supported directions languages. The list of available directions languages can be obtained from the arcpy.nax.ListDirectionsLanguages function. The default value is either en (English) or the language of the currently activated language pack. The property is applicable only when the returnDirections property is True.

String
directionsStyle
(読み書き)

The style to be used for the output turn-by-turn directions text. The property is returned and set as a member of the DirectionsStyle enumeration and is applicable only when the returnDirections property is True.

The default value is Desktop.

Object
distanceUnits
(読み書き)

The units to use when reporting the travel distance in the analysis output. Regardless of the units of the cost attributes in the network dataset, the output will be transformed to and reported in the units set in this property. The property is returned and set as a member of the DistanceUnits enumeration. The default is DistanceUnits.Kilometers.

Object
excessTransitFactor
(読み書き)

Rates the importance of reducing excess transit time of order pairs. Excess transit time is the amount of time exceeding the time required to travel directly between the paired orders. Excess time can be caused by driver breaks or travel to intermediate orders and depots.

The property is returned and set as a member of the Importance enumeration. The default is Importance.Medium.

When set to Importance.Low, the solver tries to find a solution that minimizes overall solution cost, regardless of excess transit time. This setting is commonly used with courier services. Since couriers transport packages as opposed to people, they don't need to worry about ride time. Using this setting allows the couriers to service paired orders in the proper sequence and minimize the overall solution cost.

When set to Importance.Medium, the solver looks for a balance between reducing excess transit time and reducing the overall solution cost.

When set to Importance.High, the solver tries to find a solution with the least excess transit time between paired orders at the expense of increasing the overall travel costs. It makes sense to use this setting if you are transporting people between paired orders and you want to shorten their ride time. This is characteristic of taxi services.

Object
ignoreInvalidLocations
(読み書き)

Specifies whether invalid input locations will be ignored. A value of True indicates that invalid input locations will be ignored so that the analysis will succeed using only valid locations. A value of False indicates that invalid locations will not be ignored and will cause the analysis to fail.

Boolean
networkDataSource
(読み取り専用)

The full catalog path to the network dataset used for the analysis.

String
overrides
(読み書き)

Specify additional settings that can influence the behavior of the solver when finding solutions for the network analysis problems.

The value for this parameter must be specified in JavaScript Object Notation (JSON). For example, a valid value is of the following form: {"overrideSetting1" : "value1", "overrideSetting2" : "value2"}. The override setting name is always enclosed in double quotation marks. The values can be either a number, Boolean, or string.

The default value for this parameter is no value, which indicates to not override any solver settings.

Overrides are advanced settings that should be used only after careful analysis of the results obtained before and after applying the settings. A list of supported override settings for each solver and their acceptable values can be obtained by contacting Esri Technical Support.

String
returnDirections
(読み書き)

Specifies whether turn-by-turn directions will be generated. A value of True means that turn-by-turn directions will be generated when solving the analysis. A value of False means that directions will not be generated. Generating directions may slow down the analysis. The default is False.

Boolean
returnStopShapes
(読み書き)

When set to True, the solver generates point shapes for the output stops. When set to False, the output stops are returned in table format. The default is False.

Boolean
routeShapeType
(読み書き)

The type of shape to be generated to represent output routes. The routes are always calculated along the network; however, you can choose to represent them by shapes that do not reflect the network paths. The property is returned and set as a member of the RouteShapeType enumeration. The default is RouteShapeType.TrueShapeWithMeasures.

Object
searchQuery
(読み書き)

When locating inputs on the network, specifies a query to restrict the search to a subset of the features within a source feature class. This is useful if you don't want to find features that may be unsuited for a network location. For example, if you are loading centroids of polygons and don't want to locate on local roads, you can define a query that searches for major roads only.

Learn more about locating inputs on the network

The parameter value is specified as a list with nested lists. The nested list is composed of two values indicating the name and the SQL expression for all of the network sources. The syntax for the SQL expression differs slightly depending on the type of the network source feature class. For example, if you're querying source feature classes stored in file or enterprise geodatabases, shapefiles, or SDC, enclose field names in double quotation marks: "CFCC". If you're querying source feature classes stored in personal geodatabases, enclose fields in square brackets: [CFCC].

If you don't want to specify a query for a particular source, use "#" as the value for the SQL expression or exclude the source name and the SQL expression from the parameter value. If you don't want to specify a query for all of the network sources, use "#" as the parameter value.

For example, the parameter value [["Streets","\"CFCC\" = 'A15'"], ["Streets_ND_Junctions",""]] specifies an SQL expression for the Streets source feature class and no expression for the Streets_ND_Junctions source feature class. Note that the double quotation marks used to enclose the field name CFCC are escaped using backslash characters to avoid a parsing error from the Python interpreter.

By default, no query is used.

List
searchTolerance
(読み書き)

The maximum search distance to use when locating the input features on the network. The property is returned and set as a double, and the units of this value are accessed through the searchToleranceUnits property. The default is 5000.

Learn more about locating inputs on the network

Double
searchToleranceUnits
(読み書き)

The units of the searchTolerance property. The property is returned and set as a member of the DistanceUnits enumeration. The default is DistanceUnits.Meters.

Object
spatiallyClusterRoutes
(読み書き)

When set to True, the orders assigned to a route will be spatially clustered. Clustering orders tends to keep routes in smaller areas and reduce how often route lines intersect one another; yet, clustering can increase overall travel times. When set to False, the solver will not prioritize spatially clustering orders, and the route lines might intersect. Use this option if route zones are specified.

The default is True.

Boolean
timeUnits
(読み書き)

The units to use when reporting the travel time in the analysis output. Regardless of the units of the cost attributes in the network dataset, the outputs will be transformed to and reported in the units set in this property. The property is returned and set as a member of the TimeUnits enumeration. The default is TimeUnits.Minutes.

Object
timeWindowFactor
(読み書き)

Rates the importance of honoring time windows.

The property is returned and set as a member of the Importance enumeration. The default is Importance.Medium.

When set to Importance.Low, the solver places more importance on minimizing drive times and less on arriving at stops on time. You may want to use this setting if you have a growing backlog of service requests. For the purpose of servicing more orders in a day and reducing the backlog, you can choose this setting even though customers may be inconvenienced with your late arrivals.

When set to Importance.Medium, the solver balances the importance of minimizing drive times and arriving within time windows.

When set to Importance.High, the solver places more importance on arriving at stops on time than on minimizing drive times. Organizations that make time-critical deliveries or that are very concerned with customer service would choose this setting.

Object
timeZoneForTimeFields
(読み書き)

Indicates whether the time fields in the input data should be interpreted as local time at the input locations or as coordinated universal time (UTC). The property is returned and set as a member of the TimeZoneUsage enumeration and is applicable only when the timeOfDay property is not None. The default is TimeZoneUsage.LocalTimeAtLocations.

Object
travelMode
(読み書き)

The travel mode to use for the analysis.

VRP only solves with a time-based impedance, so only time-based impedance travel modes are available.

The value is returned and set as an arcpy.nax.TravelMode object, but it can also be set using the string name of the travel mode or a string containing the valid JSON representation of a travel mode. The default is the default travel mode defined on the network dataset used for the analysis.

Learn more about travel modes

Object

手法の概要

手法説明
count (input_type)

Returns the number of rows added for an input type.

fieldMappings (input_type, {use_location_fields}, {list_candidate_fields})

Generates a dictionary that maps the field names of the input type to arcpy.nax.NAClassFieldMap objects that allow you to map fields from your input data to the properties of the solver. The dictionary can be used as input to the load method's field_mappings argument.

fieldNames (input_type, {use_location_fields})

Get a list of field names supported by the specified input type.

insertCursor (input_type, field_names)

Establishes a write cursor on the specified input type. This cursor can be used to add rows directly to the input.

load (input_type, features, {field_mappings}, {append}, {max_features})

Set input features to use for the analysis.

solve ()

Perform the vehicle routing problem analysis using the properties set on the VehicleRoutingProblem object and the loaded inputs.

手法

count (input_type)
パラメーター説明データ タイプ
input_type

The type of input features to count.

The parameter should be set using the VehicleRoutingProblemInputDataType enumeration.

Object
戻り値
データ タイプ説明
Integer

The number of rows.

fieldMappings (input_type, {use_location_fields}, {list_candidate_fields})
パラメーター説明データ タイプ
input_type

The type of input for which the field mappings are returned.

The parameter should be set using the VehicleRoutingProblemInputDataType enumeration.

Object
use_location_fields

Indicates whether network location fields should be included in the returned field mappings dictionary. Network location fields describe the point on the network where an object is located. You can use network location fields to more precisely control how your analysis inputs locate on the network and to save time when calling the solve method because the solver will not have to calculate the location fields from the geometry of the inputs. You can calculate location fields for a feature class using the Calculate Locations tool.

Learn more about network location fields and how inputs are located on the network

When this argument is set to True, the returned field mappings dictionary will contain network location fields. The default is False; the field mapping dictionary will not include network location fields.

(デフォルト値は次のとおりです False)

Boolean
list_candidate_fields
[list_candidate_fields,...]

A list of arcpy.Field objects used to generate the mapped field names, which can be obtained from a given feature class or table using the arcpy.ListFields function. Field names not already matching a field for the input type will also be returned in the map. Calling the load method with these extra fields will add them to the solver's input features so the fields can be passed through and included in the output. If this argument is not specified, the field mappings will be created with only the default values for the appropriate properties.

(デフォルト値は次のとおりです None)

Field
戻り値
データ タイプ説明
Dictionary

A dictionary in which the keys are the field name and values are arcpy.nax.NAClassFieldMap objects.

fieldNames (input_type, {use_location_fields})
パラメーター説明データ タイプ
input_type

The type of input for which the supported field names are returned.

The parameter should be set using the VehicleRoutingProblemInputDataType enumeration.

Object
use_location_fields

Indicates whether network location fields will be included in the returned list of field names. Network location fields describe the point on the network where an object is located. You can use network location fields to more precisely control how your analysis inputs locate on the network and to save time when calling the solve method because the solver will not have to calculate the location fields from the geometry of the inputs. You can calculate location fields for a feature class using the Calculate Locations tool.

Learn more about network location fields and how inputs are located on the network

When this argument is set to True, the returned list of field names will contain network location fields. The default is False; the list of field names will not include network location fields.

(デフォルト値は次のとおりです False)

Boolean
戻り値
データ タイプ説明
String

A list of field names supported by the specified input type.

insertCursor (input_type, field_names)
パラメーター説明データ タイプ
input_type

The type of input into which the cursor can be used to insert rows.

The parameter should be set using the VehicleRoutingProblemInputDataType enumeration.

Object
field_names
[field_names,...]

A list of field names of the input type whose values you want to set when inserting rows using the cursor. You can get the names of the fields supported by an input type by using the fieldNames method. In addition to regular fields, you can also set the geometry of the input using one of the following geometry tokens:

  • SHAPE@XYA tuple of the feature's centroid x,y coordinates.
  • SHAPE@XYZ
  • SHAPE@JSON The esri JSON string representing the geometry.
  • SHAPE@WKBThe well-known binary (WKB) representation for OGC geometry. It provides a portable representation of a geometry value as a contiguous stream of bytes.
  • SHAPE@WKTThe well-known text (WKT) representation for OGC geometry. It provides a portable representation of a geometry value as a text string.
  • SHAPE@A geometry object for the feature.

The SHAPE@XY and SHAPE@XYZ tokens are only supported for point-based input types.

String
戻り値
データ タイプ説明
Object

A SolverInsertCursor object that can be used to write features.

load (input_type, features, {field_mappings}, {append}, {max_features})
パラメーター説明データ タイプ
input_type

The type of input feature to load.

The parameter should be set using the VehicleRoutingProblemInputDataType enumeration.

Object
features

The input features to load. This parameter accepts the following input types:

For layer inputs, only selected features will be loaded. If a layer has a definition query, only the subset of features visible with the definition query will be loaded. The method also honors the Extent geoprocessing environment; only features in the specified extent will be loaded.

String
field_mappings

A dictionary that maps the field names of the input type to arcpy.nax.NAClassFieldMap objects representing the mapping of fields from the input features. Valid input for this parameter can be constructed using the fieldMappings method.

If field mappings are not specified, all fields from the input features that have the same name as the supported fields for the input type will be mapped.

(デフォルト値は次のとおりです None)

Dictionary
append

Indicates whether the features being loaded should be appended to the existing set of features for the input type. A value of True indicates that the new features should be appended; the existing features will be preserved. This is useful if you want to load inputs from multiple feature classes or tables to use in a single analysis. This is the default. A value of False indicates that any existing features for the input type should be deleted and replaced with the features currently being loaded.

(デフォルト値は次のとおりです True)

Boolean
max_features

The maximum number of features that can be loaded into the input type. This is useful if you are creating a tool or service and want an error returned if the size of the input exceeds the available resources. The load method will return an arcpy.nax.LimitError if the number of input features exceeds the max_features limit.

If a value is not specified, no limit is enforced for the count of the input features.

(デフォルト値は次のとおりです None)

Integer
solve ()
戻り値
データ タイプ説明
Object

An arcpy.nax.VehicleRoutingProblemResult object that can be used to access outputs and solver messages.

コードのサンプル

VehicleRoutingProblem example

Perform vehicle routing problem analysis.

# An example showing how to perform vehicle routing problem analysis using inputs from feature classes and tables.
import arcpy
arcpy.CheckOutExtension("network")

nds = "C:/data/NorthAmerica.gdb/Routing/Routing_ND"
nd_layer_name = "Routing_ND"
input_orders = "C:/data/io.gdb/Orders"
input_depots = "C:/data/io.gdb/Depots"
input_routes = "C:/data/io.gdb/Vehicles"
output_stops = "C:/data/io.gdb/AssignedStops"
output_routes = "C:/data/io.gdb/Routes"
output_directions = "C:/data/io.gdb/Directions"
unassigned_stops = "C:/data/io.gdb/UnassignedStops"

# Create a network dataset layer and get the desired travel mode for analysis
arcpy.nax.MakeNetworkDatasetLayer(nds, nd_layer_name)
nd_travel_modes = arcpy.nax.GetTravelModes(nd_layer_name)
travel_mode = nd_travel_modes["Driving Time"]

# Instantiate a VehicleRoutingProblem solver object
vrp = arcpy.nax.VehicleRoutingProblem(nds)
# Set properties
vrp.travelMode = travel_mode
vrp.distanceUnits = arcpy.nax.DistanceUnits.Miles
vrp.routeShapeType = arcpy.nax.RouteShapeType.TrueShape
vrp.returnDirections = True
vrp.returnStopShapes = True
# Load inputs
vrp.load(arcpy.nax.VehicleRoutingProblemInputDataType.Orders, input_orders)
vrp.load(arcpy.nax.VehicleRoutingProblemInputDataType.Depots, input_depots)
vrp.load(arcpy.nax.VehicleRoutingProblemInputDataType.Routes, input_routes)
# Solve the analysis
result = vrp.solve()

# Export the results to feature classes
if result.solveSucceeded:
    result.export(arcpy.nax.VehicleRoutingProblemOutputDataType.Stops, output_stops)
    result.export(arcpy.nax.VehicleRoutingProblemOutputDataType.Routes, output_routes)
    result.export(arcpy.nax.VehicleRoutingProblemOutputDataType.Directions, output_directions)
    if result.isPartialSolution:
        print("Some of the orders were not assigned.")
        result.export(arcpy.nax.VehicleRoutingProblemOutputDataType.UnassignedStops, unassigned_stops)
else:
    print("Solved failed")
    print(result.solverMessages(arcpy.nax.MessageSeverity.All))