c++ 使用增压累加器库的正确包括哪些内容?

v8wbuo2f  于 2023-02-10  发布在  其他
关注(0)|答案(5)|浏览(125)

我正在编译这个例子
但是一个头文件是不够的,我已经花了半个小时试图清除所有的错误。为什么不指定必需的包含?
我是这么做的

#include <boost/accumulators/statistics/weighted_p_square_cumulative_distribution.hpp>
#include <vector>
#include <boost/accumulators/accumulators.hpp> 
#include <boost/test/test_tools.hpp>
#include <boost/random/lagged_fibonacci.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/fusion/support/void.hpp>
#include <boost/accumulators/framework/depends_on.hpp>

using namespace boost::accumulators;
using namespace boost;

int main() {
    // tolerance in %
    double epsilon = 4;

    typedef accumulator_set<double, stats<tag::weighted_p_square_cumulative_distribution>, double > accumulator_t;

    accumulator_t acc_upper(tag::weighted_p_square_cumulative_distribution::num_cells = 100);
    accumulator_t acc_lower(tag::weighted_p_square_cumulative_distribution::num_cells = 100);

    // two random number generators
    double mu_upper = 1.0;
    double mu_lower = -1.0;
    boost::lagged_fibonacci607 rng;
    boost::normal_distribution<> mean_sigma_upper(mu_upper,1);
    boost::normal_distribution<> mean_sigma_lower(mu_lower,1);
    boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_upper(rng, mean_sigma_upper);
    boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_lower(rng, mean_sigma_lower);

    for (std::size_t i=0; i<100000; ++i)
    {
        double sample = normal_upper();
        acc_upper(sample, weight = std::exp(-mu_upper * (sample - 0.5 * mu_upper)));
    }

    for (std::size_t i=0; i<100000; ++i)
    {
        double sample = normal_lower();
        acc_lower(sample, weight = std::exp(-mu_lower * (sample - 0.5 * mu_lower)));
    }

    typedef iterator_range<std::vector<std::pair<double, double> >::iterator > histogram_type;
    histogram_type histogram_upper = weighted_p_square_cumulative_distribution(acc_upper);
    histogram_type histogram_lower = weighted_p_square_cumulative_distribution(acc_lower);

    // Note that applaying importance sampling results in a region of the distribution 
    // to be estimated more accurately and another region to be estimated less accurately
    // than without importance sampling, i.e., with unweighted samples

    for (std::size_t i = 0; i < histogram_upper.size(); ++i)
    {
        // problem with small results: epsilon is relative (in percent), not absolute!

        // check upper region of distribution
        if ( histogram_upper[i].second > 0.1 )    
            BOOST_CHECK_CLOSE( 0.5 * (1.0 + erf( histogram_upper[i].first / sqrt(2.0) )), histogram_upper[i].second, epsilon );
        // check lower region of distribution
        if ( histogram_lower[i].second < -0.1 )    
            BOOST_CHECK_CLOSE( 0.5 * (1.0 + erf( histogram_lower[i].first / sqrt(2.0) )), histogram_lower[i].second, epsilon );
    }
}

我得到了这些错误:

In file included from /opt/local/include/boost/accumulators/statistics/weighted_p_square_cumulative_distribution.hpp:17,
                 from a.cc:1:
/opt/local/include/boost/accumulators/framework/extractor.hpp: In instantiation of 'boost::accumulators::detail::accumulator_set_result<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double>, boost::accumulators::tag::weighted_p_square_cumulative_distribution>':
/opt/local/include/boost/mpl/eval_if.hpp:38:   instantiated from 'boost::mpl::eval_if<boost::accumulators::detail::is_accumulator_set<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double> >, boost::accumulators::detail::accumulator_set_result<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double>, boost::accumulators::tag::weighted_p_square_cumulative_distribution>, boost::accumulators::detail::argument_pack_result<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double>, boost::accumulators::tag::weighted_p_square_cumulative_distribution> >'
/opt/local/include/boost/accumulators/framework/extractor.hpp:57:   instantiated from 'boost::accumulators::detail::extractor_result<boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::weighted_p_square_cumulative_distribution, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, double>, boost::accumulators::tag::weighted_p_square_cumulative_distribution>'
a.cc:47:   instantiated from here
/opt/local/include/boost/accumulators/framework/extractor.hpp:36: error: no type named 'result_type' in 'struct boost::fusion::void_'
a.cc: In function 'int main()':
a.cc:47: error: no match for call to '(const boost::accumulators::extractor<boost::accumulators::tag::weighted_p_square_cumulative_distribution>) (main()::accumulator_t&)'
a.cc:48: error: no match for call to '(const boost::accumulators::extractor<boost::accumulators::tag::weighted_p_square_cumulative_distribution>) (main()::accumulator_t&)'
rryofs0p

rryofs0p1#

文档中显示的示例摘自库的单元测试。

qncylg1j

qncylg1j2#

error: no type named ‘result_type’ in ‘struct boost::fusion::void_’

我很抱歉我不能提供一个清晰的解决方案,但我希望我能帮助找出问题所在。Boost fusion使您能够构建结构体等价的对象,其中的字段可以使用模板参数访问。我为什么要告诉您这些?因为您得到的错误通常是当您试图访问一个不存在于fusion-struct中的字段时得到的错误。
这些错误由以下行生成:

accumulator_t acc_upper(tag::weighted_p_square_cumulative_distribution::num_cells = 100);
accumulator_t acc_lower(tag::weighted_p_square_cumulative_distribution::num_cells = 100);

我非常肯定,如果删除构造函数的参数,错误就会消失

accumulator_t acc_upper;
accumulator_t acc_lower;

我对增压蓄能器了解不多,但错误可能有两个原因:要么是accumulator_set的定义不正确(你使用的“stats”和boost accumulator文档中建议的“features”之间有区别吗?),要么是构造函数中使用的参数的定义不正确.找出是哪一个,你就可能解决了编译错误.

bweufnob

bweufnob3#

使用boost的bcp --list创建依赖项列表,然后您可以确保包含了所有依赖项。

sbdsn5lh

sbdsn5lh4#

不幸的是,您没有显示所得到的编译错误,因此我将尝试在黑暗中进行一次尝试:
你有没有用

using namespace boost::accumulators;

大多数示例都要求这样做以保持简洁。

  • 编辑:*

现在你已经发布了你的代码(我也检查了链接中的代码样本),在我看来这个例子是坏的,关键错误似乎是:
错误:"struct boost::fusion::void_"中没有名为"result_type"的类型
但由于我没有 * boost::accumulators * 的经验,我将不得不放弃这里:-(
也许你可以联系 * boost::accumulators * 的作者,让他知道这个例子不是开箱即用的。他应该能够修复这个例子。你甚至可以因为发现了这个问题而得到赞扬:-)

z31licg0

z31licg05#

刚遇到这个问题。我的解决方案是

include〈增强/累加器/统计信息/统计数据.hpp〉

相关问题