Python繪制TSP、VRP問(wèn)題求解結(jié)果圖全過(guò)程
【代碼】Python繪制TSP、VRP問(wèn)題求解結(jié)果圖(包含靜態(tài)圖與動(dòng)態(tài)圖)。
一、靜態(tài)圖
import matplotlib.pyplot as plt
def plot_tour(data, best_path, is_save):
"""
繪制旅行圖
:param data: 包含位置坐標(biāo)的字典類(lèi)型數(shù)據(jù)
:param best_path: 最優(yōu)旅行路徑
: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結(jié)果圖

VRP結(jié)果圖
二、動(dòng)態(tài)圖
原理:
利用matplotlib的animation模塊進(jìn)行動(dòng)態(tài)圖的制作,其中保存為gif圖片時(shí)需要使用PIL包,否則無(wú)法保存。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import PIL
def plot_tour(data, best_path, is_save):
"""
繪制旅行圖
:param data: 包含位置坐標(biāo)的字典類(lèi)型數(shù)據(jù)
:param best_path: 最優(yōu)旅行路徑
: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()
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python基于opencv實(shí)現(xiàn)的簡(jiǎn)單畫(huà)板功能示例
這篇文章主要介紹了Python基于opencv實(shí)現(xiàn)的簡(jiǎn)單畫(huà)板功能,結(jié)合實(shí)例形式分析了Python使用opencv模塊進(jìn)行圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Python RuntimeWarning:invalid value encounter
這篇文章主要介紹了Python RuntimeWarning:invalid value encountered in double_scalars處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
帶你學(xué)習(xí)Python如何實(shí)現(xiàn)回歸樹(shù)模型
這篇文章主要介紹了Python如何實(shí)現(xiàn)回歸樹(shù)模型,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
用Python的SimPy庫(kù)簡(jiǎn)化復(fù)雜的編程模型的介紹
這篇文章主要介紹了用Python的SimPy庫(kù)簡(jiǎn)化復(fù)雜的編程模型的介紹,本文來(lái)自于官方的開(kāi)發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04
pycharm 在debug循環(huán)時(shí)快速debug到指定循環(huán)次數(shù)的操作方法
在 PyCharm 中,可以使用條件斷點(diǎn)來(lái)實(shí)現(xiàn)在特定循環(huán)次數(shù)后停止調(diào)試,本文重點(diǎn)介紹pycharm 在debug循環(huán)時(shí)快速debug到指定循環(huán)次數(shù)的操作方法,需要的朋友可以參考下2024-04-04
Python線程之定位與銷(xiāo)毀的實(shí)現(xiàn)
這篇文章主要介紹了Python線程之定位與銷(xiāo)毀的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python處理表格dataframe時(shí)實(shí)現(xiàn)行轉(zhuǎn)列和列轉(zhuǎn)行的方法
在python中,我們可以使用pandas庫(kù)來(lái)處理表格數(shù)據(jù),而pandas庫(kù)中的核心數(shù)據(jù)類(lèi)型就是DataFrame,在處理DataFrame數(shù)據(jù)時(shí),有時(shí)候需要將行轉(zhuǎn)換為列,或者將列轉(zhuǎn)換為行,所以本文給大家介紹了如何使用pandas庫(kù)實(shí)現(xiàn)行轉(zhuǎn)列和列轉(zhuǎn)行,需要的朋友可以參考下2025-12-12
使用Python Flask實(shí)現(xiàn)簡(jiǎn)易文件上傳功能
在平時(shí)工作中,文件上傳是一項(xiàng)常見(jiàn)的需求,例如將應(yīng)用異常時(shí)通過(guò)腳本生成的dump文件收集起來(lái)進(jìn)行分析,但實(shí)現(xiàn)起來(lái)卻可能相當(dāng)復(fù)雜,在本文中,我們將探討如何使用Flask實(shí)現(xiàn)文件上傳功能,編寫(xiě)Dockerfile將應(yīng)用程序通過(guò)docker部署,需要的朋友可以參考下2024-05-05

