private void AddBinaryConstraint() { foreach (var col in colBinary) { double[] newRow = new double[linearProgram.ColumnCount + 1]; newRow[col] = 1; newRow[linearProgram.ColumnCount - 1] = 1; newRow[linearProgram.ColumnCount] = 1; linearProgram.LinearProgramMatrix = LpTools.AddRow(newRow, linearProgram); linearProgram.CountS++; } }
public LinearProgram Solve() { bool solved = false; ProblemNode currentOptimal = problems.ElementAt(0); //loop runs until all problems are solved, exit via break while (true) { ProblemNode currentProblem = (ProblemNode)problems.ElementAt(0); //this loop checks to see if all problems are solved for (int i = 0; i < problems.Count; i++) { if (!problems.ElementAt(i).Solved) { currentProblem = problems.ElementAt(i); break; } if (problems.Count == i + 1) { solved = true; } } if (solved) { break; } if (LpTools.IsSpecialCase(currentProblem.Problem)) { currentProblem.Solved = true; continue; } Dual dual = new Dual(currentProblem.Problem); currentProblem.Problem = dual.Solve(); currentProblem.ZValue = currentProblem.Problem.LinearProgramMatrix[0, currentProblem.Problem.ColumnCount - 1]; currentProblem.Solved = true; //double[] xValues = new double[currentProblem.Problem.CountX]; double[] xValues = LpTools.findXValues(currentProblem.Problem); //for (int i = 0; i < xValues.Length; i++) // xValues[i] = Math.Round(currentProblem.Problem.LinearProgramMatrix[i+1, currentProblem.Problem.ColumnCount-1],5); currentProblem.XValues = xValues; Console.WriteLine("\n\nAdding Constraints:"); Console.WriteLine("==================================================================="); for (int i = 0; i < xValues.Length; i++) { if ((xValues[i] % 1) != 0) { problems.Add(new ProblemNode( LpTools.AddBasicConstraint(currentProblem.Problem, i + 1, LpTools.LESS_THAN, (int)xValues[i]), false, null, 0)); Console.WriteLine("\n"); problems.Add(new ProblemNode( LpTools.AddBasicConstraint(currentProblem.Problem, i + 1, LpTools.GREATER_THAN, (int)xValues[i] + 1), false, null, 0)); } } Console.WriteLine("==================================================================="); //todo: check for infinte loop when initial problem cannot be solved if ((type == LPType.Max && currentProblem.ZValue > currentOptimal.ZValue) || (type == LPType.Min && currentOptimal.ZValue > currentProblem.ZValue) || currentOptimal.XValues.Any((x) => x % 1 != 0)) { currentOptimal = currentProblem; } } return(currentOptimal.Problem); }
//New Main Menu with file,Alg& sensitivity ananlysis selection public static void Menu() { GetInputAndOutputFiles(); //TODO Move this to different place #region Stuff to Move List <String> unformatedLP = FileHandler.ReadLP(); foreach (var item in unformatedLP) { Console.WriteLine(item); } #endregion bool done = false; do { try { Console.WriteLine(@" IP SOLVER ________________________________________________________ PLEASE SELECT AN ALGORITHM 1.PRIMAL 2.TWO PHASE 3.DUAL 4.BRANCH & BOUND 5.CUTTING PLANE "); int userinput = int.Parse(Console.ReadLine()); Algorithm menu = (Algorithm)userinput; switch (menu) { case Algorithm.Primal: linearProgram = new LpFormatter(unformatedLP, Algorithm.Primal).GetLinearProgram(); linearProgram.DisplayCanonicalForm(); PrimalSimplex simplex = new PrimalSimplex(linearProgram); linearProgram = simplex.Solve(); break; case Algorithm.TwoPhase: linearProgram = new LpFormatter(unformatedLP, Algorithm.TwoPhase).GetLinearProgram(); linearProgram.IsTwoPhase = true; TwoPhase twoPhase = new TwoPhase(linearProgram); linearProgram.DisplayCanonicalForm(); linearProgram = twoPhase.Solve(); break; case Algorithm.Dual: linearProgram = new LpFormatter(unformatedLP, Algorithm.Dual).GetLinearProgram(); linearProgram.DisplayCanonicalForm(); Dual dual = new Dual(linearProgram); linearProgram = dual.Solve(); break; case Algorithm.BranchAndBound: linearProgram = new LpFormatter(unformatedLP, Algorithm.Dual).GetLinearProgram(); linearProgram.DisplayCanonicalForm(); Dual bbDual = new Dual(linearProgram); linearProgram = bbDual.Solve(); BranchAndBound BB = new BranchAndBound(linearProgram); linearProgram = BB.Solve(); break; case Algorithm.CuttingPlane: linearProgram = new LpFormatter(unformatedLP, Algorithm.Dual).GetLinearProgram(); linearProgram.DisplayCanonicalForm(); Dual cutDual = new Dual(linearProgram); linearProgram = cutDual.Solve(); CuttingPlane cutingPlane = new CuttingPlane(linearProgram); linearProgram = cutingPlane.Solve(); break; default: break; } //todo check for input errors, set done to false if there arent any done = true; } catch (FormatException) { done = false; Console.WriteLine("Invalid Input"); } } while (!done); if (LpTools.CheckIfIPIsSolved(linearProgram)) { linearProgram.DisplaySolution(); } else { Console.WriteLine("No Solution!"); Console.ReadKey(); } Console.Clear(); if (LpTools.CheckIfIPIsSolved(linearProgram)) { do { SensitivityAnalysisMenu(); } while (true); } }
//Loop this? public static void SensitivityAnalysisMenu() { try { //Sensitivity Analysis Menu Console.WriteLine(@" IP SOLVER ________________________________________________________________________________________ SENSITIVITY ANALYSIS 1. Display the range of a selected Non-Basic Variable. 2. Apply and display a change of a selected Non-Basic Variable. 3. Display the range of a selected Basic Variable. 4. Apply and display a change of a selected Basic Variable. 5. Display the range of a selected constraint right-hand-side value. 6. Apply and display a change of a selected constraint right-hand-side value. 7. Display the range of a selected variable in a Non-Basic Variable column. 8. Apply and display a change of a selected variable in a Non-Basic Variable column. 9. Add a new activity to an optimal solution. 10. Add a new constraint to an optimal solution. 11. Display the shadow prices. 12. Duality "); Console.WriteLine("\nSolved LP\n"); linearProgram.DisplayCurrentTable(); Console.WriteLine(); int userInputSensitivityAnalysis = int.Parse(Console.ReadLine()); SensitivityMenu smenu = (SensitivityMenu)userInputSensitivityAnalysis; switch (smenu) { case SensitivityMenu.display1: //TODO Display the range of a selected Non-Basic Variable. //SensivitityAnalysis.GetNonBasicVariables(linearProgram); Console.WriteLine("Enter the column Number: (Z Column = Column 0)"); int rowNumber = int.Parse(Console.ReadLine()); Console.WriteLine("Ranges for Non Basic Variables"); SensivitityAnalysis.GetNBVRange(SensivitityAnalysis.GetFormatedSensistivityMatrix(linearProgram.LinearProgramMatrix), rowNumber); Console.ReadKey(); break; case SensitivityMenu.display2: //TODO Display the range of a selected Non-Basic Variable. Console.WriteLine("Enter the column Number: (Z Column = Column 0)"); int columnNumber = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the row Number: (Z Column = Column 0)"); rowNumber = int.Parse(Console.ReadLine()); if (linearProgram.GetBasicVariables()[columnNumber] != 0) { Console.WriteLine("Not NBV"); } else { Console.WriteLine("ENter NEw Value:"); int valuenew = int.Parse(Console.ReadLine()); linearProgram.LinearProgramMatrix[rowNumber, columnNumber] = valuenew; linearProgram.DisplayCurrentTable(); if (LpTools.CheckIfIPIsSolved(linearProgram)) { Console.WriteLine("No Change"); Console.ReadKey(); } else { LinearProgram linearProgramNew = (LinearProgram)linearProgram.Clone(); Dual dual2 = new Dual(linearProgramNew); dual2.Solve(); if (LpTools.CheckIfIPIsSolved(linearProgramNew)) { linearProgramNew.DisplaySolution(); } else { Console.WriteLine("No solution"); Console.ReadKey(); } } } break; case SensitivityMenu.display3: //TODO Display the range of a selected Basic Variable. Console.WriteLine("Enter the column Number: (Z Column = Column 0)"); rowNumber = int.Parse(Console.ReadLine()); Console.WriteLine("Ranges for Basic variables"); SensivitityAnalysis.GetRangesForSelectedBV(SensivitityAnalysis.GetFormatedSensistivityMatrix(linearProgram.LinearProgramMatrix), rowNumber); Console.ReadKey(); break; case SensitivityMenu.display4: //TODO Apply and display a change of a selected Basic Variable. break; case SensitivityMenu.display5: //TODO Display the range of a selected constraint right-hand-side value. //Console.WriteLine("Enter the row Number: (Z Row = Row 0)"); //rowNumber = int.Parse(Console.ReadLine()); Console.WriteLine("Ranges for RHS variables"); SensivitityAnalysis.GetRangesForRHS(SensivitityAnalysis.GetFormatedSensistivityMatrix(linearProgram.LinearProgramMatrix), linearProgram); Console.ReadKey(); break; case SensitivityMenu.display6: //TODO Apply and display a change of a selected constraint right-hand-side value. break; case SensitivityMenu.display7: //TODO Display the range of a selected variable in a Non-Basic Variable column. break; case SensitivityMenu.display8: //TODO Apply and display a change of a selected variable in a Non-Basic Variable column. Console.WriteLine("Under Construction"); Console.ReadKey(); break; case SensitivityMenu.display9: //TODO Add a new activity to an optimal solution. break; case SensitivityMenu.display10: //TODO Add a new constraint to an optimal solution. Console.WriteLine("Enter the X Number: (Z Row = Row 0)"); rowNumber = int.Parse(Console.ReadLine()); Console.WriteLine("Type:\n1. <= \n2 . >="); int sign = int.Parse(Console.ReadLine()); Console.WriteLine("RHS:"); int rhs = int.Parse(Console.ReadLine()); linearProgram = LpTools.AddBasicConstraint(linearProgram, rowNumber, sign - 1, rhs); Console.WriteLine("New Table"); linearProgram.DisplayCurrentTable(); LinearProgram newLP = (LinearProgram)linearProgram.Clone(); Dual dual = new Dual(newLP); dual.Solve(); if (LpTools.CheckIfIPIsSolved(newLP)) { linearProgram.DisplaySolution(); } else { Console.WriteLine("No solution"); Console.ReadKey(); } break; case SensitivityMenu.display11: //TODO Display the shadow prices. Console.WriteLine("shadow prices."); SensivitityAnalysis.GetShadowPrices(SensivitityAnalysis.GetFormatedSensistivityMatrix(linearProgram.LinearProgramMatrix), linearProgram); Console.ReadKey(); break; case SensitivityMenu.display12: //TODO Duality Duality duality = new Duality(linearProgram); duality.DeterminDuality(); Console.ReadKey(); break; default: break; } } catch (FormatException) { Console.WriteLine("Invalid Input"); } }
public LinearProgram Solve() { int targetRow = 0; double fractionDifferance = 1; for (int c = 1; c < linearProgram.CountX + 1; c++) { for (int r = 1; r < linearProgram.RowCount; r++) { double currentCell = linearProgram.LinearProgramMatrix[r, c]; double currentValue = linearProgram.LinearProgramMatrix[r, linearProgram.ColumnCount - 1]; if (currentCell != 0 && currentCell != 1) { break; } if (currentCell == 1) { double thisFraction = Math.Abs(currentValue) - (int)Math.Abs(currentValue); double tempDiff = Math.Abs(0.5 - thisFraction); if (tempDiff < fractionDifferance) { targetRow = r; fractionDifferance = tempDiff; } } } } if (fractionDifferance == 1) { return(linearProgram); } double[] fractionArray = new double[linearProgram.ColumnCount + 1]; for (int i = 0; i < linearProgram.ColumnCount; i++) { fractionArray[i] = linearProgram.LinearProgramMatrix[targetRow, i]; if (fractionArray[i] % 1 == 0) { fractionArray[i] = 0; } else if (fractionArray[i] > 0) { fractionArray[i] -= (int)fractionArray[i]; } else { fractionArray[i] += (((int)fractionArray[i] * -1) + 1); } fractionArray[i] = Math.Round(fractionArray[i], 9); fractionArray[i] *= -1; } fractionArray[fractionArray.Length - 1] = fractionArray[fractionArray.Length - 2]; fractionArray[fractionArray.Length - 2] = 1; linearProgram.LinearProgramMatrix = LpTools.AddRow(fractionArray, linearProgram); linearProgram.CountS++; Dual dual = new Dual(linearProgram); LinearProgram solvedLp = dual.Solve(); while (!LpTools.CheckIfIPIsSolved(solvedLp, true)) { if (LpTools.IsSpecialCase(solvedLp)) { return(solvedLp); } solvedLp = new CuttingPlane(solvedLp).Solve(); } return(solvedLp); }