本文整理汇总了Python中Graph.fromTSPFile方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.fromTSPFile方法的具体用法?Python Graph.fromTSPFile怎么用?Python Graph.fromTSPFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.fromTSPFile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import fromTSPFile [as 别名]
def main():
'''
Find a close solution to traveling salesman problem
through genetic inver-over algorithm
'''
#variable delcaration
populationSize = 100
probability = .07
filename = "cites.tsp"
aGraph = Graph.fromTSPFile(filename)
#aGraph = Graph.Graph(["one","two","three","four","five","six"],3,1.0)
population = []
path = aGraph.getNames()
stable = 0
repeat = True
bestDistance = None
oldBestDistance = None
bestPath = []
oldBestPath = []
inversions = 0
iterations = 0
#Build the population
for i in range(0, populationSize):
shuffle(path)
population.append(copy.copy(path))
print aGraph
#Loop until same results occur 10 times across total population
while (stable < 10):
iterations = iterations + 1
#For each path, pick a city and flip sub paths to another city
for i in range (0, len(population)):
testPath = copy.copy(population[i])
city = testPath[random.randint(0, len(testPath) - 1)]
repeat = True;
bestDistance = None
#While swaps can be made
while repeat:
#Possibly mutate and pick a city at random
if (random.random() <= probability):
otherCity = testPath[random.randint(0, len(testPath) - 1)]
while (city == otherCity or (city == 6 and otherCity == 15) or (city == 13 and otherCity == 3) or (city == 11 and otherCity == 18) or (city == 1 and otherCity == 7) or (city == 9 and otherCity == 16)):
otherCity = testPath[random.randint(0, len(testPath) - 1)]
#Otherwise look for solution from another path at random
#By finding a node next to city
else:
otherPath = population[random.randint(0, len(population) - 1)]
#If city is at the end of the other path, cycle to the first node
if (otherPath.index(city) == (len(otherPath) - 1)):
otherCity = otherPath[0]
#Else, grab the next city
else:
otherCity = otherPath[otherPath.index(city) + 1]
#If the cities to flip around are next to eachother, stop
if ((testPath.index(otherCity) + 1 == testPath.index(city)) or
(testPath.index(otherCity) - 1 == testPath.index(city)) or
((testPath.index(otherCity) == 0) and (testPath.index(city) == len(testPath) - 1)) or
((testPath.index(city) == 0) and (testPath.index(otherCity) == len(testPath) - 1))):
repeat = False
#Otherwise, flip the subpath
else:
testPath = copy.copy(flip(testPath, testPath.index(city), testPath.index(otherCity)))
inversions = inversions + 1
#Updates the path to faster version if needed
if (evaluatePath(aGraph, testPath) <= evaluatePath(aGraph, population[i])):
population[i] = copy.copy(testPath)
#Updates the fastest distance and path for this interation of the
#population if needed
if (bestDistance == None) or (evaluatePath(aGraph, population[i]) < bestDistance):
bestDistance = evaluatePath(aGraph, population[i])
bestPath = copy.copy(population[i])
#Updates the fastest distance and path for all iterations of the population
#so far, if needed.
if (oldBestDistance == None) or (bestDistance < oldBestDistance):
oldBestDistance = bestDistance
oldBestPath = copy.copy(bestPath)
stable = 0
#If no faster path is found increment the stable counter (exit condition)
else:
stable += 1
print "working"
#.........这里部分代码省略.........
示例2: heappush
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import fromTSPFile [as 别名]
heappush(priorityQueue, newNode)
else: # Nothing left in priority queue is better so we're done!
return (visitedNodes, optimalTour)
# We've explored all the nodes in the priority queue and found none
# really better, so return the best we found. Note that if we
# didn't find ANY tour, then optimalTour will be None - an
# appropriate value to return anyway
return (visitedNodes, optimalTour)
if __name__ == '__main__':
# Create a random graph with 7 vertices. 3 is the random seed for
# populating the graph and 1.0 says it has 100% density so it's fully
# connected - less dense graphs may or may not have a tour!
aGraph = Graph.fromTSPFile("eil51.tsp")
print aGraph
# Run the Traveling Salesperson on the graph and find out how many
# nodes we visited
(visited, solution) = travelingSalesperson(aGraph)
print 'Visisted ', visited, ' nodes'
if solution:
print 'Shortest tour is ',solution.pathLength, ' long:'
print solution.path
else:
print 'No tour found'