Pattern Recognition: Design and implementation of simulated annealing algorithm

Source: Internet
Author: User

The purpose of this section is to record the following learning and mastering the process of simulated annealing (simulated annealing, abbreviated SA algorithm). Simulated annealing algorithm is a general probability algorithm, which is used to find the optimal solution of proposition in a large search space. The stochastic simulated annealing algorithm and deterministic simulated annealing algorithm are used to program on MATLAB platform to find the energy minimization model of a 6-unit fully connected network.

Reference book: Richard O. Duda, Peter E. Hart, David G. Stork "Pattern Classification"

First, technical discussion

1. Random Methods

Learning plays a central role in the construction pattern classifier. A common practice is to first assume a single-parameter or multi-parameter model, and then estimate the values of each parameter based on the training sample. When the model is fairly simple and low-dimensional, analytic methods, such as the function derivative, can be used to solve the equation explicitly to obtain the optimal parameters. If the model is relatively complex, the gradient descent algorithm can be used to calculate the local derivative, such as artificial neural network or some other maximum likelihood method. For more complex models, often there are many local extrema, the effect of the above method is often unsatisfactory.

The more complex a problem, or the less prior knowledge and training samples, the stronger the dependency on a complex search algorithm that can automatically search for a workable solution, such as a parametric search-based approach. A common practice is to move the search toward the area of the desired optimal solution, while allowing a certain amount of random perturbations to find a better solution.

2. Random Search

This paper mainly studies the method of searching the optimal solution in multiple candidate solutions. Suppose that given multiple variables si,i=1,2,..., N, where each variable has a value of two discrete values (such as-1 and 1). The optimization problem is described in this way: to determine the appropriate value of n si, the current energy function (also known as the cost function) the smallest:

Where W_ij is a real symmetric matrix, the value can be negative, which makes its own feedback right of 0 (that is, w_ii=0), this is because the non-0 W_ii only on the energy function E on the addition of an SI-independent constant, does not affect the nature of the problem. This optimization problem can be expressed as a network and a node, as shown in which the links between nodes correspond to each weight value w_ij.

3. Limitations of greedy algorithms

As mentioned above, for solving the energy e minimization problem with n variables si, unless the value of n is very small, it is often impossible to solve directly because the number of configurations is as high as n^2. If the greedy algorithm is used to search for the optimal configuration, it is necessary to select the initial state of each node randomly, then examine each node in sequence to calculate the energy of the associated Si=1 state and Si=-1 state, and finally select the state migration which can reduce the energy. This judgment only uses adjacent nodes with a non-0 weighted value that are directly connected to it.

This greedy search algorithm is very unlikely to succeed in finding the optimal solution, because the system often falls into the local energy minimum, or does not converge at all, so you need to choose a different search method.

4. Simulated annealing

In thermodynamics, the annealing process of solids is mainly composed of the following three parts:

    1. Heating process. The aim is to increase the heat movement of the particles and make them deviate from the equilibrium position. When the temperature is high enough, the solid melts into a liquid, thus eliminating the non-uniform state that might have existed before, so that the subsequent cooling process begins with a certain equilibrium state. The melting process is actually the entropy increment process of the system, and the energy of the system increases with the increase of temperature.
    2. Isothermal process. The knowledge of physics tells us that the spontaneous change of system State is always in the direction of the reduction of free energy, and when the free energy reaches the minimum, the system achieves the equilibrium state for the closed systems that exchange heat with the surrounding environment without changing the temperature.
    3. Cooling process. The aim is to weaken the heat movement of the particles and gradually reduce the energy of the system, thus obtaining the low energy crystal structure.

The importance sampling method, Metropolis, was presented in 1953, which is to accept the new state by probability size. In particular, at the temperature T, the current state I produces a new state J, the energy of both EI and EJ, if the EJ is less than EI, then accept the new state J is the current state; otherwise, calculate the probability p (? E):

If P (? E) is greater than the random number within the [0,1] interval, the new state J is still accepted as the current state, and if not, the current state is retained, where k is the Boltzmann constant and T is the system temperature. The above importance sampling process is often referred to as the Metropolis guideline:

    1. The new state with large energy difference at the current state can be accepted at high temperature.
    2. At low temperature, only the new state with less current energy difference is accepted.
    3. And when the temperature tends to zero, it cannot accept a new state of higher energy than the current state.

1983 Kirkpatrick realized the similarity between combinatorial optimization and physical annealing, and was inspired by the metropolis Criterion, and proposed a simulated annealing algorithm. The simulated annealing algorithm is a stochastic optimization algorithm based on Monte Carlo iterative solution, whose starting point is based on the similarity between the physical annealing process and combinatorial optimization, and the simulated annealing method starts with a higher initial temperature. The random search in the solution space is carried out by using the Metropolis sampling strategy with probabilistic jump characteristics, with the constant decrease of temperature and the repeated sampling process, the global optimal of the problem is finally obtained. Compared to greedy algorithm, the main advantage of simulated annealing algorithm is that it makes the system possible to jump out from the local smallest point.

For an optimization problem:

The solution process of optimization problem is compared with the thermal equilibrium problem of statistical thermodynamics, and by simulating the annealing process of high temperature object, we try to find the global optimal or approximate global optimal solution of the optimization problem.

Allows the target function to evolve in the direction of the energy increase (corresponding to the energy sometimes rising) as the parameter is adjusted, in order to facilitate jumping out of the local minimum area, as the imaginary temperature drops (corresponding to the annealing of the object), the system activity decreases and eventually stabilizes in the region where the global minimum is located.

5. Two kinds of simulated annealing algorithms

Two simulated annealing algorithms, namely stochastic simulated annealing and deterministic simulated annealing, are implemented as follows:

The stochastic simulated annealing algorithm converges very slowly, in part because of the discrete nature of the entire configuration space in which the configuration space is an n-dimensional hypercube. Each search trajectory can only go along one edge of the hypercube, and the state only falls on the vertices of the hypercube, thus losing the complete gradient information. The gradient information can be provided using the continuous state values inside the hypercube. A faster method is the following deterministic simulated annealing algorithm:

Second, the experimental results discussed

Constructs a 6-cell fully connected network, the energy function uses the formula:

Where the network connection weights matrix is as follows:

The design steps include the following parts:
Write the program [E, S_out] = randomsimulatedannealing (T_max, Time_max, C, S, W) and implement the stochastic simulated annealing algorithm described above in algorithm 1. The following parameters need to be set: T_max=10,t (m+1) =c*t (m), c=0.9, experiments, energy with the change in temperature of the number of changes in the Curve 2 (due to the simulated annealing algorithm has a certain randomness of the results, so the following steps are performed four times the algorithm to observe), Four times the resulting configuration S3 is shown.

Change parameters: Initial temperature: T_max=5,t (m+1) =c*t (m), c=0.5, experiment, energy with the change in temperature decrease number of curve 4, four times the resulting configuration S5 shown.

Write the program [E, S_out] = deterministicannealing (T_max, Time_max, C, S, W) to achieve the deterministic simulated annealing algorithm described above in algorithm 2. The following parameters need to be set: T_max=10,t (m+1) =c*t (m), c=0.9, experiment, the energy is shown in the change curve 6 of the temperature decrease number, four times the resulting configuration S7 shown.

Change parameters: Initial temperature: T_max=5,t (m+1) =c*t (m), c=0.5, experiment, energy with the change in temperature decrease number of curve 8, four times the resulting configuration S9 shown.

Conclusion: Fig. 2 and 3 show the operation result of multiple random simulated annealing algorithm, we can see that the configuration S is not exactly the same; the waveform of the energy function e basically converges to the global minimum-19 after a number of gradually decreasing shocks. When the T (1) =5,c=0.5 is changed, the waveform of the energy function e can be observed from Figure 4, and the lower value is achieved, and the process of temperature gradient and oscillation adjustment is reduced.

Fig. 6, 7 gives the results of multiple deterministic simulated annealing algorithms, and the final configuration s are all the same; the waveform of the energy function e converges to the global minimum-19 without the violent oscillation in the stochastic simulated annealing. When the T (1) =5,c=0.5 is changed, the waveform of energy function e can be observed from Figure 8, which also shows the trend of rapid descent, and achieves a smaller value, the middle of which is less a process of temperature gradation and adjustment.

Combined with the above experimental results, we find that the stochastic annealing and deterministic annealing can give a similar final solution, but for some large-scale practical problems, stochastic simulated annealing runs slowly, while the deterministic annealing algorithm is much faster, sometimes two or three orders of magnitude faster.

In addition, the selection of the initial temperature T (1) and the temperature drop coefficient C has a great influence on the performance of the algorithm. The initial temperature determination should compromise the optimization of quality and optimization efficiency, the common methods include the following:

    • Uniform sampling of a set of States, the variance of the target value of each state is the initial temperature;
    • Randomly generate a set of states to determine the maximum target value difference between 22 states |? Max|, then the initial temperature is determined by a certain function according to the difference value. For example T (1) =-?/LN
      P_r, of which P_r is the initial acceptance probability;
    • The empirical formula is used to give.

There are three important functions in the design of simulated annealing algorithm: State generating function, state accepting function, temperature updating function, and in programming, the internal loop termination criterion and outer loop termination criterion should be followed. The design of these links will determine the optimal performance of the simulated annealing algorithm.

Third, the experimental results

Four, simple code implementation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% random simulated annealing function% input parameter:%T_max: Initial temperature% Time_max: Maximum number of iterations% c: Temperature drop ratio% s: Initial configuration% w: Weight matrix% output parameter:%E: Energy Change matrix% s_out: algorithm-calculated configuration%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [E, s_out] =randomsimulatedannealing(T_max, Time_max, C, S, W) [x, Y] = size (s); time =1; % Iteration CountT(Time) =T_max; % Initial temperature setting while(Time < (Time_max +1)) % (T(time) >T_min) fori =1: +num = ceil (rand (1) * y); % selection produces a1Random number to Y forj =1: YE (j) = W (num, j) * s (num) * s (j);End        Ea(Time) =-1/2* SUM (e);Eb(Time) =-Ea(time);if Eb(Time) <Ea(time) s (num) =-s (num); ElseIf (exp (-(Eb(Time)-Ea(time)) /T(time)) > rand ()) s (num) =-s (num);ElseS (num) = s (num);End    End% Computational EnergyE    E(Time) =0; forit =1:6         forJT =1:6            E(Time) =E(time) + W (IT, JT) * S (IT) * s (JT);End    End    E(Time) =E(Time) * (-0.5); S_out (Time,:) = s; % each form of the configuration time = times +1;T(Time) =T(Time-1) * C;End
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% deterministic simulated annealing function% input parameter:%T_max: Initial temperature% Time_max: Maximum number of iterations% c: Temperature drop ratio% s: Initial configuration% w: Weight matrix% middle function:% Tanh (L/T): Response function, which has an implied re-normalization of the function% output parameter:%E: Energy Change matrix% s_out: algorithm-calculated configuration%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [E, s_out] =deterministicannealing(T_max, Time_max, C, S, W) [x, Y] = size (s); time =1; % Iteration CountT(Time) =T_max; % Initial temperature setting while(Time < (Time_max +1) num = ceil (rand (1) * y); % selection produces a1Random number to Y forj =1: YE (j) = W (num, j) * S (j);EndL (time) = SUM (e); S (num) = Tanh (L (Time)/T(time)); % Computational EnergyE    E(Time) =0; forit =1:6         forJT =1:6            E(Time) =E(time) + W (IT, JT) * S (IT) * s (JT);End    End    E(Time) =E(Time) * (-0.5); S_out (Time,:) = s; % each form of the configuration time = times +1;T(Time) =T(Time-1) * C;End
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Simulated annealing algorithm experiment%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Clear;close all;% Network Connection weight Matrix w = [0 5-3 4 4 1;...5 0-1 2-3 1;...     -3-1 0 2 2 0;...4 2 2 0 3-3;...4-3 2 3 0 5;...1 1 0-3 5 0];num =6; % Total Production6Number s_in = rand (1, num); % Build1and1Random sequence of s_in (S_in >0.5) =1; S_in (S_in <0.5) = -1;d ISP ([' Initial configuration S is: ', Num2str (s_in)]); The following is a randomized simulated annealing algorithmT_max=Ten; % initial temperature setting Time_max = -; % maximum number of iterations C =0.9; % temperature change ratio [E1, S_OUT1] =randomsimulatedannealing(T_max, Time_max, C, S_in, W); Subplot (221), Plot (E1); Grid On;title ([' T (1) = ', Num2str (T_max),', c = ', Num2str (c),', stochastic simulated annealing algorithm energy change curve ']);d ISP ([' T (1) = 10,c = 0.9, the final configuration s of the stochastic simulated annealing algorithm is: ', Num2str (S_OUT1 (Time_max,:))]);T_max=5; % initial temperature setting Time_max = -; % maximum number of iterations C =0.5; % temperature change ratio [E2, S_out2] =randomsimulatedannealing(T_max, Time_max, C, S_in, W); Subplot (222), Plot (E2); Grid On;title ([' T (1) = ', Num2str (T_max),', c = ', Num2str (c),', stochastic simulated annealing algorithm energy change curve ']);d ISP ([' T (1) = 5,c = 0.5, the final configuration s of the stochastic simulated annealing algorithm is: ', Num2str (S_out2 (Time_max,:)]); The following is a deterministic simulated annealing algorithmT_max=Ten; % initial temperature setting Time_max = -; % maximum number of iterations C =0.9; % temperature change ratio [E3, S_OUT3] =deterministicannealing(T_max, Time_max, C, S_in, W); Subplot (223), Plot (E3); Grid On;title ([' T (1) = ', Num2str (T_max),', c = ', Num2str (c),', deterministic simulated annealing algorithm energy change curve ']);d ISP ([' T (1) = 10,c = 0.9, the final configuration of the deterministic simulated annealing algorithm S is: ', Num2str (S_OUT3 (Time_max,:))]);T_max=5; % initial temperature setting Time_max = -; % maximum number of iterations C =0.5; % temperature change ratio [E4, S_out4] =deterministicannealing(T_max, Time_max, C, S_in, W); Subplot (224), Plot (E4); Grid On;title ([' T (1) = ', Num2str (T_max),', c = ', Num2str (c),', deterministic simulated annealing algorithm energy change curve ']);d ISP ([' T (1) = 5,c = 0.5, the final configuration of the deterministic simulated annealing algorithm S is: ', Num2str (S_out4 (Time_max,:))]);

Reference Link: http://www.cnblogs.com/growing/archive/2010/12/16/1908255.html

Pattern Recognition: Design and implementation of simulated annealing algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.