private Solution SolveTwoPhase(StandartSimplexModel simplexModel) { Solution tmp_solution = new Solution() { Quality = Enums.SolutionQuality.Infeasible }; simplexModel.CurrentPhase = 1; simplexModel.PrintMatrix(); //1) Solve the matrix for phase I /* * Steps * 1. Modify the constraints so that the RHS of each constraint is nonnegative (This requires that each constraint with a negative RHS be multiplied by -1. Remember that if you multiply an inequality by any negative number, the direction of the inequality is reversed!). After modification, identify each constraint as a ≤, ≥ or = constraint. * 2. Convert each inequality constraint to standard form (If constraint i is a ≤ constraint, we add a slack variable si; and if constraint i is a ≥ constraint, we subtract an excess variable ei). * 3. Add an artificial variable ai to the constraints identified as ≥ or = constraints at the end of Step 1. Also add the sign restriction ai ≥ 0. * 4. In the phase I, ignore the original LP’s objective function, instead solve an LP whose objective function is minimizing w = ai (sum of all the artificial variables). The act of solving the Phase I LP will force the artificial variables to be zero. 5. Since each artificial variable will be in the starting basis, all artificial variables must be eliminated from row 0 before beginning the simplex. Now solve the transformed problem by the simplex. */ VariableType tmp_inclusive = VariableType.Original | VariableType.Slack | VariableType.Excess; m_ColumnSelector = ColumnSelectorFactory.GetSelector(ObjectiveType.Minumum); tmp_solution = Solve(simplexModel.VarTypes, tmp_inclusive, simplexModel.ArtificialObjectiveMatrix, simplexModel.ConstarintMatrix, simplexModel.RightHandMatrix, simplexModel.BasicVariables, simplexModel.ObjectiveCost, true); //Solving the Phase I LP will result in one of the following three cases: //I.Case : If w = 0 //TODO test //tmp_solution.RightHandValues[tmp_solution.RightHandValues.GetLength(0) - 1, 0] = 0; if (tmp_solution.ResultValue <= m_epsilon) { simplexModel.CurrentPhase = 2; //transfer the phaseoneobjective function factors simplexModel.TruncatePhaseResult(tmp_solution); simplexModel.PrintMatrix(); //II.Case : If w = 0, and no artificial variables are in the optimal Phase I basis: // i.Drop all columns in the optimal Phase I tableau that correspond to the artificial variables.Drop Phase I row 0. // ii.Combine the original objective function with the constraints from the optimal Phase I tableau(Phase II LP).If original objective function coefficients of BVs are nonzero row operations are done. // iii.Solve Phase II LP using the simplex method.The optimal solution to the Phase II LP is the optimal solution to the original LP. //if ( ) //III.Case : If w = 0, and at least one artificial variable is in the optimal Phase I basis: // i.Drop all columns in the optimal Phase I tableau that correspond to the nonbasic artificial variables and any variable from the original problem that has a negative coefficient in row 0 of the optimal Phase I tableau. Drop Phase I row 0. // ii.Combine the original objective function with the constraints from the optimal Phase I tableau(Phase II LP).If original objective function coefficients of BVs are nonzero row operations are done. // iii.Solve Phase II LP using the simplex method.The optimal solution to the Phase II LP is the optimal solution to the original LP. //if ( ) tmp_solution = Solve(simplexModel.VarTypes, tmp_inclusive, simplexModel.ObjectiveMatrix, simplexModel.ConstarintMatrix, simplexModel.RightHandMatrix, simplexModel.BasicVariables, simplexModel.ObjectiveCost, simplexModel.GoalType == ObjectiveType.Minumum); System.Diagnostics.Debug.WriteLine("Solution " + tmp_solution.Quality.ToString()); } //II.Case : If w > 0 then the original LP has no feasible solution(stop here). else { tmp_solution.Quality = SolutionQuality.Infeasible; } //assign the actual value to the result terms PrepareSolutionResult(simplexModel.ConstarintMatrix, simplexModel.RightHandMatrix, simplexModel.ObjectiveFunction.Terms, tmp_solution); return(tmp_solution); }
private Solution SolveStandart(StandartSimplexModel simplexModel) { //simplexModel.ConvertStandardModel(); simplexModel.PrintMatrix(); //simplexModel.CreateMatrixSet(); VariableType tmp_inclusive = VariableType.Original | VariableType.Slack; Solution tmp_solution = Solve(simplexModel.VarTypes, tmp_inclusive, simplexModel.ObjectiveMatrix, simplexModel.ConstarintMatrix, simplexModel.RightHandMatrix, simplexModel.BasicVariables, simplexModel.ObjectiveCost, simplexModel.GoalType == ObjectiveType.Minumum); PrepareSolutionResult(simplexModel.ConstarintMatrix, simplexModel.RightHandMatrix, simplexModel.ObjectiveFunction.Terms, tmp_solution); return(tmp_solution); }
internal static void PhaseOnePrintMatrix(this StandartSimplexModel model) { model.PrintMatrix(); string tmp_sign = string.Empty; System.Diagnostics.Debug.WriteLine("Goal :" + model.GoalType.ToString()); System.Diagnostics.Debug.Write("New Objective Function - " + model.GoalType.ToString() + ": "); foreach (Term item in model.PhaseObjectiveFunction.Terms) { tmp_sign = string.Empty; if (Math.Sign(item.Factor) > -1) { tmp_sign = "+"; } System.Diagnostics.Debug.Write(tmp_sign + item.Factor + "*" + item.Vector + " "); } System.Diagnostics.Debug.Write(" = "); System.Diagnostics.Debug.WriteLine(model.PhaseObjectiveFunction.RightHandValue); System.Diagnostics.Debug.WriteLine("*********************************"); }