Ejemplo n.º 1
0
        /// <inheritdoc />
        protected override PointValuePair <T> DoOptimize()
        {
            // reset the tableau to indicate a non-feasible solution in case
            // we do not pass phase 1 successfully
            if (solutionCallback != null)
            {
                solutionCallback.Tableau = null;
            }

            var tableau =
                new SimplexTableau <T, TPolicy>(Function,
                                                Constraints,
                                                GoalType,
                                                IsRestrictedToNonNegative,
                                                epsilon,
                                                maxUlps);

            SolvePhase1(tableau);
            tableau.DropPhase1Objective();

            // after phase 1, we are sure to have a feasible solution
            if (solutionCallback != null)
            {
                solutionCallback.Tableau = tableau;
            }

            while (!tableau.IsOptimal())
            {
                DoIteration(tableau);
            }

            // check that the solution respects the nonNegative restriction in case
            // the epsilon/cutOff values are too large for the actual linear problem
            // (e.g. with very small constraint coefficients), the solver might actually
            // find a non-valid solution (with negative coefficients).
            PointValuePair <T> solution = tableau.GetSolution();

            if (IsRestrictedToNonNegative)
            {
                T[] coeff = solution.Point;
                for (int i = 0; i < coeff.Length; i++)
                {
                    if (Precision <T, TPolicy> .CompareTo(coeff[i], Policy.Zero(), epsilon) < 0)
                    {
                        throw new NoFeasibleSolutionException();
                    }
                }
            }
            return(solution);
        }