public override ConstructiveResult Solve(KnapsackInstance instance)
        {
            ConstructiveStrategy strategy;

            if (instance.KnapsackSize < instance.GetPriceOfAllItems())
            {
                strategy = new ConstructiveDPCapacity();
            }
            else
            {
                strategy = new ConstructiveDPPrice();
            }

            return(strategy.Solve(instance));
        }
        public KnapsackResult Solve(KnapsackInstance instance)
        {
            BestConfiguration = new KnapsackConfiguration {
                Price = 0, Weight = 0, ItemVector = new bool[instance.ItemCount].ToList()
            };

            FindBestConfiguration(0, new KnapsackConfiguration {
                Price = 0, Weight = 0, ItemVector = new List <bool>()
            }, instance.GetPriceOfAllItems(), instance);

            var result = new KnapsackResult
            {
                KnapsackInstance = instance,
                Configuration    = BestConfiguration
            };

            return(result);
        }
Example #3
0
        public override ConstructiveResult Solve(KnapsackInstance instance)
        {
            BestConfiguration = new KnapsackConfiguration {
                Price = int.MinValue, Weight = 0, ItemVector = CreateEmptySolution(instance.ItemCount)
            };

            FindBestConfiguration(0, new KnapsackConfiguration {
                Price = 0, Weight = 0, ItemVector = new List <bool>()
            }, instance.GetPriceOfAllItems(), instance);

            var result = new ConstructiveResult
            {
                KnapsackInstance = instance,
                NumberOfSteps    = numberOfSteps,
                Configuration    = BestConfiguration
            };

            return(result);
        }
Example #4
0
        protected override void InitializeTable(KnapsackInstance instance)
        {
            memoryTable = new DPCell[instance.GetPriceOfAllItems() + 1, instance.ItemCount];
            toVisit     = new Queue <int>();

            toVisit.Enqueue(0);
            memoryTable[0, 0] = new DPCell {
                AddedItem = false, Value = 0
            };
            var firstItem = instance.Items.First();

            if (firstItem.Weight <= instance.KnapsackSize && firstItem.Price > 0)
            {
                toVisit.Enqueue(firstItem.Price);

                memoryTable[firstItem.Price, 0] = new DPCell {
                    AddedItem = true, Value = firstItem.Weight
                };
            }
        }