public static Answer Solve(Model _model) { if (_model == null) { return(null); } model = _model; if (model.CanonicalForm == null) { model.CanonicalForm = CanonicalGenerator.GenerateCanonicalForm(model, Algorithm.PrimalSimplex); } double[][] table = model.CanonicalForm; //if (GetRHSValues(table).Where(x => x < 0).Count() > 0) //{ // return AnswerGenerator.GenerateInfeasibleAnswer(InfeasiblityReason.UnsolvableWithAlgorithm); //} Console.WriteLine(model.GenerateDisplayableCanonical()); FileHandler.Append(model.GenerateDisplayableCanonical(), "output.txt"); while (CheckIfOptimal(table) == false) { //Console.WriteLine("Running"); double[] zRow = table[0]; int pivotColumnIndex = IdentifyPivotColumn(zRow); //Console.WriteLine(pivotColumnIndex); double[] rhsValues = GetRHSValues(table); double[] columnValues = GetPivotColumnValues(table, pivotColumnIndex); int pivotRowIndex = IdentifyPivotRow(columnValues, rhsValues); //Console.WriteLine(pivotRowIndex); table = Pivoting.PivotTable(table, pivotColumnIndex, pivotRowIndex); Console.WriteLine(CommonFunctions.GenerateTableIteration(model.DecisionVariables, table)); } return(AnswerGenerator.GenerateAnswer(table, model.DecisionVariables)); }
public static Answer Solve(Model _model) { if (_model == null) { return(null); } model = _model; if (model.CanonicalForm == null) { model.CanonicalForm = CanonicalGenerator.GenerateCanonicalForm(model, Algorithm.DualSimplex); } double[][] table = model.CanonicalForm; Console.WriteLine(model.GenerateDisplayableCanonical()); FileHandler.Append(model.GenerateDisplayableCanonical(), "output.txt"); while (CheckIfDoneWithDualPhase(table) == false) { //Console.WriteLine("Running"); double[] rhsValues = GetRHSValues(table); int pivotRowIndex = IdentifyPivotRow(rhsValues); double[] zRow = table[0]; double[] pivotRowValues = GetPivotRowValues(table, pivotRowIndex); if (CheckIfInfeasible(pivotRowValues)) { return(AnswerGenerator.GenerateInfeasibleAnswer(InfeasiblityReason.GeneralUnsolvability)); } int pivotColumnIndex = IdentifyPivotColumn(zRow, pivotRowValues); //Console.WriteLine(pivotColumnIndex); //Console.WriteLine(pivotRowIndex); table = Pivoting.PivotTable(table, pivotColumnIndex, pivotRowIndex); Console.WriteLine(CommonFunctions.GenerateTableIteration(model.DecisionVariables, table)); } Model modelForSimplex = model; modelForSimplex.CanonicalForm = table; return(PrimalSimplex.Solve(modelForSimplex)); }