Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- import copy
- import collections
- import sys
- #N = 10
- vertexCount = 10
- antCount = 100
- count_of_arcs = 0
- vertex = [[[i*antCount + j for j in range(0,antCount)],[],[]] for i in range(0, vertexCount)]
- d = []
- pheromones = []
- vertexVisited = [[] for i in range(0,antCount*antCount)]
- t = 0
- maxIter=100
- M = maxIter*100
- a1=0.5
- a2=0.5
- def algorithm():
- c1 = a1*(M-t)/M
- c2 = t/M
- c3 = a2*(M-t)/M
- for i in range(0, vertexCount):
- ants = vertex[i]
- for ant in ants[0]:
- L = []
- for k in range(0, vertexCount):
- if i==k:
- continue
- if (not (i,k) in vertexVisited[ant]) and (d[i][k]!=0):
- L.append((i,k))
- # Розрахунок показника D :
- if len(L)>0:
- Lcost = [d[g[0]][g[1]] for g in L]
- pheromones_saturation = [pheromones[g[0]][g[1]] for g in L]
- sumLcost = sum(Lcost)
- sumPher_saturation = sum(pheromones_saturation)
- maxD = -sys.maxsize
- nextL = ()
- for g in range(0,len(Lcost)):
- D = c1*(1-Lcost[g]/sumLcost)+c2*pheromones_saturation[g]/sumPher_saturation+c3*random.uniform(0,1)
- if D>maxD:
- maxD=D
- nextL = L[g]
- vertexVisited[ant].append(nextL)
- pheromones[nextL[0]][nextL[1]]+=maxIter/d[nextL[0]][nextL[1]]
- pheromones[nextL[1]][nextL[0]]+=maxIter/d[nextL[0]][nextL[1]]
- ants[0].remove(ant)
- vertex[nextL[1]][0].append(ant)
- else:
- if len(vertexVisited[ant])==count_of_arcs:
- ants[1].append(ant)
- else:
- ants[2].append(ant)
- ants[0].remove(ant)
- for i in range(0, vertexCount):
- ants = vertex[i]
- for ant in ants[1]:
- if len(vertexVisited[ant])==0:
- continue;
- elif len(vertexVisited[ant])==1:
- vertex[vertexVisited[ant][-1][0]][0].append(ant)
- else:
- vertex[vertexVisited[ant][-1][0]][1].append(ant)
- pheromones[i][vertexVisited[ant][-1][0]]+=maxIter/d[i][vertexVisited[ant][-1][0]]
- pheromones[vertexVisited[ant][-1][0]][i]+=maxIter/d[i][vertexVisited[ant][-1][0]]
- ants[1].remove(ant)
- del vertexVisited[ant][-1]
- for ant in ants[2][:]:
- if len(vertexVisited[ant])==0:
- continue;
- elif len(vertexVisited[ant])==1:
- vertex[vertexVisited[ant][-1][0]][0].append(ant)
- else:
- vertex[vertexVisited[ant][-1][0]][2].append(ant)
- pheromones[i][vertexVisited[ant][-1][0]]-=maxIter/d[i][vertexVisited[ant][-1][0]]
- pheromones[vertexVisited[ant][-1][0]][i]-=maxIter/d[i][vertexVisited[ant][-1][0]]
- ants[2].remove(ant)
- del vertexVisited[ant][-1]
- return
- # matrix=[[]]
- # matrix = [line.strip() for line in open('matrix.txt', 'r')]
- #with open('example_ant_colony.txt', 'r') as f:
- with open('matrixx.txt', 'r') as f:
- file = f.read()
- file= [line.split("\t") for line in file.split("\n")]
- for i in range(0, vertexCount):
- d.append([])
- pheromones.append([])
- for j in range(0, vertexCount):
- pheromones[i].append(0)
- d[i].append(int(file[i][j]))
- for i in range(0, vertexCount):
- for j in range(0, vertexCount-i):
- if i==j+i:
- d[i][i]=0
- continue;
- pheromones[i][j+i] = random.uniform(0.0001,0.001)
- pheromones[j+i][i] = pheromones[i][j+i]
- if d[i][j+i]!=0:
- count_of_arcs+=1
- while t<maxIter:
- algorithm()
- t+=1
- N = []
- startPoint = 0
- curStep = startPoint
- nextStep = startPoint
- print(pheromones)
- visited = [startPoint]
- while len(N)< vertexCount-1:
- maxD = -sys.maxsize
- for j in range(0,len(pheromones)):
- if (j in visited) or (curStep==j):
- continue;
- if pheromones[curStep][j]>maxD:
- maxD = pheromones[curStep][j]
- nextStep=j
- visited.append(nextStep)
- N.append((curStep,nextStep))
- curStep = nextStep
- N.append((nextStep,startPoint))
- print(N)
- print(sum([d[n[0]][n[1]] for n in N]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement