Esempio n. 1
0
        public static ProcResult Scheme4(int steps, GaData gasd)
        {
            var randy      = Rando.Standard(gasd.Data.GetSeed());
            var strRet     = String.Empty;
            var _stopwatch = new Stopwatch();

            _stopwatch.Reset();
            _stopwatch.Start();

            for (var i = 0; i < steps; i++)
            {
                gasd = gasd.EvolveStageDimerSortersAndSortables(randy);
                gasd.Data.SetCurrentStep(gasd.Data.GetCurrentStep() + 1);
            }

            _stopwatch.Stop();
            gasd.Data.SetSeed(randy.NextInt());

            var dRet = new Dictionary <string, object>();

            dRet.SetGaSortingData(gasd);
            return(new ProcResult(data: dRet,
                                  err: strRet,
                                  stepsCompleted: steps,
                                  time: _stopwatch.ElapsedMilliseconds));
        }
Esempio n. 2
0
        public void TestSortliness()
        {
            var       seed   = 1223;
            const int order  = 24;
            const int trials = 5000;
            var       randy  = Rando.Standard(seed);

            for (var i = 0; i < trials; i++)
            {
                var stagey  = randy.ToFullSorterStage(order, 0);
                var stagey2 = randy.ToFullSorterStage(order, 0);
                var stagey3 = randy.ToFullSorterStage(order, 0);
                var stagey4 = randy.ToFullSorterStage(order, 0);

                var permy = randy.ToPermutation(order);

                var strey0 = stagey.Sort(permy);
                var strey2 = stagey2.Sort(strey0.Item2);
                var strey3 = stagey3.Sort(strey2.Item2);
                var strey4 = stagey4.Sort(strey3.Item2);
                var strey5 = stagey.Sort(strey4.Item2);
                var strey6 = stagey2.Sort(strey5.Item2);
                var strey7 = stagey3.Sort(strey6.Item2);
                var strey8 = stagey4.Sort(strey7.Item2);

                var pr   = permy.SortednessSq();
                var psr0 = strey0.Item2.SortednessSq();
                var psr2 = strey8.Item2.SortednessSq();

                Console.WriteLine($"{pr} {psr0} {psr2}");
            }
        }
Esempio n. 3
0
        public void TestMutate()
        {
            const int    seed         = 434;
            const uint   order        = 64;
            const double mutationRate = 0.003;

            var randy     = Rando.Standard(seed);
            var stageBits = randy.ToStageBits(order: order);
            var perm      = stageBits.ToPermutation();

            Stopwatch sw = new Stopwatch();

            sw.Start();

            var sameCount = 0;

            for (var i = 0; i < 100000; i++)
            {
                var stageBitsM = stageBits.Mutate(randy, mutationRate);
                var permM      = stageBitsM.ToPermutation();
                var p2         = perm.Multiply(permM);
                if (p2.IsEqualTo(PermutationEx.Identity(order)))
                {
                    sameCount++;
                }
            }

            sw.Stop();
            Console.WriteLine(sameCount);
            Console.WriteLine("Elapsed={0}", sw.Elapsed);
        }
Esempio n. 4
0
        public void TestSorterSort()
        {
            var        seed        = 1223;
            const uint order       = 24;
            var        permCount   = 2000u;
            var        sorterCount = 200u;
            var        randy       = Rando.Standard(seed);

            var permies = 0u.CountUp(permCount)
                          .Select(i => randy.ToPermutation(order).ToSortable())
                          .ToList();

            var sorters = 0u.CountUp(sorterCount)
                          .Select(i => randy.ToSorter(order, i))
                          .ToList();

            for (var i = 0; i < sorterCount; i++)
            {
                var res = permies.Select(p => sorters[i].Sort(p)).ToList();
                for (var j = 0; j < res.Count(); j++)
                {
                    Console.WriteLine($"{j} {i} {res[j].Sortedness}");
                }
            }
        }
Esempio n. 5
0
        public static UpdateGaVm GaConjOrbitVm()
        {
            var randy = Rando.Standard(Seed);
            var gasd  = randy.ToGaConjOrbitData(
                order: Order,
                sorterCount: SorterCount,
                sortableCount: SortableCount,
                stageCount: StageCount,
                sortableWinRate: SortableWinRate,
                sorterWinRate: SorterWinRate);

            var gaRet = new UpdateGaVm(
                gaSortingData: gasd,
                order: Order,
                width: GridSpan,
                height: GridSpan,
                sorterWinRate: SorterWinRate,
                sortableWinRate: SortableWinRate,
                sorterCount: SorterCount,
                stageCount: StageCount,
                sortableCount: SortableCount,
                proc: ProcScheme5,
                update_params: UpdateParams);

            return(gaRet);
        }
Esempio n. 6
0
        public static UpdateGaVm Direct()
        {
            var randy = Rando.Standard(Seed);
            var gasd  = randy.ToDirectGaSortingData(
                order: Order,
                sorterCount: SorterCount,
                sortableCount: SortableCount,
                stageCount: StageCount,
                sortableWinRate: SortableWinRate,
                sorterWinRate: SorterWinRate,
                stageReplacementMode: StageReplacementMode);

            var gaRet = new UpdateGaVm(
                gaSortingData: gasd,
                order: Order,
                width: GridSpan,
                height: GridSpan,
                sorterWinRate: SorterWinRate,
                sortableWinRate: SortableWinRate,
                sorterCount: SorterCount,
                stageCount: StageCount,
                sortableCount: SortableCount,
                proc: ProcScheme2,
                update_params: UpdateParams);

            return(gaRet);
        }
Esempio n. 7
0
        public void TestToRandomPairs()
        {
            const int order = 24;
            var       randy = Rando.Standard(1444);

            var src = Enumerable.Range(0, order).ToRandomPairs(randy).ToList();
        }
Esempio n. 8
0
        public void TestHc()
        {
            uint orderly = 11;
            var  pd      = PermutationEx.PermutationDictionary(orderly);
            var  randy   = Rando.Standard(51323);

            for (var i = 0; i < 1000; i++)
            {
                var perm  = randy.ToFullSorterStage(orderly, 0);
                var perm2 = randy.RewireSorterStage(perm);

                if (perm.IsEqualTo(perm2))
                {
                    var s = "s";
                }

                if (pd.ContainsKey(perm2))
                {
                    pd[perm2]++;
                }
                else
                {
                    pd.Add(perm2, 1);
                }
            }
        }
Esempio n. 9
0
        public void TestToStageBits()
        {
            const int  seed  = 5234;
            const uint order = 17;

            var randy     = Rando.Standard(seed);
            var stageBits = randy.ToStageBits(order: order);
        }
Esempio n. 10
0
        public void TestToRandomPairs()
        {
            const int seed  = 23196;
            var       randy = Rando.Standard(seed);
            var       prep  = 0u.CountUp(10);

            var posp = prep.ToRandomPairs(randy).ToList();
        }
Esempio n. 11
0
        public void TestCompareSortingGaData()
        {
            const int    seed            = 23196;
            const uint   order           = 48;
            const int    stageCount      = 18;
            const int    sorterCount     = 256;
            const int    sortableCount   = 256;
            const double sorterWinRate   = 0.25;
            const double sortableWinRate = 0.75;
            const StageReplacementMode stageReplacementMode = StageReplacementMode.RandomRewire;
            const int  rounds         = 200;
            const uint rollingListCap = 10;

            var randy = Rando.Standard(seed);

            var dsgOld = randy.ToDirectGaSortingData(
                order: order,
                sorterCount: sorterCount,
                sortableCount: sortableCount,
                stageCount: stageCount,
                sorterWinRate: sorterWinRate,
                sortableWinRate: sortableWinRate,
                stageReplacementMode: stageReplacementMode
                );

            var rlSgd = new RollingList <GaSortingData>(rollingListCap);

            for (var i = 0; i < rollingListCap; i++)
            {
                var dsgNew = dsgOld.EvolveSortersDirect(randy);
                rlSgd.Add(dsgNew);
                dsgOld = dsgNew;
            }


            Stopwatch sw = new Stopwatch();

            sw.Start();
            Console.Write($"\n");

            for (var i = 0; i < rounds; i++)
            {
                var dsgNew = dsgOld.EvolveSortersConjSortablesReplace(randy);
                var rl     = rlSgd.Select(sgd => sgd.CompareReport(dsgNew)).ToList();
                foreach (var r in rl)
                {
                    Console.Write($"{r} ");
                }
                Console.Write($"\n");

                dsgOld = dsgNew;
                rlSgd.Add(dsgNew);
            }


            sw.Stop();
            Console.WriteLine("\nElapsed={0}", sw.Elapsed);
        }
Esempio n. 12
0
 public void TestReplaceStage()
 {
     var        seed       = 1223;
     const uint order      = 24;
     const uint stageCount = 5;
     var        randy      = Rando.Standard(seed);
     var        oldSorter  = randy.ToSorter(order, stageCount);
     var        newSorter  = oldSorter.Mutate(randy, StageReplacementMode.RandomConjugate);
 }
Esempio n. 13
0
 public void TestRecombo()
 {
     var        seed       = 1223;
     const uint order      = 24;
     const uint stageCount = 5;
     var        randy      = Rando.Standard(seed);
     var        sorterA    = randy.ToSorter(order, stageCount);
     var        sorterB    = randy.ToSorter(order, stageCount);
 }
Esempio n. 14
0
        public void TestNextUint()
        {
            var randy = Rando.Standard(123);

            for (var i = 0; i < 16; i++)
            {
                Console.WriteLine($"{randy.NextUint()}");
            }
        }
Esempio n. 15
0
        public void TestRefineGa()
        {
            const int seed            = 7234;
            const int order           = 36;
            const int stageCount      = 12;
            const int genomeCount     = 256;
            const int sortableCount   = 128;
            const int selectionFactor = 16;
            const int rounds          = 75;
            const int srounds         = 4;

            var sw = new Stopwatch();

            sw.Start();

            var randy = Rando.Standard(seed);
            var genomePoolDualSorter = randy.ToGenomePoolDualSorter(order, stageCount, genomeCount);

            var randomSortablePool = randy.ToRandomSortablePool(
                order: order,
                poolCount: sortableCount);

            var gaDualSorter = new GaDualSorter(
                genomePoolDualSorter: genomePoolDualSorter,
                sortablePool: randomSortablePool,
                randy: randy);

            var gaDualSorterR = new GaDualSorter(
                genomePoolDualSorter: genomePoolDualSorter,
                sortablePool: randomSortablePool,
                randy: randy);


            Console.WriteLine("n_Avg n_Min r_Avg r_Min");

            for (var i = 0; i < rounds; i++)
            {
                SortingResults eval  = null;
                SortingResults evalR = null;
                for (var j = 0; j < srounds; j++)
                {
                    eval          = gaDualSorter.Eval(false);
                    evalR         = gaDualSorterR.Eval(false);
                    gaDualSorter  = gaDualSorter.EvolveSorters(eval.SorterResults, randy, selectionFactor);
                    gaDualSorterR = gaDualSorterR.EvolveSortersRecomb(eval.SorterResults, randy, selectionFactor);
                }

                var avgE = eval.SorterResults.Select(sr => sr.Value.AverageSortedness).ToList();
                var avgR = evalR.SorterResults.Select(sr => sr.Value.AverageSortedness).ToList();

                Console.WriteLine($"{avgE.Average()} {avgE.Min()} {avgR.Average()} {avgR.Min()}");
            }

            sw.Stop();
            Console.WriteLine("\nElapsed={0}", sw.Elapsed);
        }
Esempio n. 16
0
        public void TestRandomTwoCycleSquared()
        {
            const int order = 24;
            var       rando = Rando.Standard(5123);
            var       perm  = rando.ToPermutation(order);

            var p2 = perm.Multiply(perm);

            Assert.IsTrue(p2.IsEqualTo(PermutationEx.Identity(order)));
        }
Esempio n. 17
0
        public void TestToGenomeStageDimer()
        {
            const int order = 12;
            var       randy = Rando.Standard(1444);
            var       gsd   = randy.ToGenomeStageDimer(order: order);

            var res1 = gsd.Stage1.Multiply(gsd.Stage1);
            var res2 = gsd.Stage2.Multiply(gsd.Stage2);
            var mod  = gsd.Modifier.Multiply(gsd.Modifier);
        }
Esempio n. 18
0
        public void MakeDualSorterGenomePool()
        {
            const int seed       = 5234;
            const int order      = 7;
            const int stageCount = 2;
            const int poolCount  = 2;

            var randy = Rando.Standard(seed);
            var dsg   = randy.ToGenomePoolDualSorter(order, stageCount, poolCount);
        }
Esempio n. 19
0
        public void TestToSorter()
        {
            var       seed       = 1223;
            const int order      = 50;
            const int stageCount = 20;

            var randy  = Rando.Standard(seed);
            var gd     = randy.ToGenomeDimer(order: order, stageCount: stageCount);
            var sorter = gd.ToSorter();
        }
Esempio n. 20
0
 public void TestBitFlip()
 {
     var  randy      = Rando.Standard(1444);
     uint start      = 213533;
     var  mutato     = start.BitFlip(2);
     var  startArray = start.ToIntArray();
     var  mutArray   = mutato.ToIntArray();
     var  diff       = start ^ mutato;
     var  diffArray  = diff.ToIntArray();
 }
Esempio n. 21
0
        public void TestMutate()
        {
            const uint   arrayLen     = 1000;
            const double mutationRate = 0.1;
            var          randy        = Rando.Standard(123);
            var          ulongA       = 0u.CountUp(arrayLen).Select(i => randy.NextUint()).ToArray();
            var          ulongAm      = ulongA.Mutate(randy, mutationRate).ToArray();

            var diff = arrayLen * 32 - ulongA.BitOverlaps(ulongAm).Sum(i => i);
        }
Esempio n. 22
0
        public void TestToPhenotype()
        {
            const int order = 12;
            var       randy = Rando.Standard(1444);
            var       gsd   = randy.ToGenomeStageDimer(order: order);

            var pl = gsd.ToPhenotype().ToArray();

            var pl1s = pl[0].Multiply(pl[0]);
            var pl2s = pl[1].Multiply(pl[1]);
        }
Esempio n. 23
0
        public void MakeDualSorterGenome()
        {
            const int seed       = 5234;
            const int order      = 7;
            const int stageCount = 2;

            var randy = Rando.Standard(seed);
            var dsg   = randy.ToDualSorterGenome(order, stageCount);

            var mutant = dsg.Mutate(randy);
        }
Esempio n. 24
0
        public void TestConjugateByRandomSingleTwoCycle()
        {
            const int order = 24;
            var       randy = Rando.Standard(5123);
            var       perm  = randy.ToPermutation(order);

            for (var i = 0; i < 1000; i++)
            {
                perm = perm.ConjugateByRandomSingleTwoCycle(randy);
            }
        }
Esempio n. 25
0
        public void TestToRecombo()
        {
            var       seed        = 1223;
            const int order       = 5;
            const int stageCount  = 2;
            const int sorterCount = 1000;

            var randy = Rando.Standard(seed);
            var p1    = randy.ToRandomSorterPool(order, stageCount, sorterCount);

            var pp = p1.ToRecombo(randy);
        }
Esempio n. 26
0
        public void TestSingleTwoCyclePermutationConjugateIsATwoCycle()
        {
            const int order = 24;
            var       randy = Rando.Standard(5123);

            for (var i = 0; i < 1000; i++)
            {
                var twoCycle   = randy.ToSingleTwoCyclePermutation(order);
                var twoCycleSq = twoCycle.Multiply(twoCycle);
                Assert.IsTrue(twoCycleSq.IsEqualTo(PermutationEx.Identity(order)));
            }
        }
Esempio n. 27
0
        public void TestRecomboSortingGaData()
        {
            const int    seed            = 23196;
            const uint   order           = 256;
            const int    stageCount      = 20;
            const int    sorterCount     = 128;
            const int    sortableCount   = 128;
            const double sorterWinRate   = 0.25;
            const double sortableWinRate = 0.25;
            const StageReplacementMode stageReplacementMode = StageReplacementMode.RandomRewire;
            const int batchSize   = 10;
            const int batchRounds = 10;

            var randy = Rando.Standard(seed);

            var dsg = randy.ToDirectGaSortingData(
                order: order,
                sorterCount: sorterCount,
                sortableCount: sortableCount,
                stageCount: stageCount,
                sorterWinRate: sorterWinRate,
                sortableWinRate: sortableWinRate,
                stageReplacementMode: stageReplacementMode
                );

            var dsg2 = randy.ToDirectGaSortingData(
                order: order,
                sorterCount: sorterCount,
                sortableCount: sortableCount,
                stageCount: stageCount,
                sorterWinRate: sorterWinRate,
                sortableWinRate: sortableWinRate,
                stageReplacementMode: stageReplacementMode
                );

            Stopwatch sw = new Stopwatch();

            sw.Start();

            for (var j = 0; j < batchRounds; j++)
            {
                for (var i = 0; i < batchSize; i++)
                {
                    dsg = dsg.EvolveSortersConjSortablesReplace(randy);
                    //dsg2 = dsg2.EvolveBothAndRecombineDirect(randy);
                    Console.WriteLine($"{dsg.Report()}"); // { dsg2.Report()}");
                }
            }

            sw.Stop();
            Console.WriteLine("\nElapsed={0}", sw.Elapsed);
        }
Esempio n. 28
0
        public void TestInverse()
        {
            const int order = 23;
            var       randy = Rando.Standard(5123);

            for (var i = 0; i < 1000; i++)
            {
                var perm  = randy.ToPermutation(order);
                var permI = perm.ToInverse();
                var prod  = perm.Multiply(permI);
                Assert.IsTrue(prod.IsEqualTo(PermutationEx.Identity(order)));
            }
        }
Esempio n. 29
0
        public void TestFullTwoCyclePermutationConjugateIsATwoCycle()
        {
            const int order = 24;
            var       randy = Rando.Standard(5123);

            for (var i = 0; i < 1000; i++)
            {
                var twoCycle  = randy.ToFullTwoCyclePermutation(order);
                var conjugate = twoCycle.ConjugateByRandomPermutation(randy);
                var p2        = conjugate.Multiply(conjugate);
                Assert.IsTrue(p2.IsEqualTo(PermutationEx.Identity(order)));
            }
        }
Esempio n. 30
0
        public void TestRecombL2()
        {
            const int  seed      = 23196;
            const uint strandLen = 5;
            var        randy     = Rando.Standard(seed);
            var        strandA   = 0u.CountUp(strandLen).ToList();
            var        strandB   = 100u.CountUp(100 + strandLen).ToList();

            for (uint i = 0; i < strandLen + 1; i++)
            {
                var res = strandA.RecomboL2(strandB, i, TestCrossPtFunc);
            }
        }