Simulated annealing algorithm (SA algorithm)

The simulated annealing algorithm is a stochastic algorithm, and it does not necessarily find the global optimal solution, but can find the approximate optimal solution of the problem relatively quickly. If the parameters are set properly, the search efficiency of the simulated annealing algorithm is higher than the exhaustive method.

1. Before getting into the topic, let’s briefly introduce the principle of solid annealing in physics.

In thermodynamics, annealing (annealing) refers to a physical phenomenon in which an object gradually cools down. The lower the temperature, the lower the energy state of the object; when it is low enough, the liquid begins to condense and crystallize. In the crystalline state, the energy state of the system is the lowest. When nature slowly cools down (ie, annealing), it can "find" the lowest energy state: crystallization. However, if the process is too rapid or too fast, rapid cooling (also known as "quenching") will result in an amorphous form that is not the lowest energy state.

As shown in the picture below, first (left picture) the object is in an amorphous state. We heat the solid to a sufficiently high level (middle image), then let it cool down slowly, and then anneal it (right image). When heating, the internal particles of the solid become disordered with the temperature rise, and the internal energy increases. When slowly cooling, the particles gradually become orderly, reaching an equilibrium state at each temperature, and finally reach the ground state at room temperature, and the internal energy decreases. Is the smallest (the object appears in the form of a crystal at this time).
Write picture description here

Slowly cool down so that the object molecules have enough time to find a place to settle at each temperature, and then gradually, until the lowest energy state can be obtained at the end, the system is the most stable.

2. Briefly introduce its history

The earliest idea of ​​simulated annealing algorithm (Simulated Annealing, SA) was proposed by N. Metropolis [1] et al. in 1953. In 1983, S. Kirkpatrick and others successfully introduced the annealing idea into the field of combinatorial optimization. It is a stochastic optimization algorithm based on Monte-Carlo iterative solution strategy. Its starting point is based on the similarity between the annealing process of solid matter in physics and general combinatorial optimization problems. The simulated annealing algorithm starts from a certain higher initial temperature, and with the continuous decrease of temperature parameters, combined with the probability of sudden jump characteristics, it randomly finds the global optimal solution of the objective function in the solution space, that is, the local optimal solution can probabilistically jump out and merge. Eventually tends to the global optimum.
At almost the same time, the European physicist V. Carny also published almost the same results, but the two were discovered independently; it was just that Carny was "unlucky" and no one noticed his masterpiece at the time; perhaps it can be said that "Science "The magazine is marketed globally, and has a high degree of "exposure" and is well-known. However, Carny published his results in another special academic journal "J.Opt.Theory Appl." with a small circulation, which did not arouse the due attention.
The simulated annealing algorithm is a general optimization algorithm. In theory, the algorithm has probabilistic global optimization performance. It has been widely used in engineering, such as VLSI, production scheduling, control engineering, machine learning, neural networks, and signal processing.
The simulated annealing algorithm is an optimization algorithm that gives the search process a time-varying and eventually zero probability jump, which can effectively avoid falling into a local minimum and eventually a global optimal serial structure.

3. Simulate Anneal

If you are still dizzy about the physical meaning of annealing, it doesn't matter that we have a simpler way to understand it. Imagine if we now have the following function, and now we want to find the (global) optimal solution of the function.
Write picture description here

1. First briefly introduce the hill climbing algorithm (this algorithm is flawed)
Before introducing simulated annealing, first introduce the hill climbing algorithm. The hill climbing algorithm is a simple greedy search algorithm. The algorithm selects an optimal solution from the adjacent solution space of the current solution as the current solution each time until a local optimal solution is reached.

The hill-climbing algorithm is very simple to implement, and its main disadvantage is that it will fall into a local optimal solution, and may not necessarily be able to search for the global optimal solution. Assuming that the starting point is A (hill climbing algorithm), when point B is reached, the process is over. Because neither B nor left can move to a point lower than it, the global optimal solution cannot be found at this time.

2. Simulated annealing algorithm
If the Greedy strategy is adopted, then start the trial from point A, if the function value continues to decrease, then the trial process will continue. And when we reach point B, obviously our exploration process is over (because no matter which direction we work hard in, the result will only get bigger and bigger). In the end, we can only find a partial final solution B. (Same as the hill climbing algorithm above)

Simulated annealing is actually a Greedy algorithm, but its search process introduces random factors . The simulated annealing algorithm accepts a solution that is worse than the current solution with a certain probability, so it may jump out of this local optimal solution and reach the global optimal solution. Take the above figure as an example, after the simulated annealing algorithm has searched for the local optimal solution B, it will continue to move to the right with a certain probability. Perhaps after several such moves that are not local optimal, the peak point between B and C will be reached, and the local minimum B will be jumped out.

According to Metropolis criterion, the probability that a particle tends to equilibrium at temperature T is exp(-ΔE/(kT)), where E is the internal energy at temperature T, ΔE is the number of changes, and k is Boltzmann's constant. The Metropolis criterion is often expressed as the
Write picture description here
Metropolis criterion. It indicates that when the temperature is T, the probability of a temperature drop with an energy difference of dE is P(dE), expressed as: P(dE) = exp(dE/(kT)). Where k is a constant, exp represents the natural exponent , and dE<0. So P and T are positively correlated. This formula means: the higher the temperature, the greater the probability of a cooling with an energy difference of dE; the lower the temperature, the smaller the probability of cooling. Also, since dE is always less than 0 (because the annealing process is a process of gradually decreasing temperature), dE/kT <0, so the value range of P(dE) is (0,1). As the temperature T decreases, P(dE) will gradually decrease.

We regard a move to a poor solution as a temperature jump process, and we accept such a move with probability P(dE). In other words, when using solid annealing to simulate the combinatorial optimization problem, the internal energy E is simulated as the objective function value f, and the temperature T evolves into the control parameter t, that is, the simulated annealing algorithm for solving the combinatorial optimization problem is obtained: the initial solution i and the control Starting with the initial value of the parameter t, repeat the iteration of “generate a new solution → calculate the objective function difference → accept or discard” for the current solution, and gradually attenuate the value of t. The current solution at the end of the algorithm is the approximate optimal solution obtained, which is based on A heuristic random search process of Monte Carlo iterative solution method. The annealing process is controlled by the Cooling Schedule, including the initial value t of the control parameter and its attenuation factor Δt, the number of iterations L at each value of t, and the stopping condition S.

In summary:

(1) If f( Y(i+1)) <= f( Y(i)) (that is, a better solution is obtained after the movement), the movement is always accepted;
(2) If f( Y(i+1) ))> f( Y(i)) (that is, the solution after the movement is worse than the current solution), then the movement is accepted with a certain probability, and this probability gradually decreases over time (gradually decreases to stabilize), which is equivalent to the above In the figure, when moving from B to the small peak between BC, the probability of each shift to the right (that is, to accept a worse value) gradually decreases. If this slope is particularly long, then it is very likely that we will not climb this slope in the end. If it is not too long, it is likely to be overturned, depending on the setting of the attenuation t.

Regarding the ordinary Greedy algorithm and simulated annealing, there is an interesting analogy:

(1) Ordinary Greedy algorithm: The rabbit jumps towards a place lower than it is now. It found the lowest valley not far away. But this valley is not necessarily the lowest. This is the ordinary Greedy algorithm, and it cannot guarantee that the local optimal value is the global optimal value.
(2) Simulated annealing: the rabbit is drunk. It jumped randomly for a long time. During this period, it may go low, or it may step into flat ground. However, it gradually became sober and jumped in the lowest direction. This is simulated annealing.

3. Model of simulated annealing algorithm

1. The simulated annealing algorithm can be decomposed into three parts : solution space , objective function and initial solution .
2. The basic idea of ​​simulated annealing:
(1) Initialization: initial temperature T (sufficiently large), initial solution state S (the starting point of the algorithm iteration), the number of iterations for each T value L
(2) for k=1,… , L does the steps (3) to 6:
(3) Generate a new solution S′
(4) Calculate the increment ΔT=C(S′)-C(S), where C(S) is the evaluation function
(5) If ΔT<0, accept S′ as the new current solution, otherwise accept S′ as the new current solution with the probability exp(-ΔT/T).
(6) If the termination condition is met, output the current solution as the optimal solution, and end program.
The termination condition is usually taken as the termination of the algorithm when several consecutive new solutions are not accepted.
(7) T decreases gradually, and T->0, then go to step 2.
3 Steps of the
simulated annealing algorithm The generation and acceptance of a new solution of the simulated annealing algorithm can be divided into the following four steps:

The first step is to generate a new solution in the solution space from the current solution by a generating function; in order to facilitate subsequent calculations and acceptance, and reduce the time consumption of the algorithm, the method of generating a new solution from the current new solution through simple transformation is usually selected. , Such as the replacement and exchange of all or part of the elements that constitute the new solution. Note that the transformation method that produces the new solution determines the neighborhood structure of the current new solution, which has a certain impact on the selection of the cooling schedule.

The second step is to calculate the difference of the objective function corresponding to the new solution. Because the objective function difference is only generated by the transformation part, the calculation of the objective function difference is best calculated in increments. Facts have shown that for most applications, this is the fastest way to calculate the difference of the objective function.

The third step is to judge whether the new solution is accepted. The basis for judgment is an acceptance criterion. The most commonly used acceptance criterion is the Metropolis criterion: if ΔT<0, accept S′ as the new current solution S, otherwise use the probability exp(-ΔT /T) Accept S'as the new current solution S.

The fourth step is to replace the current solution with the new solution when the new solution is determined to be accepted. This only needs to realize the transformation part of the current solution that corresponds to when the new solution is generated, and at the same time modify the objective function value. At this point, the current solution has achieved one iteration. The next round of trials can be started on this basis. When the new solution is judged to be discarded, the next round of trials will continue on the basis of the original current solution.
The simulated annealing algorithm has nothing to do with the initial value, and the solution obtained by the algorithm has nothing to do with the initial solution state S (which is the starting point of the algorithm iteration); the simulated annealing algorithm has asymptotic convergence, which has been theoretically proved to be a kind of convergence with probability l The global optimization algorithm of the global optimal solution; the simulated annealing algorithm is parallel.
Write picture description here
4. Pseudo code of simulated annealing algorithm

/*
* J(y):在状态y时的评价函数值
* Y(i):表示当前状态
* Y(i+1):表示新的状态
* r: 用于控制降温的快慢
* T: 系统的温度,系统初始应该要处于一个高温的状态
* T_min :温度的下限,若温度T达到T_min,则停止搜索
*/
while( T > T_min )
{
  dE = J( Y(i+1) ) - J( Y(i) ) ; 

  if ( dE >=0 ) //表达移动后得到更优解,则总是接受移动
Y(i+1) = Y(i) ; //接受从Y(i)到Y(i+1)的移动
  else
  {
// 函数exp( dE/T )的取值范围是(0,1) ,dE/T越大,则exp( dE/T )也越大
if ( exp( dE/T ) > random( 0 , 1 ) )
Y(i+1) = Y(i) ; //接受从Y(i)到Y(i+1)的移动
  }
  T = r * T ; //降温退火 ,0<r<1 ,r一般被设置为0.98或0.99等等。r越大,降温越慢;r越小,降温越快
  /*
  * 若r过大,则搜索到全局最优解的可能会较高,但搜索的过程也就较长。若r过小,则搜索的过程会很快,但最终可能会达到一个局部最优值
  */
  i ++ ;
}

4. Simulated annealing algorithm to solve TSP (Traveling Salesman Problem)

Traveling Salesman Problem (TSP, Traveling Salesman Problem): There are N cities, and it is required to start from one of the problems, and only traverse all the cities, and then return to the city of departure to find the shortest route. (Hamilton circuit)

  The traveling salesman problem is a so-called NP-complete problem. The TSP can only be accurately solved by exhaustively enumerating all the path combinations, and its time complexity is O(N!).

  The simulated annealing algorithm can be used to find an approximate optimal path of TSP relatively quickly. (It is also possible to use genetic algorithms)
  The idea of ​​simulated annealing to solve TSP:

  1. Generate a new traversal path P(i+1), and calculate the length L( P(i+1)) of the path P(i+1)

  2. If L(P(i+1)) <L(P(i)), then accept P(i+1) as the new path, otherwise accept P(i+1) with the probability of simulated annealing, and then cool down

  3. Repeat steps 1 and 2 until the exit conditions are met

      There are many ways to generate new traversal paths, three of which are listed below:

  4. Randomly select 2 nodes and exchange the order of these 2 nodes in the path.

  5. Randomly select 2 nodes, and reverse the order of the nodes between these 2 nodes in the path.

  6. Randomly select 3 nodes m, n, k, and then shift the node between nodes m and n to the back of node k.

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <time.h>
#include <math.h>

#define N     30      //城市数量
#define T     3000    //初始温度
#define EPS   1e-8    //终止温度
#define DELTA 0.98    //温度衰减率

#define LIMIT 1000   //概率选择上限
#define OLOOP 20    //外循环次数
#define ILOOP 100   //内循环次数

using namespace std;

//定义路线结构体
struct Path
{
    int citys[N];
    double len;
};

//定义城市点坐标
struct Point
{
    double x, y;
};

Path bestPath;        //记录最优路径
Point p[N];       //每个城市的坐标
double w[N][N];   //两两城市之间路径长度
int nCase;        //测试次数

double dist(Point A, Point B)
{
    return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}

void GetDist(Point p[], int n)
{
    for(int i = 0; i < n; i++)
        for(int j = i + 1; j < n; j++)
            w[i][j] = w[j][i] = dist(p[i], p[j]);
}

void Input(Point p[], int &n)
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%lf %lf", &p[i].x, &p[i].y);
}

void Init(int n)
{
    nCase = 0;
    bestPath.len = 0;
    for(int i = 0; i < n; i++)
    {
        bestPath.citys[i] = i;
        if(i != n - 1)
        {
            printf("%d--->", i);
            bestPath.len += w[i][i + 1];
        }
        else
            printf("%d\n", i);
    }
    printf("\nInit path length is : %.3lf\n", bestPath.len);
    printf("-----------------------------------\n\n");
}

void Print(Path t, int n)
{
    printf("Path is : ");
    for(int i = 0; i < n; i++)
    {
        if(i != n - 1)
            printf("%d-->", t.citys[i]);
        else
            printf("%d\n", t.citys[i]);
    }
    printf("\nThe path length is : %.3lf\n", t.len);
    printf("-----------------------------------\n\n");
}

Path GetNext(Path p, int n)
{
    Path ans = p;
    int x = (int)(n * (rand() / (RAND_MAX + 1.0)));
    int y = (int)(n * (rand() / (RAND_MAX + 1.0)));
    while(x == y)
    {
        x = (int)(n * (rand() / (RAND_MAX + 1.0)));
        y = (int)(n * (rand() / (RAND_MAX + 1.0)));
    }
    swap(ans.citys[x], ans.citys[y]);
    ans.len = 0;
    for(int i = 0; i < n - 1; i++)
        ans.len += w[ans.citys[i]][ans.citys[i + 1]];
    cout << "nCase = " << nCase << endl;
    Print(ans, n);
    nCase++;
    return ans;
}

void SA(int n)
{
    double t = T;
    srand((unsigned)(time(NULL)));
    Path curPath = bestPath;
    Path newPath = bestPath;
    int P_L = 0;
    int P_F = 0;
    while(1)       //外循环,主要更新参数t,模拟退火过程
    {
        for(int i = 0; i < ILOOP; i++)    //内循环,寻找在一定温度下的最优值
        {
            newPath = GetNext(curPath, n);
            double dE = newPath.len - curPath.len;
            if(dE < 0)   //如果找到更优值,直接更新
            {
                curPath = newPath;
                P_L = 0;
                P_F = 0;
            }
            else
            {
                double rd = rand() / (RAND_MAX + 1.0);
                //如果找到比当前更差的解,以一定概率接受该解,并且这个概率会越来越小
                if(exp(dE / t) > rd && exp(dE / t) < 1)
                    curPath = newPath;
                P_L++;
            }
            if(P_L > LIMIT)
            {
                P_F++;
                break;
            }
        }
        if(curPath.len < bestPath.len)
            bestPath = curPath;
        if(P_F > OLOOP || t < EPS)
            break;
        t *= DELTA;
    }
}

int main(int argc, const char * argv[]) {

    freopen("TSP.data", "r", stdin);
    int n;
    Input(p, n);
    GetDist(p, n);
    Init(n);
    SA(n);
    Print(bestPath, n);
    printf("Total test times is : %d\n", nCase);
    return 0;
}

5. The general source of the content, reference:

1.http://blog.csdn.net/sci_m3/article/details/51539003
2.https://www.cnblogs.com/ranjiewen/p/6084052.html
3.https://baike.baidu.com/item/%E6%A8%A1%E6%8B%9F%E9%80%80%E7%81%AB%E7%AE%97%E6%B3%95/355508?fr=aladdin

Guess you like

Origin blog.csdn.net/Puppet__/article/details/79382113