0
def update_basis(A, basis, i, j):
    for k, var in enumerate(basis):
        idx = int(var[1:])
        if A[i][j] == 1:
            basis[k] = "x" + str(j+1)
            break
    return basis

I wrote the above code, and I am getting error as stated. I even tried range(enumerate(basis)), after reading one of the answers here. That too doesn't seem to work. How do I get around this? PS. I took this code from - https://github.com/pyaf/operations-research/blob/master/simplex-method/utils.py I know there are many similar questions on this, but I just cant get one that answers me problem.

Full traceback error:
TypeError                                 Traceback (most recent call last)
<ipython-input-7-9809e74f4f64> in <module>
    120     print("\nIteration number : %d" % iter_num)
    121     #updating basis as variables enter and leave
--> 122     basis= update_basis(i,j,basis,nonbasic)
    123     #updating table
    124     A,b,c= row_operations(A,b,c,i,j)

<ipython-input-7-9809e74f4f64> in update_basis(A, basis, i, j)
     76 
     77 def update_basis(A, basis, i, j):
---> 78     for k, var in enumerate(basis):
     79         idx = int(var[1:])
     80         if A[i][j] == 1:

TypeError: 'int' object is not iterable
huy
  • 176
  • 2
  • 13

2 Answers2

0

Your update_basis function is defined in https://github.com/pyaf/operations-research/blob/master/simplex-method/utils.py, and then used in https://github.com/pyaf/operations-research/blob/master/simplex-method/simplex.py, where one can see that basis is expected to be an array / list. So, the error will disappear if you pass a list as second argument, instead of a number.
EDIT: I think now that this

basis = update_basis(i, j, basis, nonbasic)

is your problem. You mixed up the order of the arguments. In the function definition, they are like this:

def update_basis(A, basis, i, j):

So, it should work if you change line 122 to:

basis = update_basis(nonbasic, basis, i, j)
TheEagle
  • 5,808
  • 3
  • 11
  • 39
0

The reason you get this error is because the function update_basis is called with the incorrect signature. Somewhere in this code (which you will see in the error message), an int is passed as the basis parameter, when this in fact should be an iterable collection. The problem is not in the function itself, but rather where it is called.

So to solve it, find where the function is called that produces this error and correct the argument

Marksverdhei
  • 31
  • 1
  • 1
  • could you look at the entire code once? because I'm not able to see the error – huy Jan 14 '21 at 14:33
  • Look at @MisterMiyagi 's answer. Instead of calling it with `update_basis(i,j,basis,nonbasic)` you should probaly call it with this way: `update_basis(nonbasic, basis, i,j)` – Marksverdhei Jan 14 '21 at 14:50