/// <summary>
 ///   Creates a new instance of the L-BFGS optimization algorithm.
 /// </summary>
 ///
 /// <param name="function">The function to be optimized.</param>
 ///
 public BroydenFletcherGoldfarbShanno(NonlinearObjectiveFunction function)
     : this(function.NumberOfVariables, function.Function, function.Gradient)
 {
 }
示例#2
0
 /// <summary>
 ///   Creates a new <see cref="Subplex"/> optimization algorithm.
 /// </summary>
 ///
 /// <param name="function">The objective function whose optimum values should be found.</param>
 ///
 public Subplex(NonlinearObjectiveFunction function)
     : base(function)
 {
     init(function.NumberOfVariables);
 }
 /// <summary>
 ///   Creates a new instance of the L-BFGS optimization algorithm.
 /// </summary>
 ///
 /// <param name="function">The function to be optimized.</param>
 ///
 public ResilientBackpropagation(NonlinearObjectiveFunction function)
     : this(function.NumberOfVariables, function.Function, function.Gradient)
 {
 }
示例#4
0
 /// <summary>
 ///   Creates a new instance of the Augmented Lagrangian algorithm.
 /// </summary>
 ///
 /// <param name="function">The objective function to be optimized.</param>
 /// <param name="constraints">
 ///   The <see cref="NonlinearConstraint"/>s to which the solution must be subjected.</param>
 ///
 public AugmentedLagrangian(NonlinearObjectiveFunction function, IEnumerable <NonlinearConstraint> constraints)
     : base(function.NumberOfVariables)
 {
     init(function, constraints, null);
 }
示例#5
0
 /// <summary>
 ///   Creates a new <see cref="NelderMead"/> non-linear optimization algorithm.
 /// </summary>
 ///
 /// <param name="function">The objective function whose optimum values should be found.</param>
 ///
 public NelderMead(NonlinearObjectiveFunction function)
     : base(function)
 {
     init(function.NumberOfVariables);
 }
示例#6
0
        /// <summary>
        ///   Implements the actual optimization algorithm. This
        ///   method should try to minimize the objective function.
        /// </summary>
        protected override bool Optimize()
        {
            if (Function == null)
            {
                throw new InvalidOperationException("function");
            }

            if (Gradient == null)
            {
                throw new InvalidOperationException("gradient");
            }

            NonlinearObjectiveFunction.CheckGradient(Gradient, Solution);


            int n = NumberOfVariables;
            int m = corrections;

            String task  = "";
            String csave = "";

            bool[] lsave  = new bool[4];
            int    iprint = 101;

            int[]  nbd   = new int[n];
            int[]  iwa   = new int[3 * n];
            int[]  isave = new int[44];
            double f     = 0.0d;

            double[] x     = new double[n];
            double[] l     = new double[n];
            double[] u     = new double[n];
            double[] g     = new double[n];
            double[] dsave = new double[29];

            int totalSize = 2 * m * n + 11 * m * m + 5 * n + 8 * m;

            if (work == null || work.Length < totalSize)
            {
                work = new double[totalSize];
            }

            int i = 0;

            {
                for (i = 0; i < UpperBounds.Length; i++)
                {
                    bool hasUpper = !Double.IsInfinity(UpperBounds[i]);
                    bool hasLower = !Double.IsInfinity(LowerBounds[i]);

                    if (hasUpper && hasLower)
                    {
                        nbd[i] = 2;
                    }
                    else if (hasUpper)
                    {
                        nbd[i] = 3;
                    }
                    else if (hasLower)
                    {
                        nbd[i] = 1;
                    }
                    else
                    {
                        nbd[i] = 0;  // unbounded
                    }
                    if (hasLower)
                    {
                        l[i] = LowerBounds[i];
                    }
                    if (hasUpper)
                    {
                        u[i] = UpperBounds[i];
                    }
                }
            }


            // We now define the starting point.
            {
                for (i = 0; i < n; i++)
                {
                    x[i] = Solution[i];
                }
            }

            double newF = 0;

            double[] newG = null;

            // We start the iteration by initializing task.
            task = "START";

            iterations = 0;

            //
            // c        ------- the beginning of the loop ----------
            //
L111:

            iterations++;

            //
            // c     This is the call to the L-BFGS-B code.
            //
            setulb(n, m, x, 0, l, 0, u, 0, nbd, 0, ref f, g, 0,
                   factr, pgtol, work, 0, iwa, 0, ref task, iprint, ref csave,
                   lsave, 0, isave, 0, dsave, 0);


            //
            if ((task.StartsWith("FG", StringComparison.OrdinalIgnoreCase)))
            {
                newF = Function(x);
                newG = Gradient(x);
                evaluations++;

                f = newF;

                for (int j = 0; j < newG.Length; j++)
                {
                    g[j] = newG[j];
                }
            }

            // c
            else if ((task.StartsWith("NEW_X", StringComparison.OrdinalIgnoreCase)))
            {
            }
            else
            {
                if (task == "ABNORMAL_TERMINATION_IN_LNSRCH")
                {
                    Status = BoundedBroydenFletcherGoldfarbShannoStatus.LineSearchFailed;
                }
                else if (task == "CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH")
                {
                    Status = BoundedBroydenFletcherGoldfarbShannoStatus.FunctionConvergence;
                }
                else if (task == "CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL")
                {
                    Status = BoundedBroydenFletcherGoldfarbShannoStatus.GradientConvergence;
                }
                else
                {
                    throw OperationException(task, task);
                }

                for (int j = 0; j < Solution.Length; j++)
                {
                    Solution[j] = x[j];
                }

                newF = Function(x);
                newG = Gradient(x);
                evaluations++;

                if (Progress != null)
                {
                    Progress(this, new OptimizationProgressEventArgs(iterations, 0, newG, 0, null, 0, newF, 0, true)
                    {
                        Tag = new BoundedBroydenFletcherGoldfarbShannoInnerStatus(
                            isave, dsave, lsave, csave, work)
                    });
                }

                return(true);
            }


            if (Progress != null)
            {
                Progress(this, new OptimizationProgressEventArgs(iterations, 0, newG, 0, null, 0, f, 0, false)
                {
                    Tag = new BoundedBroydenFletcherGoldfarbShannoInnerStatus(
                        isave, dsave, lsave, csave, work)
                });
            }

            goto L111;
        }