コード例 #1
0
        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());
        }
コード例 #2
0
        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());
        }
コード例 #3
0
        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 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()));
        }
コード例 #5
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());
        }
コード例 #6
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());
        }