Пример #1
0
        private void KnapsackAlgorithm()
        {
            foreach (var cost in Costs)
            {
                int categoryIndex = 0;
                foreach (var category in Categories)
                {
                    foreach (var element in category.Elements)
                    {
                        var kvp = new KeyValuePair <decimal, Element>(cost, element);

                        decimal param1 = 0;
                        if (categoryIndex != 0)
                        {
                            var previous = Categories.ElementAtOrDefault(categoryIndex - 1);
                            param1 = MaxValueInCategory(previous, cost);
                        }

                        decimal param2 = 0;
                        if (cost - element.Cost >= 0)
                        {
                            if (cost - element.Cost > 0 && categoryIndex != 0)
                            {
                                var previous = Categories.ElementAtOrDefault(categoryIndex - 1);
                                param2 = MaxValueInCategory(previous, cost - element.Cost);
                            }
                            param2 += element.Value;
                        }

                        var max    = param1 >= param2 ? param1 : param2;
                        var chosen = param1 <= param2;
                        Table.Add(kvp, max);
                        DecisionTable.Add(kvp, chosen);
                    }
                    categoryIndex++;
                }
            }
        }
Пример #2
0
 private void btOK_Click(object sender, EventArgs e)
 {
     Result = new DecisionTable();
     for (int i = 0; i < _dataTable.Rows.Count; i++)
     {
         Result.Add((MathNodeRoot)(_dataTable.Rows[i][0]), (ActionList)(_dataTable.Rows[i][1] == DBNull.Value ? null : _dataTable.Rows[i][1]));
     }
     Result.ConditionColumnWidth = dataGridView1.Columns[0].Width;
     Result.ActionColumnWidth    = dataGridView1.Columns[1].Width;
     Result.ControlSize          = this.ClientSize;
     if (EditFinished != null)
     {
         EditFinished(this, EventArgs.Empty);
     }
     else
     {
         Form f = FindForm();
         if (f != null)
         {
             f.DialogResult = DialogResult.OK;
         }
     }
 }