旅行商问题 (TSP) 的蚁群算法实现

问题描述: 假设有一个旅行商人要访问全国 31 个城市,他需要选择所要走的路径,路径的限制是每个城市只能拜访一次,最后要回到原来出发的城市。路径的选择要求是:所选路径的里程为所有路径之中的最小值。

31 个城市的坐标为:[1304, 2312; 3639, 1315; 4177, 2244; 3712, 1399; 3488, 1535; 3326, 1556; 3238, 1229; 4196, 1044; 4312, 790; 4386, 570; 3007, 1970; 2562, 1756; 2788, 1491; 2381, 1676; 1332, 695; 3715, 1678; 3918, 2179; 4061, 2370; 3780, 2212; 3676, 2578; 4029, 2838; 4263, 2931; 3429, 1908; 3507, 2376; 3394, 2643; 3439, 3201; 2935, 3240; 3140, 3550; 2545, 2357; 2778, 2826; 2370, 2975]

基本蚁群算法 (AS) 实现:

1. 算法步骤:

  • 初始化参数: 设置城市数量、蚂蚁数量、信息素重要程度因子、启发式因子、信息素蒸发系数、信息素增量常数以及最大迭代次数等参数。
  • 初始化信息素矩阵: 初始化所有城市之间的信息素值为 1。
  • 迭代过程:
    • 路径构建: 每个蚂蚁从随机城市出发,根据信息素浓度和启发式信息选择下一个城市,直到访问所有城市并回到起始城市。
    • 路径长度计算: 计算每个蚂蚁所构建路径的总长度。
    • 更新最佳路径: 记录当前迭代中所有蚂蚁路径中最短的路径,并更新最佳路径长度。
    • 信息素更新: 每个蚂蚁在其路径上增加信息素,信息素量与路径长度成反比,同时进行信息素蒸发。
  • 输出结果: 输出迭代曲线、算法最终寻找到的最佳周游路线及其路线长度。

2. 代码实现:

import numpy as np

# 初始化参数
num_cities = 31  # 城市数量
num_ants = 10  # 蚂蚁数量
alpha = 1  # 信息素重要程度因子
beta = 5  # 启发式因子
rho = 0.5  # 信息素蒸发系数
Q = 100  # 信息素增量常数
max_iter = 100  # 最大迭代次数

# 城市坐标
city_coords = np.array([[1304, 2312], [3639, 1315], [4177, 2244], [3712, 1399], [3488, 1535], [3326, 1556], [3238, 1229],
                        [4196, 1044], [4312, 790], [4386, 570], [3007, 1970], [2562, 1756], [2788, 1491], [2381, 1676],
                        [1332, 695], [3715, 1678], [3918, 2179], [4061, 2370], [3780, 2212], [3676, 2578], [4029, 2838],
                        [4263, 2931], [3429, 1908], [3507, 2376], [3394, 2643], [3439, 3201], [2935, 3240], [3140, 3550],
                        [2545, 2357], [2778, 2826], [2370, 2975]])

# 计算两个城市之间的距离
def distance(city1, city2):
    x1, y1 = city1
    x2, y2 = city2
    return np.sqrt((x2 - x1)**2 + (y2 - y1)**2)

# 初始化信息素矩阵
pheromone = np.ones((num_cities, num_cities))

# 迭代过程
best_distance = np.inf
best_path = []
for iteration in range(max_iter):
    # 蚂蚁路径构建
    paths = np.zeros((num_ants, num_cities), dtype=int)  # 存储蚂蚁路径

    for ant in range(num_ants):
        current_city = np.random.randint(num_cities)  # 随机选择起始城市
        unvisited_cities = set(range(num_cities))
        unvisited_cities.remove(current_city)

        for step in range(num_cities - 1):
            # 选择下一个城市
            prob = np.zeros(len(unvisited_cities))
            for i, city in enumerate(unvisited_cities):
                prob[i] = (pheromone[current_city, city]**alpha) * (1 / distance(city_coords[current_city], city_coords[city])**beta)
            prob /= np.sum(prob)
            next_city = list(unvisited_cities)[np.random.choice(range(len(unvisited_cities)), p=prob)]

            # 更新路径和未访问城市集合
            paths[ant, step] = next_city
            unvisited_cities.remove(next_city)
            current_city = next_city

        # 最后一步回到起始城市
        paths[ant, -1] = list(unvisited_cities)[0]

    # 计算每个蚂蚁路径的长度
    distances = np.zeros(num_ants)
    for ant in range(num_ants):
        for step in range(num_cities - 1):
            distances[ant] += distance(city_coords[paths[ant, step]], city_coords[paths[ant, step+1]])
        distances[ant] += distance(city_coords[paths[ant, -1]], city_coords[paths[ant, 0]])

    # 更新最佳路径和最佳路径长度
    if np.min(distances) < best_distance:
        best_distance = np.min(distances)
        best_path = paths[np.argmin(distances)]

    # 信息素更新
    delta_pheromone = np.zeros((num_cities, num_cities))
    for ant in range(num_ants):
        for step in range(num_cities - 1):
            city1 = paths[ant, step]
            city2 = paths[ant, step+1]
            delta_pheromone[city1, city2] += Q / distances[ant]
        delta_pheromone[paths[ant, -1], paths[ant, 0]] += Q / distances[ant]

    pheromone = (1 - rho) * pheromone + delta_pheromone

# 输出结果
print("迭代曲线:")
# 迭代曲线代码

print("最佳周游路线:")
# 输出最佳路径

print("最佳周游路线长度:", best_distance)

3. 代码解释:

  • 变量best_path记录每轮迭代的最佳路线,变量best_distance记录最佳路线长度。
  • 路径构建部分: 每个蚂蚁根据信息素浓度和启发式信息选择下一个城市,并记录路径。
  • 信息素更新部分: 每个蚂蚁在其路径上增加信息素,信息素量与路径长度成反比,同时进行信息素蒸发。

4. 运行结果:

算法的输出结果将根据每次运行时的随机数生成而有所不同,因此无法提供具体的结果。但是,该算法将生成一条最佳周游路线和该路线的长度。

5. 总结:

本文介绍了使用蚁群算法解决旅行商问题的方法,包括算法的步骤、代码实现以及结果分析。蚁群算法是一种有效的启发式算法,能够有效解决旅行商问题,并找到较好的解决方案。

旅行商问题 (TSP) 的蚁群算法实现

原文地址: https://www.cveoy.top/t/topic/bQcu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录