I need to implement Lazy Constraints.
I implement TSP model (This model: http://www.opl.ufc.br/pt/post/tsp/)
But i dont know how i implement subtour in lazy constraint.
file = open('17.txt')
lines = file.readlines()
cost_matrix = 
for i in range(len(lines)):
aux = lines[i][:-1].split('\t')
aux = [int(i) for i in aux if i!= '']
tam = len(cost_matrix)
n = list(range(tam))
m = list(range(1, tam))
for i in range(tam):
cost_matrix[i][i] = 999999999999
import gurobipy as gp
model = gp.Model()
x = model.addVars(n, n, vtype=gp.GRB.BINARY, name="x")
u = model.addVars(n, vtype=gp.GRB.INTEGER, name="u")
gp.quicksum(x[i, j] * cost_matrix[i][j] for i in n for j in n),
c1 = model.addConstrs(
gp.quicksum(x[i, j] for i in n) == 1 for j in n if (i != j))
c2 = model.addConstrs(
gp.quicksum(x[j, i] for i in n) == 1 for j in n if (i != j))
# R3 (subtour constraint)
c3 = model.addConstrs(u[i] - u[j] + tam * x[i, j] <= tam-1
for i in m
for j in n
if (i != j))
Someone can help me?
Detail, I know there is a very good model with lazy constraints, but I need a slower model to do some analysis in my PHD.
I also understand how callbacks work and I tried, based on the existing implementation, to adapt to my code but I tried in several ways and I didn't get any results, just errors.
Thanks for the help ^^
Please sign in to leave a comment.