Here is the function that I receive the warning from (when v.varValue >= 1, it says "'>=' not supported between instances of 'NoneType' and 'int')
def print_prob(prob, timelimit):
for v in prob.variables():
if (v.varValue >= 1):
print(v.name, "=", v.varValue)
print(value(prob.objective))
if timelimit >= 0:
print("Tempo de execução restante:", timelimit)
print("Tempo de execução total:", max_time - timelimit)
else:
print("O ótimo não foi encontrado com o tempo dado.")
#print(prob)
return
What I'm doing in a global way is going through a list of files, each file having different sizes and different values. I open the file, get what I need from it (size and matrix of costs), assign it to some variables and send them to a function. This function creates other variables, including the 'v', using the variables I've got from the file (basically making a dictionary/list of lists), then I try solving a function using this v and a bunch of other restrictions. After solving it, I try to print it along with the variables which got "chosen" (if v[i][j] >= 1, then it's chosen and should be printed).
def testing_all_3(i):
#these are the file names
file = ['wi29', 'dj38', 'qa194', 'uy734', 'zi929', 'lu980', 'rw1621', 'mu1979', 'nu3496', 'ca4663']
print("===============")
print("Testando o arquivo", file[i] + ".tsp")
#I open the current file using a special function
#(the file type is TSP, which has its own library to work with)
bench = _tspfile(file[i] + ".tsp")
n = bench.dimension
#n = testing_all_3(i)
#I use the data that I got from the file, making a list of nodes
#(which I represent with numbers)
cidades, destinos = _append(n)
#the weight of each node that I transform into a dictionary for Pulp
costs = _tsp(n, bench)
costs = makeDict([cidades, destinos], costs, 0)
#prob = creating(cidades, destinos)
#Then I call two function that are very similar for solving the problem
print("Resolvendo com MTZ")
solvingMTZ(cidades, destinos, n, costs, file[i])
print("===============")
print("Testando o arquivo", file[i] + ".tsp")
print("Resolvendo com DL")
solvingDL(cidades, destinos, n, costs, file[i])
for i in range(5,10):
#I would make this range from 0 to 9
#but I only get the warning at the 4th and beyond file
testing_all_3(i)
This is the function where the other functions are called and variables are created
def solvingMTZ(cidades, destinos, n, costs, file):
prob = LpProblem("Teste", LpMinimize)
road = [(cid_i, cid_j) for cid_i in cidades for cid_j in destinos]
var = LpVariable.dicts("Road", (cidades, destinos), 0, 1, LpInteger)
prob += lpSum([var[cid_i][cid_j] * costs[cid_i][cid_j] for (cid_i, cid_j) in road]) #função obj
for i in cidades:
prob += lpSum([var[i][cid_j] for cid_j in destinos if i != cid_j]) == 1 #única entrada
for j in destinos:
prob += lpSum([var[cid_i][j] for cid_i in cidades if j != cid_i]) == 1 #única saída
prob = mtz(n, prob, cidades, var)
timelimit = max_time
start = time.time()
prob.solve(GUROBI_CMD(timeLimit=timelimit, msg=1, gapRel=0))
end = time.time()
timelimit = timelimit - end + start
print(timelimit)
#imprimo os valores das variáveis e o resultado final da função
print_prob(prob, timelimit)
make_node_fileMTZ(prob, file, timelimit)
return
#the function I called to help solving the problem
#the DL one (solvingDL) is nearly identical, so I didn't add it here
def mtz(n, prob, cidades, var):
u = LpVariable.dicts('u', (i for i in cidades), 1, n-1, LpInteger)
for i in cidades[1:]:
for j in cidades[1:]:
#if i != j:
prob += u[i] - u[j] + (n - 1) * var[i][j] <= n - 2
return prob
I'm currently coding on python and I started to get this problem "'>=' not supported between instances of 'NoneType' and 'int'". Basically, there's a dictionary from Pulp, called 'v' for variable, and I try printing out each one of its contents, saying "if this value is >= 1, then print the variable name and its value".
The real thing is, I don't know how this get a None type, because it's not the first time I'm running the code like this, I actually run the same thing with 3 different sizes and all of them work, but at the 4th time and beyond it keeps stating this warning. I looked upon other similar stuff, but I don't think I understand it quite well, like, why this is getting a None "after I increase the number of variables", but it does just fine before that?
Thanks in advance for any advice you may have, kind stranger