-1

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

  • 1
    Please [edit] your question to show an example code snippet that causes the error you are asking about. – Code-Apprentice Jan 16 '23 at 22:03
  • 3
    To understand what `NoneType` is, check here: https://stackoverflow.com/questions/21095654/what-is-a-nonetype-object. This won't exactly solve your current problem. Note that I googled "python what is nonetype" to find this (basically the words in your title). – Code-Apprentice Jan 16 '23 at 22:04
  • 1
    You are alluding to your code but not actually showing it. – John Coleman Jan 16 '23 at 22:05
  • I recommend that you copy/paste the error message into Google. You will find a lot of examples of people that encountered the same problem. Hopefully you will also be able to use their solutions to fix it. – Code-Apprentice Jan 16 '23 at 22:06
  • There is no name in the built-in scope bound to this type. You can get a reference to it, though, with either `type(None)` or via the `types` module with `types.NoneType`. – chepner Jan 16 '23 at 22:32

1 Answers1

1

So the None type exists because everything in Python is an object, and all Python functions return an object, so you need an object that represents "no object", which is why there's a None object.

Hard to answer the rest of your question without seeing the code, but the exception will lead you to the exact line of code where it's happening.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • I'm not sure you actually *need* a dedicated type. The vast majority of things that use `None` would work the same with an ordinary name `None = object()`. – chepner Jan 16 '23 at 22:35
  • @chepner you don't need it much like you don't really *need* a distinction between signed and unsigned integers, but often it is useful to express salient information through the type system. A distinct type turns out to be useful in subtle emergent permeating ways - for example, it let's things like dynamic dispatch (c.f. `singledispatch` in the standard library) properly dispatch `None` as its own unique case, it means that `isinstance(foo, type(bar))` is actually useful when `bar` happens to be `None`, and composes with constructing instances from `type(foo)` or `class_or_factory_function`. – mtraceur Jan 16 '23 at 23:18
  • @chepnerI suppose it depends on how you interpret "needs". `None` is _bottom_ in the type lattice. If you have nothing for the bottom of the type lattice you need something to serve by convention, as with C NULL being((void*)0). – Charlie Martin Feb 04 '23 at 18:15