当前位置: 首页>>代码示例>>C++>>正文


C++ Bracket::setB方法代码示例

本文整理汇总了C++中Bracket::setB方法的典型用法代码示例。如果您正苦于以下问题:C++ Bracket::setB方法的具体用法?C++ Bracket::setB怎么用?C++ Bracket::setB使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Bracket的用法示例。


在下文中一共展示了Bracket::setB方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: bracketMinimum

Bracket OneDimensionOptimizationTools::bracketMinimum(
    double a,
    double b,
    Function * function,
    ParameterList parameters)
{
    Bracket bracket;
    // Copy the parameter to use.
    bracket.a.x = a;
    parameters[0].setValue(bracket.a.x);
    bracket.a.f = function->f(parameters);
    bracket.b.x = b;
    parameters[0].setValue(bracket.b.x);
    bracket.b.f = function->f(parameters);
    if (bracket.b.f > bracket.a.f)
    {
        // Switch roles of first and second point so that we can go downhill
        // in the direction from a to b.
        NumTools::swap<double>(bracket.a.x, bracket.b.x);
        NumTools::swap<double>(bracket.a.f, bracket.b.f);
    }

    // First guess for third point:
    bracket.c.x = bracket.b.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.b.x - bracket.a.x);
    parameters[0].setValue(bracket.c.x);
    bracket.c.f = function->f(parameters);

    // Keep returning here until we bracket:
    while (bracket.b.f > bracket.c.f)
    {
        // Compute xu by parabolic extrapolation from a, b, c. TINY is used to prevent
        // any possible division by 0.
        double r = (bracket.b.x - bracket.a.x) * (bracket.b.f - bracket.c.f);
        double q = (bracket.b.x - bracket.c.x) * (bracket.b.f - bracket.a.f);

        double xu = bracket.b.x - ((bracket.b.x - bracket.c.x) * q - (bracket.b.x - bracket.a.x) * r) /
                    (2.0 * NumTools::sign(NumTools::max(NumTools::abs(q - r), NumConstants::VERY_TINY()), q - r));
        double xulim = (bracket.b.x) + GLIMIT * (bracket.c.x - bracket.b.x);
        double fu;

        // We don't go farther than this.
        // Test various possibilities:
        if ((bracket.b.x - xu) * (xu - bracket.c.x) > 0.0)
        {
            parameters[0].setValue(xu);
            fu = function->f(parameters);
            if (fu < bracket.c.f)
            {
                bracket.setA(bracket.b.x, bracket.b.f);
                bracket.setB(xu, fu);
                return bracket;
            }
            else if (fu > bracket.b.f)
            {
                bracket.setC(xu, fu);
                return bracket;
            }
            // Parabolic fit was no use.
            // Use default magnification.
            xu = bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x);
            parameters[0].setValue(xu);
            fu = function->f(parameters);
        }
        else if ((bracket.c.x - xu) * (xu - xulim) > 0.0)
        {
            // Parabolic fit is between point 3 and its allowed limit.
            parameters[0].setValue(xu);
            fu = function->f(parameters);
            if (fu < bracket.c.f)
            {
                NumTools::shift<double>(bracket.b.x, bracket.c.x, xu, bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x));
                parameters[0].setValue(xu);
                NumTools::shift<double>(bracket.b.f, bracket.c.f, fu, function->f(parameters));
            }
        }
        else if ((xu - xulim) * (xulim - bracket.c.x) >= 0.0)
        {
            // Limit parabolic xu to maximum allowed value.
            xu = xulim;
            parameters[0].setValue(xu);
            fu = function->f(parameters);
        }
        else
        {
            // Reject parabolic xu, use default magnification.
            xu = bracket.c.x + NumConstants::GOLDEN_RATIO_PHI() * (bracket.c.x - bracket.b.x);
            parameters[0].setValue(xu);
            fu = function->f(parameters);
        }
        // Eliminate oldest point and continue.
        NumTools::shift<double>(bracket.a.x, bracket.b.x, bracket.c.x, xu);
        NumTools::shift<double>(bracket.a.f, bracket.b.f, bracket.c.f, fu);
    }
    return bracket;
}
开发者ID:ajnebro,项目名称:MO-Phylogenetics,代码行数:95,代码来源:OneDimensionOptimizationTools.cpp


注:本文中的Bracket::setB方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。