Trying to solve TSP as linear programming task using cvxpy and have problem with this. It is my first experience so thanks for help. As a result I want to have matrix with 0 and 1 that shows every next city for salesman.
need to use exactly cvxpy
thanks for help
import cvxpy as cp
import numpy as np
np.random.seed(1)
N = 10
distances = np.random.rand(N, N)
x = cp.Variable((N, N), boolean=True)
u = cp.Variable(N, integer=True)
constraints = []
for j in range(N):
indices = list(range(0, j)) + list(range(j + 1, N))
constraints.append(cp.sum(x[indices, j]) == 1)
for i in range(N):
indices = list(range(0, i)) + list(range(i + 1, N))
constraints.append(cp.sum(x[i, indices]) == 1)
for i in range(1, N):
for j in range(1, N):
if i != j:
constraints.append(u[i] - u[j] + N*x[i, j] <= N-1)
for i in range(N):
for j in range(N):
if i != j:
сost += (x[i,j]*distances[i,j])
prob = cp.Problem(cp.Minimize(cost), constraints)
prob.solve()
print(prob.value)
receive "None"
feel like the problem in cost defining, but don`t know how to make it correct maybe I should use cvxpy.multiply or cvxpy.sum?
don't know what was the problem. maybe smth with Jupyter. everything became ok after it refreshing thanks all for help!