Matlab implementation based on simulated annealing and tsp problem

This article has participated in the "Newcomer Creation Ceremony" event to start the road of Nuggets creation together

Definition and Flowchart

The simulated annealing algorithm is derived from the principle of solid annealing. It is a probability-based algorithm. The solid is heated to a sufficiently high level, and then slowly cooled. During heating, the particles inside the solid become disordered as the temperature rises, and the internal energy increases. However, when the particles gradually cool down, they will gradually become orderly, reaching an equilibrium state at each temperature, and finally reaching the ground state at room temperature, and the internal energy is reduced to a minimum. ==This is a popular science website about simulated annealing== Simulated annealing Simulated annealing is mainly divided into three parts: 1. Heating process. Its purpose is to enhance the thermal motion of the particles away from the equilibrium position. When the temperature is high enough, the solid will melt into a liquid, thereby eliminating the pre-existing inhomogeneous state of the system. 2. Isothermal process. For a closed system that exchanges heat with the surrounding environment and the temperature is constant, the spontaneous change of the system state is always in the direction of decreasing free energy. When the free energy is the smallest, the system reaches an equilibrium state. 3. Cooling process. The thermal motion of the particles is weakened, the system capacity is reduced, and the crystal structure is obtained. As an intelligent algorithm, simulated annealing can be used to find the optimal value of some complex problems. For example, it can be used well in the tsp problem. Next, the author implements the tsp problem based on simulated annealing in Matlab, so that readers can have a deeper understanding of the simulation. The process of annealing.insert image description here

Topics and Analysis

Here is the title link tsp example , the title is in English, readers can use Baidu translation to translate it. topic:insert image description here

Analysis: We first generate a matrix with 0 for each element except those element positions with n stones. The position of these non-zero elements is the coordinates to which our bulldozer will move. According to the requirements of the title, there are 10 non-zero elements (other problems are similar, change the data or other small details). Find the shortest path through all points, which can be converted into a tsp problem, so it is very familiar. Without further ado, look at the ==Matlab== code:

code and result

Please refer to my website for detailed code: detailed code

clear
clc
T0 = 1000;      % 起始温度
Tend = 1e-3;    % 终止温度
L = 200;    % 各温度下的迭代次数
q = 0.9;    % 降温速率
position = [2 8
    1 1
    5 9
    10 3
    4 5
    7 1
    3 2
    6 4
    5 5
    9 3];

% 计算各点之间的距离矩阵
[m,n] = size(position);
D = zeros(m);
for i = 1:m
    for j = 1:m
        if i == j
            D(i,j) = 0;
        else
            D(i,j) = sqrt((position(i,1)-position(j,1))^2+(position(i,2)-position(j,2))^2);
        end
    end
end

s1 = randperm(m); % 产生一个解、画图并输出路径
drawpath(s1,position);
disp('一可行解的路径')
outputpath(s1);
r = pathlength(D,s1);
disp(['回路长度为:',num2str(r)]);
count = 0;

% 开始迭代
while T0 > Tend
    temp = zeros(L,m+1);
    count = count + 1;
    for k = 1:L
        s2 = newanswer(s1);
        [s1,r] = metropolis(s1,s2,D,T0);
        temp(k,:) = [s1,r];
    end
    % 记录每次迭代的最优路线
    [~,index] = min(temp(:,end));
    record(count) = temp(index,end);
    track(count,:) = temp(index,1:end-1); 
    T0 = q*T0;
end
% 画图显示结果
[~,tip] = min(record);
drawpath(track(tip,:),position);
hold on 
title('最优路线')
figure
hold on
plot(1:count,record)
xlabel('迭代次数')
ylabel('距离')
title('优化过程')
disp(['最优路径与距离',num2str(record(tip))])
outputpath(track(tip,:));

Please refer to my website for detailed code: detailed code insert image description here insert image description here

insert image description hereFigure 1 is an arbitrary solution, Figure 2 is the optimal solution obtained by the simulated annealing algorithm, and Figure 3 is the optimization process of the distance in the iterative process. It can be seen from the figure that the optimal solution is finally found, indicating that simulated annealing is a good way to solve this problem.

Guess you like

Origin juejin.im/post/7117819043888758797