Beispiel #1
0
        private IObservable <Unit> IterativelySolveAndUpdateAsync(OptimizationProblem problem, IOptimizationModel optimizationModel, DispatcherScheduler scheduler)
        {
            var optimizationSequence = Minimize(problem);

            var optimumObservations =
                optimizationSequence.ToObservable(Scheduler.ThreadPool);

            var solutionSequence = from optimumBuffer in optimumObservations.Buffer(TimeSpan.FromSeconds(1)) // take optimization result every 1 second
                                   where optimumBuffer.Any()
                                   let optimum = optimumBuffer.Last()
                                                 from unit in Observable.Start(() =>
            {
                optimizationModel.UpdateSolution(optimum);
                NotifySnapComplete();
            }, scheduler)
                                                 select unit;

            return(solutionSequence.TakeLast(1));
        }
Beispiel #2
0
        private IEnumerable <double[]> Minimize(OptimizationProblem problem)
        {
            var lastOptimum = problem.InitialValue;

            foreach (var optimum in constrainedOptimizer.Minimize(problem.Objective, problem.Constraints, problem.Variables, problem.InitialValue))
            {
                var shouldStop = Interlocked.CompareExchange(ref stopOptimization, 0, 1);
                if (shouldStop == 1)
                {
                    yield return(lastOptimum);

                    yield break;
                }
                else
                {
                    lastOptimum = optimum;
                    yield return(optimum);
                }
            }
        }