예제 #1
0
        /// <summary>
        /// metóda pre spracovanie uzlu stromu
        /// </summary>
        /// <param name="nextUsed"></param>
        public void Process(bool nextUsed)
        {
            myUsed[myProcessedIndex] = nextUsed ? 1 : 0;
            ComputeValueAndWeight();
            if (!IsHeavier && myValue > KnapsackNode.BestValue)
            {
                if (KnapsackNode.BestNode != null)
                {
                    KnapsackNode.BestNode.IsBestNode = false;
                }
                KnapsackNode.BestValue = myValue;
                KnapsackNode.BestNode = this;
                IsBestNode = true;
            }

            double futureValue = myValue;
            for (int i = myProcessedIndex + 1; i < myItems.Count; i++)
            {
                futureValue += myItems.Count;
            }
            if (!IsHeavier && !AreAllUsed && futureValue > KnapsackNode.BestValue)
            {
                myLeftChild = new KnapsackNode(myItems, myUsed);
                myLeftChild.Process(true);

                myRightChild = new KnapsackNode(myItems, myUsed);
                myRightChild.Process(false);
            }
        }
예제 #2
0
        /// <summary>
        /// metóda pre spustenie algoritmu
        /// </summary>
        public void Start()
        {
            if (myItems == null || myItems.Count == 0)
            {
                return;
            }

            try
            {
                int tempVarNoItems = 0;
                double tempVarAllItems = 0;
                foreach (KnapsackItem item in myItems)
                {
                    if (item.Weight > KnapsackNode.MaxWeight)
                    {
                        tempVarNoItems++;
                    }
                    tempVarAllItems += item.Weight;
                }
                if (tempVarNoItems == myItems.Count)
                {
                    Solution = "Do batoha sa nezmesí žiaden predmet";
                    return;
                }
                if (tempVarAllItems < KnapsackNode.MaxWeight)
                {
                    Solution = "Do batoha sa zmestia všetky predmety";
                    return;
                }

                myItems.Sort();

                //predmet 1
                KnapsackNode rootLeft = new KnapsackNode(myItems);
                rootLeft.Process(true);

                //predmet 0
                KnapsackNode rootRight = new KnapsackNode(myItems);
                rootRight.Process(false);

                StringBuilder builder = new StringBuilder();
                builder.AppendLine("Do batoha sa zoberú predmety:");
                int index = 0;
                foreach (int? item in KnapsackNode.BestNode.Used)
                {
                    if (item.HasValue && item.Value == 1)
                    {
                        builder.AppendLine(string.Format("váha = {0:0.00}, hodnota = {1:0.00}", myItems[index].Value, myItems[index].Weight));
                    }
                    index++;
                }
                Solution = builder.ToString();
            }
            catch
            {
                Solution = "Úlohu sa nepodarilo vyriešiť.";
            }
        }