Towards Excellency: Routing Engine Microservice with Python + Fast API and OR-Tools 🚀

Alperen Adatepe
Volt Lines
Published in
5 min readFeb 4, 2022

At Volt Lines, we strive on excellence every day at each function we have. In this blog post, I’ll mostly talk about how we are improving our services by integrating artificial intelligence and how we are involving algorithms in our decision-making processes. I am an AI Engineer at Volt Lines for almost 8 months and from day one, contributing to the development and the integration of AI into our products.

We carry our passengers between their offices and homes every day and we make sure that all processes go perfectly for our users. While these end-to-end processes involve many things from using the app to taking the shuttle, I’ll focus on the trip part and on how we are helping our passengers to save the most valuable element for our passengers. Their times.. ⏳

In our modern world, among all other things, we are also trying to deal with traffic congestion. Though we all are aware of this, not much initiative presents for the moment. As Volt Lines, we are aware of this problem and we are dedicated to solve it. Because we know that solving this, will not just improve our quality of life, it will also take the lead on green transportation.

But solving the traffic congestion and routing is already a huge and complex problem. On top of that, reducing CO2 emissions, increasing passenger wellness, shortening trip durations, minimizing walking distances and many more criteria need to be considered. Obviously, solving this optimally in a short period of time is almost impossible. Even more, solving this manually is certainly impossible.

In the light of these situations, our full awareness makes us seek all possibilities to utilize technology in our services. We know that by integrating technology, specifically AI, into our service we can optimize all of the objectives given above and satisfy the criteria. In the meantime, we will take the workload of manual routing from our operations and sales team, so that they can focus on other improvement areas for our customers, passengers, and many other stakeholders.

So when it is considered through these optics, it is clearly a win-win scenario for everyone. But this scenario requires lots of brainstorming, discussion, work, and so on. In this blog post, I’ll try to open a door to you and show you a small portion of how we are handling this problem.

Let’s dive into our example!

I’d like to start with what kind of project structure we are going to have. So that it will be easy to follow up with the rest of the article. You can find the project structure below:

In this blog post, our focus mostly will be on the app folder. But still, I’d like to talk about the other files briefly.

Above you see docker-compose file which helps us to define and run our services in multiple Docker containers. Though we design our applications as microservices, there will be only one service for the moment.

Nginx_config.conf file is used for server configurations of the service. Finally, input.json and output.json files give us the expected schema for inputs and outputs of the service.

Now let’s look at the routing-service folder:

We have a Dockerfile to create a container for the routing service and a requirements.txt file to install necessary python packages to run the app.

With the main.py we define the starting point of our application:

Here we define Fast API app and our router for routing-service. We run the app through port 8000. With the router we defined here, we open endpoints to access and trigger the optimizer. For this small example, I defined only one endpoint which takes the input, optimizes, and returns the output. Let’s look at the endpoint under app/routing-service.py:

As you see we have a function that gets a payload in the type of RoutingInput. Optimize with RoutingEngine. Returns the RoutingOutput. Now we can divide this sentence and inspect what are the input, the output, and the engine.

Input and Output model:

On the code above, you see the model for RoutingInput and RoutingOutput model. The endpoint expects a list of vehicles, commutes to operate, and a matrix to see the graph representation. On the other hand, it returns the total duration of the routes and the sequences at each route.

Now we have only one part to check but the most vital one. The optimizer.

Since it is a long file to put here, I only brought the optimizer function which is the most essential one. To optimize our problem, we define our vehicles with their capacity, the nodes to pick up and drop off the passengers. We create necessary models for OR-Tools, such as RoutingIndexManager and RoutingModel. We add duration and demand dimensions to optimize and satisfy their values. Finally, we drop the nodes that don’t have any passengers waiting.

Now, since we go through each file, I’d like to share an example with you, but don’t get limited by that. If you’d like to run and test the code, you can head over to the related repository.

Routing Service Endpoint Documentation

We’ve headed to the docs link. We can try it out with the input.json file.

Input — Request Body

Then we click on execute and scroll down:

Output — Response Body

Voila! You see the algorithm has run and found the optimal solution for the given input in just 2 seconds. This example shows us how we can solve Open-Ended Heterogeneous Capacitated Vehicle Routing Problem.

With improvements to these algorithms hours of workloads can be saved and better experiences can be provided to the users, just as we do at Volt Lines! 🚀

I’d like to finish this blog post by stating that routing optimization is one of the core problems that are waiting to be solved to achieve higher success, a better quality of life, and a greener world. At Volt Lines, we are brainstorming, developing, and serving the best algorithms with the best people in the industry. Are you thinking of joining here? Don’t forget to check our job openings!

You can reach out to me from my e-mail address and I’d be to happy to help you :)

--

--