This is a Django project that provides APIs for solving TSP, VRP, and VRPTW problems. This project uses RabbitMQ as message broker to communicate with the TSP Solver service.
This project designed to provide APIs to communicate with the TSP Solver service. It is able to use cases related following problems:
- Traveling Salesperson Problem (TSP)
- Vehicle Routing Problem (VRP)
- Vehicle Routing Problem with Time Window (VRPTW)
- Clone the repository
- Create a virtual environment using the command
virtualenv -p python3 venv
- Activate the virtual environment using the command
source venv/bin/activate
- Install dependencies using the command
pip install -r requirements.txt
- Collect static files using command
python manage.py collectstatic
- Considering the RabbitMQ is running on localhost, Run this command:
export MESSAGE_BROKER="localhost"
- To run the Django server, use the command
python manage.py runserver 0.0.0.0:8000
.
Run following command in the project directory
docker-compose up
The API documentation is provided upon opening each of the following endpoint addresses.
Endpoint
: http://127.0.0.1:8000/api/v1/vrp-tsp/
Method
: POST
Request Data:
For TSP request, the num_vehicles must be 1. Otherwise, it will be considered as VRP problem.
{
"id": 1,
"depot": 0,
"num_vehicles": 1,
"locations": [
{"latitude": 40.7128, "longitude": -74.0060},
{"latitude": 34.0522, "longitude": -118.2437},
{"latitude": 41.8781, "longitude": -87.6298},
{"latitude": 29.7604, "longitude": -95.3698},
{"latitude": 39.9526, "longitude": -75.1652},
{"latitude": 33.4484, "longitude": -112.0740},
{"latitude": 29.4241, "longitude": -98.4936},
{"latitude": 32.7157, "longitude": -117.1611},
{"latitude": 32.7767, "longitude": -96.7970},
{"latitude": 37.3382, "longitude": -121.8863}
],
"max_distance": 100000,
"cost_coefficient": 100
}
Response:
- Status: 200 OK
- Body:
{
"code": 200,
"message": "Operation successful",
"result": {
"job": "1"
}
}
Use the job identifier in order to retrieve result related to this job.
Endpoint
: http://127.0.0.1:8000/api/v1/vrptw/
Method
: POST
Request Data:
{
"message_type": "VRPTW",
"id": "968ae423-2a40-447e-943c-de18b3a2ef12",
"depot": 0,
"num_vehicles": 2,
"locations": [
{"latitude": 40.7128, "longitude": -74.0060},
{"latitude": 34.0522, "longitude": -118.2437},
{"latitude": 41.8781, "longitude": -87.6298},
{"latitude": 29.7604, "longitude": -95.3698},
{"latitude": 39.9526, "longitude": -75.1652},
{"latitude": 33.4484, "longitude": -112.0740},
{"latitude": 29.4241, "longitude": -98.4936},
{"latitude": 32.7157, "longitude": -117.1611},
{"latitude": 32.7767, "longitude": -96.7970},
{"latitude": 37.3382, "longitude": -121.8863}
],
"time_windows": [
[0, 5],
[7, 12],
[10, 15],
[16, 18],
[10, 13],
[0, 5],
[5, 10],
[0, 4],
[5, 10],
[0, 3]
],
"wait_time": 30,
"max_time_vehicle": 30
}
Response:
- Status: 200 OK
- Body:
{
"code": 200,
"message": "Operation successful",
"result": {
"job": "968ae423-2a40-447e-943c-de18b3a2ef12"
}
}
To retrieve results related to a particular job, utilize its identifier. However, keep in mind that as this project is stateless and the tsp-solver requires time to solve the problem and publish the results on topic, there may be instances where a valid id returns a 404 error. If this occurs, you will need to make the request again until the result is obtained.
The API extracts the ID from the validated data and consumes messages from the TSP_OUTPUT_QUEUE using the pika library. It checks each message for a matching correlation ID and returns the result if found. If no matching message is found, it Nack all the consumed messages and returns a 404 NOT FOUND response.
Endpoint
: http://127.0.0.1:8000/api/v1/status/?id=968ae423-2a40-447e-943c-de18b3a2ef12
Method
: GET
Query Parameters:
id
: The unique identifier of the job provided in earlier APIs response
Response:
- Status: 200 OK
- Body:
{
"code": 200,
"message": "Operation successful",
"result": {
"id": "ehsanmgh",
"solution": [
{
"route": [
0,
4,
8,
3,
6,
5,
7,
1,
9,
2,
0
],
"vehicle": 0,
"distance": 10476
}
],
"code": 200,
"message": "Operation successful."
}
}
If the job is not yet completed, the response will be:
{
"code": 404,
"message": "No matching message found with message id = job_id"
}
The project uses RabbitMQ as the message broker to handle the communication between the TSP solver service and the client. The connection parameters are read from Django settings file.
The input queue name and output queue name are set in Django settings file as well.
This project depends on the following packages:
- Django
- djangorestframework
- pika (for RabbitMQ connection)
The CreateVrpRequest API view has 5 implemented tests. To conduct tests on this view, utilize the following command:
export MESSAGE_BROKER="localhost"
python manage.py test