Code Monkey home page Code Monkey logo

simulated-annealing-tsp's Introduction

Simulated Annealing algorithm to solve Travelling Salesman Problem in Python

Using simulated annealing metaheuristic to solve the travelling salesman problem, and visualizing the results.

Starts by using a greedy algorithm (nearest neighbour) to build an initial solution.

A simple implementation which provides decent results.


An example of the resulting route on a TSP with 100 nodes.

Route Graph

The fitness (objective value) through iterations.

Learning Plot


References

Kirkpatrick et al. 1983: "Optimization by Simulated Annealing"

http://www.blog.pyoung.net/2013/07/26/visualizing-the-traveling-salesman-problem-using-matplotlib-in-python/

simulated-annealing-tsp's People

Contributors

chncyhn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

simulated-annealing-tsp's Issues

Dense graph case

What you are doing below is calculating an Adjacency matrix with distances, for this case with just 45 nodes, it is good, what about the case if this is a graph with 500,000 nodes, any ideas?

def to_dist_matrix(self, coords):
        """
        Returns nxn nested list from a list of length n
        Used as distance matrix: mat[i][j] is the distance between node i and j
        'coords' has the structure [[x1,y1],...[xn,yn]]
        """
        n = len(coords)
        mat = [[self.dist(coords[i], coords[j]) for i in range(n)] for j in range(n)]
        return mat

3D geometry

Thanks for the code for 2D geometry. Is there a simple way to extend your code for 3D?
Tsp-3D

absolute value of the energy

According to the paper you quoted, you shouldn't use the absolute value of the energy value in the p_accept function

return math.exp(-abs(candidate_fitness - self.cur_fitness) / self.T)

Rather use:

return math.exp(-(candidate_fitness - self.cur_fitness) / self.T)

Euclidean Distance function

Thanks , for this informative repo. While using the coords.txt file for the node coordinates, I found the Euclidean distance function refers the x- coordinates of a node with indices 0 and the y-coordinates by indices 1. I think they should be respectively 1 and 2 because the 0 indices are the node numbers. I separately tested with the modified indices and got the distance equal to that calculated manually. Please correct me if my understanding is wrong? Below is the snippet of the dist function with modified indices:

 def dist(self, node_0, node_1):
        """
        Euclidean distance between two nodes.
        """
        coord_0, coord_1 = self.coords[node_0], self.coords[node_1]
        return math.sqrt((coord_0[1] - coord_1[1]) ** 2 + (coord_0[2] - coord_1[2]) ** 2)

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.