Advertisement
Guest User

weqd

a guest
Jan 19th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.44 KB | None | 0 0
  1. import random
  2. import math
  3. import copy
  4. import collections
  5. import sys
  6.  
  7. #N = 10
  8.  
  9.  
  10. vertexCount = 10
  11. antCount = 100
  12. count_of_arcs = 0
  13.  
  14. vertex = [[[i*antCount + j for j in range(0,antCount)],[],[]] for i in range(0, vertexCount)]
  15.  
  16.  
  17. d = []
  18.  
  19. pheromones = []
  20.  
  21. vertexVisited = [[] for i in range(0,antCount*antCount)]
  22.  
  23. t = 0
  24. maxIter=100
  25. M = maxIter*100
  26. a1=0.5
  27. a2=0.5
  28. def algorithm():
  29.  
  30.     c1 = a1*(M-t)/M
  31.     c2 = t/M
  32.     c3 = a2*(M-t)/M
  33.  
  34.     for i in range(0, vertexCount):
  35.         ants = vertex[i]
  36.         for ant in ants[0]:
  37.             L = []
  38.             for k in range(0, vertexCount):
  39.                 if i==k:
  40.                     continue
  41.                 if (not (i,k) in vertexVisited[ant]) and (d[i][k]!=0):
  42.                     L.append((i,k))
  43.             # Розрахунок показника D :
  44.             if len(L)>0:
  45.                 Lcost = [d[g[0]][g[1]] for g in L]
  46.                 pheromones_saturation = [pheromones[g[0]][g[1]] for g in L]
  47.                 sumLcost = sum(Lcost)
  48.                 sumPher_saturation = sum(pheromones_saturation)
  49.                 maxD = -sys.maxsize
  50.                 nextL = ()
  51.                 for g in range(0,len(Lcost)):
  52.                     D = c1*(1-Lcost[g]/sumLcost)+c2*pheromones_saturation[g]/sumPher_saturation+c3*random.uniform(0,1)
  53.                     if D>maxD:
  54.                         maxD=D
  55.                         nextL = L[g]
  56.                 vertexVisited[ant].append(nextL)
  57.                 pheromones[nextL[0]][nextL[1]]+=maxIter/d[nextL[0]][nextL[1]]
  58.                 pheromones[nextL[1]][nextL[0]]+=maxIter/d[nextL[0]][nextL[1]]
  59.                 ants[0].remove(ant)
  60.                 vertex[nextL[1]][0].append(ant)
  61.             else:
  62.                 if len(vertexVisited[ant])==count_of_arcs:
  63.                     ants[1].append(ant)
  64.                 else:
  65.                     ants[2].append(ant)
  66.                 ants[0].remove(ant)
  67.  
  68.     for i in range(0, vertexCount):
  69.         ants = vertex[i]
  70.         for ant in ants[1]:
  71.             if len(vertexVisited[ant])==0:
  72.                 continue;
  73.             elif len(vertexVisited[ant])==1:
  74.                 vertex[vertexVisited[ant][-1][0]][0].append(ant)
  75.             else:
  76.                 vertex[vertexVisited[ant][-1][0]][1].append(ant)
  77.  
  78.             pheromones[i][vertexVisited[ant][-1][0]]+=maxIter/d[i][vertexVisited[ant][-1][0]]
  79.             pheromones[vertexVisited[ant][-1][0]][i]+=maxIter/d[i][vertexVisited[ant][-1][0]]
  80.             ants[1].remove(ant)
  81.             del vertexVisited[ant][-1]
  82.         for ant in ants[2][:]:
  83.             if len(vertexVisited[ant])==0:
  84.                 continue;
  85.             elif len(vertexVisited[ant])==1:
  86.                 vertex[vertexVisited[ant][-1][0]][0].append(ant)
  87.             else:
  88.                 vertex[vertexVisited[ant][-1][0]][2].append(ant)
  89.  
  90.             pheromones[i][vertexVisited[ant][-1][0]]-=maxIter/d[i][vertexVisited[ant][-1][0]]
  91.             pheromones[vertexVisited[ant][-1][0]][i]-=maxIter/d[i][vertexVisited[ant][-1][0]]
  92.             ants[2].remove(ant)
  93.             del vertexVisited[ant][-1]
  94.     return
  95.  
  96. # matrix=[[]]
  97. # matrix = [line.strip() for line in open('matrix.txt', 'r')]
  98.  
  99. #with open('example_ant_colony.txt', 'r') as f:
  100. with open('matrixx.txt', 'r') as f:
  101.     file = f.read()
  102.  
  103. file= [line.split("\t")  for line in file.split("\n")]
  104.  
  105.  
  106.  
  107. for i in range(0, vertexCount):
  108.     d.append([])
  109.     pheromones.append([])
  110.     for j in range(0, vertexCount):
  111.         pheromones[i].append(0)
  112.         d[i].append(int(file[i][j]))
  113.  
  114.  
  115.  
  116. for i in range(0, vertexCount):
  117.         for j in range(0, vertexCount-i):
  118.             if i==j+i:
  119.                 d[i][i]=0
  120.                 continue;
  121.             pheromones[i][j+i] = random.uniform(0.0001,0.001)
  122.             pheromones[j+i][i] = pheromones[i][j+i]
  123.             if d[i][j+i]!=0:
  124.                 count_of_arcs+=1
  125.  
  126.  
  127.  
  128. while t<maxIter:
  129.     algorithm()
  130.     t+=1
  131.  
  132.  
  133. N = []
  134.  
  135. startPoint = 0
  136.  
  137. curStep = startPoint
  138. nextStep = startPoint
  139. print(pheromones)
  140. visited = [startPoint]
  141. while len(N)< vertexCount-1:
  142.     maxD = -sys.maxsize
  143.     for j in range(0,len(pheromones)):
  144.         if (j in visited) or (curStep==j):
  145.             continue;
  146.         if pheromones[curStep][j]>maxD:
  147.             maxD = pheromones[curStep][j]
  148.             nextStep=j
  149.     visited.append(nextStep)
  150.     N.append((curStep,nextStep))
  151.     curStep = nextStep
  152. N.append((nextStep,startPoint))
  153. print(N)
  154. print(sum([d[n[0]][n[1]] for n in N]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement