コード例 #1
0
        /// <summary>
        ///     Finds the minimum value of a function. The solution vector
        ///     will be made available at the <see cref="IOptimizationMethod{TInput, TOutput}.Solution" /> property.
        /// </summary>
        /// <returns>
        ///     Returns <c>true</c> if the method converged to a <see cref="IOptimizationMethod{TInput, TOutput}.Solution" />.
        ///     In this case, the found value will also be available at the
        ///     <see cref="IOptimizationMethod{TInput, TOutput}.Value" />
        ///     property.
        /// </returns>
        public override bool Minimize()
        {
            if (Gradient is null)
            {
                Gradient = FiniteDifferences.Gradient(Function, NumberOfVariables);
            }

            NonlinearObjectiveFunction.CheckGradient(Gradient, Solution);

            return(base.Minimize());
        }
コード例 #2
0
        /// <summary>
        ///     Finds the maximum value of a function. The solution vector
        ///     will be made available at the <see cref="IOptimizationMethod{TInput, TOutput}.Solution" /> property.
        /// </summary>
        /// <returns>
        ///     Returns <c>true</c> if the method converged to a <see cref="IOptimizationMethod{TInput, TOutput}.Solution" />.
        ///     In this case, the found value will also be available at the
        ///     <see cref="IOptimizationMethod{TInput, TOutput}.Value" />
        ///     property.
        /// </returns>
        public override bool Maximize()
        {
            if (Gradient is null)
            {
                Gradient = FiniteDifferences.Gradient(Function, NumberOfVariables);
            }

            NonlinearObjectiveFunction.CheckGradient(Gradient, Solution);

            var g = Gradient;

            Gradient = x => g(x).Multiply(-1);

            var success = base.Maximize();

            Gradient = g;

            return(success);
        }