0

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
Progman
  • 16,827
  • 6
  • 33
  • 48
NEWO-o
  • 1
  • 4
  • Using a modeling tool that does not use a matrix interface might be easier. E.g. Pulp or Pyomo. That allows you to be closer to the mathematical model. – Erwin Kalvelagen May 31 '23 at 13:00
  • Thanks for the advice. However, I need to try solving the TSP with my own simplex algorithm and the scipy function seems closer to what I could use. (Also, deadlines won't allow me to take the time to change libraries and all). – NEWO-o Jun 03 '23 at 16:38
  • Does not makes sense to me. TSP cannot be solved by a simplex method. Simplex is only for continuous LP problems. – Erwin Kalvelagen Jun 03 '23 at 16:42
  • Well I am new to this but it seems you might be able solve the LP relaxation of the problem (with the simplex) and then branch-and-cut your way out of subtours to fine the optimal tour. Does this seem feasible to you ? – NEWO-o Jun 03 '23 at 22:21
  • I think you are confusing integrality with subtour elimination. They are different things. You need both in a tsp. – Erwin Kalvelagen Jun 04 '23 at 06:52
  • It might be the case. To me, "integrality = 1 : Integer variable; decision variable must be an integer within bounds." as described in the scipy docs, is just a scipy.optimize.linprog parameter... @ErwinKalvelagen – NEWO-o Jun 04 '23 at 10:11
  • It looks like you have three tasks. (1) implement a tsp model in scipy, (2) implement an LP solver and (3) implement a mip solver. (1) is by far the easiest but already poses an insurmountable stumbling block. You may want to reassess your goals. – Erwin Kalvelagen Jun 05 '23 at 04:02
  • @ErwinKalvelagen Would you mind explaining what you mean by MIP solver ? Mixed-Integer Programming I guess but how would that be different from solving the LP relaxation of the TSP then finding the first subtours, then adding the constraints, then branching on fractional variables until finding an integer solution, then adding other subtours constraints ? (as described in the linked document) – NEWO-o Jun 05 '23 at 07:36
  • Also, I trust you have far more knowledge in LP than I have as I am starting to think/understand I have been mislead by my teachers, however since I have the LP relaxation and a subtour-finding function already, don't I "just" need to figure out the branch-and-cut part before looping the problem for the subtour constraints ? Then I have seen simplex algorithms written in not too many LOC so I would just need to "plug it in", I am not trying to implement a whole solver. – NEWO-o Jun 05 '23 at 07:42
  • Obviously you need to talk to your teacher who gave you this assignment. I don't think you can implement a solver if you don't know what they are supposed to solve. Note that there are good textbooks. – Erwin Kalvelagen Jun 05 '23 at 08:27
  • I _do_ know what I am trying to solve. An LP formulation of the TSP, with the simplex algorithm, then there are the subtours constraints to add on the go through a branch-and-cut algorithm. Maybe I am not clear enough but I don't have enough knowledge to know how I could be clearer. – NEWO-o Jun 05 '23 at 12:18
  • *An LP formulation of the TSP, with the simplex algorithm,* There are no standard LP formulations for TSP (there are standard MIP formulations). So the Simplex method cannot solve TSPs. I suggest to talk to your teacher, as there is much confusion here, – Erwin Kalvelagen Jun 05 '23 at 12:29
  • Ok so my teacher is not helping. That is why I am here. Feel free to ignore me if I start to annoy you, I have no other choice but to continue. So the formulation I am mentioning is the one I linked in my question above. The author says "the constraint matrix so far is totally unimodular, so we wouldn’t even need to worry about integer programming techniques". That is before adding the subtour constraints. So the simplex really should be able to solve _this part_ of the problem at least. – NEWO-o Jun 05 '23 at 13:08
  • But my problem isn't in knowing that yet. In this question I was trying to find the right constraints. I still have redundant constraints. Then I have to represent my solutions (basic tableau and map for TSP). Then I have to figure out the branch-and-cut part... – NEWO-o Jun 05 '23 at 13:13

1 Answers1

-1

This works :

d = [[distance_ij(coord_rad,i,j) for j in range(len(coord_rad))] for i in range(len(coord_rad))]

n = len(d)
E = [[i, j] for i in range(n) for j in range(n)]

TSP_A_eq = []

for k in range(n) :
    TSP_A_eq.append([1 if i == k and k != j else 0 for [i, j] in E]) # somme des arêtes sortantes
    TSP_A_eq.append([1 if k == j and i != k else 0 for [i, j] in E]) # somme des arêtes rentrantes

for i in range(n) :
    TSP_A_eq.append([1 if i == j else 0 for [i, j] in E]) # pas d'arête (i, i)

TSP_c = [d[i][j] for [i, j] in E]
TSP_b_eq = [1] * (2 * n) + [0] * n
bounds = [(0, 1)] * (n * n)

TSP_res = linprog(c=TSP_c, A_eq=TSP_A_eq, b_eq=TSP_b_eq, bounds=bounds, method='simplex', integrality=1)

Then there are the subtour elimination constraints...

NEWO-o
  • 1
  • 4
  • There is an OptimizeWarning for the rank of A_eq though. The "no edge (i,i)" constraints seem to be responsible. – NEWO-o Jun 05 '23 at 12:20