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


C++ SolutionPtr::tours_from_depot方法代码示例

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


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

示例1: cw_savings

/**
 * Implementation of the Clarke and Wright savings algorithm
 * This function modifies an existing solution by merging tours from the same depot if this results in a net
 * cost reduction. The tours are merged in order of net reduction from highest to lowest. This is primarily
 * used to initiate the solver with a sensible first guess.
 */
SolutionPtr cw_savings(SolutionPtr prevsol)
{
    set<TourPtr> newsol;

    for (auto depot : prevsol->get_problem()->get_depots())
    {
        vector<TourPtr> tours_vec = prevsol->tours_from_depot(depot);
        set<TourPtr> tours(tours_vec.begin(), tours_vec.end());

        while (true)
        {
            double reduction = -1;
            TourPtr left, right;

            for (auto t1 : tours)
            for (auto t2 : tours)
            {
                if (t1 == t2)
                    continue;

                double test_reduction = triangle(*t1->last(), *depot, *t2->first());

                // Only accept this merge if it doesn't break the daily cap condition
                if (   test_reduction > reduction
                    && t1->duration() + t2->duration() - test_reduction <= 
                               prevsol->get_problem()->get_daily_cap())
                {
                    reduction = test_reduction;
                    left = t1;
                    right = t2;
                }
            }

            if (reduction >= 0)
            {
                tours.erase(left);
                tours.erase(right);
                tours.insert(TourPtr(new Tour(*left, *right)));
            }
            else
                break;
        }

        newsol.insert(tours.begin(), tours.end());
    }

    return SolutionPtr(new Solution(prevsol->get_problem(), newsol));
}
开发者ID:TheBB,项目名称:vrp,代码行数:54,代码来源:cwsavings.cpp


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