[Path Planning] Traveling Salesman TSP Problem of Simulated Annealing Algorithm [Matlab 031 Issue]

1. Introduction

The simulated annealing algorithm is an effective algorithm for solving the optimal solution. The simulated annealing algorithm is derived by imitating the natural annealing phenomenon. It takes advantage of the similarity between the annealing process of solid materials in physics and general optimization problems. It starts from a certain initial temperature, and with the continuous decrease of temperature, combined with the probability of sudden jump characteristics, it is random in the solution space. Find the global optimal solution.
Insert picture description here
The traveling salesman problem, the TSP problem (Travelling Salesman Problem), is also translated into 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

Four, remarks

Complete code or writing to add QQ912100926 past review
>>>>>>
[Path planning] Particle swarm optimization algorithm for three-dimensional UAV path planning [Matlab 012]
[Path planning] Genetic algorithm for open vehicles in multiple logistics centers Path planning [Matlab 013]
[Path planning] Particle swarm algorithm for robot grid path planning [Matlab 014]
[Path planning] Ant colony algorithm for solving the shortest path [Matlab 015]
[Path planning] Immune algorithm for logistics center Site selection [Matlab 016]
[Path planning] Three-dimensional path planning of drones with artificial bee colony [Matlab 017]
[Path planning] Genetic algorithm based on grid map robot path planning [Matlab 018]
[Path planning] Ant Swarm algorithm for multiple drone attack scheduling [Matlab 019]
[Path planning] Genetic algorithm for optimal path planning of robots based on grid map [Matlab 020]
[Path planning] Genetic algorithm for multiple unmanned systems considering the order of distribution [Matlab 021 Issue]
[Path Planning]
Multi-center VRP Problem of Ant Colony Algorithm [Matlab 022] [Path Planning] Ant Colony Algorithm Solving Multi-center VRP with Time Window [Matlab 023 Issue]
[ Path planning] Multi-center VRP solution based on genetic algorithm [Matlab 024]
[Path planning] Simulated annealing to solve VRP problem [Matlab 025]
[Path planning] A star grid path planning [Matlab 026]
[Path planning] Based on A two-way optimization particle swarm grid map path planning with cross factor [Matlab 027]
[Path Planning] [TSP] Ant Colony Algorithm for Solving TSP Problem with GUI [Matlab 028]
[Path Planning] Ant Colony Algorithm for Raster Map Path Planning [Matlab 029]
[Path Planning] Genetic Algorithm for Traveling Salesman TSP [ Matlab Issue 030]

Guess you like

Origin blog.csdn.net/m0_54742769/article/details/113048181