Esempio n. 1
0
        public override DecisionResult Solve(DecisionKnapsackInstance instance)
        {
            //the list is being processed backwards in the algorithm, so it must be sorted in ascending order
            instance.Items = instance.Items.OrderBy(a => a.Weight).ToList();

            return(new DecisionBranchBound().Solve(instance));
        }
Esempio n. 2
0
        bool DoesSolutionExist(int itemsRemaining, int currentPrice,
                               int currentWeight, int maxRemainingPrice, DecisionKnapsackInstance knapsackInstance)
        {
            numberOfSteps++;

            if (currentWeight > knapsackInstance.KnapsackSize)
            {
                return(false);
            }
            if (maxRemainingPrice < knapsackInstance.MinimalPrice)
            {
                return(false);
            }

            if (itemsRemaining == 0)
            {
                return(currentWeight <= knapsackInstance.KnapsackSize && currentPrice >= knapsackInstance.MinimalPrice);
            }

            KnapsackItem currentItem = knapsackInstance.Items[itemsRemaining - 1];

            //Put item in
            if (DoesSolutionExist(itemsRemaining - 1, currentPrice + currentItem.Price, currentWeight + currentItem.Weight, maxRemainingPrice, knapsackInstance))
            {
                return(true);
            }
            //Don't put item in
            if (DoesSolutionExist(itemsRemaining - 1, currentPrice, currentWeight, maxRemainingPrice - currentItem.Price, knapsackInstance))
            {
                return(true);
            }
            return(false);
        }
Esempio n. 3
0
        public override DecisionSolution Solve(DecisionKnapsackInstance instance)
        {
            var priceSum = instance.GetPriceOfAllItems();

            if (instance.MinimalPrice > priceSum)
            {
                return new DecisionSolution {
                           KnapsackInstance = instance, NumberOfSteps = 1, PermutationExists = false
                }
            }
            ;
            if (instance.MinimalPrice == 0)
            {
                return new DecisionSolution {
                           KnapsackInstance = instance, NumberOfSteps = 1, PermutationExists = true
                }
            }
            ;

            var priceRange = Math.Max(0.01, priceSum - instance.MinimalPrice);

            //the list is being processed backwards in the algorithm, so it must be sorted in ascending order
            instance.Items = instance.Items.OrderBy(a => Math.Max(a.Weight / instance.KnapsackSize, a.Price / priceRange)).
                             ToList();

            return(new DecisionBranchBound().Solve(instance));
        }
    }
}
Esempio n. 4
0
        public override DecisionSolution Solve(DecisionKnapsackInstance knapsackInstance)
        {
            numberOfSteps = 0;

            bool permutationExists = DoesSolutionExist(knapsackInstance.Items.Count, 0, 0, knapsackInstance);

            return(new DecisionSolution {
                KnapsackInstance = knapsackInstance, NumberOfSteps = numberOfSteps, PermutationExists = permutationExists
            });
        }
Esempio n. 5
0
 public abstract DecisionSolution Solve(DecisionKnapsackInstance instance);