Skip to content

Instantly share code, notes, and snippets.

@kamalbanga
Last active March 29, 2017 10:23
Show Gist options
  • Save kamalbanga/9791119 to your computer and use it in GitHub Desktop.
Save kamalbanga/9791119 to your computer and use it in GitHub Desktop.
To find TSP through Ant Colony Optimization
import random
import math
seed = 1
boost = 5
iter = 3
numCities = 4
maxDistance = 40000
upperX = 24000
upperY = 32000
upperDemand = 1200
def randomMatrix(n, upperBound, seed):
random.seed(seed)
cities = []
for r in range(n):
sm = []
cities.append(sm)
for c in range(n):
sm.append(upperBound*random.random())
return cities
def sampleMatrix():
return [[0,10,15,100],[100,0,100,5],[15,100,0,5],[100,5,5,0]]
def cityMatrix(n, seed):
random.seed(seed)
cityC = []
for r in range(n):
coordinate = []
coordinate.append(upperX*random.random())
coordinate.append(upperY*random.random())
cityC.append(coordinate)
cities = [[None for _ in range(n)] for _ in range(n)]
for r in range(n):
for c in range(n):
cities[r][c] = math.sqrt(math.pow((cityC[r][0]-cityC[c][0]),2) +
math.pow((cityC[r][1] - cityC[c][1]),2))
return cities
def Demand(n,upperDemand):
demand = []
for _ in range(n):
demand.append(upperDemand*random.random())
return demand
def pathLength(cities,path):
return sum([cities[r][c] for (r,c) in zip(path,path[1:]+[path[0]])])
def updatePher(pher,path,boost):
for (r,c) in zip(path,path[1:]+[path[0]]):
pher[r][c] = pher[r][c] + boost
def evaporatePher(pher,maxIter,boost):
decr = boost/float(maxIter)
for r in range(len(pher)):
for c in range(len(pher[r])):
if pher[r][c] > decr:
pher[r][c] = pher[r][c] - decr
else:
pher[r][c] = 0.0
def doSumWeight(cities, pher, used, current):
runningTotal = 0.0
for city in range(len(cities)):
if not used.has_key(city):
runningTotal = (runningTotal +
cities[current][city]*(1.0+pher[current][city]))
return runningTotal
def findSumWeight(cities, pher, used, current, soughtTotal):
runningTotal = 0.0
next = 0
for city in range(len(cities)):
if runningTotal >= soughtTotal:
break
if not used.has_key(city):
runningTotal = (runningTotal +
cities[current][city]*(1.0+pher[current][city]))
next = city
return next
def genPath(cities, pher):
#current = random.randint(0,len(cities)-1)
current = 0
path = [current]
used = {current:1}
while len(used) < len(cities):
sumWeight = doSumWeight(cities,pher,used,current)
rndValue = random.random()*sumWeight
current = findSumWeight(cities,pher,used,current,rndValue)
path.append(current)
used[current] = 1
return path
def bestPath(cities,seed,maxIter,boost):
pher = randomMatrix(len(cities),0,0)
random.seed(seed)
bestLen = numCities*maxDistance
bestPath = []
for iter in range(maxIter):
print (iter,bestLen)
path = genPath(cities,pher)
pathLen = pathLength(cities,path)
if pathLen < bestLen:
updatePher(pher,path,boost)
bestLen = pathLen
bestPath = path
evaporatePher(pher,maxIter,boost)
return bestPath
def main():
print "starting"
#cities = randomMatrix(numCities,maxDistance,seed)
cities = cityMatrix(numCities,seed)
path = bestPath(cities,seed,iter,boost)
print path
print "len = ",pathLength(cities,path)
if __name__ == "__main__":
main()
@kamalbanga
Copy link
Author

@anubhavthakur25
Copy link

how to run this program..can u explain please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment