0

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!

Thorsen
  • 53
  • 5

1 Answers1

0

i solved it by creating a copy of my propability matrix to mark the visited vertex with 0, so after a ant finish his decision making process i pass the original probability matrix to the copy to repeat the process for the next ant.

moviment_ant = 1
ant_number = 0
for j in range(vertice):
    sum += probability[current_position][j]
copy_probability = deepcopy(probability)
while(ant_number != ant):
    while(moviment_ant != vertice): 
        draw = random.uniform(0, sum)
        while draw == 0.0:
            draw = random.uniform(0, sum)
        accumulated = 0
        for j in range(vertice):
            prob = copy_probability[current_position][j] / sum
            accumulated += prob
            if(draw <= accumulated):
                next_position = j
                j = vertice
                break          
        ant_path[ant_number][moviment_ant] = next_position
        moviment_ant += 1
        previous_position = current_position
        for i in range(vertice):
            for j in range(vertice):
                if i == previous_position or j == previous_position:
                    copy_probability[i][j] = 0
        current_position = next_position
        sum = 0
        for i in range(vertice):
            sum += copy_probability[current_position][i]
    ant_number += 1
    copy_probability = deepcopy(probability)
    sum = 0
    current_position = draw_first_position
    for j in range(vertice):
        sum += copy_probability[current_position][j]
    moviment_ant = 1      
print(ant_path)

my full code if anyone wants to see: https://pastebin.com/TwpamksP

Thorsen
  • 53
  • 5