Giter Club home page Giter Club logo

tspy's Introduction

tspy: An optimization package for the traveling salesman problem.

The tspy package gives a Python framework in which to study the famous Traveling Salesman Problem (TSP). In this package, one can work on specific instances of the TSP. Approximation methods and lower bounds are included by default. The structure of the package makes it easy to create and include your own methods.

Installation

The tspy package can be installed using pip:

pip install tspy

How to use tspy

Creating an instance

To create an instance of the TSP, use:

from tspy import TSP
tsp = TSP()

Currently, data can be given to the instance in two ways: by giving it the NxN distance matrix D or, in the case of an Euclidian TSP, a NxP data matrix X and a distance function.

# Using the distance matrix
tsp.read_mat(D)

# Using the data matrix and a distance function
tsp.read_data(X, dist)

# Using a list of coordinates (with the Euclidean distance)
tsp.read_data([(0,0), (0,1), (1,0), (1,1)])

Computing approximate solutions

The module tsp.solvers contains several algorithms providing approximate solutions of TSP instances. For example, the 2-opt algorithm gives good solutions rather quickly. Here is how it can be used in the tspy package:

from tspy.solvers import TwoOpt_solver
two_opt = TwoOpt_solver(initial_tour='NN', iter_num=100)
two_opt_tour = tsp.get_approx_solution(two_opt)

Other solvers are used similarly.

Current solutions are stored in the dictionary tsp.tours. If a data matrix has been provided to the instance, a plot of the solution can be shown:

tsp.plot_solution('TwoOpt_solver')

alt text

At any point, the best solution that has been computed can be retrieved:

best_tour = tsp.get_best_solution()

Computing lower bounds

The TSP being NP-hard, it is difficult to get exact solutions for large instances. However, by computing lower bounds we can know how good our approximations are. The tspy package provides several lower bounds methods. One example is given by the Simple_LP_bound which gives the optimal solution of the LP relaxation of the TSP:

from tspy.lower_bounds import Simple_LP_bound
lower_bound = tsp.get_lower_bound(Simple_LP_bound())

At any point, the best lower bound that has been computed can be retrieved:

best_lower_bound = tsp.get_best_lower_bound()

Future

The following features will be added soon:

  • Genetic programming
  • Ant colony optimization
  • Lin–Kernighan heuristic
  • Better LP lower bounds

Feel free to contact me if you would like to contribute.

Author

tspy was written by William Borgeaud (williamborgeaud[at]gmail.com).

tspy's People

Contributors

wborgeaud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

tspy's Issues

Error while using tspy package

D = [[ 0, 60, 66, 258, 256, 682, 770, 888],
[ 60, 0, 50, 225, 213, 630, 719, 832],
[ 66, 50, 0, 275, 260, 618, 707, 829],
[258, 225, 275, 0, 52, 689, 772, 840],
[256, 213, 260, 52, 0, 637, 719, 789],
[682, 630, 618, 689, 637, 0, 88, 245],
[770, 719, 707, 772, 719, 88, 0, 181],
[888, 832, 829, 840, 789, 245, 181, 0]]

from tspy import TSP
tsp = TSP()

tsp.read_mat(D)

from tspy.solvers import TwoOpt_solver
two_opt = TwoOpt_solver(initial_tour='NN', iter_num=100)
two_opt_tour = tsp.get_approx_solution(two_opt)

The code does not exceute and gives the following error:
TypeError: list indices must be integers or slices, not tuple

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.