Algorithm 滑动窗口搜索算法

Algorithm 滑动窗口搜索算法,algorithm,search,Algorithm,Search,我需要一个数据存储类型和算法来跟踪我所看到的最后N项的状态。每个项目都有一个通过或失败的状态,但如果一行中有M个项目失败,我正在监视的系统将被视为失败。一旦系统被认为出现故障,我就需要扫描数据历史,找到最后一个宽度为W的窗口,其中所有项目都处于“良好”状态 例如,当M=4且W=3时: 1 Good 2 Good 3 Good 4 Good 5 Good | 6 Good |- Window of size 3 where all are good.

我需要一个数据存储类型和算法来跟踪我所看到的最后N项的状态。每个项目都有一个通过或失败的状态,但如果一行中有M个项目失败,我正在监视的系统将被视为失败。一旦系统被认为出现故障,我就需要扫描数据历史,找到最后一个宽度为W的窗口,其中所有项目都处于“良好”状态

例如,当M=4且W=3时:

1 Good 2 Good 3 Good 4 Good 5 Good | 6 Good |- Window of size 3 where all are good. 7 Good | 8 Bad 9 Bad 10 Good 11 Good 12 Bad 13 Good 14 Bad 15 Bad 16 Bad 17 Bad <== System is deemed bad at this point So scan backwards to find "Good" window. 1好的 2好 3好 4好 5好| 6好|-尺寸为3的窗户,所有窗户都很好。 7好的| 8坏 9糟糕 10好 11好 12坏 13好 14坏 15坏 16坏 17糟糕我知道这将以类似正则表达式搜索的方式结束
事实上,问题要简单得多。我们可以利用这样一个事实,即我们正在搜索只包含坏结果(或只包含好结果)的子序列

像这样的东西应该有用

// how many consecutive bad results we have at this point
int consecutiveFailures = 0;
// same for good results
int consecutivePasses = 0;
for each result
    if result == 'pass' then
        consecutiveFailures = 0;
        ++consecutivePasses;
    else if result == 'fail' then
        consecutivePasses = 0;
        ++consecutiveFailures;        
    end

    if consecutiveFailures == M
        // M consecutive failures, stop processing
        ...
    end
    if consecutivePasses >= W
        // record last set of W consecutive passes for later use
        ...
    end
end
我知道这将以类似正则表达式搜索的方式结束
事实上,问题要简单得多。我们可以利用这样一个事实,即我们正在搜索只包含坏结果(或只包含好结果)的子序列

像这样的东西应该有用

// how many consecutive bad results we have at this point
int consecutiveFailures = 0;
// same for good results
int consecutivePasses = 0;
for each result
    if result == 'pass' then
        consecutiveFailures = 0;
        ++consecutivePasses;
    else if result == 'fail' then
        consecutivePasses = 0;
        ++consecutiveFailures;        
    end

    if consecutiveFailures == M
        // M consecutive failures, stop processing
        ...
    end
    if consecutivePasses >= W
        // record last set of W consecutive passes for later use
        ...
    end
end

这就是我在过去的两周里被剥夺了睡眠,每天工作14-16小时在这个愚蠢的项目上所得到的。哦,很简单。不要让我开始意识到我甚至不应该自己解决这个问题。@Peter发生在每个人身上(比我愿意承认的更多),这就是我在过去两周里睡眠不足,每天工作14-16个小时在这个愚蠢的项目上所得到的。哦,很简单。不要让我开始说我甚至不应该自己解决这部分问题。@Peter发生在每个人身上(比我愿意承认的更多)