Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何有效缓解“风险”;周期偏差“;_C++_C_Algorithm - Fatal编程技术网

C++ 如何有效缓解“风险”;周期偏差“;

C++ 如何有效缓解“风险”;周期偏差“;,c++,c,algorithm,C++,C,Algorithm,我循环遍历矩阵元素的邻域,以找到最小的元素,如下所示,同时保存其坐标: int minimum_r = curr_r; int minimum_c = curr_c; uint8_t tmp_value = 255; for (int i = curr_r - 1; i <= curr_r + 1; i++) { for (int j = curr_c - 1; j <= curr_c + 1; j++) { uint8_t value = src[i *

我循环遍历矩阵元素的邻域,以找到最小的元素,如下所示,同时保存其坐标:

int minimum_r = curr_r; 
int minimum_c = curr_c;
uint8_t tmp_value = 255;

for (int i = curr_r - 1; i <= curr_r + 1; i++) {
    for (int j = curr_c - 1; j <= curr_c + 1; j++) {
        uint8_t value = src[i * cols + j];
        if (value < tmp_value) {
            tmp_value = value;
            minimum_r = i;
            minimum_c = j;
        }
    }
}
int最小值=当前值;
int最小值=当前值;
uint8_t tmp_值=255;

对于(int i=curr_r-1;i如果所考虑的元素数量足够大且选择不需要稳定,则随机化所选元素是局部解决方案

最简单的方法是将要查看的第一个元素随机化(即不要总是从同一个角落开始)

如果不选择随机化,则使用“从哪个角开始”计数器,每次检查递增

要实现这两种方法之一,请将偏移移动到列表中:

int offs[][2] = { {-1,-1 }, { -1, 0 }, { -1, 1 }, .... };
现在,您可以循环执行以下操作:

for(int neighbourIdx = 0; neighbourIdx < 8; ++neighbourIdx)
// look at neighbour [curr_r+offs[neighbourIdx][0], curr_c+offs[neighbourIdx][1]]

如果所考虑的元素数量足够大,且选择不需要稳定,则随机化所选元素是局部解决方案

最简单的方法是将要查看的第一个元素随机化(即不要总是从同一个角落开始)

如果不选择随机化,则使用“从哪个角开始”计数器,每次检查递增

要实现这两种方法之一,请将偏移移动到列表中:

int offs[][2] = { {-1,-1 }, { -1, 0 }, { -1, 1 }, .... };
现在,您可以循环执行以下操作:

for(int neighbourIdx = 0; neighbourIdx < 8; ++neighbourIdx)
// look at neighbour [curr_r+offs[neighbourIdx][0], curr_c+offs[neighbourIdx][1]]
如何有效地缓解“循环偏差”?…我注意到的问题是,如果有多个最小邻居具有相同的值,那么将选择第一个要检查的邻居

正如我在评论中提到的,避免偏见的一种方法是随机选择其中一个邻居

为了使它合理快速和准确,我会创建一个邻居列表——为了不让任何邻居超过其他邻居,我会创建一个包含所有排列的表格,以便搜索邻居。然后,您只需随机选择一个排列,以确保无偏选

想法:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
内联标准::mt19937和生成器(){
静态线程_local std::mt19937 gen(std::random_device{}());
返回发电机;
}
模板
T我的随机数(T最小值,T最大值){
标准:均匀分布距离(最小值、最大值);
返回距离(生成器());
}
自动生成排列(){
std::向量结果;
result.reserve(40320);//*2==80640字节
//重要的是,这是作为第一个排列(排序)输入的
阵列{
{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}};
执行{//循环所有置换并存储它们
结果:推回(ne);
}while(std::next_置换(ne.begin(),ne.end());
返回结果;
}
使用rc=std::pair;//行-列别名
rc获得最低邻(大小当前、大小当前、,
const uint8_t(&src)[行*cols]){
static const auto nperm=make_permutations();
//选择一个随机排列
const auto&neights=nperm[my_rand(0ul,nperm.size()-1)];
//用排列中的第一个邻居初始化低分数
auto it=neights.begin();
大小=当前值+当前值->第一;
大小=当前大小+当前大小->秒;
uint8_t tmp_值=src[r*cols+c];
rc result={r,c};
//搜索其他邻居以获得较低的分数
for(++it;it!=neights.end();++it){
r=curr\u r+it->first;
c=当前c+it->秒;
uint8_t value=src[r*cols+c];
如果(值
如何有效地缓解“循环偏差”?…我注意到的问题是,如果有多个最小邻居具有相同的值,那么将选择第一个要检查的邻居

正如我在评论中提到的,避免偏见的一种方法是随机选择其中一个邻居

为了使它合理快速和准确,我会创建一个邻居列表——为了不让任何邻居超过其他邻居,我会创建一个包含所有排列的表格,以便搜索邻居。然后,您只需随机选择一个排列,以确保无偏选

想法:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
内联标准::mt19937和生成器(){
静态线程_local std::mt19937 gen(std::random_device{}());
返回发电机;
}
模板
T我的随机数(T最小值,T最大值){
标准:均匀分布距离(最小值、最大值);
返回距离(生成器());
}
自动生成排列(){
std::向量结果;
result.reserve(40320);//*2==80640字节
//重要的是,这是作为第一个排列(排序)输入的
阵列{
{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}};
执行{//循环所有置换并存储它们
结果:推回(ne);
}while(std::next_置换(ne.begin(),ne.end());
返回结果;
}
使用rc=std::pair;//行-列别名
rc获得最低邻(大小当前、大小当前、,
const uint8_t(&src)[行*cols]){
static const auto nperm=make_permutations();
//选择一个随机排列
const auto&neights=nperm[my_rand(0ul,nperm.size()-1)];
//用排列中的第一个邻居初始化低分数
auto it=neights.begin();
大小=当前值+当前值->第一;
大小=当前大小+当前大小->秒;
uint8_t tmp_值=src[r*cols+c];
rc result={r,c};
//搜索其他邻居以获得较低的分数
for(++it;it!=neights.end();++it){
r=curr\u r+it->first;
c=当前c+it->秒;
uint8_t value=src[r*cols+c];
如果(值