TSP问题蚁群算法

出一期简单又智能的算法------蚁群算法

import math

import matplotlib.pyplot as plt
import numpy as np
import random

(ALPHA, BETA, RHO, Q) = (1.0, 2.0, 0.5, 100.0)
# 城市数,蚁群
(city_num, ant_num) = (50, 50)
distance_x = [
    178, 272, 176, 171, 650, 499, 267, 703, 408, 437, 491, 74, 532,
    416, 626, 42, 271, 359, 163, 508, 229, 576, 147, 560, 35, 714,
    757, 517, 64, 314, 675, 690, 391, 628, 87, 240, 705, 699, 258,
    428, 614, 36, 360, 482, 666, 597, 209, 201, 492, 294]
distance_y = [
    170, 395, 198, 151, 242, 556, 57, 401, 305, 421, 267, 105, 525,
    381, 244, 330, 395, 169, 141, 380, 153, 442, 528, 329, 232, 48,
    498, 265, 343, 120, 165, 50, 433, 63, 491, 275, 348, 222, 288,
    490, 213, 524, 244, 114, 104, 552, 70, 425, 227, 331]

# 城市距离和信息素
pheromone_graph = [[1.0 for col in range(city_num)] for raw in range(city_num)]
distance_graph = [[0.0 for col in range(city_num)] for raw in range(city_num)]
for i in range(city_num):
    for j in range(city_num):
        if i == j:
            distance_graph[i][j] = 100000
        else:
            x1 = distance_x[i]
            y1 = distance_y[i]
            x2 = distance_x[j]
            y2 = distance_y[j]
            distance_graph[i][j] = math.sqrt(((x1 - x2) ** 2 + (y1 - y2) ** 2))

max_iter = 150
'''开始迭代'''
dis_total = []
min_length = []
min_path = None
for i in range(max_iter):
    '''纪录每次迭代时,每个蚂蚁的路径'''
    route = []
    '''每个蚂蚁开始行动'''
    all_path_city = []
    for j in range(ant_num):
        '''未遍历城市'''
        pass_city = list(range(city_num))
        '''遍历完成的条件'''
        start = random.choice(list(range(city_num)))
        '''将路径储存'''
        path_city = [start]
        while len(pass_city) != 1:
            pass_city.remove(start)
            P_dis_choice = [1 / distance_graph[start][n] for n in pass_city]

            P_pho_choice = [pheromone_graph[start][n] for n in pass_city]

            P = [(P_dis_choice[n] ** ALPHA) * (P_pho_choice[n] ** BETA) for n in range(len(P_dis_choice))]
            P_choice = [sum(P[0:n + 1]) / sum(P) for n in range(len(P))]
            next_city = 0

            p = random.random()
            for n in range(len(P_choice)):
                if p - P_choice[n] < 0:
                    next_city = pass_city[n]
                    break
            path_city.append(next_city)
            start = next_city

        all_path_city.append(path_city)

    print('第{}轮迭代后'.format(i))
    '''得到第一轮最短的距离'''
    '''后面更新信息素也有用'''
    path_all = []
    for n in range(len(all_path_city)):
        path = 0
        for k in range(len(all_path_city[n]) - 1):
            path += distance_graph[all_path_city[n][k]][all_path_city[n][k + 1]]
        path += distance_graph[all_path_city[n][0]][all_path_city[n][-1]]
        path_all.append(path)
    if i != 0:
        if min(path_all) < min(min_length):
            min_path = all_path_city[path_all.index(min(path_all))]
    min_length.append(min(path_all))
    print(min(min_length))
    print(min_path)
    '''更新信息素'''
    '''首先先更新原先的信息素'''
    for n in range(len(pheromone_graph)):
        for k in range(len(pheromone_graph[0])):
            pheromone_graph[n][k] *= RHO
            
    for n in range(len(all_path_city)):
        for k in range(len(all_path_city[0]) - 1):
            pheromone_graph[all_path_city[n][k]][all_path_city[n][k + 1]] += Q / path_all[n]
            pheromone_graph[all_path_city[n][k + 1]][all_path_city[n][k]] += Q / path_all[n]
        pheromone_graph[all_path_city[n][0]][all_path_city[n][-1]] += Q / path_all[n]
        pheromone_graph[all_path_city[n][-1]][all_path_city[n][0]] += Q / path_all[n]

你可能感兴趣的:(人工智能)