由于模拟退火方法,我正在尝试解决以下问题:
我已经将 c_i,j,f 值存储在一维数组中,所以
c_i,j,f <=> c[i + j * n + f * n * n]
我的模拟退火函数如下所示:
int annealing(int n, int k_max, int c[]){
// Initial point (verifying the constraints )
int x[n * n * n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
for (int f = 0; f < n; f++){
if (i == j && j == f && f == i){
x[i + j * n + f * n * n] = 1;
}else{
x[i + j * n + f * n * n] = 0;
}
}
}
}
// Drawing y in the local neighbourhood of x : random permutation by keeping the constraints verified
int k = 0;
double T = 0.01; // initial temperature
double beta = 0.9999999999; // cooling factor
int y[n * n * n];
int permutation_i[n];
int permutation_j[n];
while (k <= k_max){ // k_max = maximum number of iterations allowed
Permutation(permutation_i, n);
Permutation(permutation_j, n);
for (int f = 0; f < n; f++){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
y[i + j * n + f * n * n] = x[permutation_i[i] + permutation_j[j] * n + f * n * n];
}
}
}
if (f(y, c, n) < f(x, c, n) || rand()/(double)(RAND_MAX) <= pow(M_E, -(f(y, c, n)-f(x, c, n))/T)){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
for (int f = 0; f < n; f++){
x[i + j * n + f * n * n] = y[i + j * n + f * n * n];
}
}
}
}
T *= beta;
++k;
}
return f(x, c, n);
}
过程 Permutation(int permutation[], n) 用 [[0,n-1]] 的随机排列填充数组排列(例如,它会将 [0,1,2,3,4] 转换为 [ 3,0,4,2,1])。
问题是,1000000 次迭代需要太多时间,并且目标函数的值在 78 - 79 之间波动,而我应该得到 0 作为解决方案。
我还想在复杂性方面我可以做得更好......有人可以帮助我吗?
提前致谢!