Пример #1
0
        // Solve button \\

        private void solveButton_Click(object sender, EventArgs e)
        {
            outputDataGrid.Rows.Clear();

            solver.weightLimit = int.Parse(weightUpDown.Text);
            ProfitTestState maxState = solver.solve();

            // Display maxState
            foreach (Item item in maxState.subItems)
            {
                outputDataGrid.Rows.Add(item.name, item.profit);
            }
            outputDataGrid.Rows.Add();
            outputDataGrid.Rows.Add("Total:", maxState.overallProfit);
        }
Пример #2
0
        // Solve() returns the max ProfitTestState as the solution.
        public ProfitTestState solve()
        {
            // Create f sub j matrix.
            profitMatrix = new ProfitTestState[items.Count, weightLimit + 1];
            for (int i = 0; i < items.Count; i++)
            {
                Item item = items[i];
                for (int j = 0; j <= weightLimit; j++)
                {
                    if (item.weight <= j + 1 && item.weight != 0)
                    {
                        int numberOfItems = j / item.weight;

                        ProfitTestState pItem = new ProfitTestState();
                        pItem.overallProfit = numberOfItems * item.profit;
                        for (int p = 0; p < numberOfItems; p++)
                        {
                            pItem.subItems.Add(item);
                        }
                        profitMatrix[i, j] = pItem;
                    }
                    else
                    {
                        // Assign an empty one so we don't have null collisions.
                        profitMatrix[i, j] = new ProfitTestState();
                    }
                }
            }

            // Find max of d and m matrix.
            double          max = double.MinValue;
            ProfitTestState maxProfitTestState = new ProfitTestState();

            for (int j = 0; j < items.Count; j++)
            {
                for (int u = 0; u <= weightLimit; u++)
                {
                    ProfitTestState state = maxForJandU(j + 1, u + 1);
                    if (state.overallProfit > max)
                    {
                        max = state.overallProfit;
                        maxProfitTestState = state;
                    }
                }
            }

            return(maxProfitTestState);
        }
Пример #3
0
        // Recursive method that returns a combination from j and u.
        private ProfitTestState maxForJandU(int j, int u)
        {
            if (j == items.Count)
            {
                return(profitMatrix[j - 1, u - 1]);
            }
            else
            {
                ProfitTestState maxState = new ProfitTestState();
                for (int x = 0; x < u; x++)
                {
                    // Recursive forumla.
                    ProfitTestState currentState    = profitMatrix[j - 1, x];
                    ProfitTestState currentMaxState = maxForJandU(j + 1, u - x);
                    if (currentMaxState.overallProfit == 0)
                    {
                        currentMaxState.subItems = new List <Item>();
                    }

                    double value = currentState.overallProfit + currentMaxState.overallProfit;
                    if (value > maxState.overallProfit)
                    {
                        maxState = new ProfitTestState();
                        maxState.overallProfit = value;

                        maxState.subItems = new List <Item>(currentMaxState.subItems);
                        foreach (Item item in currentState.subItems)
                        {
                            maxState.subItems.Add(item);
                        }
                    }
                }

                return(maxState);
            }
        }