ContinuousOptimizationMinimize Method (FuncDoubleMatrix, Double, DoubleMatrix) |
Namespace: Novacta.Analytics
public static DoubleMatrix Minimize( Func<DoubleMatrix, double> objectiveFunction, DoubleMatrix initialArgument )
Exception | Condition |
---|---|
ArgumentNullException | objectiveFunction is
null. -or- initialArgument is null. |
ArgumentException | initialArgument is not a row vector. |
It is assumed that the objectiveFunction will accept row vectors as valid representations of an argument. As a consequence, initialArgument is expected to be a row vector.
In the following example, function
is minimized in the sub-domain
.
To obtain such result, arguments outside
that interval are penalized accordingly.
The search for the minimizer starts from the initial argument .
using System; namespace Novacta.Analytics.CodeExamples { public class ContinuousOptimizationExample0 { public void Main() { // Define the objective function as follows: // f(x) = exp(-(x-2)^2) + (.8) * exp(-(x+2)^2)), // penalized for minimization if x is not in [-3, 3]. static double objectiveFunction(DoubleMatrix x) { double y = 0.0; var x_0 = x[0]; y += Math.Exp(-Math.Pow(x_0 - 2.0, 2.0)); y += .8 * Math.Exp(-Math.Pow(x_0 + 2.0, 2.0)); // Penalize arguments not in [-3, 3]. if (x_0 < -3.0 || 3.0 < x_0) { y += 100.0 * Math.Abs(x_0 - 3.0); } return y; } // Define the argument at which the method starts // the search for optimality. var initialArgument = DoubleMatrix.Dense(1, 1, -3.0); // Minimize the objective function. var minimizer = ContinuousOptimization.Minimize( objectiveFunction: objectiveFunction, initialArgument: initialArgument); // Show the results. Console.WriteLine(); Console.WriteLine("The minimizer of the objective function is:"); Console.WriteLine(minimizer); Console.WriteLine(); Console.WriteLine("The minimum value of the objective function is:"); Console.WriteLine(objectiveFunction(minimizer)); } } } // Executing method Main() produces the following output: // // // The minimizer of the objective function is: // -0.0318684014 // // // // The minimum value of the objective function is: // 0.032734891180979234