I am trying to solve the Linear Programming formulation of the Traveling Salesman Problem (TSP) using scipy.optimize.linprog in Python.
This document clearly defines the problem and, though I understand the idea, I can't figure out how to translate it into the required parameters.
I've already flattened the distance matrix, so the function should be fine. What about the constraint matrix and constraint vector ?
I've tried this...
n = len(d)
TSP_c = [d[i][j] for i in range(n) for j in range(n)]
E = [(i,j) for i in range(len(d)) for j in range(len(d))]
TSP_A = [[1 if k in (i, j) else 0 for (i, j) in E] for k in range(n)]
TSP_b = [2] * n
TSP_res = linprog(TSP_c, TSP_A, TSP_b, bounds = bounds, method = 'simplex')
TSP_res
...in an attempt to reproduce this example (I don't know how to use panda by the way).
Updates :
- No bounds : some variables eaqual to 2, null function
- Bounds : 'bounds = [(0, 1)] * n' returns ValueError: Invalid input for linprog: unable to interpret bounds with this dimension tuple: (10, 2).
- Correct bounds : 'bounds = [(0, 1)] * (n*n)', variables equal to 1 or 0 but still null function + it seems only the diagonal (e.g. variables (i,j) = (0,0), (1,1)...) is equal to 1