Python绘制TSP、VRP问题求解结果图

【代码】Python绘制TSP、VRP问题求解结果图(包含静态图与动态图)。

一、静态图

import matplotlib.pyplot as plt


def plot_tour(data, best_path, is_save):
    """
    绘制旅行图
    :param data: 包含位置坐标的字典类型数据
    :param best_path: 最优旅行路径
    :param is_save: 是否保存绘图
    :return:
    """
    x = []
    y = []
    text_list = []
    for v in best_path:
        x.append(data[v][0])
        y.append(data[v][1])
        text_list.append(str(v))

    for i in range(len(text_list)):
        plt.text(x[i], y[i], text_list[i], ha='center', va='center_baseline')

    plt.plot(x, y, 'co--', linewidth=2, markersize=12)
    if is_save:
        plt.savefig("best_tour.png")
    plt.show()


def vrp():
    data = {
        1: (710.0, 1310.0),
        2: (630.0, 1660.0),
        3: (40.0, 2090.0),
        4: (750.0, 1100.0),
        5: (750.0, 2030.0),
        6: (1030.0, 2070.0),
        7: (1650.0, 650.0),
        8: (1490.0, 1630.0),
        9: (790.0, 2260.0),
        10: (1150.0, 1760.0),
        11: (840.0, 550.0),
        12: (1170.0, 2300.0),
        13: (970.0, 1340.0),
        14: (510.0, 700.0),
        15: (750.0, 900.0),
        16: (1280.0, 1200.0),
        17: (230.0, 590.0),
        18: (460.0, 860.0),
        19: (1040.0, 950.0),
        20: (590.0, 1390.0),
        21: (830.0, 1770.0),
        22: (490.0, 500.0),
        23: (1840.0, 1240.0),
        24: (1260.0, 1500.0),
        25: (1280.0, 790.0),
        26: (490.0, 2130.0),
        27: (1460.0, 1420.0),
        28: (1260.0, 1910.0),
        29: (360.0, 1980.0)
    }
    best_path = [1, 4, 15, 18, 17, 14, 22, 11, 19, 25, 7, 23, 27, 8, 24, 16, 13, 1,
                 1, 21, 10, 28, 6, 12, 9, 5, 26, 29, 3, 2, 20, 1]
    plot_tour(data, best_path, False)


def tsp():
    data = {
        1: (1150.0, 1760.0),
        2: (630.0, 1660.0),
        3: (40.0, 2090.0),
        4: (750.0, 1100.0),
        5: (750.0, 2030.0),
        6: (1030.0, 2070.0),
        7: (1650.0, 650.0),
        8: (1490.0, 1630.0),
        9: (790.0, 2260.0),
        10: (710.0, 1310.0),
        11: (840.0, 550.0),
        12: (1170.0, 2300.0),
        13: (970.0, 1340.0),
        14: (510.0, 700.0),
        15: (750.0, 900.0),
        16: (1280.0, 1200.0),
        17: (230.0, 590.0),
        18: (460.0, 860.0),
        19: (1040.0, 950.0),
        20: (590.0, 1390.0),
        21: (830.0, 1770.0),
        22: (490.0, 500.0),
        23: (1840.0, 1240.0),
        24: (1260.0, 1500.0),
        25: (1280.0, 790.0),
        26: (490.0, 2130.0),
        27: (1460.0, 1420.0),
        28: (1260.0, 1910.0),
        29: (360.0, 1980.0)
    }
    best_path = [1, 28, 6, 12, 9, 5, 26, 29, 3, 2, 20, 10, 4, 15, 18, 17,
                 14, 22, 11, 19, 25, 7, 23, 27, 8, 24, 16, 13, 21, 1]
    plot_tour(data, best_path, False)


vrp()

TSP结果图

VRP结果图

二、动态图

原理:利用matplotlib的animation模块进行动态图的制作,其中保存为gif图片时需要使用PIL包,否则无法保存。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import PIL


def plot_tour(data, best_path, is_save):
    """
    绘制旅行图
    :param data: 包含位置坐标的字典类型数据
    :param best_path: 最优旅行路径
    :param is_save: 是否保存绘图
    :return:
    """
    fig, ax = plt.subplots()
    x = []
    y = []
    figure_list = []
    text_list = []
    for v in best_path:
        x.append(data[v][0])
        y.append(data[v][1])
        text_list.append(str(v))

        ax.plot(x, y, 'c^', linewidth=2, markersize=15)
        ax.text(data[v][0], data[v][1], str(v), ha='center', va='center_baseline', size=8)

        figure = ax.plot(x, y, '--', linewidth=2, markersize=20)

        figure_list.append(figure)
    ani = animation.ArtistAnimation(fig, figure_list, interval=200, repeat_delay=0)
    
    # 保存图片    
    ani.save("test.gif")

    plt.show()


def vrp():
    data = {
        1: (1150.0, 1760.0),
        2: (630.0, 1660.0),
        3: (40.0, 2090.0),
        4: (750.0, 1100.0),
        5: (750.0, 2030.0),
        6: (1030.0, 2070.0),
        7: (1650.0, 650.0),
        8: (1490.0, 1630.0),
        9: (790.0, 2260.0),
        10: (710.0, 1310.0),
        11: (840.0, 550.0),
        12: (1170.0, 2300.0),
        13: (970.0, 1340.0),
        14: (510.0, 700.0),
        15: (750.0, 900.0),
        16: (1280.0, 1200.0),
        17: (230.0, 590.0),
        18: (460.0, 860.0),
        19: (1040.0, 950.0),
        20: (590.0, 1390.0),
        21: (830.0, 1770.0),
        22: (490.0, 500.0),
        23: (1840.0, 1240.0),
        24: (1260.0, 1500.0),
        25: (1280.0, 790.0),
        26: (490.0, 2130.0),
        27: (1460.0, 1420.0),
        28: (1260.0, 1910.0),
        29: (360.0, 1980.0)
    }
    best_path = [10, 4, 15, 18, 17, 14, 22, 11, 19, 25, 7, 23, 27, 8, 24, 16, 13, 10,
                 10, 21, 1, 28, 6, 12, 9, 5, 26, 29, 3, 2, 20, 10]
    plot_tour(data, best_path, False)


def tsp():
    data = {
        1: (1150.0, 1760.0),
        2: (630.0, 1660.0),
        3: (40.0, 2090.0),
        4: (750.0, 1100.0),
        5: (750.0, 2030.0),
        6: (1030.0, 2070.0),
        7: (1650.0, 650.0),
        8: (1490.0, 1630.0),
        9: (790.0, 2260.0),
        10: (710.0, 1310.0),
        11: (840.0, 550.0),
        12: (1170.0, 2300.0),
        13: (970.0, 1340.0),
        14: (510.0, 700.0),
        15: (750.0, 900.0),
        16: (1280.0, 1200.0),
        17: (230.0, 590.0),
        18: (460.0, 860.0),
        19: (1040.0, 950.0),
        20: (590.0, 1390.0),
        21: (830.0, 1770.0),
        22: (490.0, 500.0),
        23: (1840.0, 1240.0),
        24: (1260.0, 1500.0),
        25: (1280.0, 790.0),
        26: (490.0, 2130.0),
        27: (1460.0, 1420.0),
        28: (1260.0, 1910.0),
        29: (360.0, 1980.0)
    }
    best_path = [1, 28, 6, 12, 9, 5, 26, 29, 3, 2, 20, 10, 4, 15, 18, 17,
                 14, 22, 11, 19, 25, 7, 23, 27, 8, 24, 16, 13, 21, 1]
    plot_tour(data, best_path, False)


tsp()

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TSP问题(Traveling Salesman Problem)是一个经典的组合优化问题,指的是在给定的一些城市之间,寻找一条最短的路径,使得每个城市恰好被访问一次。 贪心算法是求解TSP问题的一种简单有效的方法,具体实现步骤如下: 1. 随机选择一个起始城市(可以通过输入来确定)。 2. 从起始城市开始,依次访问与当前城市距离最近的未访问城市,直到所有城市被访问。 3. 最后返回到起始城市,构成一条回路。 4. 计算回路的总距离,并输出路径和距离。 下面是使用python编写的TSP问题贪心算法求解程序: ```python import numpy as np # 定义城市的数量 N = 8 # 定义城市之间的距离矩阵 distance = np.array([ [0, 2, 4, 5, 3, 7, 6, 8], [2, 0, 7, 9, 3, 5, 8, 6], [4, 7, 0, 8, 6, 3, 2, 5], [5, 9, 8, 0, 4, 6, 7, 3], [3, 3, 6, 4, 0, 8, 9, 2], [7, 5, 3, 6, 8, 0, 3, 1], [6, 8, 2, 7, 9, 3, 0, 4], [8, 6, 5, 3, 2, 1, 4, 0] ]) # 定义起始城市 start_city = 0 # 定义已访问城市的集合 visited_cities = set([start_city]) # 定义路径和距离 path = [start_city] distance_sum = 0 # 依次访问所有城市 for i in range(N-1): # 找到与当前城市距离最近的未访问城市 min_distance = np.inf nearest_city = None for j in range(N): if j not in visited_cities and distance[path[-1]][j] < min_distance: min_distance = distance[path[-1]][j] nearest_city = j # 加入路径和更新距离 path.append(nearest_city) visited_cities.add(nearest_city) distance_sum += min_distance # 回到起始城市 path.append(start_city) distance_sum += distance[path[-2]][start_city] # 输出路径和距离 print("->".join([str(city) for city in path])) print("Total distance:", distance_sum) ``` 这个程序中,我们首先定义了城市的数量和城市之间的距离矩阵(这里采用了一个随机生成的距离矩阵)。然后定义起始城市为0,并将其加入已访问城市的集合中。接着从起始城市开始,依次访问与当前城市距离最近的未访问城市,直到所有城市被访问。最后回到起始城市,构成一条回路。程序输出路径和距离。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值