Network Analyst VRP not working

4560
6
Jump to solution
01-16-2015 11:46 AM
Highlighted
New Contributor III

Right now I am just trying to get a simple route created using VRP.  Eventually, I have 180 files or "orders" that I would like to run routes for too, but right now I just want my code to work for one route.  Below is my code.  When I try to run it, I get an error at  line 44: class 'Queue.Empty'.  Any help on this is greatly appreciated as I am pretty new to both python and network analyst.  Also, any input on repeating the VRP by looping through and changing the input order & depot files is greatly appreciated.

# -*- coding: utf-8 -*-

# ---------------------------------------------------------------------------

# RouteCreation.py

# Created on: 2015-01-06

# Created by: L.M. Blackburn and ArcGIS/ModelBuilder

# ArcGIS 10.1

# Usage: RouteCreation for all grid center layers

# Description:

# solves routing problem for all point layers in a folder

# ---------------------------------------------------------------------------

# Import arcpy module

import arcpy, os, sys, traceback

from arcpy import env

# Check out any necessary licenses

arcpy.CheckOutExtension("Network")

# Set environment settings

env.workspace = 'F:\\Workspace\\Sandy\\GM_costAnalysis\\analysis2\\'

env.overwriteOutput = True

# Set Local variables:

inNetworkDataset = "F:\\Workspace\\Sandy\\GM_costAnalysis\\streets" # it seems that this value is empty

outNALayerName = "TrapRoute_MO1_6K" # may want this name to change with each run thru

impedanceAttribue = "Time"

distanceAttribute = "Length"

timeUnits = "Minutes"

distanceUnits = "Miles"

inOrder = "Grids/Block_MO1_6000_label.shp" #This value will change with each run - may want to define this via a loop

inDepots = "Depots/Depot_MO1.shp" # This value will change with each run - may want to define this via a loop

inRoutes = "RoutesTable"

outLayerFile = "F:/Workspace/Sandy/GM_costAnalysis/analysis2/Routes/" + outNALayerName + ".lyr" # may want to change w/ each run

searchTolerance = "4243 Meters" # This value will change based on the inOrder number of features

try:

    #Test out paths and layer locations

    print 'Starting the VRP'

    # orderList = arcpy.ListWorkspaces("analysis2", "Folder")

    # print ("network: {}".format(inNetworkDataset))

    # print outNALayerName

   

    # Process: Make Vehicle Routing Problem Layer

    outNALayer = arcpy.MakeVehicleRoutingProblemLayer_na(inNetworkDataset, outNALayerName, impedanceAttribute, distanceAttribute, timeUnits, distanceUnits, "USE_HIERARCHY", "TRUE_LINES_WITH_MEASURES")

    # outNALayer = arcpy.MakeVehicleRoutingProblemLayer_na(str(inNetworkDataset), outNALayerName, impedanceAttribute, distanceAttribute, timeUnits, distanceUnits, "", "", "Medium", "Medium", "ALLOW_UTURNS", "'4WD Road';'Pedestrian Walkway';'Pedestrian Ferry';TurnRestriction;OneWay", "USE_HIERARCHY", "", "TRUE_LINES_WITH_MEASURES")

    # Get the layer object from the result object to reference the VRP layer

    outNALayer = outNALayer.getOutput(0)

    # Get the names of all the sublayers within the VRP layer

    subLayerNames = arcpy.na.GetNAClassNames(outNALayer)

    #Stores the layer names that we'll use later

    ordersLayerName = subLayerNames["Orders"]

    depotsLayerName = subLayerNames["Depots"]

    routesLayerName = subLayerNames["Routes"]

    # Process: Add Trap Locations - Order - will want to alter the search_tolerance to match the

    arcpy.AddLocations_na(outNALayer, ordersLayerName, inOrders, "Name FID #", searchTolerance, "", "'SDC Edge Source' SHAPE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "'SDC Edge Source' #")

    # Process: Add Locations - Depot

    arcpy.AddLocations_na(outNALayer, depotsLayerName, inDepots, "Name Name #", searchTolerance, "", "'SDC Edge Source' SHAPE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "'SDC Edge Source' #")

   

    # Process: Add Locations - Route

    arcpy.AddLocations_na(outNALayer, routesLayerName, inRoutes, "Name Name #;Description Description #;StartDepotName StartDepotName #;EndDepotName EndDepotName #;StartDepotServiceTime StartDepotServiceTime #;EndDepotServiceTime EndDepotServiceTime #;EarliestStartTime EarliestStartTime '8:00:00 AM';LatestStartTime LatestStartTime '10:00:00 AM';ArriveDepartDelay ArriveDepartDelay #;Capacities Capacities #;FixedCost FixedCost #;CostPerUnitTime CostPerUnitTime 1;CostPerUnitDistance CostPerUnitDistance 1;OvertimeStartTime OvertimeStartTime #;CostPerUnitOvertime CostPerUnitOvertime #;MaxOrderCount MaxOrderCount 30;MaxTotalTime MaxTotalTime #;MaxTotalTravelTime MaxTotalTravelTime #;MaxTotalDistance MaxTotalDistance #;SpecialtyNames SpecialtyNames #;AssignmentRule AssignmentRule 1", searchTolerance, "", "'SDC Edge Source' SHAPE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "'SDC Edge Source' #")

    # Process: Solve

    arcpy.Solve_na(outNALayer, "HALT", "TERMINATE", "")

   

    print 'VRP complete'

except:

    print 'Program failed.'   

   

    tb = sys.exc_info()[2]

    tbinfo = traceback.format_tb(tb)[0]

    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " +        str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"

    msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

    arcpy.AddError(msgs)

    arcpy.AddError(pymsg)

    print msgs

    print pymsg

   

    arcpy.AddMessage(arcpy.GetMessages(1))

    print arcpy.GetMessages(1)

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Regular Contributor

Everything in your script up to that point looks fine to me...I'm kind of at a loss for what the problem might be.  It might be time to contact Esri Support and get them to take a look.

View solution in original post

Reply
0 Kudos
6 Replies
Highlighted
Esri Regular Contributor

Hi.  Can you post the exact error message you're getting?  Was that class Queue.empty thing all you got?

Also, is this actually a path to a network dataset?

"F:\\Workspace\\Sandy\\GM_costAnalysis\\streets"

Can you add it to ArcMap and create a Route or Service Area with it there?  If not, then your script certainly won't work.

As far as looping goes, that shouldn't be too difficult.  If you want to use all the same settings and stuff, you can just create a list of your input orders and depots and loop through it.  Each time you run Add Locations, take the correct ones from your list.  Make sure you set Add Locations to "CLEAR" instead of "APPEND" so that the existing orders and routes are cleared out and replaced with the new ones, rather than the new ones being added to the existing ones.  Then, make sure you're doing whatever you need to do with your solved output before the next iteration of the loop, because it will be overwritten.

Reply
0 Kudos
Highlighted
New Contributor III

Hi Melinda,

Here is the error that I am getting.  Yes, that is the path to a network dataset.  I suppose I could add it to ArcMap, but I was hoping to run this process in IDLE, outside of ArcMap.  I guess I just wasn't sure how to reference a layer in a map document when I am not running the code from within that environment.  Any help you can offer is greatly appreciated.

ARCPY ERRORS:

PYTHON ERRORS:

Traceback Info:

  File "F:\Workspace\Sandy\GM_costAnalysis\scripts\RouteCreation.py", line 44, in <module>

    outNALayer = arcpy.MakeVehicleRoutingProblemLayer_na(inNetworkDataset, outNALayerName, impedanceAttribute, distanceAttribute, timeUnits, distanceUnits, "USE_HIERARCHY", "TRUE_LINES_WITH_MEASURES")

Error Info:

     <class 'Queue.Empty'>:

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Sorry, I was unclear.  What I meant is, have you tried adding the network dataset to ArcMap just to play around with it to make sure it works at all?  Just try running a few manual tests in ArcMap.  If you can't get it to work manually within ArcMap, then you certainly won't be able to get it to work in your script.

Regardless, I think I see where the problem is in your MakeVehicleRoutingProblem statement:

arcpy.MakeVehicleRoutingProblemLayer_na(inNetworkDataset, outNALayerName, impedanceAttribute, distanceAttribute, timeUnits, distanceUnits, "USE_HIERARCHY", "TRUE_LINES_WITH_MEASURES")

You have to have the input parameters in the correct order and placement.  Everything is fine up until "USE_HIERARCHY".  In that place, it's expecting the default_date parameter, and it's confused because you're telling it to use a value of "USE_HIERARCHY" for default_date, which it doesn't understand.  If you don't want to set a default_date or any of the other parameters between there and hierarchy, you can just explicitly identify the parameter you're using like hierarchy="USE_HIERARCHY":

arcpy.MakeVehicleRoutingProblemLayer_na(inNetworkDataset, outNALayerName, impedanceAttribute, distanceAttribute, timeUnits, distanceUnits, hierarchy="USE_HIERARCHY", output_path_shape="TRUE_LINES_WITH_MEASURES")

A complete list of Vehicle Routing Problem parameters, and their correct ordering, is here:

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

Reply
0 Kudos
Highlighted
New Contributor III

Hi Melinda,

Thanks so much for your help.  I did get this to work in ArcMap, manually.  I just don't want to have to do all 180 routes manually.  So, I am trying to do it w/ python to automate the process.  Only now I am having problems trying to figure out what is causing my code to fail.  So, any help in troubleshooting is much appreciated.

I tried explicitly identifying the hierarchy and output_shape_path, but still get the same error in the same place...so something is not set up right.  I have patterned my code after the example 3 in the make vehicle routing problem layer.  I was wondering if I needed to have a geodatabase to make this work properly or if there was a problem with my in_network_dataset or out_network_analysis layer?

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Everything in your script up to that point looks fine to me...I'm kind of at a loss for what the problem might be.  It might be time to contact Esri Support and get them to take a look.

View solution in original post

Reply
0 Kudos
Highlighted
New Contributor III

I got the code to run, it ended up being a few typos and my "empty" route that was creating an issue.  Now I am trying to figure out how to perform the looping operation and export the route name, route distance and route time to a table or csv file.

# Import arcpy module

import arcpy, os, sys, traceback

from arcpy import env

# Check out any necessary licenses

arcpy.CheckOutExtension("Network")

# Set environment settings

env.workspace = 'F:\\Workspace\\Sandy\\GM_costAnalysis\\analysis2\\'

env.overwriteOutput = True

# Set Local variables:

inNetworkDataset = "F:\\Workspace\\Sandy\\GM_costAnalysis\\streets" # it seems that this value is empty

outNALayerName = "TrapRoute_MO1_6K" # may want this name to change with each run thru

impedanceAttribute = "Time"

distanceAttribute = "Length"

timeUnits = "Minutes"

distanceUnits = "Miles"

inOrders = "Grids/Block_MO1_6000_label.shp" #This value will change with each run - may want to define this via a loop

inDepots = "Depots/Depot_MO1.shp" # This value will change with each run - may want to define this via a loop

startDepot = "Block_MO1" # this value will change with each run

inRoutes = "Routes/sampleRoute.shp"

outLayerFile = "F:/Workspace/Sandy/GM_costAnalysis/analysis2/Routes/" + outNALayerName + ".lyr" # may want to change w/ each run

searchTolerance = "4243 Meters" # This value will change based on the inOrder number of features

#Test out paths and layer locations

print 'Starting the VRP'

# orderList = arcpy.ListWorkspaces("analysis2", "Folder")

# print ("network: {}".format(inNetworkDataset))

# print outNALayerName

# Process: Make Vehicle Routing Problem Layer

outNALayer = arcpy.MakeVehicleRoutingProblemLayer_na(inNetworkDataset, outNALayerName, impedanceAttribute, distanceAttribute, timeUnits, distanceUnits, hierarchy="USE_HIERARCHY", output_path_shape="TRUE_LINES_WITH_MEASURES")

#outNALayer = arcpy.MakeVehicleRoutingProblemLayer_na(inNetworkDataset, outNALayerName, impedanceAttribute, distanceAttribute, timeUnits, distanceUnits, "", "", "Medium", "Medium", "ALLOW_UTURNS", "'4WD Road';'Pedestrian Walkway';'Pedestrian Ferry';TurnRestriction;OneWay", hierarchy="USE_HIERARCHY", output_path_shape="TRUE_LINES_WITH_MEASURES")

# Get the layer object from the result object to reference the VRP layer

outNALayer = outNALayer.getOutput(0)

# Get the names of all the sublayers within the VRP layer

subLayerNames = arcpy.na.GetNAClassNames(outNALayer)

#Stores the layer names that we'll use later

ordersLayerName = subLayerNames["Orders"]

depotsLayerName = subLayerNames["Depots"]

routesLayerName = subLayerNames["Routes"]

# Process: Add Trap Locations - Order - will want to alter the search_tolerance to match the

arcpy.AddLocations_na(outNALayer, ordersLayerName, inOrders, "Name FID #", searchTolerance, "", "'SDC Edge Source' SHAPE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "'SDC Edge Source' #")

# Process: Add Locations - Depot

arcpy.AddLocations_na(outNALayer, depotsLayerName, inDepots, "Name Name #", searchTolerance, "", "'SDC Edge Source' SHAPE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "'SDC Edge Source' #")

print startDepot

# be sure to set the startDepotName & EndDepotName values

routeFieldMappings = arcpy.na.NAClassFieldMappings(outNALayer, routesLayerName)

routeFieldMappings["StartDepotName"].defaultValue = startDepot

routeFieldMappings["EndDepotName"].defaultValue = startDepot

# Process: Add Locations - Route

arcpy.AddLocations_na(outNALayer, routesLayerName, inRoutes, routeFieldMappings, searchTolerance, "", "'SDC Edge Source' SHAPE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "'SDC Edge Source' #")

# Process: Solve

arcpy.Solve_na(outNALayer, "HALT", "TERMINATE", "")

#Save the solved route layer as a layer file on disk with relative paths

arcpy.management.SaveToLayerFile(outNALayer, outLayerFile, "RELATIVE")

#export out to layer file or table

print 'VRP complete'

Reply
0 Kudos