我必须用启发式算法解决TSP,我发现了这个:https://gist.github.com/westphahl/432876我创建了自己的变体:推销员现在去了最近的城市......但是重复不起作用。在某些方面,推销员只在两个城市之间进行,我有无限循环。请帮忙;)
routes = []
def find_paths_closest(node, cities, path, distance):
path.append(node)
if len(path) > 1:
distance += cities[path[-2]][node]
# If path contains all cities and is not a dead end,
# add path from last to first city and return.
if (len(cities) == len(path)) and (cities[path[-1]].has_key(path[0])):
global routes
path.append(path[0])
distance += cities[path[-2]][path[0]]
print path, distance
routes.append([distance, path])
return
minimum = 0
for i in range(1, len(cities[node])):
if cities[node].keys()[i] not in path:
if cities[node].values()[minimum] > cities[node].values()[i]:
minimum = i
newnode = cities[node].keys()[minimum]
find_paths_closest(newnode, dict(cities), list(path), distance)
def find_paths_brute_force(node, cities, path, distance):
# Add way point
path.append(node)
# Calculate path length from current to last node
if len(path) > 1:
distance += cities[path[-2]][node]
# If path contains all cities and is not a dead end,
# add path from last to first city and return.
if (len(cities) == len(path)) and (cities[path[-1]].has_key(path[0])):
global routes
path.append(path[0])
distance += cities[path[-2]][path[0]]
print path, distance
routes.append([distance, path])
return
# Fork paths for all possible cities not yet used
for city in cities:
if (city not in path) and (cities[city].has_key(node)):
find_paths_brute_force(city, dict(cities), list(path), distance)
if __name__ == '__main__':
cities = {
'RV': {'S': 195, 'UL': 102, 'M': 178, 'BA': 180, 'Z': 91},
'UL': {'RV': 86, 'S': 107, 'N': 171, 'M': 123},
'M': {'RV': 178, 'UL': 123, 'N': 170},
'S': {'RV': 195, 'UL': 107, 'N': 210, 'F': 210, 'MA': 135, 'KA': 64},
'N': {'S': 210, 'UL': 171, 'M': 170, 'MA': 230, 'F': 230},
'F': {'N': 230, 'S': 210, 'MA': 85},
'MA': {'F': 85, 'N': 230, 'S': 135, 'KA': 67},
'KA': {'MA': 67, 'S': 64, 'BA': 191},
'BA': {'KA': 191, 'RV': 180, 'Z': 85, 'BE': 91},
'BE': {'BA': 91, 'Z': 120},
'Z': {'BA': 120, 'BE': 85, 'RV': 81}
}
print "Start: RAVENSBURG"
find_paths_brute_force('RV', cities, [], 0)
print "\n"
routes.sort()
print "Shortest route: %s" % routes[0]