Chularrula

aco new

Jul 22nd, 2022
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. from copy import deepcopy
  2. import numpy as np
  3. from numpy import inf
  4. import random
  5.  
  6. ant = 4
  7. iterations = 10
  8. initial_pheromone = 0.1
  9. alpha = 1
  10. beta = 1
  11. pheromone_evaporation_rate = 0.01
  12. Q = 10
  13. vertice = 4
  14.  
  15. distance_array = np.array([[0, 9, 7, 2,]
  16.                 ,[9, 0, 1, 5]
  17.                 ,[7, 1, 0, 6]
  18.                 ,[2, 5, 6, 0]])
  19. '''
  20. with open("C:/Users/guilo/Downloads/cos/sos/test.txt", "r") as txt_file:
  21.    distance_array = np.array([list(map(int, line.split())) for line in txt_file])
  22. '''
  23. ant_path = np.zeros((ant, vertice+1))
  24. best_distance = np.zeros((ant, 1))
  25.  
  26. draw_first_position = np.random.randint(0, vertice)
  27. size = len(ant_path)
  28.  
  29. for i in range(vertice):
  30.     ant_path[i][0] = draw_first_position
  31.     ant_path[i][size] = draw_first_position
  32.    
  33.  
  34. visibility = 1 / distance_array
  35. visibility[visibility == inf] = 0
  36.  
  37. pheromone = 0.1 * np.ones((ant, ant))
  38.  
  39. product_p_v = pheromone*visibility
  40.  
  41. sum = 0
  42. visibility_sum = np.zeros((vertice, 1))
  43. for i in range(vertice):
  44.     for j in range(vertice):
  45.         sum += product_p_v[i][j]
  46.     visibility_sum[i][0] = sum
  47.     sum = 0
  48.    
  49. probability = np.zeros((vertice, vertice))
  50. for i in range(vertice):
  51.     for j in range(vertice):
  52.         probability[i][j] = product_p_v[i][j] / visibility_sum[i][0]
  53.  
  54. current_position = draw_first_position
  55.  
  56. moviment_ant = 1
  57. ant_number = 0
  58.  
  59. for j in range(vertice):
  60.     sum += probability[current_position][j]
  61.  
  62. copy_probability = deepcopy(probability)
  63.  
  64. while(ant_number != ant):
  65.     while(moviment_ant != vertice):
  66.         draw = random.uniform(0, sum)
  67.         while draw == 0.0:
  68.             draw = random.uniform(0, sum)
  69.         accumulated = 0
  70.        
  71.         for j in range(vertice):
  72.             prob = copy_probability[current_position][j] / sum
  73.             accumulated += prob
  74.             if(draw <= accumulated):
  75.                 next_position = j
  76.                 j = vertice
  77.                 break
  78.            
  79.         ant_path[ant_number][moviment_ant] = next_position
  80.         moviment_ant += 1
  81.         previous_position = current_position
  82.         for i in range(vertice):
  83.             for j in range(vertice):
  84.                 if i == previous_position or j == previous_position:
  85.                     copy_probability[i][j] = 0
  86.  
  87.         current_position = next_position
  88.         sum = 0
  89.         for i in range(vertice):
  90.             sum += copy_probability[current_position][i]
  91.  
  92.     ant_number += 1
  93.     copy_probability = deepcopy(probability)
  94.     sum = 0
  95.     current_position = draw_first_position
  96.     for j in range(vertice):
  97.         sum += copy_probability[current_position][j]
  98.     moviment_ant = 1
  99.        
  100. print(ant_path)
Advertisement
Add Comment
Please, Sign In to add comment