# Simulated annealing learning notes

Simulated annealing is a randomization algorithm. When the number of schemes of a problem is huge (even infinite) and it is not a unimodal function, we often use simulated annealing to solve it. In one sentence: if the solution of the new state is better, modify the answer, otherwise accept the new state with a certain probability.
-OI WIKI

### Approximate operation

Define the current temperature as \ (T \), the new state will be randomly transferred from the known state, and the energy difference between the old and new states (the absolute value of the difference of the objective function) is \ (\ Delta E(\Delta E \ge 0) \). If the new state is legal and better, it is obvious to transfer directly. Otherwise, transfer according to the probability of \ (P(\Delta E)=e^{\frac{-\Delta E}{T} \). Obviously, when \ (\ Delta E \) is certain, the smaller the \ (T \), the smaller the probability of transfer. This is just in line with people's intuitive impression of annealing. We hope that when annealing is completed (i.e. \ (T\rightarrow 0 \), there will be no state transition and come to the end point - the optimal solution. The smaller the \ (\ Delta E \), the smaller the difference between the two states, which is a more acceptable perturbation, and the greater the probability of transition.

With the decrease of temperature, the jump is more and more non random, and the interval of the optimal solution is more and more stable (reduced to a point). ### Concrete implementation

Here, we use exponential cooling, that is \ (T=d · T(d\rightarrow 1 ^ -) \). Other cooling methods will be added later.
Set three parameters: initial temperature \ (T_0 \), cooling coefficient \ (d \), and termination temperature \ (T_k \). In order not to end the attempt too quickly, \ (T_0 \) should be large, \ (d\rightarrow 1^-,T_k\rightarrow 0 ^ + \). When \ (T < T_k \), the annealing ends, and the current optimal solution (note that it is not the current solution, because there is a probability that the solution is not good enough but transfers) is the desired answer.
We can start cooling down from any point in the definition domain and jump to a point randomly (see the code for details).
Take the simple problem of finding the maximum value of polynomial function as an example.

#### code

#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <cmath>
const double d=0.96;
const double T_0=10000;
const double T_k=0.001;
double y;

double min(double x,double y) {return x<y?x:y;}
double frand() {return (double)rand()/RAND_MAX;}//\in_[0, 1]
double calc(double x)
{
return 6.0*pow(x, 7)+8.0*pow(x, 6)+7.0*pow(x, 3)+5.0*x*x-y*x;
}
double stimulateAnneal()
{
double x=frand()*100,y=calc(x);//Randomly select a current state
double t=T_0,ans=y;
//Set initial state

while(t>T_k)
{
double ux=x+t*(frand()*2-1);
if (ux<0.0||ux>100.0)
continue;
double uy=calc(ux);
double dE=uy-y;
ans=min(ans, uy);
if (dE<0.0||exp(-dE/t)>frand())//Consider whether to transfer
x=ux,y=uy;
t*=d;//Exponential cooling
}

for (int i=1;i<=1000;i++)
{
double ux=x+t*(frand()*2-1);
if (ux<0.0||ux>100.0)
continue;
double uy=calc(ux);
ans=min(ans, uy);
}
//Try to find a more accurate answer nearby

return ans;
}
{
int res=0;
char ch=getchar();
while(!isdigit(ch))
ch=getchar();
while(isdigit(ch))
res=res*10+ch-'0',ch=getchar();
return res;
}
int main()
{
srand((unsigned int)time(0));
while(Q--)
{
scanf("%lf",&y);
double ans=1e60;
for (int i=1;i<=2;i++)
ans=min(ans, stimulateAnneal());
printf("%.4lf\n", ans);
}
// system("pause");
return 0;
}


### Scope of application

Simulated annealing algorithm can efficiently solve \ (NP \) complete problems, such as salesman problem \ (\ text {traveling salesman problem, TSP}) \), maximum cut problem \ (\ text{Max Cut Problem}) \), 0-1 knapsack problem \ (\ text{Zero One Knapsack Problem}) \), graph coloring problem \ (\ text {graph coloring problem}) \), and so on.

### Some skills

• The parameters of simulated annealing are difficult to control and can not guarantee that one annealing will converge to the optimal value. After one annealing, you can try to get a better solution by multiple random states near the obtained solution at the current temperature (its process is similar to simulated annealing).
• The set value can be determined by dichotomous parameters in actual operation.
• When there are too many peaks in the function, it can be annealed separately in blocks, and then the best answer can be selected in the final several blocks.
• Time can be used to replace the parameter of temperature to force the annealing duration, so as to ensure that the cooling will not be too fast or timeout.

### Reference content

Posted on Wed, 10 Nov 2021 20:06:11 -0500 by neugi