示例#1
0
        //TODO move this to another place, as ratio test has been taken out and can be used for simplex
        override public LinearProgram Solve()
        {
            int tableauNumber = 0;

            bool done        = false;
            bool answerFound = true;

            Console.WriteLine("\nPhase 1 - Table 1");
            LinearProgram.DisplayCurrentTable();

            //Loops till final table
            do
            {
                tableauNumber++;
                int pivotCol = 0;
                int pivotRow = 0;

                if (RatioTest(out pivotRow, out pivotCol))
                {
                    done = true;
                    break;
                }

                CalculateNewCellValues(pivotRow, pivotCol);

                //Displays the table
                Console.WriteLine("\nTable " + tableauNumber);
                LinearProgram.DisplayCurrentTable();

                done        = true;
                answerFound = true;

                if (CheckIfContinue())
                {
                    done        = false;
                    answerFound = false;
                }
            } while (!done);

            if (answerFound)
            {
                PrimalSimplex simplex = new PrimalSimplex(LinearProgram);

                LinearProgram = simplex.Solve();
            }

            //Checks if there is an answer
            //TODO Handle the case when there is no solution found, as currently it will display no solution but still return the lp
            //if (answerFound == true)
            //{

            //}
            //else
            //{
            //    Console.WriteLine("No Solution");
            //}

            return(LinearProgram);
        }
示例#2
0
        override public LinearProgram Solve()
        {
            int tableauNumber = 1;

            bool done        = false;
            bool answerFound = false;

            int pivotRow = 0;
            int pivotCol = 0;


            Console.WriteLine("\nPhase 1 - Table 1");
            LinearProgram.DisplayCurrentTable();

            //Loops till final table
            do
            {
                tableauNumber++;

                pivotRow = 0;

                if (RatioTest(out pivotRow, out pivotCol))
                {
                    done = true;
                    break;
                }

                CalculateNewCellValues(pivotRow, pivotCol);

                Console.WriteLine("\nPhase 1 - Table " + tableauNumber);
                LinearProgram.DisplayCurrentTable();

                done        = true;
                answerFound = true;

                if (CheckIfContinue())
                {
                    done        = false;
                    answerFound = false;
                }

                double wRHS = Math.Round(LinearProgram.LinearProgramMatrix[0, LinearProgram.ColumnCount - 1], 10);

                //Checks if the W rhs amount is 0
                if (wRHS == 0)
                {
                    done        = true;
                    answerFound = true;
                }
            } while (!done);

            //Checks if an answer is found
            if (answerFound)
            {
                //Finds BVs
                double[]   bvs    = LinearProgram.GetBasicVariables();
                List <int> bvCols = new List <int>();

                for (int i = 0; i < bvs.Length; i++)
                {
                    if (bvs[i] != 0)
                    {
                        bvCols.Add(i);
                    }
                }

                bool deleteNegatives = false;

                //If A is BV, delete Negatives
                for (int aCol = 0; aCol < LinearProgram.ColOfA.Count; aCol++)
                {
                    for (int bvCol = 0; bvCol < bvCols.Count; bvCol++)
                    {
                        if (LinearProgram.ColOfA[aCol] + 1 == bvCols[bvCol])
                        {
                            //Breaks out of outer loop
                            aCol = LinearProgram.ColOfA.Count;

                            deleteNegatives = true;
                            break;
                        }
                    }
                }

                //Deletes the A's
                if (Math.Round(LinearProgram.LinearProgramMatrix[0, LinearProgram.ColumnCount - 1], 10) == 0)
                {
                    if (deleteNegatives)
                    {
                        for (int i = 0; i < LinearProgram.ColumnCount; i++)
                        {
                            if (LinearProgram.LinearProgramMatrix[0, i] < 0)
                            {
                                for (int j = 0; j < LinearProgram.RowCount; j++)
                                {
                                    LinearProgram.LinearProgramMatrix[j, i] = 0;
                                }
                            }
                        }
                    }

                    for (int i = 0; i < simplexLP.GetLength(0); i++)
                    {
                        for (int j = 0; j < simplexLP.GetLength(1); j++)
                        {
                            simplexLP[i, j] = LinearProgram.LinearProgramMatrix[i + 1, j + 1];
                        }
                    }

                    //Sets the Amounts in the A coulmns to 0
                    foreach (var item in LinearProgram.ColOfA)
                    {
                        if (!bvCols.Contains(item + 1))
                        {
                            for (int i = 0; i < simplexLP.GetLength(0); i++)
                            {
                                simplexLP[i, item] = 0;
                            }
                        }
                    }

                    LinearProgram.IsTwoPhase = false;

                    Console.WriteLine("\nPhase 2 - Initial Table");

                    LinearProgram.LinearProgramMatrix = simplexLP;

                    LinearProgram.DisplayCurrentTable();

                    PrimalSimplex simplex = new PrimalSimplex(LinearProgram);

                    //Calls the appropriate simplex method
                    LinearProgram = simplex.Solve();

                    ///TODO handle what happens if theres no solution
                }
                else
                {
                    Console.WriteLine("No Solution");
                }
            }
            //else
            //{
            //    if (pivotRow == 0)
            //    {
            //        Console.WriteLine("This LP is unbounded and has no solution");
            //    }

            //    Console.WriteLine("No Solution");
            //}

            return(LinearProgram);
        }
示例#3
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);
            }
        }