nikbelikov / tsp-solver

This package will help you in solving the traveling salesman problem.
https://www.npmjs.com/package/@nikbelikov/tsp-solver
ISC License
7 stars 2 forks source link

The library gives different result for same input while running multiple times #2

Closed shabin-slr closed 3 years ago

shabin-slr commented 3 years ago

If we rerun the sample deployed at https://codesandbox.io/s/musing-davinci-i3tss, we'll get different results.

E.g:

First result :
[{"id":0,"name":"Praha"},{"id":6,"name":"Brno"},{"id":5,"name":"Budapest"},{"id":7,"name":"Warszava"},{"id":3,"name":"Amsterdam"},{"id":4,"name":"Hamburg"},{"id":2,"name":"Rennes"},{"id":1,"name":"Paris"},{"id":8,"name":"Milano"}]

Second result :
[{"id":0,"name":"Praha"},{"id":5,"name":"Budapest"},{"id":6,"name":"Brno"},{"id":7,"name":"Warszava"},{"id":4,"name":"Hamburg"},{"id":3,"name":"Amsterdam"},{"id":1,"name":"Paris"},{"id":8,"name":"Milano"},{"id":2,"name":"Rennes"}]

Is this expected behaviour?

nikbelikov commented 3 years ago

Yes.

That's because there is a lot of cities here, so tsp-solver decided to solve the problem with a simplified genetic algorithm. In this case, we can not have a better solution – only the good one. Try to use the latestPopulation as you can see here https://github.com/nikbelikov/tsp-solver/blob/master/demo/index.ts#L68

Also please use https://github.com/nikbelikov/tsp-solver#parameters to calibrate the algorithm.