Exemplo n.º 1
0
        public static ISgMutantProfile ToSgMutantProfile
        (
            this ISorterGenomeEval parentGenomeEval,
            ISorterMutateParams sorterMutateParams
        )
        {
            var rando = Rando.Fast(sorterMutateParams.Seed);

            var layer = SorterLayer.Make(new[] { parentGenomeEval.SorterGenome }, 0)
                        .Reproduce(
                seed: rando.NextInt(),
                newGenomeCount: sorterMutateParams.MutantCount,
                mutationRate: sorterMutateParams.SorterMutationRate,
                insertionRate: sorterMutateParams.SorterInsertionRate,
                deletionRate: sorterMutateParams.SorterDeletionRate
                );

            var compPool = CompPool.MakeEmpty(parentGenomeEval.SorterGenome.KeyCount)
                           .AddSorterEvalsParallel(layer.Genomes.Select(g => g.ToSorter()));

            return(new SgMutantProfileImpl(
                       parentGenomeEval: parentGenomeEval,
                       sorterGenomeEvals: compPool.SorterEvals.Where(ev => ev.SwitchUseCount <= sorterMutateParams.MaxScore)
                       .Select(ev => SorterGenomeEval.Make
                               (
                                   sorterGenome: layer.GetGenome(ev.Sorter.Guid),
                                   parentGenomeEval: parentGenomeEval,
                                   sorterEval: ev,
                                   generation: 1,
                                   success: ev.Success
                               )),
                       scores: compPool.SorterEvals.Select(ev => (double)ev.SwitchUseCount),
                       sorterMutateParams: sorterMutateParams
                       ));
        }
        public static ISorterCompParaPoolWorkflow Make(
            int seed,
            SwitchableGroupGenomeType switchableGroupGenomeType,
            int keyCount,
            int keyPairCount,
            int switchableGroupSize,
            SorterCompParaPoolParams sorterCompParaPoolParams
            )
        {
            var randy = Rando.Fast(seed);

            return(new SorterCompParaPoolWorkflowImpl
                   (
                       sorterLayer: SorterLayer.Create
                       (
                           seed: randy.NextInt(),
                           genomeCount: sorterCompParaPoolParams.PopulationSize,
                           keyCount: keyCount,
                           keyPairCount: keyPairCount
                       ),
                       switchableGroupLayer: SwitchableLayer.Create
                       (
                           seed: randy.NextInt(),
                           switchableGroupGenomeType: switchableGroupGenomeType,
                           genomeCount: sorterCompParaPoolParams.SwitchableLayerStartingGenomeCount,
                           keyCount: keyCount,
                           groupSize: switchableGroupSize
                       ),
                       sorterCompParaPoolParams: sorterCompParaPoolParams,
                       generation: 0
                   ));
        }
        private ISorterCompParaPoolWorkflow EvaluateResultsStep(int seed)
        {
            var sorterLayerEval =
                CompParaPool.SorterOnSwitchableGroupSets.Select(
                    t => GenomeEval.Make(
                        genome: SorterLayer.GetGenome(t.Sorter.Guid),
                        score:  t.SwitchesUsed,
                        generation: Generation,
                        success: t.SorterOnSwitchableGroups.All(sg => sg.Success)
                        )
                    ).Make <ISorterGenome, IGenomeEval <ISorterGenome> >();


            var switchableGroupLayerEval =
                CompParaPool.SorterOnSwitchableGroups.GroupBy(t => t.SwitchableGroupGuid).Select(
                    g => GenomeEval.Make(
                        genome: SwitchableGroupLayer.GetGenome(g.Key),
                        score: g.Sum(s => - s.SwitchUseCount),
                        generation: Generation,
                        success: g.All(m => m.Success)
                        )
                    ).Make <ISwitchableGroupGenome, IGenomeEval <ISwitchableGroupGenome> >();

            return(new SorterCompParaPoolWorkflowImpl
                   (
                       compWorkflowState: CompWorkflowState.UpdateGenomes,
                       sorterLayer: SorterLayer,
                       switchableGroupLayer: SwitchableGroupLayer,
                       compParaPool: CompParaPool,
                       sorterLayerEval: sorterLayerEval,
                       switchableGroupLayerEval: switchableGroupLayerEval,
                       sorterCompParaPoolParams: SorterCompParaPoolParams,
                       generation: Generation
                   ));
        }
Exemplo n.º 4
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;
        }
        private ISorterCompParaPoolWorkflow ReproStep(int seed)
        {
            var randy = Rando.Fast(seed);

            var sorterLayer = SorterLayer.ReproducePreserveParents(seed: randy.NextInt(), newGenomeCount: SorterCompParaPoolParams.ChildCount, mutationRate: SorterCompParaPoolParams.SorterMutationRate, insertionRate: SorterCompParaPoolParams.SorterMutationRate, deletionRate: SorterCompParaPoolParams.SorterDeletionRate);

            var switchableGroupLayer = SwitchableGroupLayer.Reproduce(seed: randy.NextInt(), newGenomeCount: SorterCompParaPoolParams.SwitchableLayerExpandedGenomeCount, mutationRate: SorterCompParaPoolParams.SwitchableLayerExpandedGenomeCount, insertionRate: SorterCompParaPoolParams.SwitchableGroupInsertionRate, deletionRate: SorterCompParaPoolParams.SwitchableGroupDeletionRate);

            return(new SorterCompParaPoolWorkflowImpl
                   (
                       compWorkflowState: CompWorkflowState.RunCompetition,
                       sorterLayer: sorterLayer,
                       switchableGroupLayer: switchableGroupLayer,
                       compParaPool: null,
                       sorterLayerEval: null,
                       switchableGroupLayerEval: null,
                       sorterCompParaPoolParams: SorterCompParaPoolParams,
                       generation: Generation
                   ));
        }
        public static IScpWorkflow Make(
            int seed,
            int keyCount,
            int keyPairCount,
            IScpParams scpParams
            )
        {
            var randy = Rando.Fast(seed);

            return(new ScpWorkflowImpl
                   (
                       sorterLayer0: null,
                       sorterLayer1: SorterLayer.Create
                       (
                           seed: randy.NextInt(),
                           genomeCount: scpParams.PopulationCount,
                           keyCount: keyCount,
                           keyPairCount: keyPairCount
                       ),
                       scpParams: scpParams,
                       generation: 0,
                       report: string.Empty
                   ));
        }