Exemple #1
0
        // Full enumeration of the gift boxes
        static void FullEnumerationOfGiftBoxes(ref List <Chocolate> AllChocolates, ref List <GiftBox> GiftBoxes)
        {
            // How many enumerations in total the program will have to perform.
            int SolutionSpace = (int)Math.Pow(2, AllChocolates.Count) - 1;

            // Start from i = 2 ^ MinimumChocolates as all the enumerations of i below that
            // will be guaranteed to not be optimal.
            for (int i = (int)Math.Pow(2, iMinimumChocolates) - 1; i <= SolutionSpace; i++)
            {
                string bitString = Convert.ToString(i, 2);
                bitString = bitString.PadLeft(AllChocolates.Count);

                // Count how many chocolates the program will evaluate in the current iteration.
                int charOccurence = 0;
                foreach (char ch in bitString)
                {
                    if (ch == '1')
                    {
                        charOccurence++;
                    }
                }
                // Only enumerate a possible outcome if it meets the MinimumChocolates constraint.
                if (charOccurence >= iMinimumChocolates)
                {
                    List <Chocolate> chocolates = new List <Chocolate>();

                    decimal productionCost = 0M;
                    decimal retailValue    = 0M;

                    for (int j = 0; j < bitString.Length; j++)
                    {
                        // '1' represents the item is taken, '0' represents the item being not taken
                        if (bitString[j] == '1')
                        {
                            chocolates.Add(AllChocolates[j]);
                            productionCost += AllChocolates[j].Cost;
                            retailValue    += AllChocolates[j].Value;
                        }
                    }
                    // Only add the gift box to potential candidates for the most optimal solution
                    // if it meets production cost contraints.
                    if (productionCost <= dMaximumProductionCost)
                    {
                        GiftBox giftbox = new GiftBox
                        {
                            Chocolates     = chocolates,
                            RetailValue    = retailValue,
                            ProductionCost = productionCost,
                            //Profit = retailValue - productionCost
                        };
                        GiftBoxes.Add(giftbox);
                    }
                }
            }
        }
Exemple #2
0
        // Iterate over all possible gift boxes and select
        // the most optimal one
        static GiftBox GetOptimalSolution(ref List <GiftBox> GiftBoxes)
        {
            GiftBox mostValued = new GiftBox();

            foreach (var giftbox in GiftBoxes)
            {
                // To get the gift box with highest profit, change
                // RetailValue to Profit
                if (giftbox.RetailValue > mostValued.RetailValue)
                {
                    mostValued = giftbox;
                }
            }
            return(mostValued);
        }
Exemple #3
0
        static void Main()
        {
            // Part 1 - Making Chocolates.
            // Calculate all possible permutations and save to output file.
            MakingChocolates();

            // Part 2 - Making Gift Boxes
            // Read the chocolates file to list
            List <Chocolate> allChocolates = ReadFileToList();

            // Then output the chocolates from the file to the screen.
            Console.WriteLine("Outputting All Chocolates from file.\n");
            foreach (Chocolate chocolate in allChocolates)
            {
                Console.WriteLine(chocolate);
            }

            Console.WriteLine("\nEnumerating all permutations.");
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // Most optimal solution = Gift Box with >= 14 chocolates, AND GiftBox.ProductionCost <= 1.96
            // List storing gift box enumerations.
            List <GiftBox> giftBoxes = new List <GiftBox>();

            // This gets all the gift box candidates that meet the constraints.
            FullEnumerationOfGiftBoxes(ref allChocolates, ref giftBoxes);

            stopwatch.Stop();
            Console.WriteLine("Full enumeration of {0} Gift Boxes took {1} ms\n",
                              (int)Math.Pow(2, allChocolates.Count) - 1 - (int)Math.Pow(2, iMinimumChocolates),
                              stopwatch.ElapsedMilliseconds);

            // The optimal solution for this problem is defined as
            // the gift box with the highest retail value
            GiftBox mostValued = GetOptimalSolution(ref giftBoxes);

            Console.WriteLine("The GiftBox with highest Retail Value is:\n" + mostValued);
            mostValued.PrintChocolates();
            mostValued.SaveToFile(sOptimalSolutionOutput);

            Console.WriteLine("\nProgram has finished executing. Press Enter to exit.");
            Console.ReadLine();
        }