Skip to content

Instantly share code, notes, and snippets.

@hollemawoolpert
Last active April 8, 2024 01:16
Show Gist options
  • Save hollemawoolpert/612b51f5a489d276a4240f298a3cc9ed to your computer and use it in GitHub Desktop.
Save hollemawoolpert/612b51f5a489d276a4240f298a3cc9ed to your computer and use it in GitHub Desktop.
Simple VRP with Google Developer Resources

Binder

Simple VRP with Google Developer Resources¶

Demonstrates a solution for the simple multi-vehicle routing problem (VRP) using a combination of Google libraries and services. Sample depot and shipment locations randomly chosen in the San Antonio, TX metro area. Distances and times are based on Google's road network.

Getting Started

You will need a Google Maps Platform API Key.

Run on binder

Run locally

Prerequisites
  • Python 3 environment
  • JupyterLab
pip install jupyterlab
Installation
pip install -r requirements.txt
jupyter nbextension enable --py --sys-prefix gmaps
Run notebook
jupyter notebook

Libraries and Services

#!/bin/bash
jupyter nbextension enable --py --sys-prefix gmaps
gmaps==0.9.0
googlemaps==3.0.2
ortools==7.3.7083
pandas==0.25.0
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@akrv
Copy link

akrv commented Jan 5, 2022

Google Distance Matrix API won't allow more than 10 elements for the calculation
if the change the shipments[0:9] to shipments[0:].
How can I overcome this limitation? MAX_ELEMENTS_EXCEEDED

@hollemawoolpert
Copy link
Author

Distance Matrix API has the following per request limits:

  • a maximum of 25 origins or 25 destinations
  • 100 elements, where an element is a source-destination pair

To allow for matrices beyond 10 x 10, you would have to split up the input matrix into pieces that meet the usage limits above, make multiple Distance Matrix API calls, and join the results to form a single matrix.

@jiang-yuan
Copy link

jiang-yuan commented Jan 6, 2022

Sorry. Binder ask me to build and I click it. But it returns

Build failed with 504, please run 'jupyter lab build' on the server for full output

The Map Depot and Shipments code block prints:

Error displaying widget: model not found

@akrv
Copy link

akrv commented Jan 6, 2022

Thank you @hollemawoolpert for the response.

Reading the VRP documentation again, this link helps you compute bigger matrices with multiple requests.

@bhavyaverma1
Copy link

bhavyaverma1 commented Jan 19, 2023

Hi, So running the vrp for lets say 25 points and 5 vehicles, outputs the routes but they are only assigned to 1 or 2 vehicles and the rest 3 vehicles are left no job to do. Is there a workaround to this so as to make sure all the vehicles do at least some visits/distance and not just zero. Actually the problem we currently face is our depot is outside the locations' cluster and so I think the VRP is assigning only 1-2 vehicles. If we set max_distance to a lower value than the deafult 2*() then program gets stuck in infinite loop.
Thanks!

@solomonleo12345
Copy link

Hi, while running the function, gmaps.symbol_layer
I am getting the following error,

AttributeError                            Traceback (most recent call last)
      1 depot = {
      2     'location': (29.399013114383962, -98.52633476257324)
      3 }
----> 5 depot_layer = gmaps.symbol_layer(
      6     [depot['location']], hover_text='Depot', info_box_content='Depot', 
      7     fill_color='white', stroke_color='red', scale=8
      8 )

File ~\AppData\Local\Apps\envs\route_opt\lib\site-packages\gmaps\marker.py:477, in symbol_layer(locations, hover_text, fill_color, fill_opacity, stroke_color, stroke_opacity, scale, info_box_content, display_info_box)
    328 def symbol_layer(
    329         locations, hover_text='', fill_color=None,
    330         fill_opacity=1.0, stroke_color=None, stroke_opacity=1.0,
    331         scale=3, info_box_content=None, display_info_box=None):
    332     """
    333     Symbol layer
    334 
   (...)
    475         A :class:`gmaps.Markers` instance.
    476     """
--> 477     options = _symbol_layer_options(
    478         locations, hover_text, fill_color,
    479         fill_opacity, stroke_color, stroke_opacity, scale,
    480         info_box_content, display_info_box)
    481     symbols = [Symbol(**option) for option in options]
    482     return Markers(markers=symbols)

File ~\AppData\Local\Apps\envs\route_opt\lib\site-packages\gmaps\marker.py:275, in _symbol_layer_options(locations, hover_text, fill_color, fill_opacity, stroke_color, stroke_opacity, scale, info_box_content, display_info_box)
    273 if is_atomic(hover_text):
    274     hover_text = [hover_text] * number_markers
--> 275 if is_atomic(scale):
    276     scale = [scale] * number_markers
    277 if is_color_atomic(fill_color):

File ~\AppData\Local\Apps\envs\route_opt\lib\site-packages\gmaps\options.py:40, in is_atomic(elem)
     34 def is_atomic(elem):
     35     """
     36     True if an element is a single atom and false if it's a collection
     37     """
     38     return (
     39         isinstance(elem, string_types) or
---> 40         not isinstance(elem, collections.Iterable)
     41     )

AttributeError: module 'collections' has no attribute 'Iterable'

Please help.

@VladimirOscanoaC
Copy link

Hola, mientras ejecuta la función, gmaps.symbol_layer Estoy recibiendo el siguiente error,

AttributeError                            Traceback (most recent call last)
      1 depot = {
      2     'location': (29.399013114383962, -98.52633476257324)
      3 }
----> 5 depot_layer = gmaps.symbol_layer(
      6     [depot['location']], hover_text='Depot', info_box_content='Depot', 
      7     fill_color='white', stroke_color='red', scale=8
      8 )

File ~\AppData\Local\Apps\envs\route_opt\lib\site-packages\gmaps\marker.py:477, in symbol_layer(locations, hover_text, fill_color, fill_opacity, stroke_color, stroke_opacity, scale, info_box_content, display_info_box)
    328 def symbol_layer(
    329         locations, hover_text='', fill_color=None,
    330         fill_opacity=1.0, stroke_color=None, stroke_opacity=1.0,
    331         scale=3, info_box_content=None, display_info_box=None):
    332     """
    333     Symbol layer
    334 
   (...)
    475         A :class:`gmaps.Markers` instance.
    476     """
--> 477     options = _symbol_layer_options(
    478         locations, hover_text, fill_color,
    479         fill_opacity, stroke_color, stroke_opacity, scale,
    480         info_box_content, display_info_box)
    481     symbols = [Symbol(**option) for option in options]
    482     return Markers(markers=symbols)

File ~\AppData\Local\Apps\envs\route_opt\lib\site-packages\gmaps\marker.py:275, in _symbol_layer_options(locations, hover_text, fill_color, fill_opacity, stroke_color, stroke_opacity, scale, info_box_content, display_info_box)
    273 if is_atomic(hover_text):
    274     hover_text = [hover_text] * number_markers
--> 275 if is_atomic(scale):
    276     scale = [scale] * number_markers
    277 if is_color_atomic(fill_color):

File ~\AppData\Local\Apps\envs\route_opt\lib\site-packages\gmaps\options.py:40, in is_atomic(elem)
     34 def is_atomic(elem):
     35     """
     36     True if an element is a single atom and false if it's a collection
     37     """
     38     return (
     39         isinstance(elem, string_types) or
---> 40         not isinstance(elem, collections.Iterable)
     41     )

AttributeError: module 'collections' has no attribute 'Iterable'

Por favor ayuda.

Hola, Lograste resolverlo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment