Giter VIP home page Giter VIP logo

salesman.js's Introduction

salesman

See: demo
Author: Ophir LOJKINE

salesman npm module

Good heuristic for the traveling salesman problem using simulated annealing.

salesman~Point

Kind: inner class of salesman

new Point(x, y)

Represents a point in two dimensions. Used as the input for solve.

Param Type Description
x number abscissa
y number ordinate

salesman~solve(points, [temp_coeff], [callback], [callback], [keep_end]) ⇒ Array.<number>

Solves the following problem: Given a list of points and the distances between each pair of points, what is the shortest possible route that visits each point exactly once and returns to the origin point?

Kind: inner method of salesman
Returns: Array.<number> - An array of indexes in the original array. Indicates in which order the different points are visited.

Param Type Default Description
points Array.<Point> The points that the path will have to visit.
[temp_coeff] number 0.999 changes the convergence speed of the algorithm. Smaller values (0.9) work faster but give poorer solutions, whereas values closer to 1 (0.99999) work slower, but give better solutions.
[callback] function An optional callback to be called after each iteration.
[callback] function euclidean An optional argument to specify how distances are calculated. The function takes two Point objects as arguments and returns a number for distance. Defaults to simple Euclidean distance calculation.
[keep_end] boolean false An optional argument to specify if the last point is fixed. If false then the minimum circuit is calculated, if true the minimum path from first to last node is calculated.

Example

var points = [
      new salesman.Point(2,3)
      //other points
    ];
var solution = salesman.solve(points);
var ordered_points = solution.map(i => points[i]);
// ordered_points now contains the points, in the order they ought to be visited.

salesman.js's People

Contributors

lovasoa avatar elitemastereric avatar richjharris avatar

Stargazers

Pavel Demin avatar Bruno Reis avatar  avatar Wing Kai avatar  avatar jlongguo avatar  avatar  avatar Mathew Chan avatar  avatar Wybe avatar Martin avatar Mori Bellamy avatar  avatar Roland avatar Makeychik Roman avatar Andrew Booth avatar  avatar Vinayak Tekade avatar  avatar  avatar Kyle Villeneuve avatar Jeff Hackshaw avatar Camilo avatar Celso Wellington avatar  avatar SuperNova+ avatar L. Jiang avatar Seventh7th_Son avatar  avatar Cerberus avatar Gabriel S. L avatar Udantha Pathirana avatar David Yu avatar dori avatar Oğuz Kağan EREN avatar Miraj Pudasaini avatar Anton Matosov avatar Mohammad Taufiqurrohman avatar Enes Karaosman avatar Armando V avatar Claudio Prezzi avatar Hiroya Kato avatar  avatar Michael Rhodes avatar Mario avatar Yitzchok Gottlieb avatar  avatar Julien Malherbe avatar

Watchers

 avatar James Cloos avatar Jeroen van Grondelle avatar  avatar

salesman.js's Issues

Option for non-loop?

Is it possible to get the shortest path without having to return to the first point?

One where the last point is the furthest away so the salesman doesn't have to return all the way back to the starting point?

Origin and Destination parameters

Hello,

Firstly, I'm asking myself how this algorithm would compete with a brute force one? Like this one for example. My understanding is that your simulated annealing algorithm returns an approximation using probabilities, which means you won't always get the same output for the same input (while brute force will always find the perfect solution because it will try all possible paths). On the other hand, simulated annealing will be faster than brute force. Am I right?

Secondly, being able to define a fixed starting point and destination point would be amazing. I've no idea how this problem should be called, maybe the "divorced salesman" as seen on StackOverflow.
In this same question, they're talking about adding a dummy city whose distances to every other points is 0. Would that be a good option? Is it possible to go that way using your repository?

I will make a fork of your repository anyway because I need to change the distance calculation algorithm to take the earth's radius in account (in my case it won't be x/y but lat/lon instead).

Thank you for your help.

Add a "progress" argument to the callback function.

Since the simulated annealing temperature has a known start and end value (from 100 * distance from point 0 to point 1 to 1e-6), progress can be represented by the size of the current temperature.

Not sure of the best way to calculate the percentage from that, but the result can be passed to the callback function to be used for progress bars.

..isn't it supposed to return to the origin city ?

I've tried various things here now, but the resulting array does neither end nor start with "0", which is part of the point of the algorithm, isn't it? (it is suppsoed to return to the origin .. most of the time it just seems to pass by the origin)

e.g. test script

let salesman = require('salesman.js');
let a = [[0, 0], [20000, 200], [100, 0], [100, 100], [100, 0]];
let b = a.map(p => new salesman.Point(p[0], p[1]));
let sol = salesman.solve(b);
console.dir(sol);

Ran multiple times:

[ 1, 3, 0, 4, 2 ]
[ 4, 2, 0, 3, 1 ]
[ 4, 1, 3, 0, 2 ]
[ 0, 3, 1, 4, 2 ]
[ 3, 0, 4, 2, 1 ]

I've tried closing the loop also (duplicating the first elem last) without any big amount of luck...

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.