예제 #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
        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);
        }