Simulated Annealing mimics this process by considering each state in a search space to have an energy (proportional to the solution value of that state) and attempting to find a state with globally minimum energy (i.e. the optimum solution). SA starts with some initial state in the search space and then creates a random state within a given neighborhood of the initial state. If the new state is a better solution (lower energy), the algorithm chooses the new state as the current state. If the new state is a worse solution (higher energy), the new state is accepted as the current state with probability equal to the Boltzmann Probability: e (ΔE/T) , where ΔE is the difference in energy of the two states (ΔE < 0 ) and T is the temperature of the system. If the temperature T is lowered sufficiently slowly, the algorithm is guaranteed to find an optimal (lowest energy) state.
Pseudo Code
Step 1: Initialize t, L, α, tf
Select a traveling route Vc at random and estimate the value.
Step 2: pick a traveling route Vn from the neighborhood of Vc
if fval(Vn) is better than fval(Vc)
then select it (VcVn)
else select it with probability e (ΔE/t)
repeat this step L times
Step 3:set tαt
if t≥tf
then goto Step 2
else exit
Here we have to set the value of the parameters t, L, α and tf, which correspond to the initial temperature, the number of iterations, the cooling ratio and the frozen temperature, respectively.
typedef struct
{ // A struct to store a problem description. Welcome to pointers
char name[SIZE];
char comment[SIZE];
int type;
int capacity;
int dimension;
int edge_weight_type;?
int edge_weight_format;
int edge_data_format;
int node_coord_type;
int display_data_type;
double *node_coord;
int *matrix;
int *tour;
double *display;
int *cityindex;
} Problem;
double randomforSA(Problem *Pbq)
{
int *path;
int counter=0;
int j;
int i=1;
int k;
double sum=0;
double d;
int current=1;
path=calloc(Pbq->dimension,sizeof(int));
path[0]=1;
for(i;i<Pbq->dimension;i++)
{
// Pb->cityindex[0]==-1;
srand((unsigned)time(NULL));//initialize the seed
do
{
j=1+rand()%(Pb->dimension-1);// 1.....Pb->dimension-1
}while(Pbq->cityindex[j]==-1);
d=distance(Pbq,current,Pbq->cityindex[j]);
sum=sum+d;
path[counter+1]=Pbq->cityindex[j];
current=j;
Pbq->cityindex[j]=-1;
counter++;
}
sum=sum+distance(Pbq,current,Pbq->cityindex[0]);//to the start city
// for( k=0;k<Pbq->dimension;k++)
//{
// printf("-> %d",path[k]);
//}
//printf("/n the distance for randomforGA is: %f",sum);
return sum;
}
void sa(Problem *Pb)
{
double dc,dn;
double tmin=0.01;// initial parameters
double t=1000;
int ktime;
double ratio=0.98;
double ran,expp;
int i=0;
//---------copy the file information to another structure
Pbb=malloc(sizeof(Problem));//a new struct to read cityindex again
Pbb->cityindex=calloc(Pb->dimension,sizeof(int));
for(i=0;i<Pb->dimension;i++) Pbb->cityindex[i]=Pb->cityindex[i];
Pbb->dimension=Pb->dimension;
Pbb->edge_weight_type=Pb->edge_weight_type;
Pbb->node_coord=Pb->node_coord;
dc=randomforSA(Pb);
while(t>tmin)
{ ktime=1000;
while(ktime>0)
{
//---------malloc a new Problem struct
Ptt=malloc(sizeof(Problem));
Ptt->cityindex=calloc(Pb->dimension,sizeof(int));
for(i=0;i<Pb->dimension;i++) Ptt->cityindex[i]=Pbb->cityindex[i];
Ptt->dimension=Pbb->dimension;
Ptt->edge_weight_type=Pbb->edge_weight_type;
Ptt->node_coord=Pbb->node_coord;
dn=randomforSA(Ptt);
//printf("/ndn is %f",dn);
if(dn<dc)
{
dc=dn;
}
else {
srand( (unsigned)time( NULL ) );
ran=rand()/32767.0;
expp=exp(-((dn-dc)/ktime));
if(ran<expp)
{
dc=dn;
}
}
ktime--;
/* printf("random and expp:%f, %f",ran,expp);
printf("/n dc is %f",dc);*/
}
t=ratio*t;
// printf("t is:%f",t);
}
// printf("ktime and t are:%d,%f",ktime , t);
printf("/n distance for simulated annealing is %f:",dc);
}
这个是基于文件式benchmark,里面保存城市坐标,数据的存储用结构体。提取文件中城市坐标代码没贴出来,有的主要是模拟退火实现过程。
验证的时候注意调整温度那几个参数,对数据有很大影响。
上面的英文部分从报告中贴出来的,不想再去写了。