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


C# Problem.Weight方法代码示例

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


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

示例1: CalculateBestPlacementInGenome

        /// <summary>
        /// Calculates the best place to insert a city.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="calculator"></param>
        /// <param name="genome"></param>
        /// <param name="city_to_place"></param>
        /// <returns></returns>
        public static BestPlacementResult CalculateBestPlacementInGenome(
            Problem problem,
            FitnessCalculator calculator,
            Genome genome,
            int city_to_place)
        {
            // initialize the best placement result.
            BestPlacementResult result
                = new BestPlacementResult();
            result.RoundIdx = -1;
            result.City = city_to_place;

            // initialize the best increase.
            double increase = 0;

            if (genome.Count > 0)
            {
                if (genome.Count == 1)
                { // when there is only on city in the round, try placing after and calculate again.
                    // calculate the increase.
                    increase = problem.Weight(genome[0], city_to_place)
                        + (problem.Weight(city_to_place, genome[0]));

                    // set the result city.
                    result.CityIdx = 1;
                }
                else
                { // when there are multiple cities try to place in all position and keep the best.
                    for (int idx = 0; idx < genome.Count - 1; idx++)
                    {
                        // calculate the new weights.
                        double new_weights =
                            problem.Weight(genome[idx], city_to_place)
                                 + (problem.Weight(city_to_place, genome[idx + 1]));

                        // calculate the old weights.
                        double old_weight =
                            problem.Weight(genome[idx], genome[idx + 1]);

                        // calculate the difference to know the increase.
                        double new_increase =
                            new_weights - old_weight;
                        //if (increase == null || new_increase < increase)
                        if (new_increase < increase)
                        {
                            // set the new increase.
                            increase = new_increase;

                            // set the result city.
                            result.CityIdx = 1;
                        }
                    }
                }
            }
            else
            {
                throw new Exception("Cannot do best placement on an empty round!");
            }

            // calculate the fitness.
            result.Increase = increase;

            // return result.
            return result;
        }
开发者ID:jorik041,项目名称:osmsharp,代码行数:73,代码来源:BestPlacementHelper.cs


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