I'm quite new to Python but this is strange. So, I have a matrix (a list of lists) and I pass it to a function (creaMatrixVNS).
def creaMatrixVNS(best_matrice, time):
matrice = best_matrice
sol_init_ass = assegnamento(matrice)
iteration = 1 #numero swap
counter = 0 #numero iterazioni
while True:
temp = matrice
From this point
for x in xrange(0,iteration):
temp = swap(temp, sol_init_ass)
to this point, I see that my variable matrice
changes and is set to the value of temp. And I don't know why. That's the second part of the function.
time2 = creaSoluzione(temp)
if time2 < time:
time = time2
matrice = temp
if counter == 9:
break
else:
counter += 1
else:
if iteration < 3:
iteration += 1
counter = 0
else:
break
print(time)
return matrice
Other functions:
def swap(matriceInput, lista):
result = matriceInput
i1, j1 = giveMeAcouple(lista)
i2, j2 = giveMeAcouple(lista)
while i1 == i2 and j1 == j2:
i1, j1 = giveMeAcouple(lista)
i2, j2 = giveMeAcouple(lista)
result[i1][j1], result[i2][j2] = result[i2][j2], result[i1][j1]
return result
def giveMeAcouple(sol_init_ass):
j1 = randint(0,len(sol_init_ass)-1)
while len(sol_init_ass[j1]) <= 0:
j1 = randint(0,len(sol_init_ass)-1)
i1 = randint(0,len(sol_init_ass[j1]) - 1)
return sol_init_ass[j1][i1], j1
def assegnamento(matrice):
listElementi = []
temp = []
n = len(matrice)
m = len(matrice[0])
for i in xrange(0,m): #Per ogni colonna
for j in xrange(0,n): #Per ogni riga
if matrice[j][i] != 0:
temp.append(j)
listElementi.append(temp)
temp = []
return listElementi
def creaSoluzione(matrix):
n = len(matrix)
m = len(matrix[0])
max = 0
for j in xrange(0,m):
temp = 0
for i in xrange(0,n):
temp += matrix[i][j]
#print(temp)
if temp > max:
max = temp
return max
Any hints? Thanks