private int FindPivotRow(int pivotCol)
        {
            int pivotRow = -1;

            for (int i = 1; i < SimplexTable.GetLength(0) - 2; i++)
            {
                if (SimplexTable[i, pivotCol] > 0)
                {
                    pivotRow = i;
                    break;
                }
            }
            if (pivotRow == -1)
            {
                return(pivotRow);
            }
            for (int i = pivotRow + 1; i < SimplexTable.GetLength(0) - 2; i++)
            {
                if ((SimplexTable[i, pivotCol] > 0) && ((SimplexTable[i, 0] / SimplexTable[i, pivotCol]) <
                                                        (SimplexTable[pivotRow, 0] / SimplexTable[pivotRow, pivotCol])))
                {
                    pivotRow = i;
                }
            }
            return(pivotRow);
        }
        private void FindInitialCosts()
        {
            int costMRow = SimplexTable.GetLength(0) - 1;
            int costRow  = costMRow - 1;

            for (int j = 0; j < SimplexTable.GetLength(1); j++)
            {
                SimplexTable[costRow, j]  = 0;
                SimplexTable[costMRow, j] = 0;
                for (int i = 1; i < SimplexTable.GetLength(0) - 2; i++)
                {
                    if (SimplexTable[0, Basis[i - 1]] == -M)
                    {
                        SimplexTable[costMRow, j] += (SimplexTable[i, j] * (-1));
                    }
                    else
                    {
                        SimplexTable[costRow, j] += (SimplexTable[i, j] * SimplexTable[0, Basis[i - 1]]);
                    }
                }
                if (SimplexTable[0, j] == -M)
                {
                    SimplexTable[costMRow, j] += 1;
                }
                else
                {
                    SimplexTable[costRow, j] -= SimplexTable[0, j];
                }
            }
        }
        public Tuple <double, double[]> Calculate()
        {
            ConvertToStandardForm();
            AddArtificialVariables();
            FindInitialCosts();
            int prev = -1;

            while (true)
            {
                int pivotColumn = FindPivotColumn();
                if (pivotColumn == -1 || pivotColumn == prev)
                {
                    if (Math.Round(SimplexTable[SimplexTable.GetLength(0) - 1, 0], 10) != 0)
                    {
                        return(null);
                    }
                    else
                    {
                        double[] xOpt = new double[Prob.CountVariables];
                        for (int i = 0; i < Basis.Length; i++)
                        {
                            if (Basis[i] - 1 < Prob.CountVariables)
                            {
                                xOpt[Basis[i] - 1] = SimplexTable[i + 1, 0];
                            }
                        }
                        if (additionalVars.Count != 0)
                        {
                            foreach (KeyValuePair <int, int> pair in additionalVars)
                            {
                                xOpt[pair.Key] -= xOpt[pair.Value];
                            }
                        }
                        double optObjectiveValue = SimplexTable[SimplexTable.GetLength(0) - 2, 0];
                        if (Prob.Minimize[CriteriaInd] == true)
                        {
                            optObjectiveValue *= -1;
                        }
                        return(new Tuple <double, double[]>(optObjectiveValue, xOpt));
                    }
                }
                else
                {
                    int pivotRow = FindPivotRow(pivotColumn);
                    if (pivotRow == -1)
                    {
                        return(null);
                    }
                    Basis[pivotRow - 1] = pivotColumn;
                    FindNewSimplexTable(pivotRow, pivotColumn);
                    prev = pivotColumn;
                }
            }
        }
 public void MakeConstantsPositive()
 {
     for (int i = 0; i < Prob.CountConstraint; i++)
     {
         if (Prob.Constants[i] < 0)
         {
             for (int j = 0; j < SimplexTable.GetLength(1); j++)
             {
                 SimplexTable[i + 1, j] *= -1;
             }
         }
     }
 }
        private int FindPivotColumn()
        {
            List <int> minInd = new List <int>();
            double     minM   = SimplexTable[costMRow, 1];

            for (int j = 2; j < SimplexTable.GetLength(1); j++)
            {
                if (SimplexTable[costMRow, j] < minM)
                {
                    minM = SimplexTable[costMRow, j];
                }
            }
            for (int j = 1; j < SimplexTable.GetLength(1); j++)
            {
                if (SimplexTable[costMRow, j] == minM)
                {
                    minInd.Add(j);
                }
            }
            if (minInd.Count == 1)
            {
                return(minInd[0]);
            }
            else
            {
                double min      = SimplexTable[costRow, 1];
                int    pivotCol = minInd[0];
                foreach (int j in minInd)
                {
                    if (SimplexTable[costRow, j] < min)
                    {
                        min      = SimplexTable[costRow, j];
                        pivotCol = j;
                    }
                }
                if (min >= 0 && minM >= 0)
                {
                    return(-1);
                }
                else
                {
                    return(pivotCol);
                }
            }
        }
        private void FindNewSimplexTable(int pivotRow, int pivotColumn)
        {
            double[,] oldTable = new double[SimplexTable.GetLength(0), SimplexTable.GetLength(1)];
            Array.Copy(SimplexTable, oldTable, SimplexTable.Length);
            double pivotElem = SimplexTable[pivotRow, pivotColumn];

            for (int j = 0; j < SimplexTable.GetLength(1); j++)
            {
                SimplexTable[pivotRow, j] = oldTable[pivotRow, j] / pivotElem;
            }
            for (int i = 1; i < SimplexTable.GetLength(0); i++)
            {
                if (i == pivotRow || oldTable[i, pivotColumn] == 0)
                {
                    continue;
                }
                for (int j = 0; j < SimplexTable.GetLength(1); j++)
                {
                    SimplexTable[i, j] = oldTable[i, j] - (oldTable[i, pivotColumn] * SimplexTable[pivotRow, j]);
                }
            }
        }