Code Monkey home page Code Monkey logo

vrpfun's Introduction

A pythonic but brute-force solution for the Vehicle Routing Problem (VRP)

VRP is a generalized version of the Traveling Salesperson Problem (TSP): Given a depot, a set of delivery locations and the number of vehicles starting from the depot, we need to minimize the time (or travel distance) required to visit all locations.

This repo is a pythonic implementation of the brute-force solution to the VRP. VRP is an NP-Hard problem so the brute force obviously becomes very prohibitive very quickly as the number of destinations is increased or the number of vehicles is reduced.

However, the idea here is just to show how a few lines of python code can be used to solve hard problems.

The code expects two input files, one with locations and their geo-coordinates and the other with distances between locations. There are some simplistic assumptions:

  1. Each delivery location needs to be visited just once
  2. Vehicles come back to the depot
  3. The first location in the list of locations is the depot
  4. Distance from X to Y is the same as the distance from Y to X

Here is a sample plot of the starting input locations resulting from search query McDonalds near Lahore with the truck icon showing the depot:

alt text

Here is the output and plot created after the algorithm has been run with 3 vehicles as input:

Solution time: 51.57 seconds
Shortest route time: 72.3 minutes
Shortest route is: [[0, 4, 8, 9], [0, 1, 11, 5, 7], [0, 6, 10, 2, 12]]

alt text

To try this out, clone the repo and install dependencies (ideally in a virtualenv):

pip install -r requirements.txt

Update the settings file to include your own settings, especially this:

GOOGLE_MAPS_KEY = 'Add your own google maps key here'

Finally, supply command line arguments and run the code like this:

python solution.py <search_query> <number_of_vehicles> <location_limit>

e.g.

python solution.py "McDonalds in Lahore" 3 12

or

python solution.py "Sample" 3 12

If you run the algorithm for more than 12 locations, be prepared to wait a very long time for the results :)

After the first run, your input files will be automatically created and the locations dont need to be fetched again. You can move the depot location to the top of the created input file for locations.

vrpfun's People

Contributors

alisufian avatar ybashir avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.