N Boost Interval_set 的组合

2024-04-07

我的服务在 4 个不同的地点出现中断。我将每个位置的中断建模为 Boost ICL Interval_set。我想知道何时至少有 N 个地点发生主动中断。

因此,以下这个答案 https://stackoverflow.com/a/9430993/808091,我已经实现了组合算法,因此我可以通过interval_set交集在元素之间创建组合。

当这个过程结束时,我应该有一定数量的interval_set,每个interval_set同时定义N个位置的中断,最后一步将加入它们以获得所需的全貌。

问题是我目前正在调试代码,当打印每个交叉点的时间到来时,输出文本变得疯狂(即使我使用gdb一步步调试),而且我看不到它们,导致大量的CPU使用。

我想我以某种方式发送输出的内存比我应该输出的内存更大,但我看不出问题出在哪里。

这是一个 SSCCE:

#include <boost/icl/interval_set.hpp>
#include <algorithm>
#include <iostream>
#include <vector>


int main() {
    // Initializing data for test
    std::vector<boost::icl::interval_set<unsigned int> > outagesPerLocation;
    for(unsigned int j=0; j<4; j++){
        boost::icl::interval_set<unsigned int> outages;
        for(unsigned int i=0; i<5; i++){
            outages += boost::icl::discrete_interval<unsigned int>::closed(
                (i*10), ((i*10) + 5 - j));
        }
        std::cout << "[Location " << (j+1) << "] " << outages << std::endl;
        outagesPerLocation.push_back(outages);
    }

    // So now we have a vector of interval_sets, one per location. We will combine
    // them so we get an interval_set defined for those periods where at least
    // 2 locations have an outage (N)
    unsigned int simultaneusOutagesRequired = 2;  // (N)

    // Create a bool vector in order to filter permutations, and only get
    // the sorted permutations (which equals the combinations)
    std::vector<bool> auxVector(outagesPerLocation.size());
    std::fill(auxVector.begin() + simultaneusOutagesRequired, auxVector.end(), true);

    // Create a vector where combinations will be stored
    std::vector<boost::icl::interval_set<unsigned int> > combinations;

    // Get all the combinations of N elements
    unsigned int numCombinations = 0;
    do{
        bool firstElementSet = false;
        for(unsigned int i=0; i<auxVector.size(); i++){
            if(!auxVector[i]){
                if(!firstElementSet){
                    // First location, insert to combinations vector
                    combinations.push_back(outagesPerLocation[i]);
                    firstElementSet = true;
                }
                else{
                    // Intersect with the other locations
                    combinations[numCombinations] -= outagesPerLocation[i];
                }
            }
        }
        numCombinations++;
        std::cout << "[-INTERSEC-] " << combinations[numCombinations] << std::endl;  // The problem appears here
    }
    while(std::next_permutation(auxVector.begin(), auxVector.end()));

    // Get the union of the intersections and see the results
    boost::icl::interval_set<unsigned int> finalOutages;
    for(std::vector<boost::icl::interval_set<unsigned int> >::iterator
        it = combinations.begin(); it != combinations.end(); it++){
        finalOutages += *it;
    }

    std::cout << finalOutages << std::endl;
    return 0;
}

有什么帮助吗?


As 我猜测 https://stackoverflow.com/a/28330291/85371,这里有一个“高级”方法。

Boost ICL 容器不仅仅是“美化的间隔起点/终点对”的容器。它们旨在实现只是以一般优化的方式进行组合、搜索的业务。

So you不必。

如果你让图书馆做它应该做的事情:

using TimePoint = unsigned;
using DownTimes = boost::icl::interval_set<TimePoint>;
using Interval  = DownTimes::interval_type;
using Records   = std::vector<DownTimes>;

使用功能域 typedef 需要更高层次的方法。现在,让我们问一个假设的“业务问题”:

我们实际上想如何处理每个位置的停机时间记录?

嗯,我们本质上想要

  1. 统计所有可辨别的时间段并
  2. 过滤那些计数至少为 2 的内容
  3. 最后,我们想显示剩余的“合并”时间段。

好的,工程师:实施吧!


  1. 唔。统计。它能有多难?

    ❕ 优雅解决方案的关键是选择正确的数据结构

    using Tally     = unsigned; // or: bit mask representing affected locations?
    using DownMap   = boost::icl::interval_map<TimePoint, Tally>;
    

    现在只是批量插入:

    // We will do a tally of affected locations per time slot
    DownMap tallied;
    for (auto& location : records)
        for (auto& incident : location)
            tallied.add({incident, 1u});
    
  2. 好吧,我们来过滤一下。我们只需要适用于 DownMap 的谓词,对吧

    // define threshold where at least 2 locations have an outage
    auto exceeds_threshold = [](DownMap::value_type const& slot) {
        return slot.second >= 2;
    };
    
  3. 合并时间段!

    实际上。我们只是创建另一个 DownTimes 集,对吧。只是,这次不是每个地点。

    数据结构的选择再次获胜:

    // just printing the union of any criticals:
    DownTimes merged;
    for (auto&& slot : tallied | filtered(exceeds_threshold) | map_keys)
        merged.insert(slot);
    

Report!

std::cout << "Criticals: " << merged << "\n";

请注意,我们在任何地方都没有接近操纵数组索引、重叠或非重叠间隔、闭合或开放边界。或者,[eeeeek!] 集合元素的强力排列。

我们只是陈述了我们的目标,然后让图书馆来做这项工作。

完整演示

Live On Coliru http://coliru.stacked-crooked.com/a/751b55cbee1ba293

#include <boost/icl/interval_set.hpp>
#include <boost/icl/interval_map.hpp>
#include <boost/range.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/numeric.hpp>
#include <boost/range/irange.hpp>
#include <algorithm>
#include <iostream>
#include <vector>

using TimePoint = unsigned;
using DownTimes = boost::icl::interval_set<TimePoint>;
using Interval  = DownTimes::interval_type;
using Records   = std::vector<DownTimes>;

using Tally     = unsigned; // or: bit mask representing affected locations?
using DownMap   = boost::icl::interval_map<TimePoint, Tally>;

// Just for fun, removed the explicit loops from the generation too. Obviously,
// this is bit gratuitous :)
static DownTimes generate_downtime(int j) {
    return boost::accumulate(
            boost::irange(0, 5),
            DownTimes{},
            [j](DownTimes accum, int i) { return accum + Interval::closed((i*10), ((i*10) + 5 - j)); }
        );
}

int main() {
    // Initializing data for test
    using namespace boost::adaptors;
    auto const records = boost::copy_range<Records>(boost::irange(0,4) | transformed(generate_downtime));

    for (auto location : records | indexed()) {
        std::cout << "Location " << (location.index()+1) << " " << location.value() << std::endl;
    }

    // We will do a tally of affected locations per time slot
    DownMap tallied;
    for (auto& location : records)
        for (auto& incident : location)
            tallied.add({incident, 1u});

    // We will combine them so we get an interval_set defined for those periods
    // where at least 2 locations have an outage
    auto exceeds_threshold = [](DownMap::value_type const& slot) {
        return slot.second >= 2;
    };

    // just printing the union of any criticals:
    DownTimes merged;
    for (auto&& slot : tallied | filtered(exceeds_threshold) | map_keys)
        merged.insert(slot);

    std::cout << "Criticals: " << merged << "\n";
}

哪个打印

Location 1 {[0,5][10,15][20,25][30,35][40,45]}
Location 2 {[0,4][10,14][20,24][30,34][40,44]}
Location 3 {[0,3][10,13][20,23][30,33][40,43]}
Location 4 {[0,2][10,12][20,22][30,32][40,42]}
Criticals: {[0,4][10,14][20,24][30,34][40,44]}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

N Boost Interval_set 的组合 的相关文章

  • 有没有快速创建集合的方法?

    目前我正在创建一个像这样的新集 std set a s s insert a1 s insert a2 s insert a3 s insert a10 有没有办法创建s在一行 int myints 10 20 30 40 50 std s
  • 在 C# 中按元素相乘数组具有意想不到的性能

    我想找到按元素相乘两个数组的最佳方法 这是更广泛项目的一部分 其中性能而不是唯一的考虑因素 我今天开始用 C Linqpad 编写一些函数 因此它还没有以任何方式进行优化 下面代码的输出如下 Environment ProcessorCou
  • 使用多级解决方案计算二维网格中的最近邻

    我有一个问题 在 x y 大小的网格中 我提供了一个点 并且我需要找到最近的邻居 在实践中 我试图在 pygame 中找到距离光标最近的点 该点跨越颜色距离阈值 计算如下 sqrt rgb1 0 rgb2 0 2 rgb1 1 rgb2 1
  • 读取 C# 中的默认应用程序设置

    我的自定义网格控件有许多应用程序设置 在用户范围内 其中大部分是颜色设置 我有一个表单 用户可以在其中自定义这些颜色 并且我想添加一个用于恢复默认颜色设置的按钮 如何读取默认设置 例如 我有一个名为的用户设置CellBackgroundCo
  • 信号处理程序有单独的堆栈吗?

    信号处理程序是否有单独的堆栈 就像每个线程都有单独的堆栈一样 这是在 Linux C 环境中 来自 Linux 手册页signal 7 http kernel org doc man pages online pages man7 sign
  • 为什么这个没有特殊字符的正则表达式会匹配更长的字符串?

    我正在使用此方法来尝试查找匹配项 例如 Regex Match A2 TS OIL TS OIL RegexOptions IgnoreCase Success 我得到了真实的结果 我很困惑 我认为这应该返回 false 因为模式中没有特殊
  • 找不到 assimp-vc140-mt.dll ASSIMP

    我已经从以下位置下载了 Assimp 项目http assimp sourceforge net main downloads html http assimp sourceforge net main downloads html Ass
  • 时间:2019-03-17 标签:c#ThreadSafeDeepCopy

    我一直在阅读很多其他问题以及大量谷歌搜索 但我一直无法找到明确的解决方案 根据我读过的一些最佳实践 类的静态方法应该创建线程安全的 并且实例成员应该将线程安全留给消费者 我想为该类实现深度复制方法 该类本身还有其他引用类型成员 有没有什么方
  • 异或交换可以扩展到两个以上的变量吗?

    我一直在尝试将异或交换扩展到两个以上的变量 例如n变量 但我没有得到比这更好的地方3 n 1 对于两个整型变量x1 and x2你可以像这样交换它们 swap x1 x2 x1 x1 x2 x2 x1 x2 x1 x1 x2 所以 假设你有
  • 类的成员复制

    在学习 复制成员 概念时 书中给出了如下说法 此外 如果非静态成员是引用 const 或没有复制赋值的用户定义类型 则无法生成默认赋值 我不太明白这个声明到底想传达什么 或者说这个说法指的是哪一种场景 谢谢 该语句与编译器自动为您编写的类
  • 单例模式和 std::unique_ptr

    std unique ptr唯一地控制它指向的对象 因此不使用引用计数 单例确保利用引用计数只能创建一个对象 那么会std unique ptr与单例执行相同 单例确保只有一个实例属于一种类型 A unique ptr确保只有一个智能指针到
  • Visual Studio Code:如何配置 includePath 以获得更好的 IntelliSense 结果

    我是使用 Visual Studio Code 的完全初学者 我不知道我在做什么 我已经四处搜索 也许还不够 但我找不到像我这样的人如何配置的简单解释c cpp properties json每当我单击带有绿色波浪线下划线的行旁边的黄色灯泡
  • 如何在服务器端按钮点击时关闭当前标签页?

    我尝试在确认后关闭当前选项卡 因此我将以下代码放在确认按钮的末尾 但选项卡没有关闭 string jScript ClientScript RegisterClientScriptBlock this GetType keyClientBl
  • C++ php 和静态库

    我创建了一个library a 其中包含 cpp 和 h 文件 其中包含很多类 嵌套类和方法 我想在 php 示例中包含这个静态库并尝试使用它 我想提一下 我是 php 新手 我已经在 test cpp 文件中测试了我的 libray a
  • AES 输出是否小于输入?

    我想加密一个字符串并将其嵌入到 URL 中 因此我想确保加密的输出不大于输入 AES 是可行的方法吗 不可能创建任何始终会创建比输入更小的输出的算法 但可以将任何输出反转回输入 如果您允许 不大于输入 那么基本上您只是在谈论同构算法alwa
  • 使用restsharp序列化对象并将其传递给WebApi而不是序列化列表

    我有一个看起来像的视图模型 public class StoreItemViewModel public Guid ItemId get set public List
  • 了解使用 Windows 本机 WPF 客户端进行 ADFS 登录

    我已经阅读了大量有关 ADFS 与 NodeJS Angular 或其他前端 Web 框架集成以及一般流程如何工作的文献 并通过 Auth0 Angular 起始代码构建了概念证明 但我不明白如何这可以与本机 WPF Windows 应用程
  • 更改 Windows Phone 系统托盘颜色

    有没有办法将 Windows Phone 上的系统托盘颜色从黑色更改为白色 我的应用程序有白色背景 所以我希望系统托盘也是白色的 您可以在页面 XAML 中执行此操作
  • xsi:type 属性搞乱了 C# XML 反序列化

    我使用 XSD exe 根据 XML 架构 xsd 文件 自动生成 C 对象 我正在反序列化 OpenCover 输出 但其中一个部分类未正确生成 这是导致异常的行
  • 从 JavaScript 中的 OnClientClick 事件中阻止 C# 中的 asp:Button OnClick 事件?

    我有一个asp Button在我的网页上 它调用 JavaScript 函数和代码隐藏方法 后者进行调用以导航到另一个页面 在 JavaScript 函数中 我正在检查条件 如果不满足这个条件 我想中止导航 以便OnClick方法未被调用

随机推荐

  • Visual Studio 15.8.1 不运行 MS 单元测试

    当我将 Visual Studio 更新到最新版本时 我的一个测试项目停止运行测试并输出以下消息 测试项目 未引用任何 NET NuGet 适配器 测试 发现或执行可能不适用于该项目 这是 建议在每个测试项目中引用 NuGet 测试适配器
  • 向下浮动和双倍值的向下函数

    如何将下限函数应用于浮点或双精度值以获取整数 我得到双精度值 4 4497083717E10 float 值 4 4497084E10 从我的函数中出来 我得到的下限值为 Double 下限 4 4497083717E10 float 下限
  • GPUImage :将过滤器应用于现有视频文件

    我试图使用视频过滤器GPUImage框架 我跟着过滤和重新编码电影 http www sunsetlakesoftware com 2012 02 12 introducing gpuimage framework教程 它给了我错误Unkn
  • 如何在 SpiderMonkey JavaScript 中获取控制台输入?

    我目前正在使用 Spidermonkey 来运行我的 JavaScript 代码 我想知道是否有一个函数可以从控制台获取输入 类似于 Python 的做法 var raw input 或者在 C 中 std cin gt gt var 我环
  • 如何在 PostgreSQL 中的换行符上将一个值拆分为多行?

    我有一个名为BookInfo具有以下结构 id book name description 1 book 1 harry potter Part 2 2 我怎样才能分割该行 id 1 在换行符上分成多行 以便harry n potter n
  • iOS 14 - 如何以编程方式打开默认邮件应用程序?

    使用 iOS14 用户可以将不同的电子邮件客户端应用程序设置为默认值 有没有办法以编程方式打开选定的默认邮件应用程序 Using mailto URL 将默认邮件应用程序设置为 Gmail 后 不执行任何操作 显然你必须添加mailto t
  • 禁用 eclipselink 缓存和查询缓存 - 不起作用?

    我正在使用 eclipselink JPA 和数据库 该数据库也在我的应用程序外部进行更新 因此 我想每隔几秒钟查询一些表 即使我尝试禁用缓存和查询缓存 我也无法使其工作 例如 EntityManagerFactory entityMana
  • Google Maps Api 直线(最短)路线

    我目前正在尝试找到一种使用 Google Maps Api V3 获得直线路线的方法 我已经设法使用地理编码和方向服务来获取从 A 点到 B 点的路线 包括两条替代路线 我还尝试过 禁止高速公路 和 禁止通行费 但似乎没有什么可以完全解决这
  • 无法使用命令行解释器

    我尝试在 php 解释器中执行简单的 php 代码 当我执行命令时php a我收到消息 启用交互模式 没有任何地方可以输入 php 但我可以通过命令执行php代码php r 例如 php r echo Hello stackoverflow
  • zclip 在 jquery ui 非活动选项卡中不起作用

    我在用zclip http www steamdev com zclip 在我的 asp net 网页上的 jQuery UI 选项卡中的 jQuery UI 对话框中 它在第一个处于活动状态的选项卡中效果很好 但当我将其添加到第二个选项卡
  • 如何从 Git 存储库中删除文件?

    我怎样才能删除 file1 txt 从我的存储库 Use git rm https git scm com docs git rm 如果您想从 Git 存储库中删除该文件和文件系统 use git rm file1 txt git comm
  • SQL 将一列的值分组到另一列

    SQL 中是否有某种 聚合 函数可以将值转换为列表 一个示例可能是以下形式的表格 game id player score 1 fred 2 1 tom 1 2 fred 3 2 tom 4 我想要返回的是一个如下所示的表 player s
  • 在 postgres 中分割人名的最简单方法?

    考虑一个包含人类全名的表 create table names full name varchar not null insert into names full name values Jane Marie Doe John Doe 在
  • 在Python中分割句子

    我正在尝试将句子分成单词 words content lower split 这给了我这样的单词列表 evening and there was morning the first day 并使用以下代码 def clean up list
  • 使用表单方法将隐藏输入值设置为要提交的 blob

    我正在尝试提交一个表单 其中一个 blob 附加到一个隐藏的输入标签 如下所示
  • 增加溢出宽度以调整滚动条

    当滚动条可见时 我应该使用什么 CSS 来使 div 调整其宽度 这是场景 我有一个 div 和子元素 div ul li li li li li li ul ul li li li li li li ul div 我想在溢出时自动调整滚动
  • 带注释的代码风格[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我无法在两者之间做出决定 MyAnnotation param1 paramval public void foo and MyAnnotati
  • 将 CSS 属性排序为任意顺序?

    我想以编程方式将样式表中的所有声明 属性 不是声明块本身 而是每个块内的各个声明 排序为任意顺序 我已经能够在网上找到几种按字母顺序排序声明的方法 或者按字母顺序反向排序 甚至按字符串长度排序 但这对我来说没有帮助 因为这些排序方法本质上是
  • WPF - 为什么 ContextMenu 项目适用于 ListBox 而不是 ItemsControl?

    列表中的项目有上下文菜单 上下文菜单项绑定到路由命令 如果列表控件是一个 则上下文菜单项可以正常工作ListBox 但一旦我将其降级为ItemsControl它不再起作用了 具体来说 菜单项始终呈灰色 这CanExecute回调在我的Com
  • N Boost Interval_set 的组合

    我的服务在 4 个不同的地点出现中断 我将每个位置的中断建模为 Boost ICL Interval set 我想知道何时至少有 N 个地点发生主动中断 因此 以下这个答案 https stackoverflow com a 9430993