GithubHelp home page GithubHelp logo

ayushjain1594 / localsearchtsp Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 2.0 16 KB

Python implementation of different algorithms for solving basic TSP.

License: Apache License 2.0

Python 100.00%
traveling-salesman-problem tsp-solver 2opt 3opt graph-algorithms vehicle-routing-problem local-search

localsearchtsp's Introduction

Local Search TSP

Python based implementation of local search algorithms - Randomized Greedy, 2 Optimal and 3 Optimal

Background

Traveling Salesman Problem is one of the widely solved problems in Operations Research and is computationally intensive (NP-hard) to obtain optimal solution for. Luckily there exists relatively fast heuristics that search locally to obtain a near optimal tour. As they are called heuristics, there isn't a guarantee on the quality of solution in terms of closeness to the optimal solution. Nonetheless, local search techniques are widely used for their easy implementation and decent performance.

Following local search TSP algorithms have been implemented in this project:

  1. Randomized Greedy
  2. 2 Optimal
  3. 3 Optimal

How it works

Create TSP object by calling

from tsp import TSP

# v is list of vertices/nodes
# (Optional) edges is list of tuples where each tuple is format (u,v,w)
tsp = TSP(v, edges=[])

If edges are not added at the time of initialization they can be added by calling

# for edge between (u,v) with weight w
tsp.addEdge(u, v, w)

First create a greedy tour by calling with optional parameters

# startnode (optional) is to specify starting node for greedy tour
# randomized (optional) is boolean to run randomized greedy when True
tour = tsp.greedyTour(startnode=None, randomized=False)

The greedy tour can be further optimized using 2 OPT or 3 OPT search methods

# argument tour is a list of nodes forming a cycle
twoopttour = tsp.twoOPT(tour)

# 3 OPT may also be called upon tour generated by 2 OPT
threeopttour = tsp.threeOPT(tour)

Running time

Improvement Heuristics - 2OPT and 3OPT: The running time of improvement algorithms primarily depend on number of neighborhoods they are search for better solution. If swapping edges and updating tour length is constant time, running time of 2 OPT and 3 OPT is approximately O(n^2) and O(n^3) respectively.

localsearchtsp's People

Contributors

ayushjain1594 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.