Ejemplo n.º 1
0
        private MainElem SelectMainElement()//находим максимальный по модулю элемент  запоминаем строку со столбцом
        {
            MainElem mainElem = new MainElem();

            mainElem.Column = -1;
            mainElem.Row    = -1;
            double minValue = double.MinValue;

            foreach (var column in columns)
            {
                if (simplexTable[simplexTable.GetLength(0) - 1, column] < 0)
                {
                    continue;
                }
                double coef = Math.Round(GetCoef(column, out var row) * simplexTable[simplexTable.GetLength(0) - 1, column], Precision);
                if (coef < minValue)
                {
                    continue;
                }
                minValue        = coef;
                mainElem        = new MainElem();
                mainElem.Column = column;
                mainElem.Row    = row;
            }
            return(mainElem);
        }
Ejemplo n.º 2
0
        private bool DoAlgorithm()
        {
            MainElem pivot = SelectMainElement();//находим ведущий элемент

            if (pivot.Column < 0 && pivot.Row < 0)
            {
                return(false);
            }
            for (int col = 0; col < simplexTable.GetLength(1); col++)
            {
                if (col != pivot.Column)
                {
                    simplexTable[pivot.Row, col] =
                        Math.Round(simplexTable[pivot.Row, col] / simplexTable[pivot.Row, pivot.Column], Precision);//делим ведущую строку на ведущий элемент
                }
            }

            simplexTable[pivot.Row, pivot.Column] = 1;

            //преобразование матрицы
            for (int row = 0; row < simplexTable.GetLength(0); row++)
            {
                if (row == pivot.Row)
                {
                    continue;
                }
                for (int col = 0; col < simplexTable.GetLength(1); col++)
                {
                    if (col != pivot.Column)
                    {
                        simplexTable[row, col] =
                            Math.Round(simplexTable[row, col] - (simplexTable[pivot.Row, col] * simplexTable[row, pivot.Column]),
                                       Precision);
                    }
                }

                simplexTable[row, pivot.Column] = 0;
            }

            int temp = basis[pivot.Row];

            basis[pivot.Row] = pivot.Column;
            for (int i = 0; i < columns.Length; i++)
            {
                if (columns[i] != pivot.Column)
                {
                    continue;
                }
                columns[i] = temp;
                break;
            }

            for (int col = 0; col < simplexTable.GetLength(1) - 1; col++)
            {
                if (simplexTable[simplexTable.GetLength(0) - 1, col] > 0)
                {
                    return(true);
                }
            }

            return(false);
        }