Traveling Salesman TSP Problem Based on Simulated Annealing Algorithm [Matlab Issue 136] [Path Planning 21]

1. Introduction

     模拟退火算法是一个有效的求解最优解的算法。模拟退火算法因模仿自然界退火现象而得,利用了物理中固体物质的退火过程与一般优化问题的相似性从某一初始温度开始,伴随温度的不断下降,结合概率突跳特性在解空间中随机寻找全局最优解。

Insert picture description here
The traveling salesman problem, the TSP problem (Travelling Salesman Problem), is also translated as the traveling salesman problem and the salesman problem. It is one of the famous problems in the field of mathematics. Suppose there is a traveling merchant who wants to visit n cities and he must choose the path he wants to take. The restriction of the path is that each city can only be visited once, and he must return to the original city in the end. The path selection goal is that the required path distance is the smallest value among all paths.
So far, none of these problems has found an effective algorithm. I tend to accept the conjecture that NP-complete problems (NP-Complet or NPC) and NP-hard problems (NP-Hard or NPH) do not have effective algorithms. I believe that large-scale examples of such problems cannot be solved by accurate algorithms and must be sought for such problems. Effective approximation algorithm.
The application areas of TSP "Traveling Salesman Problem" include: how to plan the most reasonable and efficient road traffic to reduce congestion; how to better plan logistics to reduce operating costs; how to better set up nodes in the Internet environment to better Let information flow and so on.

Second, the source code

2.1 swap.m

function [ newpath , position ] = swap( oldpath , number )
% 对 oldpath 进 行 互 换 操 作
% number 为 产 生 的 新 路 径 的 个 数
% position 为 对 应 newpath 互 换 的 位 置
m = length( oldpath ) ; % 城 市 的 个 数
newpath = zeros( number , m ) ;
position = sort( randi( m , number , 2 ) , 2 ); % 随 机 产 生 交 换 的 位 置
for i = 1 : number
newpath( i , : ) = oldpath ;
% 交 换 路 径 中 选 中 的 城 市
newpath( i , position( i , 1 ) ) = oldpath( position( i , 2 ) ) ;
newpath( i , position( i , 2 ) ) = oldpath( position( i , 1 ) ) ;
end

2.2 pathfare.m

function [ objval ] = pathfare( fare , path )
% 计 算 路 径 path 的 代 价 objval
% path 为 1 到 n 的 排 列 ,代 表 城 市 的 访 问 顺 序 ;
% fare 为 代 价 矩 阵 , 且 为 方 阵 。
[ m , n ] = size( path ) ;
objval = zeros( 1 , m ) ;
for i = 1 : m
for j = 2 : n
objval( i ) = objval( i ) + fare( path( i , j - 1 ) , path( i , j ) ) ;
end
objval( i ) = objval( i ) + fare( path( i , n ) , path( i , 1 ) ) ;
end

2.3 distance.m

function [ fare ] = distance( coord )
% 根 据 各 城 市 的 距 离 坐 标 求 相 互 之 间 的 距 离
% fare 为 各 城 市 的 距 离 , coord 为 各 城 市 的 坐 标
[ v , m ] = size( coord ) ; % m 为 城 市 的 个 数
fare = zeros( m ) ;
for i = 1 : m % 外 层 为 行
for j = i : m % 内 层 为 列
fare( i , j ) = ( sum( ( coord( : , i ) - coord( : , j ) ) .^ 2 ) ) ^ 0.5 ;
fare( j , i ) = fare( i , j ) ; % 距 离 矩 阵 对 称
end
end

2.4 myplot.m

function [ ] = myplot( path , coord , pathfar )
% 做 出 路 径 的 图 形
% path 为 要 做 图 的 路 径 ,coord 为 各 个 城 市 的 坐 标
% pathfar 为 路 径 path 对 应 的 费 用
len = length( path ) ;
clf ;
hold on ;
title( [ '近似最短路径如下,路程为' , num2str( pathfar ) ] ) ;
plot( coord( 1 , : ) , coord( 2 , : ) , 'ok');
pause( 0.1 ) ;
for ii = 2 : len
plot( coord( 1 , path( [ ii - 1 , ii ] ) ) , coord( 2 , path( [ ii - 1 , ii ] ) ) , '-b');
x = sum( coord( 1 , path( [ ii - 1 , ii ] ) ) ) / 2 ;
y = sum( coord( 2 , path( [ ii - 1 , ii ] ) ) ) / 2 ;
text( x , y , [ '(' , num2str( ii - 1 ) , ')' ] ) ;
pause( 0.4 ) ;
end
plot( coord( 1 , path( [ 1 , len ] ) ) , coord( 2 , path( [ 1 , len ] ) ) , '-b' ) ;
x = sum( coord( 1 , path( [ 1 , len ] ) ) ) / 2 ;
y = sum( coord( 2 , path( [ 1 , len ] ) ) ) / 2 ;
text( x , y , [ '(' , num2str( len ) , ')' ] ) ;
pause( 0.4 ) ;
hold off ;

Three, operation effect

3.1 Test one
3.1.1 Data

[ 0.6683 0.6195 0.4    0.2439 0.1707 0.2293 0.5171 0.8732 0.6878 0.8488 ; ...
  0.2536 0.2634 0.4439 0.1463 0.2293 0.761  0.9414 0.6536 0.5219 0.3609 ] ;

3.1.2 Effect

run(‘D:\0COCO\Sys\Documents\MATLAB\tsp\main.m’)
请输入城市的坐标
Coord=[ 0.6683 0.6195 0.4 0.2439 0.1707 0.2293 0.5171 0.8732 0.6878 0.8488 ;0.2536 0.2634 0.4439 0.1463 0.2293 0.761 0.9414 0.6536 0.5219 0.3609 ] ;
近似最优路径为:7 8 10 9 3 5 4 1 2 6
近似最优路径路程 pathfare= 3.1617

Insert picture description here
3.2 Test Two
3.2.1 Data

[ 66.83 61.95 40    24.39 17.07 22.93 51.71 87.32 68.78 84.88 50 40 25 ; 
  25.36 26.34 44.39 14.63 22.93 76.1  94.14 65.36 52.19 36.09 30 20 26] ;

3.2.2 Effect

run(‘D:\0COCO\Sys\Documents\MATLAB\tsp\main.m’)
请输入城市的坐标
Coord=[ 66.83 61.95 40 24.39 17.07 22.93 51.71 87.32 68.78 84.88 50 40 25 ;
25.36 26.34 44.39 14.63 22.93 76.1 94.14 65.36 52.19 36.09 30 20 26] ;
近似最优路径为:1 11 13 4 5 6 7 8 10 9 3 12 2
近似最优路径路程 pathfare= 332.6469

Insert picture description here
Note: Add QQ1564658423 for complete code or writing.
Past review>>>>>>
[Matlab 015] [Path planning 1] 3D UAV path planning based on particle swarm matlab source code
[Matlab 017] [Path planning 2] Using genetic algorithms to prepare the opening of multiple logistics centers -Style vehicle routing problem Matlab program
[Matlab 018] [Path planning 3] Robot grid path planning based on particle swarms
[Matlab 019] [Path planning 4] Ant colony algorithm to solve the shortest path matlab
[Matlab 020] [Path planning 5] Matlab immune algorithm for logistics center location problem
[Matlab 021] [Path planning 6] Three-dimensional path planning of drones based on artificial bee colony
[Matlab 027] [Path planning 7] Based on grid map-genetic algorithm Robot optimal path planning
[Matlab 034 period] [Path planning 8] Ant colony-based multi-UAV attack scheduling
based on grid map-genetic algorithm for robot optimal path planning [Matlab 022 period] [Path planning 9]
Multi-UAV cooperative target allocation modeling and genetic algorithm solution considering allocation order [Matlab 110] [Path planning 10]
Ant colony algorithm for multi-center vrp problem matlab [Matlab 111] [Path planning 11]
Based on ant colony algorithm Solving multi-center VRP problem with time windows matlab [Matlab 112] [Path planning 12]
Based on ant colony algorithm to solve multi-center VRP problems with time window matlab [Matlab 113] [Path planning 13]
Multi-center based on genetic algorithm VRP solution matlab【Matlab 114】【Path planning 14】
Simulated annealing to solve the VRP problem matlab [Matlab 115] [Path planning 15]
A star-based grid path planning [Matlab 116] [Path planning 16]
A bidirectional optimization particle swarm grid map path planning with cross factors [Matlab 117] [Path planning 17]
[TSP] Ant colony algorithm for solving TSP problems matlab with GUI [Matlab 118] [Path planning 18]
Based on ant colony algorithm grid map path planning matlab [Matlab 119] [Path planning 19]
Traveling Salesman TSP Problem Based on Genetic Algorithm [Matlab Issue 135] [Path Planning 20]

Guess you like

Origin blog.csdn.net/TIQCmatlab/article/details/112857129