private void WriteSolutionAndData(ItemGroup requiredValueGroup)
        {
            LogFile.WriteLine("Maximum value after choosing {0} of first {1} items at weight {2} is {3}",
                              _knapsack.AllowedItems,
                              _items.Count,
                              _knapsack.Capacity,
                              requiredValueGroup.TotalValue()
                              );
            LogFile.WriteLine("No of items: {0}", requiredValueGroup.ItemCount());
            LogFile.WriteLine("Item names: {0}", requiredValueGroup.ItemNames());
            LogFile.WriteLine("Total weight: {0}", requiredValueGroup.TotalWeight());
            LogFile.WriteLine("Total value: {0}", requiredValueGroup.TotalValue());

            Console.WriteLine("Maximum value after choosing {0} of first {1} items at weight {2} is {3}",
                              _knapsack.AllowedItems,
                              _items.Count,
                              _knapsack.Capacity,
                              requiredValueGroup.TotalValue()
                              );
            Console.WriteLine("No of items: {0}", requiredValueGroup.ItemCount());
            Console.WriteLine("Item names: {0}", requiredValueGroup.ItemNames());
            Console.WriteLine("Total weight: {0}", requiredValueGroup.TotalWeight());
            Console.WriteLine("Total value: {0}", requiredValueGroup.TotalValue());

            DumpArrayToLog(m => m.TotalValue());
            DumpArrayToLog(m => m.ItemCount());
        }
        public void CalculateRecursive()
        {
            int currentCapacity = _knapsack.Capacity;

            ItemGroup requiredValueGroup = GetMaxValue(_items.Count, currentCapacity, _knapsack.AllowedItems);

            // Actual solution is not simply finding a value at capacity and allowed items
            // because we are only solving for kKP (not E-kKP)
            // Actually want to find value at AllowedItems, but potentially at lower capacity, with ItemCount = AllowedItems
            while (requiredValueGroup.ItemCount() < _knapsack.AllowedItems)
            {
                if (--currentCapacity == 0)
                {
                    break;
                }

                requiredValueGroup = GetMaxValue(_items.Count, currentCapacity, _knapsack.AllowedItems);
            }

            if (currentCapacity == 0)
            {
                Console.WriteLine(
                    "There is no valid solution with capacity constraint of {0} and count constraint of {1}.",
                    _knapsack.Capacity, _knapsack.AllowedItems);

                return;
            }

            LogFile.WriteLine("Maximum value after choosing {0} of first {1} items at weight {2} is {3}",
                              _knapsack.AllowedItems,
                              _items.Count,
                              _knapsack.Capacity,
                              requiredValueGroup.TotalValue()
                              );
            LogFile.WriteLine("No of items: {0}", requiredValueGroup.ItemCount());
            LogFile.WriteLine("Item names: {0}", requiredValueGroup.ItemNames());
            LogFile.WriteLine("Total weight: {0}", requiredValueGroup.TotalWeight());
            LogFile.WriteLine("Total value: {0}", requiredValueGroup.TotalValue());

            Console.WriteLine("Maximum value after choosing {0} of first {1} items at weight {2} is {3}",
                              _knapsack.AllowedItems,
                              _items.Count,
                              _knapsack.Capacity,
                              requiredValueGroup.TotalValue()
                              );
            Console.WriteLine("No of items: {0}", requiredValueGroup.ItemCount());
            Console.WriteLine("Item names: {0}", requiredValueGroup.ItemNames());
            Console.WriteLine("Total weight: {0}", requiredValueGroup.TotalWeight());
            Console.WriteLine("Total value: {0}", requiredValueGroup.TotalValue());
        }
        private void WriteSolutionAndData()
        {
            bool exactSolutionExists = Exact_K_SolutionExists();

            if (exactSolutionExists)
            {
                ItemGroup maxProfitGroup = GetSolution(_knapsack.AllowedItems);

                Console.WriteLine("Solution exists for exactly {0} items", _knapsack.AllowedItems);
                Console.WriteLine("  No of items:  {0}", maxProfitGroup.ItemCount());
                Console.WriteLine("  Item names:   {0}", maxProfitGroup.ItemNames());
                Console.WriteLine("  Total weight: {0}", maxProfitGroup.TotalWeight());
                Console.WriteLine("  Total value:  {0}", maxProfitGroup.TotalValue());
            }

            ItemGroup optimalGroup = GetOptimalSolution();

            Console.WriteLine("Optimal value solution:");
            Console.WriteLine("  No of items:  {0}", optimalGroup.ItemCount());
            Console.WriteLine("  Item names:   {0}", optimalGroup.ItemNames());
            Console.WriteLine("  Total weight: {0}", optimalGroup.TotalWeight());
            Console.WriteLine("  Total value:  {0}", optimalGroup.TotalValue());

            //DumpArrayToLog(m => m.TotalValue());
            //DumpArrayToLog(m => m.ItemCount());
        }
예제 #4
0
        public static double LPRelaxedApprox_kKP(List <Item> items, Knapsack knapsack)
        {
            ItemGroup group = new ItemGroup();

            List <Item> sortedItems = UtilFunctions.SortByUnitProfitDescending(items);

            Item   criticalItem         = new Item("", 0, 0);
            double criticalItemFraction = 0;

            // Fill until we reach critical item
            foreach (Item item in sortedItems)
            {
                // if count will be within max count
                if (group.ItemCount() < knapsack.AllowedItems)
                {
                }

                // if weight will be within capacity
                if (group.TotalWeight() + item.Weight <= knapsack.Capacity)
                {
                    group.AddItem(item);
                }
                else // we have reached critical item
                {
                    criticalItem         = item;
                    criticalItemFraction = (knapsack.Capacity - group.TotalWeight()) / (double)criticalItem.Weight;
                    break;
                }
            }

            // Add fractional value of next item
            double maxProfitValue = group.TotalValue() + criticalItem.Value * criticalItemFraction;

            return(maxProfitValue);
        }
        public string GroupAsText(ItemGroup group)
        {
            if (group is null)
            {
                return("null");
            }

            return(string.Format("Items: {0}, Count: {1}, Total weight: {2}",
                                 group.ItemNames(),
                                 group.ItemCount(),
                                 group.TotalWeight()));
        }
예제 #6
0
        private void WriteSolutionAndData()
        {
            ItemGroup requiredValueGroup = _maxProfitItemGroup[_items.Count, _knapsack.Capacity];

            Console.WriteLine("Maximum value for first {0} items at weight {1} is {2}", _items.Count, _knapsack.Capacity, requiredValueGroup.TotalValue());
            Console.WriteLine("No of items: {0}", requiredValueGroup.ItemCount());
            Console.WriteLine("Item names: {0}", requiredValueGroup.ItemNames());
            Console.WriteLine("Total weight: {0}", requiredValueGroup.TotalWeight());
            Console.WriteLine("Total value: {0}", requiredValueGroup.TotalValue());

            DumpArrayToLog(m => m.TotalValue());
            DumpArrayToLog(m => m.ItemCount());
        }
예제 #7
0
        public void CalculateRecursive()
        {
            if (Exact_K_SolutionExists())
            {
                ItemGroup maxProfitGroup = GetSolution(_knapsack.AllowedItems);

                Console.WriteLine("Solution exists for exactly {0} items", _knapsack.AllowedItems);
                Console.WriteLine("  No of items:  {0}", maxProfitGroup.ItemCount());
                Console.WriteLine("  Item names:   {0}", maxProfitGroup.ItemNames());
                Console.WriteLine("  Total weight: {0}", maxProfitGroup.TotalWeight());
                Console.WriteLine("  Total value:  {0}", maxProfitGroup.TotalValue());
            }

            ItemGroup optimalGroup = GetOptimalSolution();

            Console.WriteLine("Optimal value solution:");
            Console.WriteLine("  No of items:  {0}", optimalGroup.ItemCount());
            Console.WriteLine("  Item names:   {0}", optimalGroup.ItemNames());
            Console.WriteLine("  Total weight: {0}", optimalGroup.TotalWeight());
            Console.WriteLine("  Total value:  {0}", optimalGroup.TotalValue());
        }