Exemple #1
0
        public override void Solve(double capacityLeft)
        {
            string description = PickNextItemFromVault(capacityLeft);

            if (description != string.Empty)
            {
                BackPackItem item = TheVault.RemoveItem(description);
                TheBackPack.AddItem(item);
                Solve(TheBackPack.WeightCapacityLeft);
            }
        }
Exemple #2
0
 /// <summary>
 /// Naive solver implementation (but it's recursive!)
 /// </summary>
 public override void Solve(double capacityLeft)
 {
     // Keep adding the first element from the vault, until
     // the first element cannot fit into the backpack...
     if (TheVault.Items[0].Weight <= capacityLeft)
     {
         BackPackItem item = TheVault.RemoveItem(TheVault.Items[0].Description);
         TheBackPack.AddItem(item);
         Solve(TheBackPack.WeightCapacityLeft);
     }
 }
        public override void Solve(double capacityLeft)
        {
            //sort by weight
            List <BackPackItem> items = new List <BackPackItem>();

            foreach (BackPackItem item in TheVault.Items)
            {
                items.Add(item);
            }
            items = items.OrderBy(o => o.Weight).ToList();
            items.Reverse();


            if (items[0].Weight <= capacityLeft)
            {
                BackPackItem item = TheVault.RemoveItem(items[0].Description); //vault remove item
                TheBackPack.AddItem(items[0]);                                 //backpack add item
                Solve(TheBackPack.WeightCapacityLeft);                         //recursive
            }
        }
Exemple #4
0
        public override void Solve(double capacityLeft)
        {
            BackPackItem currentItem = TheVault.Items[0];

            foreach (BackPackItem item in TheVault.Items)
            {
                if ((item.Value / item.Weight) > (currentItem.Value / currentItem.Weight))
                {
                    currentItem = item;
                }
            }

            if (currentItem.Weight <= capacityLeft)
            {
                TheVault.RemoveItem(currentItem.Description);
                TheBackPack.AddItem(currentItem);
                Solve(TheBackPack.WeightCapacityLeft);
            }
            else     //most valuable item (by ratio) weighs more than we have room for
            {
                List <BackPackItem> lowWeightItems = new List <BackPackItem>();
                foreach (BackPackItem item in TheVault.Items)
                {
                    if (item.Weight < capacityLeft)
                    {
                        lowWeightItems.Add(item);
                    }
                }
                lowWeightItems = lowWeightItems.OrderBy(o => o.Value).ToList();

                if (lowWeightItems.Count > 0)
                {
                    if (lowWeightItems[0].Weight <= capacityLeft)
                    {
                        TheVault.RemoveItem(lowWeightItems[0].Description);
                        TheBackPack.AddItem(lowWeightItems[0]);
                        Solve(TheBackPack.WeightCapacityLeft);
                    }
                }
            }
        }