Exemple #1
0
        public void FillDataGrid(AbstractSimplexTable table, DataGridView dgv)
        {
            // simplexTable
            if (comboBox_ChooseMethod.SelectedIndex == 0)
            {
                // головні змінні + для функції
                dgv.RowCount = table.NumberOfRows + 1;
                // mainVariables + basis variables + basis + freeValues + EstimateRelations
                dgv.ColumnCount = table.NumberOfCollumns + 3;
            }
            else
            {
                //doubleSimplexTable
                // головні змінні + для функції + EstimateRelations
                dgv.RowCount = table.NumberOfRows + 2;
                // mainVariables + basis variables + basis + freeValues
                dgv.ColumnCount = table.NumberOfCollumns + 2;
            }
            foreach (DataGridViewColumn column in dgv.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            dgv.Rows[table.MainRowForNextStep].Cells[table.MainCollumnForNextStep + 2].Style.BackColor = Color.AntiqueWhite;
            if (table.MainRow != -1)
            {
                dgv.Rows[table.MainRow].Cells[table.MainCollumn + 2].Style.BackColor = Color.White;
            }

            // basis
            dgv.Columns[0].HeaderText = "Базис";
            for (int i = 0; i < table.NumberOfRows; i++)
            {
                dgv.Rows[i].Cells[0].Value = "X" + (table.Basis[i] + 1);
            }

            // freeValues
            dgv.Columns[1].HeaderText = "Вільні члени";
            for (int i = 0; i < table.NumberOfRows; i++)
            {
                dgv.Rows[i].Cells[1].Value = String.Format("{0:0.00}", table.FreeValues[i]);
            }

            // variables
            for (int i = 0; i < table.NumberOfCollumns; i++)
            {
                dgv.Columns[i + 2].HeaderText = "X" + (i + 1);
                for (int j = 0; j < table.NumberOfRows; j++)
                {
                    dgv.Rows[j].Cells[i + 2].Value = String.Format("{0:0.00}", table.Variables[j][i]);
                }
            }
            // variables
            for (int i = 0; i < table.NumberOfCollumns; i++)
            {
                dgv.Columns[i + 2].HeaderText = "X" + (i + 1);
                for (int j = 0; j < table.NumberOfRows; j++)
                {
                    dgv.Rows[j].Cells[i + 2].Value = String.Format("{0:0.00}", table.Variables[j][i]);
                }
            }

            // function
            dgv.Rows[table.NumberOfRows].Cells[0].Value = "F";
            dgv.Rows[table.NumberOfRows].Cells[1].Value = table.FunctionResult;
            for (int i = 0; i < table.NumberOfCollumns; i++)
            {
                dgv.Rows[table.NumberOfRows].Cells[i + 2].Value = String.Format("{0:0.00}", table.Function[i]);
            }


            // simplexTable
            if (comboBox_ChooseMethod.SelectedIndex == 0)
            {
                // Estimate relations
                for (int i = 0; i < table.NumberOfRows; i++)
                {
                    if (table.EstimateRelations[i] < 0)
                    {
                        dgv.Rows[i].Cells[dgv.ColumnCount - 1].Value = 1.0f / 0;
                    }
                    else
                    {
                        dgv.Rows[i].Cells[dgv.ColumnCount - 1].Value = String.Format("{0:0.00}", table.EstimateRelations[i]);
                    }
                }
            }
            // double simplexTable
            else
            {
                // Estimate relations
                for (int i = 2; i < table.EstimateRelations.Length + 2; i++)
                {
                    if (Double.IsNaN(table.EstimateRelations[i - 2]))
                    {
                        dgv.Rows[dgv.RowCount - 1].Cells[i].Value = 1.0f / 0;
                    }
                    else
                    {
                        dgv.Rows[dgv.RowCount - 1].Cells[i].Value = String.Format("{0:0.00}", table.EstimateRelations[i - 2]);
                    }
                }
            }
        }
Exemple #2
0
 public Homory(SimplexTable table)
 {
     Table = table;
 }
Exemple #3
0
        private void button_Calculate_Click(object sender, EventArgs e)
        {
            try
            {
                ClearDataGrid(dataGridView_Result);

                int numberOfRows     = Convert.ToInt32(numericUpDown_InputData_Rows.Value);
                int numberOfCollumns = Convert.ToInt32(numericUpDown_InputData_Collumns.Value);

                double[][] mainArray = new double[numberOfRows][];
                for (int i = 0; i < numberOfRows; i++)
                {
                    mainArray[i] = new double[numberOfCollumns];
                }

                for (int i = 0; i < numberOfRows; i++)
                {
                    for (int j = 0; j < numberOfCollumns; j++)
                    {
                        mainArray[i][j] = Convert.ToDouble(dataGridView_InputData_Methods.Rows[i].Cells[j].Value);
                    }
                }

                double[] function = new double[numberOfCollumns];
                for (int i = 0; i < numberOfCollumns; i++)
                {
                    function[i] = Convert.ToDouble(dataGridView_InputData_Function.Rows[0].Cells[i].Value);
                }

                double[] freeValues = new double[numberOfRows];
                for (int i = 0; i < numberOfRows; i++)
                {
                    freeValues[i] = Convert.ToDouble(dataGridView_InputData_FreeValues.Rows[i].Cells[0].Value);
                }

                Compare[] signs = new Compare[numberOfRows];
                for (int i = 0; i < numberOfRows; i++)
                {
                    signs[i] = Convert.ToString(dataGridView_InputData_Signs.Rows[i].Cells[0].Value) == "="
                        ? Compare.Equal
                        : Convert.ToString(dataGridView_InputData_Signs.Rows[i].Cells[0].Value) == ">="
                            ? Compare.MoreEqual
                            : Compare.LessEqual;
                }

                Direction direction = radioButton_InputData_Max.Checked ? Direction.Max : Direction.Min;

                SimplexMethod simplexMethod = new SimplexMethod(numberOfRows, numberOfCollumns, mainArray, function,
                                                                freeValues, signs, direction);


                if (comboBox_ChooseMethod.SelectedIndex == 0)
                {
                    _table = simplexMethod.MakeSimplexTable();
                }
                else
                {
                    _table = simplexMethod.MakeDoubleSimplexTable();
                }
                FillDataGrid(_table, dataGridView_Result);

                if (comboBox_ChooseMethod.SelectedIndex == 0 && _table.Function[_table.MainCollumnForNextStep] >= 0)
                {
                    MessageBox.Show("Оптимальний план знайдено.\nЗначення функції - " + _table.FunctionResult + ".");
                }
                if (comboBox_ChooseMethod.SelectedIndex == 1 && _table.FreeValues[_table.MainRowForNextStep] >= 0)
                {
                    MessageBox.Show("Оптимальний план знайдено.\nЗначення функції - " + _table.FunctionResult + ".");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
Exemple #4
0
        public void AddRestriction()
        {
            int indexOfRow = FindIndexOfMaxFractionInArr(Table.FreeValues);

            // add row and collumn
            Table.NumberOfCollumns += 1;
            Table.NumberOfRows     += 1;

            // rewrite old variables
            double[][] newVariables = new double[Table.NumberOfRows][];
            for (int i = 0; i < newVariables.Length; i++)
            {
                newVariables[i] = new double[Table.NumberOfCollumns];
            }

            for (int i = 0; i < Table.NumberOfRows - 1; i++)
            {
                for (int j = 0; j < Table.NumberOfCollumns - 1; j++)
                {
                    newVariables[i][j] = Table.Variables[i][j];
                }
            }

            // add new row
            double[] lastRow = new double[Table.NumberOfCollumns];
            for (int i = 0; i < Table.NumberOfCollumns - 1; i++)
            {
                lastRow[i] = -TakeFraction(Table.Variables[indexOfRow][i]);
            }
            lastRow[Table.NumberOfCollumns - 1] = 1;
            for (int i = 0; i < Table.NumberOfCollumns; i++)
            {
                newVariables[Table.NumberOfRows - 1][i] = lastRow[i];
            }
            Table.Variables = newVariables;

            // change freeValues
            double[] freeValues = new double[Table.NumberOfRows];
            for (int i = 0; i < freeValues.Length - 1; i++)
            {
                freeValues[i] = Table.FreeValues[i];
            }
            freeValues[freeValues.Length - 1] = -TakeFraction(Table.FreeValues[indexOfRow]);
            Table.FreeValues = freeValues;

            // change basis
            int[] basis = new int[Table.NumberOfRows];
            for (int i = 0; i < Table.NumberOfRows - 1; i++)
            {
                basis[i] = Table.Basis[i];
            }
            basis[Table.NumberOfRows - 1] = Table.NumberOfRows - 1;
            Table.Basis = basis;

            // change function
            double[] function = new double[Table.NumberOfCollumns];
            for (int i = 0; i < Table.NumberOfCollumns - 1; i++)
            {
                function[i] = Table.Function[i];
            }
            Table.Function = function;
            var functionResult = Table.FunctionResult;

            Table = new DoubleSimplexTable(Table.NumberOfRows, Table.NumberOfCollumns - Table.NumberOfRows, basis, freeValues, newVariables, function);
            Table.FunctionResult = functionResult;
        }