基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)

ps:作者是很用心写的,如果觉得不错,请给作者一点鼓励噢!(点赞收藏评论噢)

基于遗传算法求解TSP问题

摘要 巡回旅行商问题(TSP)是组合优化中的经典问题。常见的TSP问题求解算法例如穷举法、贪心算法、动态规划算法不适用于求解大量城市或是容易得到局部最优解,所以更多优化算法应运而生。文章将基于遗传算法的原理和传统求解步骤依据具体的TSP问题做出优化改进求解51个城市最短路径规划问题,并借助python语言实现交互功能(用户可从51个城市中自行选择旅游城市,程序将为用户推荐最佳旅行方案)。
关键词 TSP 遗传算法 51个城市 最短路径规划 交互

第一章 绪论

1 遗传算法

1.1 背景介绍

遗传算法是模拟生物在自然环境中的遗传和进化过程而形成的一种自适应全局优化概率搜索算法,遗传算法从任意一个初始化的群体出发,通过随机选择、交叉和变异等遗传操作,使群体一代一代地进化到搜索空间中越来越好的区域,直至抵达最优解,其基本思想是基于达尔文的进化论和孟德尔的遗传学说。遗传算法的概念最早由Bagley J.D于1967年提出,并于1975年被美国密歇根大学教授HOllan及其学生开始其理论和方法的系统性研究。此后,越来越多的学者参与到遗传算法的研究中,针对遗传算法的缺陷提出解决策略和改进算法。目前,遗传算法的研究主要集中在四个方面:基础理论、遗传策略、编码方式、并行化。由于遗传算法能够被简单编码应用于复杂问题、其启发式搜索特性能够很大概率找到问题的全局最优解,它被广泛应用于各个领域,涉及工业、经济管理、交通运输、工业设计等。但是,尽管遗传算法在解决实际问题中取得了不错的成绩,其基础理论研究至今还未取得突破性进展,数学基础仍然不够完善,尚不能解释清楚参数设置对算法性能的影响、算法收敛性、早熟现象和欺骗问题等[1-4]。

1.2 优点

  1. 对可行解表示的广泛性,无论什么结构对象例如矩阵、数列等,遗传算法都能对其进行编码得到基因个体;
  2. 搜索从群体出发,具有潜在的并行性,可以进行多个个体的同时比较;
  3. 搜索使用评价函数启发,过程简单;
  4. 使用概率机制进行迭代,具有随机性;
  5. 具有可扩展性,容易与其他算法结合;
  6. 遗传算法在搜索过程中有很大概率可以找到全局最优解。

1.3缺点

1、遗传算法的编程实现复杂,需要进行编码解码操作;
2、选择、交叉、变异三个算子的实现有许多参数,这些参数的选择严重影响解的品质,而目前这些参数的选择大部分是依靠经验;’
3、没有能够及时利用网络的反馈信息,故算法的搜索速度比较慢,要得要较精确的解需要较多的训练时间;
4、算法对初始种群的选择有一定的依赖性;
5、算法的并行机制的潜在能力在目前的研究进展中还没有得到充分的利用。

2 TSP问题

TSP问题(Travelling Salesman Problem)又译为旅行推销员问题、货郎担问题,是数学领域中著名问题之一。假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路径的限制是每个城市只能拜访一次,而且最后要回到原来出发的城市。路径的选择目标是要求得的路径路程为所有路径之中的最小值[9]。迄今为止,TSP问题没有有效算法,研究者猜想NP完全问题(NP-Complete或NPC)和NP难题(NP-Hard或NPH)不存在有效算法,认为这类问题的大型实例不能用精确算法求解,必须寻求这类问题的有效的近似算法,所以研究相应的有效算法寻找其最优或近似最优解具有重要的理论意义。目前求解 TSP问题的主要方法有启发式搜索法、模拟退火算法、遗传算法或者多个算法相结合的混合算法等[7-8]。随着研究的进行,TSP问题的求解不断突破困难,不断带来令人惊喜的成果。1980年crowder和Padberg求解了318个城市的问题,1987年Padberg和Rinaldi将这个城市数增加到了2392个。1992年,美国Rice大学的CRPC研究小组用50台工作站使用了基于”cutting planes”算法解决T3038个城市的问题,被《发现杂志》评为当年的前50条科学新闻。1994年,Applegate、Bixby、Chvatal等人使用若干台SPARCT作站组成的机群用了3-4年的CPU时间解决了7397个城市的TSP问题。1998年,CRPC研究小组使用三台Digital AlphaServer 4100s(12个处理器) 组成的集群和32台Pentium—11个人计算机解决了美国13,509个城市组成的TSP问题。 2003年2月,Hisao Tamaki使用了路径融合同]Lin-Kernighan启发的变种相结合的方法发现TTSPLIB中pla33810的一个次优解。2004年2月Keld Helsgaun发现了pla85900问题的一个次优解[5-6]。

第二章 正文

遗传算法求解TSP问题的实现

1 问题描述

基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)

; 2 算法实现步骤

2.1 初始化

初始化数据:读入数据源,将坐标转换为距离矩阵(标准化欧式距离)
初始化对象:种群规模m、运行代数n、变异概率
初始化种群:生成m条路径(编码方式:符号编码)
说明:种群初始化的编码方式有多种,常见的有二进制编码、格雷码编码、实数编码和符号编码。编码方式具有启发性,缺乏理论基础来判断各种编码的好坏,常根据实际问题和经验来确定,本算法采用符号编码。

2.2.计算种群适应度

适应度函数f=1/〖distance(x)〗^15
说明:适应度函数会影响选择压,选择压过大,会造成几个较好的可行解迅速占满整个群体,选择压过小,会让算法变成纯粹的随机行为。本算法对适应度函数进行15次方的尺度变换,就是为了避免选择压太小,轮盘赌的选择失去意义变成等概率随机选择。

2.3计算累计概率

计算每个个体适应度占适应度总和的比例并计算累计概率。

2.4迭代

1.选择算子:运用轮盘赌选择法,与传统轮盘赌选择法不同的是,本算法不选择重复个体充当父母,即如果选择出来的个体已经被选过一次,下一次再选到它就直接抛弃。这样最后选出来的个体数量m1

3 实验结果

文件:

基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
第一列为地点名称,第二,三列为地点的经纬度
基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
地点名称
ps:程序具有泛化性,将这两个文件里的内容修改即可得到不同地方的旅游路径规划

; 3.1初始设定

先根据经验值(种群大小:20 ~ 100,迭代次数:500,变异概率:0.0001~0.1)对初始化参数进行设定取种群大小100,迭代次数500,变异概率0.1,选择51个城市,设置方案数量为1,运行20次。

3.2 调参

调整参数,观察实验结果随参数的变化,通过反复尝试获得51个城市路径近似最优解

基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
根据实验规划得到的最优路径为475.1153
具体实验情况如下图所示:
运行程序,点击需要旅游的城市和需要输出的方案数量,这里选择了51个城市,输出一个方案,程序最后运行完会给出一条路径规划路线。
基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
可以看出还有一部分路径存在交叉,导致结果不是最优解,不过大部分路径都无交叉,说明此结果已经接近最优解

; 4 程序的交互性

用户还可以根据自己想法选择旅游城市
下面列举2种选择方案
第一种:

基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
第二种:
基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)

基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)
可以看出,当选择城市数量不是很多时,可以找到最优解

第三章 结论

1.种群规模过小,容易出现早熟现象,规模过大耗时又比较长,所以在选取种群规模大
小时应该选择结果差不多的最小种群大小;
2.随着种群规模的增大和变异率的增加,必须增大迭代次数才能保证收敛;
3.在一定范围内变异率越高越有利于找到最优解,但如果变异率太高高阶模式被破坏的
概率也随之增大;
4.收敛时间、进化代数、全局优化概率这三个性能指标是难以同时达到最优,所以在解
决问题时需要确定以哪一个为主要评价指标。

参考文献

[1] Holland J.H.,Adaptation in Natural andArtificial Systems.AnnArbor :University of Michigan press,1975
[2] [日]玄光难,程润传.遗传算法与工程设计[M].北京:科学出版社,2000
[3] 刘晓霞.种群规模对遗传算法性能影响研究[D].北京:华北电力大学,2010
[4] 郑冀鲁.生物质冷态流化特性与热解生物质制备液体燃料的研究[D].安徽:中国科学技术大学,2005
[5] 谢胜利,唐敏,董金祥.求解 TSP 问题的一种改进的遗传算法[J].计算机工程与应用,2002,38(8):58-60,245.

[6] 任昊南.用遗传算法求解 TSP 问题[D].山东:山东大学,2008.

[7] 叶家琪,符强,贺亦甲, 等.基于聚类集成的蚁群算法求解大规模 TSP 问题[J].计算机与现代
化,2020,(2):31-35.

[8] 王玮,吴天红,姜英姿, 等.混合粒子群算法在 TSP 问题中的研究[J].中国新通信,2020,22(9)
[9] https://baike.so.com/doc/74122-78457.html

代码

%GA.py
%遗传算法求解最短路径部分
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import math
import random

matplotlib.rcParams['font.family'] = 'STSong'

site_name = []
site_coordinate = []
with open('11.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        line = line.split('\n')[0]
        line = line.split(' ')
        site_name.append(line[0])
        site_coordinate.append([float(line[2]), float(line[1])])
site_coordinate = np.array(site_coordinate)

site_count = len(site_name)
Distance = np.zeros([site_count, site_count])
for i in range(site_count):
    for j in range(site_count):
        Distance[i][j] = math.sqrt(
            (site_coordinate[i][0] - site_coordinate[j][0]) ** 2 + (site_coordinate[i][1] - site_coordinate[j][1]) ** 2)

count = 200

itter_time = 600

mutation_rate = 0.1

site_index_dict = {}
i = 0
for site in site_name:
    site_index_dict[site] = i
    i += 1

index_site_dict = {}
i = 0
for site in site_name:
    index_site_dict[i] = site
    i += 1

def get_origin(select_site):
    global site_index_dict
    origin = site_index_dict[select_site[0]]
    select_site_index = []
    for site in select_site:
        select_site_index.append(site_index_dict[site])
    select_site_index.remove(origin)
    return origin,select_site_index

def get_total_distance(x,origin):
    distance = 0
    distance += Distance[origin][x[0]]
    for i in range(len(x)):
        if i == len(x) - 1:
            distance += Distance[origin][x[i]]

            break
        else:
            distance += Distance[x[i]][x[i + 1]]
    return distance

def generate_population(select_site_index):
    population = []
    for i in range(count):

        x = select_site_index.copy()
        random.shuffle(x)
        population.append(x)
    return population

def selection(population,origin):

    graded = [[get_total_distance(x,origin), x] for x in population]

    fit_value = []
    for i in range(len(graded)):
        fit_value.append( 1/graded[i][0]**15)

    total_fit = 0
    for i in range(len(fit_value)):
        total_fit += fit_value[i]

    newfit_value = []
    for i in range(len(fit_value)):
        newfit_value.append(fit_value[i] / total_fit)

    t = 0
    for i in range(len(newfit_value)):
        t = t + newfit_value[i]
        newfit_value[i] = t

    ms = []
    for i in range(len(population)):
        ms.append(random.random())
    ms.sort()

    i = 0
    j = 0
    parents = []
    while i < len(population):

        if(ms[i] < newfit_value[j]):
            if population[j] not in parents:
                parents.append(population[j])
            i = i + 1

        else:
            j = j + 1

    return parents

def crossover(parents):

    child_count = count - len(parents)

    children = []
    while len(children) < child_count:

        mother_index = random.randint(0, len(parents) - 1)
        father_index = random.randint(0, len(parents) - 1)
        if mother_index != father_index:
            mother = parents[mother_index]
            father = parents[father_index]

            left = random.randint(0, len(mother) - 2)
            right = random.randint(left + 1, len(mother) - 1)

            gene1 = mother[left:right]
            gene2 = father[left:right]

            child1_c = mother[right:] + mother[:right]
            child2_c = father[right:] + father[:right]
            child1 = child1_c.copy()
            child2 = child2_c.copy()

            for o in gene2:
                child1_c.remove(o)

            for o in gene1:
                child2_c.remove(o)

            child1[left:right] = gene2
            child2[left:right] = gene1

            child1[right:] = child1_c[0:len(child1) - right]
            child1[:left] = child1_c[len(child1) - right:]

            child2[right:] = child2_c[0:len(child1) - right]
            child2[:left] = child2_c[len(child1) - right:]

            children.append(child1)
            children.append(child2)

    return children

def mutation(children):
    for i in range(len(children)):
        if random.random() < mutation_rate:
            child = children[i]
            u = random.randint(0,len(child) - 2)
            v = random.randint(u+1,len(child) - 1)

            child_x = child[u+1:v]
            child_x.reverse()
            child = child[0:u+1] + child_x + child[v:]

def get_result(population,origin):
    graded = [[get_total_distance(x,origin), x] for x in population]
    graded = sorted(graded)
    return graded

def draw(origin,result_path,distance):
    global site_coordinate
    global site_name

    plt.scatter(site_coordinate[:,0],site_coordinate[:,1])

    site_name1 = []
    with open('22.txt', 'r') as f:
        lines = f.readlines()
        for line in lines:
            site_name1.append(line)

    for i in range(51):
        plt.text(site_coordinate[i,0],site_coordinate[i,1],site_name1[i],fontsize="8")

    X = []
    Y = []
    X.append(site_coordinate[origin, 0])
    Y.append(site_coordinate[origin, 1])
    i = 0
    for index in result_path:
        X.append(site_coordinate[index, 0])
        Y.append(site_coordinate[index, 1])
        plt.plot(X,Y,'-')
        plt.text((X[0]+X[1])/2,(Y[0]+Y[1])/2,i,fontsize='small')

        plt.title("distance = "+str(distance))
        del(X[0])
        del(Y[0])
        i += 1
    X.append(site_coordinate[origin, 0])
    Y.append(site_coordinate[origin, 1])
    plt.text((X[0]+X[1])/2,(Y[0]+Y[1])/2,i,fontdict={"size":12})
    plt.plot(X,Y,'-')

plt.scatter(site_coordinate[origin,0],site_coordinate[origin,1],s=150)

%GUI.py
%UI界面
import tkinter as tk
from tkinter import *
from GA import *

window = tk.Tk()
window.title('旅游规划路径')
window.geometry('1000x800')

l1 = tk.Label(window,text = '请先点击起点,再点击其他要经过的景点', height=2).pack()

frm=tk.Frame(window)
frm.pack()
frm_l=tk.Frame(frm)
frm_r=tk.Frame(frm)
frm_l.pack(side='left')
frm_r.pack(side='right')
frm_l_l=tk.Frame(frm_l)
frm_l_r=tk.Frame(frm_l)
frm_l_l.pack(side='left')
frm_l_r.pack(side='right')
frm_r_l=tk.Frame(frm_r)
frm_r_r=tk.Frame(frm_r)
frm_r_l.pack(side='left')
frm_r_r.pack(side='right')
frm_0=tk.Frame(frm_l_l)
frm_1=tk.Frame(frm_l_l)
frm_0.pack(side='left')
frm_1.pack(side='right')
frm_2=tk.Frame(frm_l_r)
frm_3=tk.Frame(frm_l_r)
frm_2.pack(side='left')
frm_3.pack(side='right')
frm_4=tk.Frame(frm_r_l)
frm_5=tk.Frame(frm_r_l)
frm_4.pack(side='left')
frm_5.pack(side='right')
frm_6=tk.Frame(frm_r_r)
frm_7=tk.Frame(frm_r_r)
frm_6.pack(side='left')
frm_7.pack(side='right')

sites = site_name
select_site = []

sites_dict = {}
i = 0
for site in sites:
    sites_dict["b"+str(i)] = site
    i += 1

def print_site_b0():
    global select_site
    t.insert('end',sites_dict['b0']+" ")
    b0.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b0'])
def print_site_b1():
    global select_site
    t.insert('end',sites_dict['b1']+" ")
    b1.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b1'])
def print_site_b2():
    global select_site
    t.insert('end',sites_dict['b2']+" ")
    b2.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b2'])
def print_site_b3():
    global select_site
    t.insert('end',sites_dict['b3']+" ")
    b3.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b3'])
def print_site_b4():
    global select_site
    t.insert('end',sites_dict['b4']+" ")
    b4.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b4'])
def print_site_b5():
    global select_site
    t.insert('end',sites_dict['b5']+" ")
    b5.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b5'])
def print_site_b6():
    global select_site
    t.insert('end',sites_dict['b6']+" ")
    b6.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b6'])
def print_site_b7():
    global select_site
    t.insert('end',sites_dict['b7']+" ")
    b7.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b7'])
def print_site_b8():
    global select_site
    t.insert('end',sites_dict['b8']+" ")
    b8.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b8'])
def print_site_b9():
    global select_site
    t.insert('end',sites_dict['b9']+" ")
    b9.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b9'])
def print_site_b10():
    global select_site
    t.insert('end',sites_dict['b10']+" ")
    b10.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b10'])
def print_site_b11():
    global select_site
    t.insert('end',sites_dict['b11']+" ")
    b11.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b11'])
def print_site_b12():
    global select_site
    t.insert('end',sites_dict['b12']+" ")
    b12.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b12'])
def print_site_b13():
    global select_site
    t.insert('end',sites_dict['b13']+" ")
    b13.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b13'])
def print_site_b14():
    global select_site
    t.insert('end',sites_dict['b14']+" ")
    b14.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b14'])
def print_site_b15():
    global select_site
    t.insert('end',sites_dict['b15']+" ")
    b15.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b15'])
def print_site_b16():
    global select_site
    t.insert('end',sites_dict['b16']+" ")
    b16.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b16'])
def print_site_b17():
    global select_site
    t.insert('end',sites_dict['b17']+" ")
    b17.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b17'])
def print_site_b18():
    global select_site
    t.insert('end',sites_dict['b18']+" ")
    b18.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b18'])
def print_site_b19():
    global select_site
    t.insert('end',sites_dict['b19']+" ")
    b19.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b19'])
def print_site_b20():
    global select_site
    t.insert('end',sites_dict['b20']+" ")
    b20.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b20'])
def print_site_b21():
    global select_site
    t.insert('end',sites_dict['b21']+" ")
    b21.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b21'])
def print_site_b22():
    global select_site
    t.insert('end',sites_dict['b22']+" ")
    b22.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b22'])
def print_site_b23():
    global select_site
    t.insert('end',sites_dict['b23']+" ")
    b23.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b23'])
def print_site_b24():
    global select_site
    t.insert('end',sites_dict['b24']+" ")
    b24.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b24'])
def print_site_b25():
    global select_site
    t.insert('end',sites_dict['b25']+" ")
    b25.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b25'])
def print_site_b26():
    global select_site
    t.insert('end',sites_dict['b26']+" ")
    b26.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b26'])
def print_site_b27():
    global select_site
    t.insert('end',sites_dict['b27']+" ")
    b27.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b27'])
def print_site_b28():
    global select_site
    t.insert('end',sites_dict['b28']+" ")
    b28.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b28'])
def print_site_b29():
    global select_site
    t.insert('end',sites_dict['b29']+" ")
    b29.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b29'])
def print_site_b30():
    global select_site
    t.insert('end',sites_dict['b30']+" ")
    b30.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b30'])
def print_site_b31():
    global select_site
    t.insert('end',sites_dict['b31']+" ")
    b31.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b31'])
def print_site_b32():
    global select_site
    t.insert('end',sites_dict['b32']+" ")
    b32.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b32'])
def print_site_b33():
    global select_site
    t.insert('end',sites_dict['b33']+" ")
    b33.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b33'])
def print_site_b34():
    global select_site
    t.insert('end',sites_dict['b34']+" ")
    b34.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b34'])
def print_site_b35():
    global select_site
    t.insert('end',sites_dict['b35']+" ")
    b35.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b35'])
def print_site_b36():
    global select_site
    t.insert('end',sites_dict['b36']+" ")
    b36.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b36'])
def print_site_b37():
    global select_site
    t.insert('end',sites_dict['b37']+" ")
    b37.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b37'])
def print_site_b38():
    global select_site
    t.insert('end',sites_dict['b38']+" ")
    b38.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b38'])
def print_site_b39():
    global select_site
    t.insert('end',sites_dict['b39']+" ")
    b39.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b39'])
def print_site_b40():
    global select_site
    t.insert('end',sites_dict['b40']+" ")
    b40.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b40'])
def print_site_b41():
    global select_site
    t.insert('end',sites_dict['b41']+" ")
    b41.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b41'])
def print_site_b42():
    global select_site
    t.insert('end',sites_dict['b42']+" ")
    b42.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b42'])
def print_site_b43():
    global select_site
    t.insert('end',sites_dict['b43']+" ")
    b43.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b43'])
def print_site_b44():
    global select_site
    t.insert('end',sites_dict['b44']+" ")
    b44.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b44'])
def print_site_b45():
    global select_site
    t.insert('end',sites_dict['b45']+" ")
    b45.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b45'])
def print_site_b46():
    global select_site
    t.insert('end',sites_dict['b46']+" ")
    b46.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b46'])
def print_site_b47():
    global select_site
    t.insert('end',sites_dict['b47']+" ")
    b47.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b47'])
def print_site_b48():
    global select_site
    t.insert('end',sites_dict['b48']+" ")
    b48.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b48'])
def print_site_b49():
    global select_site
    t.insert('end',sites_dict['b49']+" ")
    b49.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b49'])
def print_site_b50():
    global select_site
    t.insert('end',sites_dict['b50']+" ")
    b50.config(bg='DeepSkyBlue',fg='OrangeRed')
    select_site.append(sites_dict['b50'])

def print_site_b51():
    global select_site
    global sites
    for site in sites:
        if site not in select_site:
            select_site.append(site)
            t.insert('end',site+" ")

    b0.config(bg='DeepSkyBlue',fg='OrangeRed')
    b1.config(bg='DeepSkyBlue',fg='OrangeRed')
    b2.config(bg='DeepSkyBlue',fg='OrangeRed')
    b3.config(bg='DeepSkyBlue',fg='OrangeRed')
    b4.config(bg='DeepSkyBlue',fg='OrangeRed')
    b5.config(bg='DeepSkyBlue',fg='OrangeRed')
    b6.config(bg='DeepSkyBlue',fg='OrangeRed')
    b7.config(bg='DeepSkyBlue',fg='OrangeRed')
    b8.config(bg='DeepSkyBlue',fg='OrangeRed')
    b9.config(bg='DeepSkyBlue',fg='OrangeRed')
    b10.config(bg='DeepSkyBlue',fg='OrangeRed')
    b11.config(bg='DeepSkyBlue',fg='OrangeRed')
    b12.config(bg='DeepSkyBlue',fg='OrangeRed')
    b13.config(bg='DeepSkyBlue',fg='OrangeRed')
    b14.config(bg='DeepSkyBlue',fg='OrangeRed')
    b15.config(bg='DeepSkyBlue',fg='OrangeRed')
    b16.config(bg='DeepSkyBlue',fg='OrangeRed')
    b17.config(bg='DeepSkyBlue',fg='OrangeRed')
    b18.config(bg='DeepSkyBlue',fg='OrangeRed')
    b19.config(bg='DeepSkyBlue',fg='OrangeRed')
    b20.config(bg='DeepSkyBlue',fg='OrangeRed')
    b21.config(bg='DeepSkyBlue',fg='OrangeRed')
    b22.config(bg='DeepSkyBlue',fg='OrangeRed')
    b23.config(bg='DeepSkyBlue',fg='OrangeRed')
    b24.config(bg='DeepSkyBlue',fg='OrangeRed')
    b25.config(bg='DeepSkyBlue',fg='OrangeRed')
    b26.config(bg='DeepSkyBlue',fg='OrangeRed')
    b27.config(bg='DeepSkyBlue',fg='OrangeRed')
    b28.config(bg='DeepSkyBlue',fg='OrangeRed')
    b29.config(bg='DeepSkyBlue',fg='OrangeRed')
    b30.config(bg='DeepSkyBlue',fg='OrangeRed')
    b31.config(bg='DeepSkyBlue',fg='OrangeRed')
    b32.config(bg='DeepSkyBlue',fg='OrangeRed')
    b33.config(bg='DeepSkyBlue',fg='OrangeRed')
    b34.config(bg='DeepSkyBlue',fg='OrangeRed')
    b35.config(bg='DeepSkyBlue',fg='OrangeRed')
    b36.config(bg='DeepSkyBlue',fg='OrangeRed')
    b37.config(bg='DeepSkyBlue',fg='OrangeRed')
    b38.config(bg='DeepSkyBlue',fg='OrangeRed')
    b39.config(bg='DeepSkyBlue',fg='OrangeRed')
    b40.config(bg='DeepSkyBlue',fg='OrangeRed')
    b41.config(bg='DeepSkyBlue',fg='OrangeRed')
    b42.config(bg='DeepSkyBlue',fg='OrangeRed')
    b43.config(bg='DeepSkyBlue',fg='OrangeRed')
    b44.config(bg='DeepSkyBlue',fg='OrangeRed')
    b45.config(bg='DeepSkyBlue',fg='OrangeRed')
    b46.config(bg='DeepSkyBlue',fg='OrangeRed')
    b47.config(bg='DeepSkyBlue',fg='OrangeRed')
    b48.config(bg='DeepSkyBlue',fg='OrangeRed')
    b49.config(bg='DeepSkyBlue',fg='OrangeRed')
    b50.config(bg='DeepSkyBlue',fg='OrangeRed')

def print_site_b52():
    global select_site
    global sites
    for site in sites:
        if site not in select_site:
            select_site.append(site)
            t.insert('end',site+" ")

    b0.config(bg='DeepSkyBlue',fg='OrangeRed')
    b1.config(bg='DeepSkyBlue',fg='OrangeRed')
    b2.config(bg='DeepSkyBlue',fg='OrangeRed')
    b3.config(bg='DeepSkyBlue',fg='OrangeRed')
    b4.config(bg='DeepSkyBlue',fg='OrangeRed')
    b5.config(bg='DeepSkyBlue',fg='OrangeRed')
    b6.config(bg='DeepSkyBlue',fg='OrangeRed')
    b7.config(bg='DeepSkyBlue',fg='OrangeRed')
    b8.config(bg='DeepSkyBlue',fg='OrangeRed')
    b9.config(bg='DeepSkyBlue',fg='OrangeRed')
    b10.config(bg='DeepSkyBlue',fg='OrangeRed')
    b11.config(bg='DeepSkyBlue',fg='OrangeRed')
    b12.config(bg='DeepSkyBlue',fg='OrangeRed')
    b13.config(bg='DeepSkyBlue',fg='OrangeRed')
    b14.config(bg='DeepSkyBlue',fg='OrangeRed')
    b15.config(bg='DeepSkyBlue',fg='OrangeRed')
    b16.config(bg='DeepSkyBlue',fg='OrangeRed')
    b17.config(bg='DeepSkyBlue',fg='OrangeRed')
    b18.config(bg='DeepSkyBlue',fg='OrangeRed')
    b19.config(bg='DeepSkyBlue',fg='OrangeRed')
    b20.config(bg='DeepSkyBlue',fg='OrangeRed')
    b21.config(bg='DeepSkyBlue',fg='OrangeRed')
    b22.config(bg='DeepSkyBlue',fg='OrangeRed')
    b23.config(bg='DeepSkyBlue',fg='OrangeRed')
    b24.config(bg='DeepSkyBlue',fg='OrangeRed')
    b25.config(bg='DeepSkyBlue',fg='OrangeRed')
    b26.config(bg='DeepSkyBlue',fg='OrangeRed')
    b27.config(bg='DeepSkyBlue',fg='OrangeRed')
    b28.config(bg='DeepSkyBlue',fg='OrangeRed')
    b29.config(bg='DeepSkyBlue',fg='OrangeRed')
    b30.config(bg='DeepSkyBlue',fg='OrangeRed')
    b31.config(bg='DeepSkyBlue',fg='OrangeRed')
    b32.config(bg='DeepSkyBlue',fg='OrangeRed')
    b33.config(bg='DeepSkyBlue',fg='OrangeRed')
    b34.config(bg='DeepSkyBlue',fg='OrangeRed')
    b35.config(bg='DeepSkyBlue',fg='OrangeRed')
    b36.config(bg='DeepSkyBlue',fg='OrangeRed')
    b37.config(bg='DeepSkyBlue',fg='OrangeRed')
    b38.config(bg='DeepSkyBlue',fg='OrangeRed')
    b39.config(bg='DeepSkyBlue',fg='OrangeRed')
    b40.config(bg='DeepSkyBlue',fg='OrangeRed')
    b41.config(bg='DeepSkyBlue',fg='OrangeRed')
    b42.config(bg='DeepSkyBlue',fg='OrangeRed')
    b43.config(bg='DeepSkyBlue',fg='OrangeRed')
    b44.config(bg='DeepSkyBlue',fg='OrangeRed')
    b45.config(bg='DeepSkyBlue',fg='OrangeRed')
    b46.config(bg='DeepSkyBlue',fg='OrangeRed')
    b47.config(bg='DeepSkyBlue',fg='OrangeRed')
    b48.config(bg='DeepSkyBlue',fg='OrangeRed')
    b49.config(bg='DeepSkyBlue',fg='OrangeRed')
    b50.config(bg='DeepSkyBlue',fg='OrangeRed')

b0 = tk.Button(frm_0,text=sites_dict['b0'],width=13, height=1,activebackground='DeepSkyBlue',activeforeground='OrangeRed',command=print_site_b0)
b0.pack()
origin_bgcolor = b0.cget("background")
origin_fgcolor = b0.cget("foreground")
b1 = tk.Button(frm_1, text = sites_dict['b1'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b1)
b1.pack()
b2 = tk.Button(frm_2, text = sites_dict['b2'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b2)
b2.pack()
b3 = tk.Button(frm_3, text = sites_dict['b3'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b3)
b3.pack()
b4 = tk.Button(frm_4, text = sites_dict['b4'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b4)
b4.pack()
b5 = tk.Button(frm_5, text = sites_dict['b5'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b5)
b5.pack()
b6 = tk.Button(frm_6, text = sites_dict['b6'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b6)
b6.pack()
b7 = tk.Button(frm_7, text = sites_dict['b7'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b7)
b7.pack()
b8 = tk.Button(frm_0, text = sites_dict['b8'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b8)
b8.pack()
b9 = tk.Button(frm_1, text = sites_dict['b9'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b9)
b9.pack()
b10 = tk.Button(frm_2, text = sites_dict['b10'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b10)
b10.pack()
b11 = tk.Button(frm_3, text = sites_dict['b11'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b11)
b11.pack()
b12 = tk.Button(frm_4, text = sites_dict['b12'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b12)
b12.pack()
b13 = tk.Button(frm_5, text = sites_dict['b13'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b13)
b13.pack()
b14 = tk.Button(frm_6, text = sites_dict['b14'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b14)
b14.pack()
b51 = tk.Button(frm_7, text = "全选", width = 13, height = 1, bg = "Beige", command = print_site_b51)
b51.pack()

frm1 = tk.Frame(window)
frm1.pack()
frm1_l = tk.Frame(frm1)
frm1_r = tk.Frame(frm1)
frm1_l.pack(side = 'left')
frm1_r.pack(side = 'right')
frm1_l_l = tk.Frame(frm1_l)
frm1_l_r = tk.Frame(frm1_l)
frm1_l_l.pack(side = 'left')
frm1_l_r.pack(side = 'right')
frm1_r_l = tk.Frame(frm1_r)
frm1_r_r = tk.Frame(frm1_r)
frm1_r_l.pack(side = 'left')
frm1_r_r.pack(side = 'right')
b15 = tk.Button(frm_0, text = sites_dict['b15'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b15)
b15.pack()
b16 = tk.Button(frm_1, text = sites_dict['b16'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b16)
b16.pack()
b17 = tk.Button(frm_2, text = sites_dict['b17'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b17)
b17.pack()
b18 = tk.Button(frm_3, text = sites_dict['b18'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b18)
b18.pack()
b19 = tk.Button(frm_4, text = sites_dict['b19'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b19)
b19.pack()
b20 = tk.Button(frm_5, text = sites_dict['b20'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b20)
b20.pack()
b21 = tk.Button(frm_6, text = sites_dict['b21'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b21)
b21.pack()
b22 = tk.Button(frm_7, text = sites_dict['b22'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b22)
b22.pack()
b23 = tk.Button(frm_0, text = sites_dict['b23'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b23)
b23.pack()
b24 = tk.Button(frm_1, text = sites_dict['b24'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b24)
b24.pack()
b25 = tk.Button(frm_2, text = sites_dict['b25'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b25)
b25.pack()
b26 = tk.Button(frm_3, text = sites_dict['b26'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b26)
b26.pack()
b27 = tk.Button(frm_4, text = sites_dict['b27'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b27)
b27.pack()
b28 = tk.Button(frm_5, text = sites_dict['b28'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b28)
b28.pack()
b29 = tk.Button(frm_6, text = sites_dict['b29'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b29)
b29.pack()
b30 = tk.Button(frm_7, text = sites_dict['b30'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b30)
b30.pack()
b31 = tk.Button(frm_0, text = sites_dict['b31'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b31)
b31.pack()
b32 = tk.Button(frm_1, text = sites_dict['b32'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b32)
b32.pack()
b33 = tk.Button(frm_2, text = sites_dict['b33'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b33)
b33.pack()
b34 = tk.Button(frm_3, text = sites_dict['b34'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b34)
b34.pack()
b35 = tk.Button(frm_4, text = sites_dict['b35'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b35)
b35.pack()
b36 = tk.Button(frm_5, text = sites_dict['b36'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b36)
b36.pack()
b37 = tk.Button(frm_6, text = sites_dict['b37'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b37)
b37.pack()
b38 = tk.Button(frm_7, text = sites_dict['b38'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b38)
b38.pack()
b39 = tk.Button(frm_0, text = sites_dict['b39'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b39)
b39.pack()
b40 = tk.Button(frm_1, text = sites_dict['b40'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b40)
b40.pack()
b41 = tk.Button(frm_2, text = sites_dict['b41'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b41)
b41.pack()
b42 = tk.Button(frm_3, text = sites_dict['b42'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b42)
b42.pack()
b43 = tk.Button(frm_4, text = sites_dict['b43'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b43)
b43.pack()
b44 = tk.Button(frm_5, text = sites_dict['b44'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b44)
b44.pack()
b45 = tk.Button(frm_6, text = sites_dict['b45'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b45)
b45.pack()
b46 = tk.Button(frm_7, text = sites_dict['b46'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b46)
b46.pack()
b47 = tk.Button(frm_0, text = sites_dict['b47'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b47)
b47.pack()
b48 = tk.Button(frm_1, text = sites_dict['b48'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b48)
b48.pack()
b49 = tk.Button(frm_2, text = sites_dict['b49'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b49)
b49.pack()
b50 = tk.Button(frm_3, text = sites_dict['b50'], width = 13, height = 1, activebackground = 'DeepSkyBlue', activeforeground = 'OrangeRed', command = print_site_b50)
b50.pack()
b57 = tk.Button(frm_4,width=13, height=1,activebackground='DeepSkyBlue',activeforeground='OrangeRed',relief=GROOVE)
b57.pack()
b54 = tk.Button(frm_5,width=13, height=1,activebackground='DeepSkyBlue',activeforeground='OrangeRed',relief=GROOVE)
b54.pack()
b55 = tk.Button(frm_6,width=13, height=1,activebackground='DeepSkyBlue',activeforeground='OrangeRed',relief=GROOVE)
b55.pack()
b52 = tk.Button(frm_7, text = "选择所有剩余", width = 13, height = 1, bg = "Beige", command = print_site_b52)
b52.pack()

l2 = tk.Label(window,text='第一个为起点,再经过剩余景点: ').pack()
t = tk.Text(window,height=4)
t.pack()

frmx=tk.Frame(window)
frmx.pack()
frmx_l=tk.Frame(frmx)
frmx_r=tk.Frame(frmx)
frmx_l.pack(side='left')
frmx_r.pack(side='right')

def print_b53():
    t.delete('0.0','end')
    select_site.clear()     ,
    l3.config(text = "",bg = origin_bgcolor)
    t1.delete('0.0','end')
    t1.config(bg = origin_bgcolor,bd = 0)
    plt.close('all')

    b0.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b1.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b2.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b3.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b4.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b5.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b6.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b7.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b8.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b9.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b10.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b11.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b12.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b13.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b14.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b15.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b16.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b17.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b18.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b19.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b20.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b21.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b22.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b23.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b24.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b25.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b26.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b27.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b28.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b29.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b30.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b31.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b32.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b33.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b34.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b35.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b36.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b37.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b38.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b39.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b40.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b41.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b42.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b43.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b44.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b45.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b46.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b47.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b48.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b49.config(bg = origin_bgcolor , fg = origin_fgcolor)
    b50.config(bg = origin_bgcolor , fg = origin_fgcolor)

b53 = tk.Button(text="全部删除",width=30, height=1,activebackground='DeepSkyBlue',activeforeground='OrangeRed',command=print_b53)
b53.pack()

l4 = tk.Label(window,text="请选择输出方案的个数",height=2)
l4.pack()
e = tk.Entry(window,show=None)
e.pack()

def run():
    global select_site

    output = int(e.get())

    origin,select_site_index = get_origin(select_site)

    population = generate_population(select_site_index)
    DistanceAndPath=get_result(population,origin)

    register=[]
    i = 0
    while i < itter_time:

        parents = selection(population,origin)

        children = crossover(parents)

        mutation(children)

        population = parents + children

        DistanceAndPath = get_result(population,origin)
        register.append(DistanceAndPath[0][0])
        i += 1

    result_path_name = []
    result_path_name.append(index_site_dict[origin])
    for item in DistanceAndPath[0][1]:
        result_path_name.append(index_site_dict[item])

    l3.config(text="演变结束! "+"  最优路径为:",bg="Beige")
    t1.config(bg = "SkyBlue",bd = 2)
    t1.insert('end',result_path_name)

    for j in range(output):
        result_path = DistanceAndPath[j][1]
        distance = DistanceAndPath[j][0]

        plt.figure(j+1)
        draw(origin,result_path,distance)

        plt.figure(j+2)
        plt.plot(list(range(len(register))),register)
        plt.title("最优结果变化趋势")
        plt.show()

b60 = tk.Button(window,text= "RUN",width=15, height=2,activebackground='DeepSkyBlue',activeforeground='OrangeRed',command=run)
b60.pack()

l3 = tk.Label(window,height=3)
l3.pack()

t1 = tk.Text(window,height=3,bg = origin_bgcolor,bd = 0)
t1.pack()

window.mainloop()

Original: https://blog.csdn.net/qq_43659401/article/details/117337861
Author: 鸡丝米线
Title: 基于遗传算法求解TSP问题(旅游路径规划,Python实现,超详细,可视化,结果分析)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/719885/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球