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


C# IAlgorithm.CanContinueSearching方法代码示例

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


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

示例1: Cool

        /// <summary>
        /// Cools system until it reaches minimal, targeted temperature.
        /// </summary>
        /// <param name="algorithm">Simulated annealing algorithm being cooled.</param>
        /// <param name="coolingAction">Delegate of action to cool system by one step of cooling ratio.</param>
        /// <returns>Processor time cost</returns>
        //TODO: Possibly a more narrow dependency to something like ISimulatedAnnealing to not force cast for CurrentTemperature access?
        public IEnumerable<IAlgorithmProgressReport> Cool(IAlgorithm algorithm, Action coolingAction)
        {
            if (algorithm == null) throw new ArgumentException(nameof(algorithm));
            if (coolingAction == null) throw new ArgumentException(nameof(coolingAction));

            var startTime = DateTime.Now.Ticks;
            while (algorithm.CanContinueSearching())
            {
                //Pick random node from graph and keep on adding them to solution until correctness criteria is met
                HashSet<Node> proposedSolution = new HashSet<Node>();
                List<Node> nodesCopy = algorithm.Graph.Nodes;
                Stack<int> randomIndexes = new Stack<int>(Enumerable.Range(0, nodesCopy.Count).OrderBy(r => _random.Next()));
                while (!algorithm.Problem.IsSolutionCorrect(proposedSolution))
                {
                    proposedSolution.Add(nodesCopy[randomIndexes.Pop()]);
                }

                bool wasAccepted = false;

                //Accept the answer if algorithm allows
                if (algorithm.CanAcceptAnswer(proposedSolution))
                {
                    algorithm.Problem.SetNewSolution(proposedSolution);
                    wasAccepted = true;
                }

                var casted = algorithm as SimulatedAnnealing;
                var currentTemp = casted == null ? 0 : casted.CurrentTemperature;

                yield return new SimulatedAnnealingProgressReport(DateTime.Now.Ticks - startTime, algorithm.Problem.CurrentSolution, (uint)(algorithm.Graph.TotalNodes - algorithm.Problem.CurrentSolution.Count + 1), currentTemp, wasAccepted);

                //Cool system
                coolingAction.Invoke();
            }
            
        }
开发者ID:m-wilczynski,项目名称:Graphinder,代码行数:43,代码来源:AllRandomCooling.cs

示例2: BlockTestExecutionUntilLaunchAlgorithmFinishes

 protected void BlockTestExecutionUntilLaunchAlgorithmFinishes(IAlgorithm algorithm)
 {
     while (algorithm.CanContinueSearching())
     { }
 }
开发者ID:m-wilczynski,项目名称:Graphinder,代码行数:5,代码来源:AlgorithmTests.cs


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