It is amazing how many modern enterprise solutions can be built in the cloud with just a few services. Developers can combine cloud components to form a pipe that goes from retrieving data to emailing results with a few clicks. Pricing is flexible. The hardware supply is limited, especially with the serverless option. As a result, small businesses can now afford a huge amount of computing capacity. This democratization of computing power is rapidly modernizing many traditional firms. One of them must be a delivery service.
I came to Across two articles here in Medium: one at a time Sebastian Telsemeyer on the construction of a time series forecasting pipeline for GCP Cloud Run and Pub / Sub:
Sebastian’s article is inspiring. Pub / Sub follows a specific GCP bucket in its piping. When the TSV file is loaded, it activates the Cloud Run count, which produces results for another group. His entire infrastructure is serverless and coded in Terraform. I think his pipeline may be even simpler: a cloud function triggered by a
Finalise/Create the event can replace the Pub / Sub + Cloud Run combination. The simpler version requires fewer settings. Because the cloud function is essentially a lambda function, it thus eliminates the entire Docker section and the Gunicorn + Flask dependency.
Samir’s article shows how complicated it is capacitive vehicle routing problem (CVRP) can be solved with Google’s OR tools. It is assumed that the two carriers have to distribute 30 packages from the logistics center to nine different locations in Braunschweig, Germany. Each Carrier can carry a maximum of 15 packages. Google OR-Tools then tries to plan routes at the lowest cost while balancing the load and distance for each operator. The example output looks like this:
Route for driver A:
0 Parcels to Herzog Anton Ulrich Museum, Museumstrasse 1, 38100 Braunschweig ->
2 Parcels to Institute of System Security, Muehlenpfordtstrasse 23, 38106 Braunschweig ->
2 Parcels to Am Schuetzenpl. 1, 38114 Braunschweig ->
3 Parcels to Humboldtstrasse 1, 38106 Braunschweig ->
2 Parcels to Johanniterstrasse 5, 38104 Braunschweig ->
6 Parcels to Willy-Brandt-Platz 1, 38102 Braunschweig ->
Distance of the route: 14960 (m)
Parcels Delivered: 15 (parcels)
The OR tools take the ten-location distance matrix as one input. In Google’s original publication and Samir’s article, the distance matrix was a hard-coded doll of fictitious locations, and directions were printed in text only. In an actual delivery command center, we need to calculate the distance matrix (see my previous article here) on the basis of actual addresses and informs air carriers of their routes.
With these thoughts in my mind and standing on the shoulders of Sebastian and Samir, I accepted the challenge and implemented a simple solution to GCP. A cloud storage download event can trigger a cloud feature to build a distance matrix through the Google Maps API. OR-Tools then calculates the routes. Finally, it sends directions to the operator via SendGrid (Figure 2.). The code for this project is here in my Github archive.
The Json input data contains two parts. The first part contains addresses and packet numbers for all locations. The first location is always the logistics center where the carriers load the packages. The second part is the information of the carriers, including their name, capacity and e-mail address. Below is an abridged version of my feed.json.
We want to send guidance emails to operators from the Cloud feature. Goolge recommends that we use a trusted third-party service provider, such as SendGrid. SendGrid’s free plan allows 100 emails / day forever, which is enough for our project.
Please register an account first Send the Grid with a free plan. Then create a shipping ID. It is the “sender” address of the emails. Later, SendGrid will send you an email. You must confirm one sender by following its instructions before you can send emails.
Then go to the user control panel and click Open
Email API and
Integration Guide. Care about
Web API and
Python. Then create an API key (Figure 3.) and follow the test instructions until you pass the integration test (Figure 4.). Keep one sender’s email and API key in a safe place for future reference.
Create a project called
delivery-lite In the GCP console. Enable
Distance Matrix API and create a restrictive API key for it. The process involves several clicks, and you can read the previous article here instructions.
Cloud Function and create a function
command-center and select
Cloud Storage trigger type and
Finalize/Create as an event. Click “BROWSE” and create a bucket. Click
RUNTIME settings and add four environment variables:
KEYis the Distance Matrix API key.
SENDGRID_API_KEYis your SendGrid API key.
SENDERis the email address of a single sender.
PROJECTis in this case the name of the project
Then you can click
Code page. Enable
Cloud Build API and follow the instructions in the warning bar to create the credentials. When it’s done, come back
Code page. Care about
Python 3.9 while driving and type
main entry point.
Create a file named
function.py In the Inline Editor. Copy and paste the contents from my archive into the corresponding files (Figure 6). Or you can compress and download my three files. press
DEPLOY button and wait until the operation is completed.
function.py includes three helper functions. The first two are used to calculate the distance matrix for the list of places via the Maps API. The third sends email via SendGrid.
function.py. After line 12 receives the distance matrix, the script compresses it into carrier data and finally calls the OR tools to line 29. If at least one solution is found, the script formats the route and sends it to each operator separately via SendGrid on line 46. I thank Samir and the Google team for OR-Tools- code examples.
Once everything is installed, we can now test the piping. Below is
input_test.json file. edit information
carrier – list and in particular enter valid e-mail addresses
input_test.json file into a bucket (Figure 7.). This should trigger our cloud function.
If all goes well, you will see new log entries
LOGS back to the Cloud Features page. These are the standard outputs of the script (Figure 8.).
And you can check your carrier’s emails. A new email with directions should arrive soon (Figure 9.).