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