Пример #1
0
        public LPSolution Solve()
        {
            LinearProgram.VerifyVariables();

            Tableau previousTableau = new Tableau();

            ConstraintRow[] constraints = new ConstraintRow[LinearProgram.Constraints.Length];
            for (int i = 0; i < constraints.Length; i++)
            {
                constraints[i] = new ConstraintRow(i, constraints.Length, LinearProgram.Constraints[i].GetVars(), LinearProgram.Constraints[i].Constant);
            }

            previousTableau.Constraints  = constraints;
            previousTableau.ObjectiveRow = new ObjectiveRow(constraints.Length, LinearProgram.ObjectiveFunction.GetVars());

            Variable[] emptyVars      = previousTableau.ObjectiveRow.Vars.Where(var => !var.IsSlack).Select(var => var.Empty).ToArray();
            Tableau    currentTableau = Tableau.EmptyTableau(previousTableau.Constraints.Length, emptyVars);

            while (MostNegitive(previousTableau, out Variable mostNegitive))
            {
                int    index         = 0;
                double leastPositive = previousTableau.Constraints.Length > 0 ? previousTableau.Constraints[index].RatioTest(mostNegitive) : -1;
                for (int i = 0; i < previousTableau.Constraints.Length; i++)
                {
                    double rt = previousTableau.Constraints[i].RatioTest(mostNegitive);
                    if (rt > 0 && rt < leastPositive)
                    {
                        leastPositive = rt; index = i;
                    }
                }

                if (leastPositive == 0)
                {
                    throw new Exception("Least Positive is Unchanged");
                }

                currentTableau.Constraints[index] = previousTableau.Constraints[index].PivotRow(mostNegitive.Index);

                currentTableau.ObjectiveRow = previousTableau.ObjectiveRow.Normalize(mostNegitive.Index, currentTableau.Constraints[index].Vars, currentTableau.Constraints[index].RHS);

                for (int i = 0; i < currentTableau.Constraints.Length; i++)
                {
                    if (i != index)
                    {
                        currentTableau.Constraints[i] = previousTableau.Constraints[i].Normalize(mostNegitive.Index, currentTableau.Constraints[index].Vars, currentTableau.Constraints[index].RHS);
                    }
                }

                previousTableau = currentTableau.Copy();
                currentTableau  = Tableau.EmptyTableau(previousTableau.Constraints.Length, emptyVars);
            }

            Console.WriteLine(previousTableau);
            return(new LPSolution(previousTableau));
        }
Пример #2
0
        private bool MostNegitive(Tableau tablar, out Variable mostNegitive)
        {
            mostNegitive = new Variable {
                Index = -1, Value = 0
            };
            foreach (Variable var in tablar.ObjectiveRow.Vars)
            {
                if (var.Value < mostNegitive.Value)
                {
                    mostNegitive = var;
                }
            }

            return(mostNegitive.Index != -1);
        }
Пример #3
0
 public LPSolution(Tableau finalTableau)
 {
     Results      = finalTableau.BasicVariableAnalysis();
     Maximum      = finalTableau.ObjectiveRow.RHS;
     FinalTableau = finalTableau;
 }