由于模拟退火方法,我试图解决以下问题:
我已经将c_i,j,f值存储在1D数组中,以便
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作为解决方案。
我还以为在复杂性方面我可以做得更好...... 有人可以帮帮我吗?
提前致谢!
答案 0 :(得分:1)
我会使用UPDATE table1 SET P='1'
FROM phones as p inner join users as u on p.number = u.Number
cross join table1 WHERE Username='test' and (Recipient LIKE '%' + phone OR phone LIKE '%' + Recipient)
而不是数组(并定义几个常量):
std::vector<int>
初始嵌套循环归结为:
#include <vector>
#include <algorithm>
#include <random>
int annealing(int n, int k_max, std::vector<int> c) {
const int N2 = n * n;
const int N3 = N2 * n;
std::vector<int> x(N3);
std::vector<int> y(N3);
std::vector<int> permutation_i(n);
std::vector<int> permutation_j(n);
// ...
这应该是你的排列函数:
for (int i = 0; i < n; i++){
x[(i*N2) + (i + (i * n))] = 1;
}
使用前初始化向量(0到n-1):
void Permutation(std::vector<int> & x)
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(x.begin(), x.end(), g);
}
我不知道你的std::iota(permutation_i.begin(), permutation_i.end(), 0);
std::iota(permutation_j.begin(), permutation_j.end(), 0);
函数是什么,但你应该编辑它以接受std :: vector作为它的前两个参数。