Optimization Algorithm-Simulated Annealing Algorithm

1. Concept

The simulated annealing algorithm (SA) is derived from the principle of solid annealing and is a probability-based algorithm.

Heating the solid to a sufficiently high temperature and then allowing it to slowly cool down will cause the internal particles of the solid to become disordered as the temperature rises. The internal energy increases and the molecules and atoms become more unstable. As it slowly cools, the particles become more orderly, the energy decreases, and the atoms become more stable. During the cooling (lowering) process, the solid reaches an equilibrium state at each temperature, and finally reaches the ground state at room temperature, and the internal energy is reduced to a minimum.
The simulated annealing algorithm starts from a certain higher initial temperature , and as the temperature parameters continue to decrease, it combines the probability jump characteristics to randomly find the global optimal solution of the objective function in the solution space, that is, the local optimal solution can probabilistically jump out of the union. Eventually it tends to the global optimum. The simulated annealing algorithm is an optimization algorithm that gives the search process a time-varying probability jump that eventually approaches zero, thereby effectively avoiding falling into a local minimum and eventually approaching the global optimal serial structure.

2. Hill Climbing Algorithm, or Gradient Descent Algorithm.
The difference between simulated annealing and the hill climbing algorithm (Hill Climbing) based on the greedy algorithm is that the simulated annealing algorithm may in some cases overcome the problem of falling into the local optimal solution instead of finding the global optimal solution. situation .

Hill-climbing algorithms are prone to falling into local optima.

 3. Simulated annealing algorithm

The simulated annealing algorithm consists of two parts, namely the Metropolis algorithm and the annealing process , which correspond to the inner loop and the outer loop respectively. The essence of the simulated annealing algorithm is a double-layer loop.

Metropolis Algorithm: Probabilistic Acceptance of Differential Solutions

For example, search for the minimum value in the figure above. Assume that the starting state is A. After multiple iterations, it is updated to the local optimal solution of B. At this time, it is found that when updating to B, the ability is lower than A, which means that it is close to the optimal solution. Therefore, After the state reaches B, it is found that the energy has increased in the next step. If it is gradient descent, it is not allowed to continue forward. If we adopt a certain probability to accept this difference solution, there is a probability to continue the search.

The formula is as shown in the figure below. If the next solution we find is smaller than the previous solution, then we will accept this differential solution. If the solution we find is larger than the current solution, then we will go to a certain probability. Accepting this difference means that we may or may not accept it.

 

Reinterpretation of this formula: It is a simple function, and its function image is shown in the figure below. This ensures that the probability of accepting the difference solution belongs to [0,1], and T (temperature) in the formula is determined by physics. It evolved from model annealing. Here we do not need to know its specific physical meaning, but explain it from a mathematical perspective.

At the beginning, we need the T value to be larger, so according to the monotonicity of the function, we can see that the P that accepts the differential solution is larger, which facilitates our global search. However, in the later stage, when the temperature drops, the T value becomes smaller. When the temperature approaches zero, we can only accept the decrease of the objective function, which helps us end the convergence and complete the iteration as soon as possible.

4. Parameter control in simulated annealing

(1) The initial temperature T should be selected high enough to ensure that the differential solution has a high probability of being accepted at the beginning and facilitate the global search. The higher the initial temperature, the greater the probability of obtaining a high-quality solution and the longer it takes.

(2) Annealing rate, that is, temperature decrease, the simplest way of decrease is exponential decrease:
T(n) = αT(n), n =1,2,3,…
(3) Program termination: one of them is satisfied

  • Reach the specified number of iterations, for example, iterate 200 times;
  • Reaching the termination temperature: for example, the temperature is less than 0.000001; if the temperature drops to the termination temperature or reaches the user-set threshold, annealing is completed
  • The optimal solution we found has not changed for M (for example, 30) iterations.

5. Main steps of simulated annealing algorithm

The essence of the simulated annealing algorithm is a two-layer loop. The outer loop controls the temperature change from high to low; in the inner loop, the temperature is fixed, random perturbations are added to the old solution to obtain a new solution, and the new solution is accepted according to certain rules.

① Coding of the solution: If the expression of the solution is simple, real number coding can be used.

② Determine the initial solution: randomly generate a solution A, calculate the objective function value f(A) corresponding to the solution A, and determine whether it is within the definition domain

③ Generation of new solutions in the neighborhood: Randomly generate a solution B near A, and calculate the objective function value corresponding to solution B. There is no unified method for generating new solutions, but after generating a new solution, it needs to be judged whether it is still within the definition domain.

④ Determine the energy function: For convenience when using it, we can directly use the function in the picture above

The lower the energy, the higher the probability of being accepted. Therefore, when solving the maximum objective function, the reciprocal of the objective function can be used as the energy function; when solving the minimum objective function, the objective function can be directly used as the energy function.

⑤ Metropolis criterion: If f(B)>f(A), then assign solution B to solution A, and then repeat the above steps (the idea of ​​hill climbing method); if f(B)≤f(A), then we calculate and accept The probability p of B, then we generate a random number r between [0,1]. If r<p, we assign solution B to solution A, and then repeat the above steps; otherwise we return to the third step, in Regenerate a solution B near the original A, and then continue.

⑥ Search for the optimal solution and store the optimal solution

6. How to generate new solutions

The way in which new solutions are generated is most important

For the TSP problem, if there are these cities, the method in the figure below can be used, but the specific problems will be analyzed in detail.

 

7. Code example python: simulated annealing algorithm to solve TSP model

① Coding of solution 

The TSP model is solved using natural number coding, with node 0 representing the initial city and nodes 1-7 representing the other seven cities. For example, 1, 7, 6, 3, 2, 5, 4 means starting from 0 and passing through cities 1, 7, 6, 3, 2, 5, 4 and finally returning to 0.

② Determine the initial solution

Randomly generate a sequence of numbers 1-7 appearing once each.

#numbers为需要经过的其他城市的数量
def initialization(numbers):
    path_random = np.random.choice(list(range(1, numbers + 1)), replace=False, size=numbers)
    path_random = path_random.tolist()
    return path_random

③ Generation of new neighborhood solutions

The two-point reversal method is used, that is, setting two points on the sequence, and then reversing the sequence at these two points to change the sequence. The reversal operation is as follows: Assume that the number sequence is A=1,4,5,2,3,6,7, and two points 2 and 6 are randomly generated, then A1=4,5,2,3,6, after reversal A1=6 ,3,2,5,4, A=1,6,3,2,5,4,7.

def generate_new(path):
    numbers = len(path)
    #随机生成两个不重复的点
    positions = np.random.choice(list(range(numbers)), replace=False, size=2)
    lower_position = min(positions[0], positions[1])
    upper_position = max(positions[0], positions[1])
    #将数列中段逆转
    mid_reversed = path[lower_position:upper_position + 1]
    mid_reversed.reverse()
    #拼接生成新的数列
    new_path = path[:lower_position]
    new_path.extend(mid_reversed)
    new_path.extend(path[upper_position + 1:])
    return new_path

④ Determine the energy function

The TSP model uses the shortest path as the objective function, so the path length can be directly used as the energy function.

#length_mat为各个节点之间的最短距离矩阵
def e(path, length_mat):
    numbers = len(path) - 1
    length = length_mat[0][path[0]] + length_mat[path[-1]][0]
    for i in range(numbers):
        length += length_mat[path[i]][path[i + 1]]
    return length

⑤ New solution acceptance criteria

Use standard Metropolis guidelines.

def metropolis(e, new_e, t):
    if new_e <= e:
        return True
    else:
        p = math.exp((e - new_e) / t)
        return True if random.random() < p else False

⑥ Search for the optimal solution

Based on the energy of all the generated solutions, search for the solution with the lowest energy, which is the optimal solution of the simulated annealing algorithm.
 

def search(all_path, length_mat):
    best_e = 0xffff
    best_path = all_path[0]
    for path in all_path:
        ex = e(path, length_mat)
        if ex < best_e:
            best_e = ex
            best_path = path
    return best_path

 7. Main function of simulated annealing algorithm

#t0为初始温度,t_final为终止温度,alpha为冷却系数,inner_iter为内层迭代次数
#city_numbers为所需要经过的其他城市,length_mat为各个节点之间的最短距离矩阵
def sa(t0, t_final, alpha, inner_iter, city_numbers, length_mat):
    all_path = []
    all_ex = []
    init = initialization(city_numbers)
    all_path.append(init)
    all_ex.append(e(init, length_mat))
    t = t0
    while t > t_final:
        path = search(all_path, length_mat)
        ex = e(path, length_mat)
        for i in range(inner_iter):
            new_path = generate_new(path)
            new_ex = e(new_path, length_mat)
            if metropolis(ex, new_ex, t):
                path = new_path
                ex = new_ex
        all_path.append(path)
        all_ex.append(ex)
        t = alpha * t
    return all_path, all_ex

8. Main program: First find the shortest path of each node through Floyd's algorithm

graph = nx.Graph()
graph.add_nodes_from(range(0, 8))
edges = [(0, 1, 3), (0, 4, 2),
         (1, 4, 3), (1, 5, 4), (1, 7, 2),
         (2, 3, 5), (2, 4, 1), (2, 5, 4), (2, 6, 3),
         (3, 6, 1),
         (4, 5, 5),
         (5, 6, 2), (5, 7, 4),
         (6, 7, 4)]
graph.add_weighted_edges_from(edges)
shortest_length = dict(nx.shortest_path_length(graph, weight='weight'))
length_mat = []
for i in range(0, 8):
    i_j = []
    for j in range(0, 8):
        i_j.append(shortest_length[i][j])
    length_mat.append(i_j)

 9. Call simulated annealing

all_path, all_ex = sa(3000, pow(10, -1), 0.98, 200, 7, length_mat)
print(search(all_path, length_mat), round(e(search(all_path, length_mat), length_mat)))
iteration = len(all_path)
all_path = np.array(all_path)
all_ex = np.array(all_ex)
plt.xlabel("Iteration")
plt.ylabel("Length")
plt.plot(range(iteration), all_ex)
plt.show()

Reference: Simulated annealing algorithm (Python)_Zengwh_02's blog-CSDN blog_Simulated annealing algorithm python

Detailed explanation of simulated annealing algorithm (including example python code)_Eterbity's blog-CSDN blog_Simulated annealing algorithm schematic diagram

Mathematical Modeling Qingfeng’s second live broadcast: simulated annealing algorithm https://www.bilibili.com/video/BV1hK41157JL

Guess you like

Origin blog.csdn.net/weixin_68479946/article/details/129009098