Esempio n. 1
0
        public void TestCancel()
        {
            var tokenSource = new CancellationTokenSource();
            var inputs      = new[] { 42, 43, 44, 45 };

            var ibw = EnumerativeBackgroundWorker.Make
                      (
                inputs,
                (i, c) => IterationResult.Make(i + 1, ProgressStatus.StepComplete)
                      );

            var nextResult = 0;

            ibw.OnIterationResult.Subscribe(
                r =>
            {
                nextResult = r.Data;
                tokenSource.Cancel();
            });

            ibw.Start(tokenSource);
            Thread.Sleep(100);

            Assert.IsTrue(ibw.CurrentOutput < 46);
            Assert.IsTrue(ibw.CurrentIteration < inputs.Length);
            Assert.AreEqual(ibw.CurrentOutput, nextResult);
        }
Esempio n. 2
0
        public async Task OnRunAsync(CancellationTokenSource cancellationTokenSource)
        {
            var rbw = EnumerativeBackgroundWorker.Make
                      (
                inputs: SorterGenomeEvalGridVmInitial.SorterGenomeEvalVms
                .Select(
                    ev => new Tuple <ISorterGenomeEval, ISorterMutateParams>
                    (
                        ev.SorterGenomeEval,
                        SorterMutateParamsVm.GetParams
                    )),
                mapper: (s, c) =>
            {
                return(IterationResult.Make
                       (
                           data: s.Item1.ToSgMutantProfile(s.Item2),
                           progressStatus: ProgressStatus.StepComplete
                       ));
            }
                      );

            Busy = true;
            //_cancellationTokenSource = cancellationTokenSource;

            rbw.OnIterationResult.Subscribe(UpdateSorterMutateResults);
            await rbw.Start(cancellationTokenSource);

            Busy = false;
        }
        public void TestError()
        {
            var       tokenSource     = new CancellationTokenSource();
            const int initialState    = 42;
            const int totalIterations = 10;


            var ibw = RecursiveBackgroundWorker.Make
                      (
                initialState,
                (i, c) =>
            {
                Thread.Sleep(10);
                return(IterationResult.Make(i + 1, ProgressStatus.Error));
            },
                totalIterations,
                tokenSource
                      );

            var nextResult = initialState;

            ibw.OnIterationResult.Subscribe(
                r =>
            {
                nextResult = r.Data;
            });

            ibw.Start();
            Thread.Sleep(100);

            Assert.AreEqual(ibw.CurrentState, nextResult);
            Assert.AreEqual(ibw.CurrentState, initialState);
            Assert.AreEqual(ibw.CurrentIteration, 0);
        }
Esempio n. 4
0
        public void TestCtor()
        {
            var inputs = new[] { 42, 43, 44, 45 };

            var ibw = EnumerativeBackgroundWorker.Make
                      (
                inputs,
                (i, c) => IterationResult.Make(i + 1, ProgressStatus.StepComplete)
                      );

            Assert.AreEqual(ibw.CurrentInput, inputs[0]);
            Assert.AreEqual(ibw.CurrentIteration, 0);
            Assert.AreEqual(ibw.TotalIterations, inputs.Count());
        }
Esempio n. 5
0
        async Task GenerateSamples()
        {
            Busy = true;
            var samplerParams = GetSorterSamplerParams(KeyCount.Value);

            _stopwatch.Reset();
            _stopwatch.Start();
            _cancellationTokenSource = new CancellationTokenSource();

            var rando  = Rando.Fast(Seed.Value);
            var inputs = Enumerable.Range(0, 10000).Select(t => rando.NextInt());

            IEnumerativeBackgroundWorker <int, SorterSamplerResults> ibw = EnumerativeBackgroundWorker.Make
                                                                           (
                inputs: inputs,
                mapper: (i, c) =>
            {
                var sorterSamplerResults = SorterRandomSampler.SorterSampler(
                    keyCount: KeyCount.Value,
                    switchCount: samplerParams.SwitchCount,
                    histogramMin: samplerParams.HistogramMin,
                    histogramMax: samplerParams.HistogramMax,
                    seed: i,
                    repCount: ReportFrequency.Value,
                    lowRangeMax: LowRangeMax.Value,
                    highRangeMin: HighRangeMin.Value,
                    cancellationToken: _cancellationTokenSource.Token
                    );

                if (sorterSamplerResults.WasCancelled)
                {
                    return(IterationResult.Make(default(SorterSamplerResults), ProgressStatus.StepIncomplete));
                }

                return(IterationResult.Make(sorterSamplerResults, ProgressStatus.StepComplete));
            }
                                                                           );

            ibw.OnIterationResult.Subscribe(UpdateSorterSamplerResults);
            await ibw.Start(_cancellationTokenSource);


            Busy = false;
            _stopwatch.Stop();
            CommandManager.InvalidateRequerySuggested();
        }
Esempio n. 6
0
        public async Task OnRunAsync(CancellationTokenSource cancellationTokenSource)
        {
            Busy = true;
            _cancellationTokenSource = cancellationTokenSource;

            var rando = Rando.Fast(ScpParamsVm.Seed);

            var rbw = RecursiveBackgroundWorker.Make
                      (
                initialState: ScpWorkflow.Make
                (
                    sorterLayer: SorterLayer.Make(
                        sorterGenomes: _sorterGenomeEvals.Select(e => e.Value.SorterGenome),
                        generation: ScpParamsVm.CurrentGeneration
                        ),
                    scpParams: ScpParamsVm.GetParams,
                    generation: ScpParamsVm.CurrentGeneration
                ),

                recursion: (i, c) =>
            {
                var nextStep = i;
                while (true)
                {
                    if (_cancellationTokenSource.IsCancellationRequested)
                    {
                        return(IterationResult.Make <IScpWorkflow>(null, ProgressStatus.StepIncomplete));
                    }

                    nextStep = nextStep.Step(rando.NextInt());

                    if (nextStep.CompWorkflowState == CompWorkflowState.ReproGenomes)
                    {
                        return(IterationResult.Make(nextStep, ProgressStatus.StepComplete));
                    }
                }
            },
                totalIterations: ScpParamsVm.TotalGenerations - ScpParamsVm.CurrentGeneration,
                cancellationTokenSource: _cancellationTokenSource
                      );

            rbw.OnIterationResult.Subscribe(UpdateSorterTuneResults);
            await rbw.Start();

            Busy = false;
        }
        public void TestCtor()
        {
            var       tokenSource     = new CancellationTokenSource();
            const int initialState    = 42;
            const int totalIterations = 10;


            var ibw = RecursiveBackgroundWorker.Make
                      (
                initialState,
                (i, c) => IterationResult.Make(i + 1, ProgressStatus.StepComplete),
                totalIterations,
                tokenSource
                      );

            Assert.AreEqual(ibw.CurrentState, initialState);
            Assert.AreEqual(ibw.CurrentIteration, 0);
            Assert.AreEqual(ibw.TotalIterations, totalIterations);
        }
Esempio n. 8
0
        public void TestUpdate()
        {
            var tokenSource = new CancellationTokenSource();
            var inputs      = new[] { 42, 43, 44, 45 };

            var ibw = EnumerativeBackgroundWorker.Make
                      (
                inputs,
                (i, c) => IterationResult.Make(i + 1, ProgressStatus.StepComplete)
                      );

            var nextResult = 0;

            ibw.OnIterationResult.Subscribe(r => nextResult = r.Data);
            ibw.Start(tokenSource);
            Thread.Sleep(100);

            Assert.AreEqual(ibw.CurrentInput, default(int));
            Assert.AreEqual(ibw.CurrentIteration, inputs.Count());
            Assert.AreEqual(ibw.TotalIterations, inputs.Count());
            Assert.AreEqual(ibw.CurrentOutput, 46);
        }
        public void TestUpdate()
        {
            var       tokenSource     = new CancellationTokenSource();
            const int initialState    = 42;
            const int totalIterations = 10;


            var ibw = RecursiveBackgroundWorker.Make
                      (
                initialState,
                (i, c) => IterationResult.Make(i + 1, ProgressStatus.StepComplete),
                totalIterations,
                tokenSource
                      );

            var nextResult = 0;

            ibw.OnIterationResult.Subscribe(r => nextResult = r.Data);
            ibw.Start();
            Thread.Sleep(100);

            Assert.AreEqual(ibw.CurrentState, nextResult);
            Assert.AreEqual(ibw.CurrentIteration, totalIterations);
        }