python蚁群算法tsp代码

实现基于蚁群算法的TSP问题求解的Python代码:

```python

import random

class Ant:

def __init__(self, num_cities):

self.num_cities = num_cities

self.path = []

self.visited = [False] * num_cities

self.current_city = random.randint(0, num_cities-1)

self.path.append(self.current_city)

self.visited[self.current_city] = True

def choose_next_city(self, pheromone_matrix, alpha, beta):

probabilities = []

total_prob = 0.0

for city in range(self.num_cities):

if not self.visited[city]:

pheromone = pheromone_matrix[self.current_city][city]

attractiveness = 1.0 / distance_matrix[self.current_city][city]

probabilities.append(pheromone ** alpha * attractiveness ** beta)

total_prob += probabilities[-1]

if total_prob > 0.0:

rand = random.uniform(0, total_prob)

cum_prob = 0.0

for i, prob in enumerate(probabilities):

cum_prob += prob

if cum_prob >= rand:

self.current_city = i

self.visited[i] = True

self.path.append(i)

break

def path_length(self):

length = 0.0

for i in range(self.num_cities - 1):

length += distance_matrix[self.path[i]][self.path[i+1]]

length += distance_matrix[self.path[-1]][self.path[0]]

return length

class ACO:

def __init__(self, num_ants, num_iterations, alpha, beta, rho, Q):

self.num_ants = num_ants

self.num_iterations = num_iterations

self.alpha = alpha # 控制信息素的重要程度

self.beta = beta # 控制启发式信息的重要程度

self.rho = rho # 信息素挥发速度

self.Q = Q # 常系数

self.best_path = []

self.best_path_length = float('inf')

def solve(self, distance_matrix):

global distance_matrix

num_cities = len(distance_matrix)

pheromone_matrix = [[1.0 / num_cities] * num_cities for _ in range(num_cities)]

for iteration in range(self.num_iterations):

ants = [Ant(num_cities) for _ in range(self.num_ants)]

for ant in ants:

for _ in range(num_cities - 1):

ant.choose_next_city(pheromone_matrix, self.alpha, self.beta)

length = ant.path_length()

if length < self.best_path_length:

self.best_path = ant.path

self.best_path_length = length

self.update_pheromone(pheromone_matrix)

def update_pheromone(self, pheromone_matrix):

for i in range(len(pheromone_matrix)):

for j in range(len(pheromone_matrix[i])):

pheromone_matrix[i][j] *= self.rho

for ant in self.num_ants:

for i in range(len(ant.path) - 1):

pheromone_matrix[ant.path[i]][ant.path[i+1]] += self.Q / ant.path_length()

for i in range(len(pheromone_matrix)):

for j in range(len(pheromone_matrix[i])):

pheromone_matrix[i][j] = min(pheromone_matrix[i][j], 2.0)

if __name__ == '__main__':

distance_matrix = [

[0, 29, 20, 21, 16, 31, 100, 12, 4, 31, 18, 19, 8, 25, 20],

[29, 0, 15, 29, 28, 40, 72, 21, 29, 41, 12, 11, 34, 13, 25],

[20, 15, 0, 15, 14, 25, 81, 9, 23, 27, 13, 12, 21, 18, 16],

[21, 29, 15, 0, 4, 12, 92, 12, 25, 13, 25, 13, 24, 20, 25],

[16, 28, 14, 4, 0, 16, 94, 9, 20, 16, 22, 9, 20, 24, 20],

[31, 40, 25, 12, 16, 0, 95, 24, 36, 33, 37, 26, 28, 39, 35],

[100, 72, 81, 92, 94, 95, 0, 90, 101, 99, 84, 89, 88, 97, 94],

[12, 21, 9, 12, 9, 24, 90, 0, 15, 25, 13, 21, 12, 2, 12],

[4, 29, 23, 25, 20, 36, 101, 15, 0, 35, 18, 17, 28, 12, 25],

[31, 41, 27, 13, 16, 33, 99, 25, 35, 0, 32, 18, 30, 21, 46],

[18, 12, 13, 25, 22, 37, 84, 13, 18, 32, 0, 9, 26, 14, 29],

[19, 11, 12, 13, 9, 26, 89, 21, 17, 18, 9, 0, 22, 12, 27],

[8, 34, 21, 24, 20, 28, 88, 12, 28, 30, 26, 22, 0, 25, 10],

[25, 3, 18, 20, 24, 39, 97, 2, 12, 21, 14, 12, 25, 0, 15],

[20, 25, 16, 25, 20, 35, 94, 12, 25, 46, 29, 27, 10, 15, 0]

]

num_ants = 10

num_iterations = 100

alpha = 1.0

beta = 2.0

rho = 0.5

Q = 100.0

aco = ACO(num_ants, num_iterations, alpha, beta, rho, Q)

aco.solve(distance_matrix)

print("Best Path:", aco.best_path)

print("Best Path Length:", aco.best_path_length)

```

这是一个用Python实现的蚁群算法求解TSP(旅行商问题)的代码。TSP问题是在给定一组城市和城市之间的距离,找到一条经过每个城市恰好一次的最短路径。

蚁群算法是一种基于模拟蚂蚁行为的启发式搜索算法。它模拟了真实蚂蚁在寻找食物的过程中释放信息素、找到最短路径的行为。蚁群算法通过蚂蚁的行为与信息素更新来逐步搜索空间,并最终找到最优解。

在这段代码中,Ant类表示一个蚂蚁的属性和行为。初始化时,蚂蚁随机选择一个起始城市,并将其设置为已访问。然后,它通过选择下一个要访问的城市来决定下一步的行动。选择的城市是基于信息素和启发式信息计算得出的概率。概率越高,选择的可能性就越大。选择完成后,更新路径和已访问城市的信息。

ACO类表示整个蚁群算法的过程。它具有解决TSP问题的方法solve和更新信息素的方法update_pheromone。在solve方法中,先生成一组蚂蚁,然后每个蚂蚁根据信息素和启发式信息选择下一个要访问的城市,直到所有城市都被访问完毕。在每次迭代中,记录最优路径和路径长度。在update_pheromone方法中,对信息素进行更新,包括挥发信息素和释放信息素。更新后的信息素还要进行限制,以确保不超过一定范围。

通过调整参数,如蚂蚁数量、迭代次数、信息素重要程度和启发式信息重要程度等,可以进一步改进算法的性能和结果。这就是蚁群算法的优势之一,它是一种自适应的算法,能够根据问题和数据进行调整。

总结起来,蚁群算法是一种基于蚂蚁行为的启发式搜索算法,适用于TSP问题等组合优化问题的求解。它通过模拟蚂蚁的行为和信息素更新来搜索解空间,最终找到最优解。蚁群算法的实现可以通过编写类似上述代码的程序来完成。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.ynyuzhu.com/

点赞(65) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部