Esempio n. 1
0
        //gets all objects (int[,]) and outputs the solution
        private void BT_Next_Click(object sender, EventArgs e)
        {
            if (objectCurrent < this.objectAmount - 2)
            {
                GetObjectValues();
                GB_Objects.Text = "Object " + (objectCurrent + 1);
            }
            else if (objectCurrent < this.objectAmount - 1)
            {
                GetObjectValues();
                GB_Objects.Text    = "";
                GB_Objects.Enabled = false;
                BT_Next.Text       = "GO";
            }
            else
            {
                Knapsack solve = new Knapsack();
                bool[]   used;

                int maxValue = solve.Main(this.dp_table, this.objects, out used);

                LB_Solution.Text = "The maximal value of the knapsack's content is " + maxValue + " containing:";
                for (int i = 0; i < names.Length; i++)
                {
                    if (used[i])
                    {
                        LB_Solution.Text += "\r\n" + names[i];
                    }
                }
                GB_Initialise.Enabled = true;
                BT_Next.Text          = "Next";
            }
        }
Esempio n. 2
0
 private static void ComputeMaxCostForEachWeight(Knapsack knapsack, Item item, KnapsackTable table)
 {
     for (var weight = 1; weight <= knapsack.TotalWeight; weight++)
     {
         ComputeMaxCost(item, weight, table);
     }
 }
Esempio n. 3
0
        public static double GetMaxPossibleCost(List <Item> allItems, Knapsack knapsack)
        {
            var table = new KnapsackTable(knapsack.TotalWeight);

            foreach (var item in allItems)
            {
                ComputeMaxCostForEachWeight(knapsack, item, table);
                table.NewLine();
            }

            return(table.GetResult());
        }