Example #1
0
        private void Test13Choose5()
        {
            // This function tests out the binomial coefficien class for the 13 choose 5 case.
            int   n         = 13;
            int   k         = 5;
            ulong numCombos = BinCoeffBase.GetCombosCount(n, k);

            if (numCombos != 1287)
            {
                DisplayMsg("BinCoeffBase.GetNumCombos did not calculate the correct value for 13 choose 5 case.");
                return;
            }
            // Create the BinCoeff object that will be used for all the tests for the 13 choose 5 case.
            // The table is created when the 3rd argument of the constuctor is set to true.
            BC = new BinCoeff <uint>(n, k, 0, true);
            try
            {
                TestTableData();
                VerifyPascalsTriangle();
                VerifyTranslastion();
                TestOutputIndexes();
            }
            catch (Exception e)
            {
                DisplayMsg($"The Test 13 choose 5 tests did not complete successfully - {e.Message}.");
                return;
            }
            DisplayMsg("Test13Choose5: completed successfully.");
        }
        private uint BenchmarkBCRankAlt(int n, int k, out long ticks)
        {
            // This method benchmarks obtaining the rank for this n choose k case
            // using the Combination.GetRank method.
            //
            uint rank;

            GC.Collect();
            // Get the combination for rank of 100 in kIndexes.
            var bc = new BinCoeff <int>(n, k);

            int[] kIndexes = new int[k];
            bc.GetCombFromRank(bc.TotalCombos / 2, kIndexes);
            Stopwatch sw = Stopwatch.StartNew();

            rank = (uint)Combination.GetRank(kIndexes, out bool overflow, n, k);
            // Some cases overflow. So, use big integer.
            if (rank == 0)
            {
                rank = (uint)Combination.GetRankBigInt(kIndexes, n, k);
            }
            sw.Stop();
            ticks = sw.ElapsedTicks;
            return(rank);
        }
        private void BenchmarkConst(int n, int k, out long ticks)
        {
            // This method benchmarks the constructor for this n choose k case.
            //
            GC.Collect();
            Stopwatch sw = Stopwatch.StartNew();
            var       bc = new BinCoeff <int>(n, k);

            sw.Stop();
            ticks = sw.ElapsedTicks;
        }
        private uint 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.
            //
            uint numCombos;

            GC.Collect();
            Stopwatch sw = Stopwatch.StartNew();
            var       bc = new BinCoeff <int>(n, k);

            numCombos = bc.GetNumCombos();
            sw.Stop();
            ticks = sw.ElapsedTicks;
            return(numCombos);
        }
        private int[] BenchmarkBCGetComboFromRank(int n, int k, out long ticks)
        {
            // This method benchmarks obtaining the rank for this n choose k case.
            //
            GC.Collect();
            var bc = new BinCoeff <int>(n, k);

            int[]     kIndexes = new int[k];
            uint      rank     = bc.TotalCombos / 2;
            Stopwatch sw       = Stopwatch.StartNew();

            bc.GetCombFromRank(rank, kIndexes);
            sw.Stop();
            ticks = sw.ElapsedTicks;
            return(kIndexes);
        }
        private uint BenchmarkBCRank(int n, int k, out long ticks)
        {
            // This method benchmarks obtaining the rank for this n choose k case.
            //
            uint rank;

            GC.Collect();
            var bc = new BinCoeff <int>(n, k);

            int[] kIndexes = new int[k];
            bc.GetCombFromRank(bc.TotalCombos / 2, kIndexes);
            Stopwatch sw = Stopwatch.StartNew();

            rank = bc.GetRank(true, kIndexes, out bool overflow);
            sw.Stop();
            ticks = sw.ElapsedTicks;
            return(rank);
        }
        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[26];
            ulong          rankSum  = 1;
            int            kLoop;
            bool           overflow;
            BinCoeff <int> bc;
            long           ticks, ticksAlt, ticksSum = 0, ticksSumAlt = 0;
            uint           numCombos, rank, startRank, endRank, rankCount = 1000, rankLoop;
            double         d;
            Stopwatch      sw;

            // Benchmark all the specified n choose k test cases.
            for (caseLoop = 0; caseLoop < numElemArray.Length; caseLoop++)
            {
                GC.Collect();
                sw        = Stopwatch.StartNew();
                numItems  = numElemArray[caseLoop];
                groupSize = numGroupArray[caseLoop];
                bc        = new BinCoeff <int>(numItems, groupSize);
                // Benchmark the subcases > 1,000 combinations.
                for (kLoop = groupSize; kLoop > 3; kLoop--)
                {
                    numCombos = bc.GetNumCombos(numItems, kLoop);
                    rankSum  += numCombos;
                    startRank = (numCombos / 2) - (rankCount / 2);
                    endRank   = startRank + rankCount;
                    for (rankLoop = startRank; rankLoop < endRank; rankLoop++)
                    {
                        bc.GetCombFromRank(rankLoop, kIndexes, numItems, kLoop);
                        rank     = bc.GetRank(true, kIndexes, out overflow, kLoop);
                        rankSum += rank;
                    }
                }
                sw.Stop();
                ticks = sw.ElapsedTicks;
                sw    = Stopwatch.StartNew();
                // Benchmark the combination class using the same code as above.
                for (kLoop = groupSize; kLoop > 3; kLoop--)
                {
                    numCombos = (uint)Combination.GetNumCombos(numItems, kLoop);
                    rankSum  += numCombos;
                    startRank = (numCombos / 2) - (rankCount / 2);
                    endRank   = startRank + rankCount;
                    for (rankLoop = startRank; rankLoop < endRank; rankLoop++)
                    {
                        Combination.GetCombFromRank(rankLoop, kIndexes, numItems, kLoop);
                        rank     = (uint)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?");
            }
        }
Example #8
0
        private void Test35Choose16()
        {
            // This function tests out the long binomial coefficien class for the case 35 choose 16.
            // This produces one of the largest number of combinations that will fit within a uint:
            // 4,059,928,950
            //
            int    n = 35;
            int    k = 16;
            string s;
            bool   overflow;
            // Create the BinCoeff object that will be used for all the tests for the 50 choose 25 case.
            var bc = new BinCoeff <uint>(n, k);

            if (bc.TotalCombos != 4059928950)
            {
                s = $"Test35Choose16: Error calculating # of combos - {BC.TotalCombos}.";
                DisplayMsg(s);
                throw new ApplicationException(s);
            }
            int  kLoop;
            uint rank = 0, numCombos, numCombos2, highRank, rank2, rankLoop;

            int[] kIndexes = new int[k];
            // Try getting the first and last 5 combinations and ranks for this case.
            try
            {
                // Test all subcases of 35 choose 16.
                for (kLoop = k; kLoop > 0; kLoop--)
                {
                    numCombos  = bc.GetNumCombos(n, kLoop);
                    numCombos2 = (uint)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++)
                    {
                        bc.GetCombFromRank(rankLoop, kIndexes, n, kLoop);
                        rank = bc.GetRank(true, kIndexes, out overflow, kLoop);
                        if (rank != kLoop)
                        {
                            s = $"Test67Choose33: Either rank or combos was not calculated correctly. rank = {rank}, kloop = {kloop}";
                            DisplayMsg(s);
                            throw new ApplicationException(s);
                        }
                        highRank = numCombos - rankLoop;
                        rank2    = bc.GetRank(true, kIndexes, out overflow);
                        bc.GetCombFromRank(highRank, kIndexes, n, kLoop);
                        rank2 = bc.GetRank(true, kIndexes, out overflow);
                        if (highRank != rank2)
                        {
                            s = $"Test67Choose33: Either rank or combos was not calculated correctly. highRank = {highRank}, rank2 = {rank2}";
                            DisplayMsg(s);
                            throw new ApplicationException(s);
                        }
                        highRank--;
                    }
                }
            }
            catch (Exception e)
            {
                DisplayMsg($"Test67Choose33: Exception = {e.Message}.");
                return;
            }
            DisplayMsg("Test67Choose33: completed successfully.");
        }
Example #9
0
        private TestResult TestIntSubCases(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;
            BinCoeff <int> bc = new BinCoeff <int>(n, k);
            uint           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 (bc.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 = bc.GetNumCombos(nLoop, kLoop);
                    // If GetNumCombos overflowed, then return that status.
                    if (numCombos == 0)
                    {
                        return(TestResult.IntOverflow);
                    }
                    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);
                    }
                    uint 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++)
                    {
                        bc.GetCombFromRank(rankLoop, kIndexes, nLoop, kLoop);
                        rank = bc.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.
                            bc.GetCombFromRank(rankHigh, kIndexes, nLoop, kLoop);
                            // Verify that the Kindexes returned can be used to retrieve the rank of the combination.
                            rank2 = bc.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);
        }
Example #10
0
        private bool TestRankCombo(int numItems, int groupSize)
        {
            // This method tests getting the rank and combination for the specified 32-bit int case.
            // If the test failed, then false is returned. Otherwise true is returned.
            string s;
            int    n = numItems, k = groupSize;
            bool   overflow;
            // Create the bin coeff object required to get all the combos for this n choose k combination.
            BinCoeff <int> bc = new BinCoeff <int>(n, k);

            // The Kindexes array specifies the indexes for a combination.
            int[] kIndexes = new int[k];
            uint  numCombos = bc.TotalCombos, rankLoop;

            bc.GetCombFromRank(numCombos - 1, kIndexes);
            uint numCombos2 = (uint)BinCoeffBase.GetRankAlt(kIndexes, n, k, out overflow) + 1;

            if (overflow)
            {
                s = $"TestRankCombo: {n} choose {k}: # of combos overflowed with alt method.";
                Console.WriteLine(s);
                return(false);
            }
            if (numCombos != numCombos2)
            {
                s = $"TestRankCombo: {n} choose {k}: # of combos not the same with alternate method - {numCombos} .vs. {numCombos2}";
                Console.WriteLine(s);
                return(false);
            }
            uint rank2, rankHigh;
            uint 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 all the combinations for this n choose k case.
            for (rankLoop = 1; rankLoop < comboCount; rankLoop++)
            {
                // Get the k-indexes for this combination.
                bc.GetCombFromRank(rankLoop, kIndexes);
                // Verify that the Kindexes returned can be used to retrieve the rank of the combination.
                rank2 = bc.GetRank(true, kIndexes, out overflow);
                if (rank2 != rankLoop)
                {
                    s = $"TestRankCombo: {n} choose {k}: GetRank or GetCombFromRank failed - value of {rank2} != rank at {rankLoop}";
                    Console.WriteLine(s);
                    return(false);
                }
                // 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.
                    bc.GetCombFromRank(rankHigh, kIndexes);
                    // Verify that the Kindexes returned can be used to retrieve the rank of the combination.
                    rank2 = bc.GetRank(true, kIndexes, out overflow);
                    if (rank2 != rankHigh)
                    {
                        s = $"TestRankCombo: {n} choose {k}: GetRank or GetCombFromRank failed - value of {rank2} != rankHigh at {rankLoop}";
                        Console.WriteLine(s);
                        return(false);
                    }
                }
            }
            s = $"TestRankCombo: {n} choose {k} completed successfully.";
            Console.WriteLine(s);
            return(true);
        }