我将从模拟退火算法的算法介绍、代码实现、算法优化和应用举例四个方面分别对模拟退火算法作简要阐述。
首先是算法介绍。模拟退火算法来源于固体退火原理,是一种基于概率的算法。当我们将固体加热,其内部粒子会变得越发无序。而后在冷却的过程中又会趋于有序,当降温结束时,内能最小,状态最为稳定。我对此的理解为:模拟退火算法是一种优化的启发式算法,也就是通过高迭代次数,每次迭代在基于已有方案的基础上,通过对当前方案进行局部改变,获得新方案。为了尽快得到全局最优,可以提高保留原有方案比例。为了尽可能尝试不同方案,模拟退火算法提出,当新方案优于原方案时接受新方案,当新方案差于原方案时,以一定概率接受新方案。而这个接受的概率与新方案与原方案损失函数差值正相关,与迭代次数负相关,即:当新方案与原方案结果相差越少或者迭代次数越少时,我们越容易接受新方案。这样可以保证算法在最初非常活跃,去尝试全局各个位置,这样更容易探索到全局最优的邻域,而最终也能保证收敛到局部最优。虽然模拟退火算法无法保证得到全局最优解,但是充分实验数据表明,即使面对极为复杂的问题,正确使用模拟退火算法在已经可以达到非常好的结果。以上是我对模拟退火算法思想的简单介绍。
其次是代码实现。我们需要自行设置参数初始温度t0,最终温度te,降温系数r,还需要设置方案的损失函数f。我常将t0设为10000,te设为0.0001,r设为0.999,再根据代码运行情况调整。初始化参数后,只需要执行while循环使方案向最优迭代即可,退出循环条件为t<=te,循环体包括生成新方案,计算新方案损失值与原方案损失值差值,判断语句,即之前提到的:当新方案由于原方案则更新,否则计算更新概率并以此概率更新,更新概率公式一般为exp(-(deltaf / T)),这可以保证概率在0和1之间,并且与差值正相关、温度负相关。无论是否更新方案,都需要将温度进行衰减处理,即执行t=t*r。在一些论文中也出现当方案超过例如3000次迭代没有更新时退出循环。无论如何,其核心思想是相同的。以上是我对模拟退火算法代码实现的简单介绍。
然后是算法优化。模拟退火算法的代码基础框架并不复杂,我在之前的比赛中常将其与遗传算法、粒子群算法思想结合使用。在连续域,我们可以只令其中一部分参数在其邻域内随机生成新值进行替代,而保留其中大部分值,而后我们也可以令原方案与新方案随机交换一部分参数,再次生成新的两个方案,即遗传算法的遗传、变异、交叉操作,这样的方式生成新方案可以大大加快收敛速度。另外,在实际执行过程中,单一地使用模拟退火算法仍有一定概率陷入局部最优,而粒子群算法可以对此进行补充。简单地,我们可以多次执行模拟退火算法。如果有条件,我们也可以并发运行一定数量的模拟退火粒子,例如100个,而每次方案更新时受局部最优与当前全局最优的合力影响。这样做的好处在于,既保证了全局最优领域内的粒子收敛到全局最优,又令非全局最优邻域内粒子收敛与局部最优过程中探索更多邻域内的点,而非简单的梯度下降算法,这样可以大大提升结果准确率。以上是我对模拟退火算法优化的两种方式的简单介绍。
最后是应用举例。模拟退火算法一般用于无法使用枚举或贪心算法解决的问题。例如旅行商问题(TSP),若从第一个城市出发,遍历其他40个城市再回到出发城市,将有40!种方案,这一般是无法通过枚举解决的。而我们使用模拟退火算法处理此问题时,往往先随机生成一个排列作为初始方案,计算其损失函数,而后不断在原有基础上作部分改变,例如取随机一部分城市序号重新排列,或者随机取两个城市并交换访问顺序,或者取一个片段并反转访问顺序。实际上主流的使用模拟退火算法解决TSP的代码大多是等概率采取以上三种随机方法,而不会采取单一的新方案生成方式。模拟退火算法还可以应用到选址问题、投资策略等,是颇为高效的智能算法。以上是我对模拟退火算法的一些应用举例。
模拟退火算法是在1953年被提出的,后人也研究了许多基于它的优化方法,我水平有限,以上是我的所有理解了,欢迎大家查找更多相关文献学习。
I will briefly describe the simulated annealing algorithm from four aspects: the algorithm introduction, code implementation, algorithm optimization and application examples.
The first is the introduction to the algorithm. The simulated annealing algorithm is derived from the principle of solid annealing and is a probability-based algorithm. When we heat a solid, the particles inside it become more disordered. Then, in the process of cooling, it will tend to be orderly. When the cooling is over, the internal energy is the smallest and the state is the most stable. My understanding of this is that the simulated annealing algorithm is an optimized heuristic algorithm, that is, through a high number of iterations, each iteration is based on the existing scheme, and a new scheme is obtained by locally changing the current scheme. In order to obtain the global optimum as soon as possible, the proportion of retaining the original scheme can be increased. In order to try different schemes as much as possible, the simulated annealing algorithm proposes to accept the new scheme when the new scheme is better than the original scheme, and accept the new scheme with a certain probability when the new scheme is worse than the original scheme. The probability of acceptance is positively related to the difference between the loss function of the new scheme and the original scheme, and negatively related to the number of iterations. This can ensure that the algorithm is very active at the beginning, trying to try various global positions, which makes it easier to explore the global optimal neighborhood, and finally can ensure that it converges to the local optimal. Although the simulated annealing algorithm cannot guarantee the global optimal solution, sufficient experimental data show that even in the face of extremely complex problems, the correct use of the simulated annealing algorithm can achieve very good results. The above is my brief introduction to the idea of simulated annealing algorithm.
The second is the code implementation. We need to set the parameters of the initial temperature t0, the final temperature te, the cooling coefficient r, and the loss function f of the scheme. I usually set t0 to 10000, te to 0.0001, and r to 0.999, and then adjust it according to the code running. After initializing the parameters, it is only necessary to execute the while loop to make the scheme iterate optimally. The condition for exiting the loop is t<=te. The loop body includes generating a new scheme, calculating the difference between the loss value of the new scheme and the loss value of the original scheme, and judging the statement. That is, as mentioned before: when the new scheme is updated due to the original scheme, otherwise the update probability is calculated and updated with this probability. The update probability formula is generally exp(-(deltaf / T)), which can ensure that the probability is between 0 and 1 , and is positively correlated with the difference and negatively correlated with the temperature. Whether or not the scheme is updated, the temperature needs to be attenuated, that is, t=t*r. It also appears in some papers to exit the loop when the scheme is not updated for more than e.g. 3000 iterations. Regardless, the core idea is the same. The above is my brief introduction to the code implementation of the simulated annealing algorithm.
Then comes algorithm optimization. The code framework of the simulated annealing algorithm is not complicated, and I often use it in combination with the genetic algorithm and particle swarm algorithm ideas in previous competitions. In the continuous domain, we can replace only some of the parameters by randomly generating new values in its neighborhood, while retaining most of the values, and then we can also randomly exchange some parameters between the original scheme and the new scheme to generate new two schemes. Those are the same as the genetic, mutation, and crossover operations of the genetic algorithm, generating a new scheme in this way can greatly speed up the convergence speed. In addition, in the actual execution process, using the simulated annealing algorithm alone still has a certain probability of falling into the local optimum, and the particle swarm algorithm can supplement this. Simply, we can execute the simulated annealing algorithm multiple times. If possible, we can also run a certain number of simulated annealing particles concurrently, such as 100, and each update of the scheme is affected by the combined force of the local optimum and the current global optimum. The advantage of this is that it not only ensures that the particles in the global optimal field converge to the global optimal, but also makes the particles in the non-global optimal neighborhood converge and explore more points in the neighborhood during the process of convergence and local optimization, rather than simply Gradient descent algorithm, which can greatly improve the accuracy of the results. The above is my brief introduction to the two ways of optimizing the simulated annealing algorithm.
Finally, there are application examples. Simulated annealing algorithms are generally used for problems that cannot be solved using enumeration or greedy algorithms. For example, the traveling salesman problem (TSP), if starting from the first city, traversing other 40 cities and then returning to the starting city, the number of schemes will reach the factorial of 40! This solution generally cannot be solved by enumeration. When we use the simulated annealing algorithm to deal with this problem, we often randomly generate an arrangement as the initial plan, calculate its loss function, and then make some changes on the original basis, such as rearranging a random part of the city serial number, or randomly taking two City and swap the visit order, or take a slice and reverse the visit order. In fact, most of the mainstream code using simulated annealing algorithm to solve TSP adopts the above three random methods with equal probability, rather than adopting a single new scheme generation method. Simulated annealing algorithm can also be applied to site selection problems, investment strategies, etc. It is a very efficient intelligent algorithm. These are some examples of my application of the simulated annealing algorithm.
The simulated annealing algorithm was proposed in 1953, and later generations have also studied many optimization methods based on it. My level is limited. The above is all my understanding. You are welcome to find more relevant literature to learn.