I have two lists l=[0,1,7,10] and ll=[1,7,10], I want to use this lists as parameters of a function, the function uses this lists, ll, and l, in "for" cycles to iterate. When I pass the list my function does not take all the elements, specifically does not take the 0 value from list "l". I am new on python and I am still learning about functions. Cnew is just another parameter (a matrix), which is pass properly. My code is the next:
import numpy as np
from gurobipy import *
l=[0,1,7,10]
ll=[1,7,10]
def TSP_ruteo(Cnew,l,ll):
nodos=l
nodos2=ll
model=Model('TSP_ruteo') #NOMBRE DEL MODELO.
model.ModelSense=GRB.MINIMIZE OPTIMIZACIÓN.
x,u={},{}
for i in nodos:
for j in nodos:
x[i,j]=model.addVar(vtype=GRB.BINARY,name="x[%s][%s]"%(i,j))
for i in nodos2:
u[i]=model.addVar(vtype=GRB.INTEGER,lb=1.0,ub=len(Cnew)-1,name="u[%s]"%(i))
model.update()
costo=quicksum((Cnew[i,j]*x[i,j] for i in nodos for j in nodos))
model.setObjective(costo)
model.update()
for j in nodos:
model.addConstr(quicksum(x[i,j] for i in nodos)==1.0,name="R1[%s]"%(j))
for i in nodos:
model.addConstr(quicksum(x[i,j] for j in nodos)==1.0,name="R2[%s]"%(i))
for i in nodos2:
for j in nodos2:
if i!=j:
model.addConstr(u[i]-u[j]+(len(Cnew)-1)*x[i,j]<=len(Cnew)-2,name="R3[%s][%s]"%(i,j))
model.update()
model.__data=x,u
return model
model=TSP_ruteo(Cnew)
model.optimize()
x,u=model.__data
print("")
print("VALOR OPTIMO TSP= ", model.ObjVal)
edges2 = [(i,j) for (i,j) in x if x[i,j].X == 1]
model.write('lalalalala.lp')
nonzeros=model.printAttr('x')
print("Arcos utilizados:", edges2)
print("")
My code solves The Traveling Salesman Problem (TSP), using the solver Gurobi. I want to use this list to iterate, but when I run it, I can pass this list as parameters properly. The model runs right when I pass the list manually and solve itself to the optimal solution, but that is not the idea. I think my problem is in the way I am using to pass the lists as parameters. If someone could help me it would be great. Thanks and advance.
By the way, I have already tried changing the name of the list, and all tricks that I've find on-line. Please help me. :(