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); }
//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"); } }