-2

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. :(

Héctor Alonso
  • 181
  • 1
  • 2
  • 12

1 Answers1

0

I think you have misspelled over here

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))

it should be

  for i in nodos:
        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))

see the first i loop should have nodos but you mistyped it nodos2

You should name your variables properly

your function takes 3 parameters you're passing only one

aman5319
  • 662
  • 5
  • 16
  • What it means "your function takes 3 parameters you're passing only one "?. Thanks, :D. – Héctor Alonso Jul 19 '18 at 08:31
  • def TSP_ruteo(Cnew,l,ll) see it takes 3 parameters Cnew,l,ll but when calling you pass only one TSP_ruteo(Cnew) – aman5319 Jul 19 '18 at 08:32
  • Change nodos2 to nodos does not fix the problem :( – Héctor Alonso Jul 19 '18 at 08:34
  • Hey their might some logical error which you need to fix yourself I told you about syntactic errors – aman5319 Jul 19 '18 at 08:36
  • I have change TSP_ruteo(Cnew) to TSP_ruteo(Cnew,l,ll) but the problem persists. :( I don't know what is happening. – Héctor Alonso Jul 19 '18 at 08:36
  • @HéctorAlonso neither do we. Please reduce the question down to an [MCVE](https://stackoverflow.com/help/mcve) because none of us can test this locally and understand exactly what we should be seeing happen. – roganjosh Jul 19 '18 at 08:37
  • I've fixed the problem, thank you all for your help you are all great. – Héctor Alonso Jul 19 '18 at 08:42
  • post the solution with proper example input constraints and output you got with proper variable and function name with code so that other can also understand that – aman5319 Jul 19 '18 at 08:43