Ejemplo n.º 1
0
        protected BackPackingSolverBase(List <BackPackItem> items, double capacity)
        {
            _theVault    = new ItemVault();
            _theBackPack = new BackPack(capacity);

            foreach (var item in items)
            {
                _theVault.AddItem(item);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The solver follows these steps:
        /// 1) Select the "best" item from the Vault (see PickBestItemFromVault).
        ///    If (a description of) an item is returned, then
        ///    2) Remove the item from the Vault.
        ///    3) Add the item to the Backpack.
        ///    4) Call Solve again (the weight capacity will now be reduced).
        /// </summary>
        public override void Solve(ItemVault theItemVault, BackPack theBackPack)
        {
            string description = PickNextItemFromVault(theItemVault, theBackPack.WeightCapacityLeft);

            if (description != string.Empty)
            {
                BackPackItem item = theItemVault.RemoveItem(description);
                theBackPack.AddItem(item);
                Solve(theItemVault, theBackPack);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This method picks the "best" item currently in the Vault.
        /// "Best" is defined as the item for which it holds that:
        ///   1) The weight of the item does not exceed the given limit.
        ///   2) No other item (for which 1) holds) has a higher
        ///      "actual item value". The calculation of "actual item value"
        ///      is deferred to derived classes.
        /// </summary>
        /// <returns>
        /// Identifier for the "best" item (String.Empty if no item found)
        /// </returns>
        private string PickNextItemFromVault(ItemVault theItemVault, double weightLimit)
        {
            double       bestValue = 0;
            BackPackItem bestItem  = null;

            foreach (var item in theItemVault.Items)
            {
                if (item.Weight <= weightLimit)
                {
                    double candidateValue = ActualItemValue(item);

                    if (candidateValue > bestValue)
                    {
                        bestValue = candidateValue;
                        bestItem  = item;
                    }
                }
            }

            return((bestItem != null) ? bestItem.Description : string.Empty);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// This method just returns the first item in the Vault, if
 ///   1) Any items are left at all, and
 ///   2) The weight of the item does not exceed the given limit.
 /// Yes, this is a pretty stupid approach...
 /// </summary>
 /// <returns>
 /// Identifier for the next item (String.Empty if no item found)
 /// </returns>
 private string PickNextItemFromVault(ItemVault theItemVault, double weightLimit)
 {
     return((theItemVault.Items.Count > 0 && theItemVault.Items[0].Weight <= weightLimit)
         ? theItemVault.Items[0].Description
         : String.Empty);
 }
 /// <summary>
 /// Override this method to implement a specific algorithm
 /// for backpacking.
 /// </summary>
 public abstract void Solve(ItemVault theItemVault, BackPack theBackPack);