I'm trying to implement the ant colony method to solve TSP problem, and I'm having trouble creating the paths for the ants. The ant need to visit all the vertex and return to the origin vertex, without repeating the vertices. I tried to do a verification the prevent that the ant visit a already visited vertix, but it isnt working.
How can I prevent that the ant visit a already visited vertex?
My full code if need: https://pastebin.com/cDFB1JZL
The part of the code that i deal with the ant path:
current_position = draw_first_position
k = 1
k0 = 0
for j in range(vertice):
sum += probability[current_position][j]
while(k != 3):
draw = random.uniform(0, sum)
accumulated = 0
for j in range(vertice-1):
prob = probability[current_position][j] / sum
accumulated += prob
if(draw <= accumulated):
next_position = j
break
ant_path[k0][k] = next_position
current_position = next_position
sum = 0
#skip the vertex already visited
for j in range(vertice):
for i in range(len(ant_path)):
if(j != ant_path[k0][i]):
sum += probability[current_position][j]
k += 1
print(ant_path)
If information is lacking from my question, then please let me know. Thanks in advance!