コード例 #1
0
        //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);
            }
        }
コード例 #2
0
        //TODO Check if anything from here can be added into the new menu
        public static void GetInputAndOutputFiles()
        {
            bool isValid = false;

            //Creates the directory if it doesnt exist
            if (!FileHandler.DirectoryExists)
            {
                //Creates the directory
                FileHandler.CreateDirectory();

                Console.WriteLine("A folder called 'Linear Program Solver' has been created in My Documents, please place your files here" +
                                  "\n" +
                                  "\nPress any key to continue");

                Console.ReadKey();
                Console.Clear();
            }

            do
            {
                //Displys the menu
                Console.WriteLine("Enter the location of your input and output files which need to be processed inside the 'Linear Program Solver' folder the is located in My Documents" +
                                  "\nEnter the command in the following format:" +
                                  "\n" +
                                  "\nSolve <input file> <output file> - Note, the files must be in the format .txt, and the file extension must NOT be added" +
                                  "\nE.g. Solve <input> <output>");

                string userCommand = Console.ReadLine();

                string[] splitCommand = userCommand.Split(' ');

                //Makes sure the user enters correct data
                if (splitCommand[0].ToLower() != "solve" || splitCommand.Length != 3)
                {
                    Console.WriteLine("The command you entered is incorrect!" +
                                      "\nEnter the command in the following format:" +
                                      "\nSolve <input file> <output file>");
                }
                else
                {
                    char[] notAllowed = { '<', '>', '/', '\\', ':', '*', '?', '"', '|' };

                    //Removes invalid characters from the file names
                    splitCommand[1] = splitCommand[1].Trim(notAllowed);
                    splitCommand[2] = splitCommand[2].Trim(notAllowed);

                    //Checks if the input file is found
                    bool fileFound = FileHandler.CheckInputFile(splitCommand[1]);

                    //Gives error if not found
                    if (!fileFound)
                    {
                        Console.WriteLine("The input file you listed cannot be found. Please ensure that:" +
                                          "\n\tThe name is spelt correctly" +
                                          "\n\tThe command is in the correct format" +
                                          "\n\tThat the file does NOT include the file extension" +
                                          "\n\tThat the file is a textfile(.txt)" +
                                          "\n\tThat the file is located in the 'Linear Program Solver' folder in My Documents");
                    }
                    else
                    {
                        //Creates the output file
                        FileHandler.SetOutputFile = splitCommand[2];

                        Console.WriteLine("Output file created, this is where you will find the solution saved");

                        isValid = true;
                    }
                }

                Console.WriteLine("\nPress any key to continue...");
                Console.ReadKey();

                Console.Clear();
            } while (!isValid);
        }
コード例 #3
0
        public void DisplaySolution()
        {
            double[] answers = GetBasicVariables();

            double zValue = answers[0];

            Console.WriteLine("Optimal Solutions\n-----------------\nZ = + " + zValue);

            int countAnswers = 1;

            //Displays the X's
            for (int i = 0; i < CountX; i++)
            {
                bool isY = false;

                foreach (var item in ColY)
                {
                    if (item + 1 == i)
                    {
                        isY = true;
                    }
                }

                if (isY == true)
                {
                    Console.WriteLine("X" + (i + 1) + " = " + answers[countAnswers] * -1);
                }
                else
                {
                    Console.WriteLine("X" + (i + 1) + " = " + answers[countAnswers]);
                }

                countAnswers++;
            }

            //Displaye the S's
            for (int i = 0; i < CountS; i++)
            {
                Console.WriteLine("S" + (i + 1) + " = " + answers[countAnswers]);
                countAnswers++;
            }

            //Displaye the E's
            for (int i = 0; i < CountE; i++)
            {
                Console.WriteLine("E" + (i + 1) + " = " + answers[countAnswers]);
                countAnswers++;
            }

            //Displaye the A's
            for (int i = 0; i < CountA; i++)
            {
                Console.WriteLine("A" + (i + 1) + " = " + answers[countAnswers]);
                countAnswers++;
            }

            Console.WriteLine();

            //Calls the method that saves the soution
            FileHandler.SaveSolution(zValue, answers);

            Console.WriteLine("The solution has been saved!");

            //Checks if it can draw a graph
            if (CountX == 2)
            {
                //Draws graph
                UserInterfaceHandler.Graph();
            }
            else
            {
                Console.WriteLine("This LP has more than two variables, cannot draw this graph");
            }

            Console.ReadKey();
        }