tsp_descent, a Python code which solves small versions of the traveling salesperson problem (TSP), using a descent method.
The user must prepare a file beforehand, containing the city-to-city distances. The program will request the name of this file, and then read it in as a matrix d. An example of such a file is:
0 3 4 2 7 3 0 4 6 3 4 4 0 5 8 2 6 5 0 6 7 3 8 6 0The distance file d should be square, symmetric, and have a zero diagonal.
A tour of n cities can be represented as a permutation p on the integers 1 through n. The cost of the tour, that is, the length, is the sum
cost = sum ( 1 <= i <= n ) ( d(p(i),p(i+1)) )where p(n+1) is understood to mean p(1).
Given a permutation p, we can consider a variation p2 of p by selecting two indices i1 and i2, with i1 <= i2, and constructing a tour in which the ordering of cities between steps i1 and i2 is reversed.
p2 = [ p(1:i1-1), p(i2:-1:i1), p(i2+1:n) ]
We can consider a variation p2 of p by selecting two indices i1 and i2, with i1 <= i2, and constructing a tour in which we jump directly from i1 to i2, then traverse the cities between i2 and i1 in reverse order, and then resume the original tour, which we call a transposition of the tour.
p3 = [ p(1:i1), p(i2), p(i2-1:-1:i1+1), p(i2+1:n) ];
Starting with a random choice for p, we can generate random reversals and transpositions. The current value of p is replaced whenever a variation is seen to have a lower cost. By repeating this process, a sequence of tours is produced of successively lower cost. It is hoped that this process will find a good tour, if not the best.
This program is set to check only a limited number of variations, currently set to 1000. Thus, it will return an "answer" relatively quickly, even for problems of 50 or 100 cities, where the search space of possible tours is unimaginably complex.
However, even for small problems, there is no guarantee that the tour that is returned is optimal, or even close to optimal. Even if it can be shown that no reversal or transposition can improve the tour that is returned, this may merely mean that the tour is a local minimum.
The computer code and data files described and made available on this web page are distributed under the MIT license
tsp_descent is available in a MATLAB version and an Octave version and a Python version.
change_diophantine, a Python code which sets up a Diophantine equation to solve the change making problem, which counts the number of ways a given sum can be formed using coins of various denominations.
change_dynamic, a Python code which uses dynamic programming to solve the change making problem, which counts the number of ways a given sum can be formed using coins of various denominations.
change_greedy, a Python code which uses the greedy method to seek a solution to the change making problem, which tries to match a given amount by selecting coins of various denominations.
change_polynomial, a Python code which uses a polynomial multiplication algorithm to count the ways of making various sums using a given number of coins.
football_dynamic, a Python code which uses dynamic programming to count the ways of achieving a given score in football.
knapsack_01_brute, a Python code which uses brute force to solve small versions of the 0/1 knapsack problem;
knapsack_dynamic, a Python code which uses dynamic programming to solve a knapsack problem.
knapsack_greedy, a Python code which uses a greedy algorithm to estimate a solution of the knapsack problem;
mcnuggets, a Python code which counts M(N), the number of ways a given number N of Chicken McNuggets can be assembled, given that they are only available in packages of 6, 9, and 20.
mcnuggets_diophantine, a Python code which uses Diophantine methods to find the ways a given number N of Chicken McNuggets can be assembled, given that they are only available in packages of 6, 9, and 20.
partition_brute, a Python code which uses a brute force method to find solutions of the partition problem, in which a set of integers must be split into two subsets with equal sum.
partition_greedy, a Python code which uses a greedy algorithm to seek a solution of the partition problem, in which a given set of integers is to be split into two groups whose sums are as close as possible.
satisfy_brute, a Python code which uses brute force to find all assignments of values to a set of logical variables which make a complicated logical statement true.
subset_sum, a Python code which seeks solutions of the subset sum problem.
subset_sum_brute, a Python code which uses brute force to solve the subset sum problem, to find a subset of a set of integers which has a given sum.
tsp_brute, a Python code which accepts a table of city-to-city distances and solves the traveling salesperson problem (TSP), using brute force.
tsp_greedy, a Python code which accepts a table of city-to-city distances, picks a starting city, and then successively visits the nearest unvisited city, seeking a solution for the traveling salesperson problem (TSP);
tsp_random, a Python code which accepts a table of city-to-city distances, and then randomly samples a number of possible tours, to quickly seek a tour of lower length, seeking a solution for the traveling salesperson problem (TSP);