Esri / workforce-scripts

A set of scripts to help administer Workforce projects.
Apache License 2.0
79 stars 64 forks source link

Optimally Creating and Assigning Work Orders Based on Routes #99

Open mvacoyote opened 2 years ago

mvacoyote commented 2 years ago

Hello, In the 'Optimally Creating and Assigning Work Orders Based on Routes' notebook there is the following code:

assignments_to_add = []
for _, row in routes.iterrows():
    worker = random.choice(workers)
    workers.remove(worker)
    route_stops = stops.loc[(stops['RouteName'] == row["RouteName"]) & stops['globalid'].notnull()]
    for _, stop in route_stops.iterrows():
        assignments_to_add.append(workforce.Assignment(
            project,
            assignment_type="Inspection",
            location=stop["name"],
            status="assigned",
            worker=worker,
            assigned_date=datetime.now(),
            due_date=stop["DepartTime"],
            geometry=stop["SHAPE"]
        ))
assignments = project.assignments.batch_add(assignments_to_add)

I think the line workers.remove(worker) should be removed because then in assignments_to_add.append() the worker=worker will give an error of empty list. Am I wrong?

Thanks

apulverizer commented 2 years ago

@MVAC13 In the example, we create the same number or routes as workers.

results = arcgis.features.analysis.plan_routes(breweries_layer, # Feature Layer of Stops
                                    len(workers),               # Number of routes to generate
                                    5,                          # Maximum stops per route
                                    datetime.now(),             # Start time of route
                                    start_layer,                # The dictionary we created to represent the start location
                                    stop_service_time=60,       # How much time in minutes to spend at each stop
                                    max_route_time=480,         # The maximum time for the worker to complete the route
                                    )

We then iterate over each route and select a worker, and remove that worker from the possible list of workers to chose (so that a worker only is assigned one route). Then for each stop in the route we create an assignment and assign it to the selected worker.

Then after all assignments for all routes have been created, we add them to the project.

If workers.remove(worker) was removed, a worker could be assigned to multiple routes which doesn't make sense from a practical standpoint.

Does that help explain it?

mvacoyote commented 2 years ago

It does explain it. Thanks!