VRP - Vehicle Routing Problem; ChatGPT-augmented

I’m just baffled, so I wanted to share (and by doing so let you know that I’m working at bringing an VRP solver to ERPNext).

The VRP or Vehicle Routing Problem is an extension of the Traveling Salesman Problem (TSP), but with multiple vehicles and a myriad of possible constraints and objective functions. Currently, the integration with Google in ERPNext’s Delivery Trip can already optimize for the TSP, but for that optimization, when dealing with multiple vehicles a solution to the VRP is already required to set up the Delivery Trips efficiently in the first place.

To jump the hurdle of getting started on a Monday evening after a long day, I asked ChatGPT for a kickstart. As the solver, I already had selected GitHub - reinterpretcat/vrp: A Vehicle Routing Problem solver, because it’s written in rust and performance is critical for these solving problems that can easily span 5-10m and 10k and more iterations.

So here’s my starting point.


Please draft a ERPNext assistant, similar to the bank statement import assistant, which applies the vehicle routing problem solver found in github from the user “reinterprecat” and repository “vrp” such that the assistant takes a user selection of ERPNext Delivery Notes and ERPNext Vehicles and configuration flags for the available objective functions and when the user submits the assistent configuration, then it launches the mentioned vrp solver in on of ERPNext background worker processes. After the background worker process has finished solving the problem and presents a solution in GEOJson format, it should be presented to the users as a read-only ERPNext Geolocation Field in the assistent’s view. By the time the solver has finished and the assistent displays the solution to the user in the Geolocation filed, it should also already have created ERPNext Delivery Trips according to the produced solution. Please draft a solution in the form of an ERPNext DocType and associated Python code.

Certainly! I can help you draft a solution using an ERPNext DocType and associated Python code to implement the ERPNext assistant with the vehicle routing problem solver. Here’s an outline of the solution:

  1. Create a new DocType named “VRPAssistant” in ERPNext with the necessary fields, such as “Delivery Notes” (Link field to Delivery Notes), “Vehicles” (Link field to Vehicles), and “Objective Functions” (Checklist field for available objective functions).
  2. Implement a server-side Python function to handle the VRPAssistant configuration and solution generation. This function will be called when the user submits the assistant configuration. Let’s name this function “solve_vrp_assistant”.
# Import necessary modules
import frappe
from vrp_solver import solve_vrp  # Assuming you have the vrp_solver module installed

def solve_vrp_assistant(vrp_assistant):
    # Fetch the selected Delivery Notes and Vehicles from vrp_assistant
    delivery_notes = frappe.get_doc("VRPAssistant", vrp_assistant).delivery_notes
    vehicles = frappe.get_doc("VRPAssistant", vrp_assistant).vehicles

    # Fetch the selected objective functions from vrp_assistant
    objective_functions = frappe.get_doc("VRPAssistant", vrp_assistant).objective_functions

    # Create VRP problem instance and solve
    vrp_problem = create_vrp_problem(delivery_notes, vehicles, objective_functions)
    solution = solve_vrp(vrp_problem)

    # Store the solution in ERPNext Geolocation Field

    # Create ERPNext Delivery Trips according to the solution

def create_vrp_problem(delivery_notes, vehicles, objective_functions):
    # Implement logic to create the VRP problem instance
    # using the selected Delivery Notes, Vehicles, and objective functions
    # Return the created VRP problem instance

def store_solution_in_geolocation_field(solution):
    # Implement logic to store the solution in ERPNext Geolocation Field
    # Convert solution to GEOJson format if needed

def create_delivery_trips(solution):
    # Implement logic to create ERPNext Delivery Trips according to the solution
    # Set appropriate fields like source, destination, vehicle, etc.
    # Save the Delivery Trips in the database

  1. In the ERPNext client-side code, create a button or form submit action that calls the solve_vrp_assistant function and passes the VRPAssistant document’s name as a parameter.
  2. You will also need to configure an ERPNext background worker process to handle the execution of the solve_vrp_assistant function. This can be done by creating a new background worker class and specifying the function to be executed when the worker is started. Refer to the ERPNext documentation for more details on setting up background workers.

By following this outline, you can create an ERPNext assistant that allows users to select Delivery Notes, Vehicles, and objective functions, and then solves the vehicle routing problem using the provided solver. The solution is stored in a Geolocation Field and ERPNext Delivery Trips are created based on the solution.

I’d say that’s more than a fair start. VRP in ERPNext still today?!? :smile:

Looks like being able to display the final calculated routes to the user for display depends on