17-2 Using simulated annealing algorithm for path planning - looking for the optimal route and total distance passing through 14 cities (with matlab program)

1. Brief description

      The simulated annealing algorithm is derived from the principle of solid annealing and is a probability-based algorithm. The simulated annealing algorithm is an optimization algorithm that can effectively avoid falling into a local minimum and eventually tend to a global optimal serial structure by endowing the search process with a time-varying probability jump that eventually tends to zero.

flow chart

Simulated annealing algorithm:
  The principle of simulated annealing algorithm is based on the similarity between the annealing process of metal substances and the optimization problem. When the substance is heated, the Brownian motion between the particles is enhanced, and after reaching a certain intensity, annealing is carried out, and the thermal motion of the particles is weakened, and gradually tends to be orderly, and finally reaches stability.
  My understanding of this passage is: During the heating process of an object, the movement between particles is very active, which is mapped to the mountain climbing process. At this time, the mountain climber has a very strong ability and can jump from one mountain to another. At this time, the problem of falling into a local optimum in the hill-climbing algorithm can be perfectly solved. At this time, it should be noted that the theory of the hill-climbing algorithm is still running, and it is still judging the highest hill, that is, the overall jump still tends to go to the highest point. But it’s not good to be too active. It’s possible to jump over the best hill with just one jump. What should I do? It doesn't matter, this is the annealing algorithm, and the essence lies in the annealing process. When the object is heated to a certain temperature, it begins to anneal, that is, to cool down. As the temperature drops, the movement between particles weakens, that is, the physical strength of the climber gradually decreases. At this time, the jumping ability is weakened. Halfway up the mountain. At this time, due to the decline in physical strength, but the overall trend was still going to the highest mountain before, then at a certain point, you will definitely find the highest mountain, and you will not be able to jump out of the highest mountain (you know, the more physical you falling, the more jumping does not move). Until the material temperature drops to the point where the interparticle motion can be ignored, the mountain climber may be so tired that he can only walk. At this time, the whole algorithm is the same as the mountain climbing algorithm, and can only find the local optimal solution on the highest mountain. (But as the highest mountain, the local optimal solution is the global optimal solution). This is the theoretical basis that the simulated annealing algorithm finds from the annealing process.
  There is something to think about in the above explanation. The inter-particle movement of matter is caused by temperature changes. So what causes the physical problems of mountain climbers? time? If you think about this problem carefully, you can find the wisdom of the person who proposed the simulated annealing algorithm.
The following part explains the reference to Station B: Mathematical Modeling Qingfeng Second Live Broadcast: Simulated Annealing Algorithm
  The ability in the simulated annealing algorithm is changed by the probability obtained over time. This sentence can be divided into two sentences. First, changes are caused by probabilities; second, probabilities change over time. Imagine the simulated annealing algorithm. If the climber is currently in a position, the altitude becomes higher when he goes to the left, and the altitude becomes lower when he goes to the right. ), then does he want to go to the right? If your answer is to refuse to go to the right, then the algorithm you use is a hill-climbing algorithm, and you will fall into a local optimum with a high probability. Therefore, the correct answer is to accept this result with a certain probability, so how to calculate the probability? Assume the first case: the altitude of the next step is almost the same as that of the current point; the second case: the altitude of the next step is very different from the altitude of the current point. Are you more inclined to accept situation one. Restore the altitude to a function value, that is, the function value (altitude value) f(A) of the current point, and the function value f(B) of the next step. The smaller the difference between them, the more likely you are to accept the next point big.
 

2. Code

%% I. Clear the environment variable
clear all
clc

%% II. Import city location data
X = [16.4700 96.1000
     16.4700 94.4400
     20.0900 92.5400 22.3900
     93.3700
     25.2300 97.2400      22.0000
     96.0500      20.4700 97.0 200 17.2000 96.2900 16.3000 97.3800 14.0500 98.1200      16.5300 97.3800      21.5200 95.5900      19.4100 97.1300      20.0900
     92.5500      ];






%% III. Calculate distance matrix
D = Distance(X); % Calculate distance matrix with 0 on the diagonal
N = size(D,1); % Number of cities

%% IV. Initialization parameter
T0 = 1e10; % initial temperature
Tend = 1e-30; % end temperature
L = 2; % number of iterations at each temperature, not used
q = 0.9; % cooling rate
Time = ceil(double( solve([num2str(T0) '*(0.9)^x = ',num2str(Tend)]))); % calculate the number of iterations% Time = 132; count = 0; % iteration
count
Obj
= zeros(Time,1 ); % target value matrix initialization
track = zeros(Time,N); % optimal route matrix initialization for each generation

%% V. Randomly generate an initial route
S1 = randperm(N);
DrawPath(S1,X)
disp('A random value in the initial population:')
OutputPath(S1);
Rlength = PathLength(D,S1);
disp (['Total distance:',num2str(Rlength)]);

%% VI. Iterative optimization
while T0 > Tend
    count = count + 1; % Update iteration times
    temp = zeros(L,N+1);
    %%
    % 1. Generate a new solution Add a disturbance
    S2 = NewAnswer(S1);
    % %
    % 2. Metropolis rule to judge whether to accept the new solution
    [S1,R] = Metropolis(S1,S2,D,T0); %Metropolis sampling algorithm
    %%
    % 3. Record the optimal route for each iteration
    if count == 1 || R < Obj(count-1)
        Obj(count) = R; %If the optimal distance under the current temperature is less than the previous distance, record the current distance
    else
        Obj(count) = Obj(count-1);%If the current If the optimal distance under temperature is greater than the previous distance, record the previous distance
    end
    track(count,:) = S1;
    T0 = q * T0; % cool down
end

%% VII. Iterative graph of the optimization process
figure
plot(1:count,Obj)
xlabel('Number of iterations')
ylabel('Distance')
title('Optimization process')

%% VIII. Draw the optimal path graph
DrawPath(track(end,:),X)

%% IX. Output the route and total distance of the optimal solution
disp('optimal solution:')
S = track(end,:);
p = OutputPath(S);
disp(['total distance:',num2str(PathLength (D,S))]);
 

3. Running results

 

 

Guess you like

Origin blog.csdn.net/m0_57943157/article/details/130992563#comments_27449588