Exemple #1
0
        private void Test50Choose25()
        {
            // This function tests out the long binomial coefficien class for the case 50 choose 25.
            //
            // Test out the unsigned long version of GetBinCoeff.  The int version overflows.
            int n = 50;
            int k = 25;

            // Create the BinCoeff object that will be used for all the tests for the 50 choose 25 case.
            BCL = new BinCoeffL(n, k);
            ulong NumCombos50Choose25 = BCL.GetNumCombos(n, k);

            if (NumCombos50Choose25 != 126410606437752)
            {
                string s = "Test50Choose25: GetNumCombos did not calculate the correct value for 50 choose 25.";
                DisplayMsg(s);
                return;
            }
            try
            {
                VerifyTranslastion50Choose25();
            }
            catch
            {
                DisplayMsg("Test50Choose25: did not complete successfully.");
                return;
            }
            DisplayMsg("Test50Choose25: completed successfully.");
        }
        private ulong BenchmarkBCNumCombos2(int n, int k, out long ticks)
        {
            // This method benchmarks getting the number of combos for this n choose k case, including the constructor.
            //
            ulong numCombos;

            GC.Collect();
            Stopwatch sw = Stopwatch.StartNew();
            var       bc = new BinCoeffL(n, k);

            numCombos = bc.GetNumCombos();
            sw.Stop();
            ticks = sw.ElapsedTicks;
            return(numCombos);
        }
Exemple #3
0
        private void Test100Choose95SubCases()
        {
            // This method tests out the new int functionality of specifying a lower n choose k case without having to
            // create a new version of Pascal's Triangle to handle it. This version checks the 100 choose 95 case.
            //
            string    s;
            bool      overflow;
            int       n = 100, k = 95, kLoop;
            BinCoeffL bcl = new BinCoeffL(n, k);
            ulong     numCombos, numCombos2, rank, highRank;
            uint      rankLoop;

            int[] kIndexes = new int[k];

            // Test all subcases of 100 choose 95.
            for (kLoop = k; kLoop > 0; kLoop--)
            {
                numCombos  = bcl.GetNumCombos(n, kLoop);
                numCombos2 = BinCoeffBase.GetCombosCount(n, kLoop);
                if (numCombos != numCombos2)
                {
                    s = $"Test100Choose95SubCases: Error - {n} choose {k}: # of combos does not match.";
                    Console.WriteLine(s);
                    return;
                }
                // Test the lowest 10 ranks and the highest 10 ranks for each subcase test case.
                for (rankLoop = 1; rankLoop <= 10; rankLoop++)
                {
                    bcl.GetCombFromRank(rankLoop, kIndexes, n, kLoop);
                    rank = bcl.GetRank(true, kIndexes, out overflow, kLoop);
                    if (rank != rankLoop)
                    {
                        s = $"Test100Choose95SubCases: Error - {n} choose {kLoop}: rank or combo is wrong.";
                        Console.WriteLine(s);
                        return;
                    }
                    highRank = numCombos - rankLoop;
                    bcl.GetCombFromRank(highRank, kIndexes, n, kLoop);
                    rank = bcl.GetRank(true, kIndexes, out overflow, kLoop);
                    if (rank != highRank)
                    {
                        s = $"Test100Choose95SubCases: Error - {n} choose {kLoop}: high rank or combo is wrong.";
                        Console.WriteLine(s);
                        return;
                    }
                }
            }
        }
        private void BenchmarkBinCoeffMultiple(int[] numElemArray, int[] numGroupArray)
        {
            // This method benchmarks the n choose k tests for many subcases & ranks, and compares the performance against the Combination class methods.
            // The first benchmark compares the performance of single methods. This one attempts a more "real-life" analysis and calls GetRank and
            // GetComboFromRank 1,000 times for all the subcases for each test case.
            //
            int numItems, groupSize, caseLoop;

            int[]     kIndexes = new int[40];
            ulong     rankSum  = 0;
            int       kLoop;
            bool      overflow;
            BinCoeffL bcl;
            long      ticks, ticksAlt, ticksSum = 0, ticksSumAlt = 0;
            ulong     numCombos, rank, startRank, endRank, rankCount = 1000, rankLoop;
            double    d;
            Stopwatch sw;

            // Benchmark all the subcases.
            for (caseLoop = 0; caseLoop < numElemArray.Length; caseLoop++)
            {
                GC.Collect();
                sw        = Stopwatch.StartNew();
                numItems  = numElemArray[caseLoop];
                groupSize = numGroupArray[caseLoop];
                bcl       = new BinCoeffL(numItems, groupSize);
                for (kLoop = groupSize; kLoop > 0; kLoop--)
                {
                    numCombos = bcl.GetNumCombos(numItems, kLoop);
                    rankSum  += numCombos;
                    startRank = (numCombos / 2) - 500;
                    endRank   = startRank + rankCount;
                    for (rankLoop = startRank; rankLoop < endRank; rankLoop++)
                    {
                        bcl.GetCombFromRank(rankLoop, kIndexes, numItems, kLoop);
                        rank     = bcl.GetRank(true, kIndexes, out overflow, kLoop);
                        rankSum += rank;
                    }
                }
                sw.Stop();
                // Looking at the sum of the return values makes sure that the compiler does not optimize the code away.
                if (rankSum == 0)
                {
                    Console.WriteLine("Error - rankSum = 0?");
                }
                ticks = sw.ElapsedTicks;
                //
                // Benchmark the uint BinCoeff class subcases for 30 choose 15.
                rankSum = 0;
                sw      = Stopwatch.StartNew();
                for (kLoop = groupSize; kLoop > 4; kLoop--)
                {
                    numCombos = Combination.GetNumCombos(numItems, kLoop);
                    rankSum  += numCombos;
                    startRank = (numCombos / 2) - 500;
                    endRank   = startRank + rankCount;
                    for (rankLoop = startRank; rankLoop < endRank; rankLoop++)
                    {
                        Combination.GetCombFromRankLong(rankLoop, kIndexes, numItems, kLoop);
                        rank     = Combination.GetRank(kIndexes, out overflow, numItems, kLoop);
                        rankSum += rank;
                    }
                }
                sw.Stop();
                ticksAlt = sw.ElapsedTicks;
                // Display the results.
                d = (double)ticksAlt / (double)ticks;
                d = Math.Round(d, 2);
                Console.WriteLine($"Multiple methods benchmark for {numItems} choose {groupSize} & subcases .vs. Combination class ratio = {d} to 1.");
                ticksSum    += ticks;
                ticksSumAlt += ticksAlt;
            }
            d = (double)ticksSumAlt / (double)ticksSum;
            d = Math.Round(d, 2);
            Console.WriteLine($"Average for all n choose k cases & subcases .vs. Combination class ratio = {d} to 1.");
            Console.WriteLine();
            // Looking at the sum of the return values makes sure that the compiler does not optimize the code away.
            if (rankSum == 0)
            {
                Console.WriteLine("Error - rankSum = 0?");
            }
        }
Exemple #5
0
        private TestResult TestLongSubCases(int n, int k)
        {
            // This method tests out the new int functionality of specifying a lower n choose k case without having to
            // create a new version of Pascal's Triangle to handle it. n & k specifies the main n choose k case.
            //
            string    s;
            int       nLoop, kLoop;
            BinCoeffL bcl = new BinCoeffL(n, k);
            ulong     numCombos, numCombos2, rank, rankHigh, rank2, rankLoop;

            int[] kIndexes = new int[k];
            // If the # of combos overflowed, then don't try this case with uint.
            if (bcl.TotalCombos == 0)
            {
                return(TestResult.IntOverflow);
            }
            // Try getting the rank and combination for all subcases of n choose k.
            for (kLoop = k; kLoop > 0; kLoop--)
            {
                for (nLoop = n; nLoop >= kLoop; nLoop--)
                {
                    numCombos  = bcl.GetNumCombos(nLoop, kLoop);
                    numCombos2 = (uint)BinCoeffBase.GetCombosCount(nLoop, kLoop);
                    if (numCombos != numCombos2)
                    {
                        s = $"TestIntSubCases: Error - {n} choose {k}: # of combos does not match.";
                        Console.WriteLine(s);
                        return(TestResult.Failed);
                    }
                    ulong comboCount = numCombos;
                    // Loop thru all the combinations for this n choose k case if # of combos <= 1000.
                    // Otherwise, just do the lowest ranked 500 and the highest ranked 500.
                    if (numCombos > 1000)
                    {
                        comboCount = 500;
                    }
                    // Loop thru the combinations for this n choose k case.
                    for (rankLoop = 0; rankLoop < comboCount; rankLoop++)
                    {
                        bcl.GetCombFromRank(rankLoop, kIndexes, nLoop, kLoop);
                        rank = bcl.GetRank(true, kIndexes, out bool overflow, kLoop);
                        if (rank != rankLoop)
                        {
                            s = $"TestIntSubCases: Error - {n} choose {k}: rank or combo is wrong.";
                            Console.WriteLine(s);
                            return(TestResult.Failed);
                        }
                        // If not doing all the combinations, then process the high ranks.
                        if (numCombos > comboCount)
                        {
                            rankHigh = numCombos - rankLoop - 1;
                            // Get the k-indexes for this combination.
                            bcl.GetCombFromRank(rankHigh, kIndexes, nLoop, kLoop);
                            // Verify that the Kindexes returned can be used to retrieve the rank of the combination.
                            rank2 = bcl.GetRank(true, kIndexes, out overflow, kLoop);
                            if (rank2 != rankHigh)
                            {
                                s = $"TestIntSubCases: Error - {n} choose {k}: GetRank or GetCombFromRank failed - {rank2} != rankHigh at {rankLoop}";
                                Console.WriteLine(s);
                                return(TestResult.Failed);
                            }
                        }
                    }
                }
            }
            return(TestResult.Passed);
        }