Exemple #1
0
        // Possible Solutions = N!  (** OR if number of slots is different than N == Pkn = N!/((N-K)!
        // For brute force we evaluate every possible permutation of N Items
        // 123 != 321 != 132 != 321 ETC (order matters)

        //NOTE: There are duplicate solutions evaluated due to the fact we are including constraints in the acceptable solution
        //IE in the scenario of 1234 vs 1342 for Items[ 10, 15, 1, 2 ] maxWeight = 20, 1234 is really combination 134 (13), 1342 is really combination 134 (13)
        //When solving a knapsack problem consider if the end combinations are acceptable (and order that the item entered the knapsack is irrelevant) then use
        //combinations instead of permutations

        private void Swap(KSItem[] array, int index1, int index2)
        {
            KSItem temp = array[index1];

            array[index1] = array[index2];
            array[index2] = temp;
        }
Exemple #2
0
        public void ValidateTestManagerItemAttributes()
        {
            //<Item ID="1" Value="20" Weight="22" Volume="13"></Item>
            KSItem item = CreateTestManager(TestData.KSItemList, 10, 100).ItemList.Find(i => i.Id == 1);

            Assert.Equal(20, item.Value);
            Assert.Equal(22, item.Weight);
            Assert.Equal(13, item.Volume);
        }
Exemple #3
0
        public void ValidateTestManagerItemAttributes()
        {
            //<Item Id="1" Value="1" Weight="2" Volume="5"></Item>
            KnapsackTestManager tm = CreateTestManager(TestData.KSItemList, 5, 100, 100);

            KSItem item = tm.ItemList.Find(i => i.Id == 1);

            Assert.Equal(1, item.Value);
            Assert.Equal(2, item.Weight);
            Assert.Equal(5, item.Volume);
        }
        public void LoadTestData(string fileName)
        {
            XDocument testDataDoc = XDocument.Load("./" + fileName);

            //XML Structure
            //Items -> Item
            var allElements = testDataDoc.Elements();

            KSItemList.Clear();

            foreach (var itemElement in allElements.Elements())
            {
                KSItem i = new KSItem
                {
                    Id     = Convert.ToInt32(itemElement.Attribute("Id").Value),
                    Value  = Convert.ToInt32(itemElement.Attribute("Value").Value),
                    Weight = Convert.ToInt32(itemElement.Attribute("Weight").Value),
                    Volume = Convert.ToInt32(itemElement.Attribute("Volume").Value)
                };

                KSItemList.Add(i);
            }
        }
Exemple #5
0
        /*
         *    //Bottom up Dynamic Programming approach to Knapsack
         * Description / Algorithm from here:
         * https://github.com/ayzahmt/Knapsack-Problem
         *   Solution of knapsack problem using dynamic programming
         *
         *   Purpose
         *   To get as much value into the knapsack as possible given the weight constraint of the knapsack.
         *
         *   Solution Approach
         *   Evaluate the values of the items iteratively.
         *   For example, put the first item and select second item. Then evaluate first and second item. Make the most appropriate choice according to the value and weight of the items. And then evaluate the all item.
         *   In order to get rid of the recalculation, the calculation for the items are kept on a table at each step.
         *   After the items have been evaluated, the value of V[ItemCount,MaximumWeight] shows the maximum value we can get to the knapsack.
         *   Example
         *   Item Count = 4
         *   Max Weight = 5
         *   Item	1	2	3	4
         *   Value	100	20	60	40
         *   Weight	3	2	4	1
         *   Value Matrix
         *
         *   V[i,w]	w=0	1	2	3	4	5
         *   i=0	0	0	0	0	0	0
         *   1	0	0	0	100	100	100
         *   2	0	0	20	100	100	120
         *   3	0	0	20	100	100	120
         *   4	0	40	40	100	140	140
         *   Maximum value we can put the knapsack is V[4,5] = 140
         *
         *  // Processing requirements
         *   O(N*W) ** Requires int array of size [N,M] to store previous results
         */

        private void DetermineOptimalSolution(KSItem[] items)
        {
            int  n             = items.Length;
            bool includeVolume = (TM.MaxVolume != null);

            //Hard Constraints
            int maxWeight = TM.MaxWeight;
            int maxVolume = TM.MaxVolume.GetValueOrDefault(0);

            int curWeight, curVolume;

            //Value array is multidimensional (Number of Items, maxWeight, maxVolume)
            int[,,] valueArray = new int[n + 1, maxWeight + 1, maxVolume + 1];

            for (int i = 0; i <= n; i++)
            {
                for (curWeight = 0; curWeight <= maxWeight; curWeight++)
                {
                    for (curVolume = 0; curVolume <= maxVolume; curVolume++)
                    {
                        //Base case
                        if (i == 0 || curWeight == 0 || (includeVolume && curVolume == 0))
                        {
                            valueArray[i, curWeight, curVolume] = 0;
                        }

                        //If the item Can be added
                        else if (items[i - 1].Weight <= curWeight && (!includeVolume || (includeVolume && (items[i - 1].Volume <= curVolume))))
                        {
                            int nextItem  = items[i - 1].Value;
                            int prevState = valueArray[i - 1, curWeight - items[i - 1].Weight, includeVolume ? curVolume - items[i - 1].Volume : 0];

                            //Value of adding the next item VS Value of NOT adding the item
                            valueArray[i, curWeight, curVolume] = Math.Max((nextItem + prevState), valueArray[i - 1, curWeight, curVolume]);
                        }
                        //If the next item cannot be added set it to the previous state
                        else
                        {
                            valueArray[i, curWeight, curVolume] = valueArray[i - 1, curWeight, curVolume];
                        }
                    }
                }
            }

            //int maxValue = valueArray[n, maxWeight, maxVolume];
            //Next we need to "Trace Back" to get the actual solution
            //Start at the max K[n,W]
            int remWeight = maxWeight;
            int remVolume = maxVolume;

            for (int i = n; i > 0; i--)
            {
                //IF moving from i[index-1] -> i[index-2] is the same than
                // i[index-1] is NOT part of the solution
                // however if they are different than it is.
                if (valueArray[i, remWeight, remVolume] != valueArray[(i - 1), remWeight, remVolume])
                {
                    KSItem choosenItem = items[i - 1];
                    OptimalSolution.TryAddItem(choosenItem);
                    remWeight -= choosenItem.Weight;

                    if (includeVolume)
                    {
                        remVolume -= choosenItem.Volume;
                    }
                }
            }

            //If we are including Volume than use it else use 1
            SolutionCount = n * maxWeight * (includeVolume ? maxVolume : 1);
        }