Click or drag to resize

ContinuousOptimizationContext Class

Represents a Cross-Entropy context in which a continuous function is minimized or maximized.
Inheritance Hierarchy

Namespace:  Novacta.Analytics.Advanced
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public sealed class ContinuousOptimizationContext : SystemPerformanceOptimizationContext

The ContinuousOptimizationContext type exposes the following members.

Constructors
  NameDescription
Public methodContinuousOptimizationContext
Initializes a new instance of the ContinuousOptimizationContext class aimed to optimize the specified continuous function, with the given initial guess argument, optimization goal, range of iterations, and smoothing coefficients.
Top
Properties
  NameDescription
Protected propertyEliteSampleDefinition
Gets the elite sample definition for this context.
(Inherited from SystemPerformanceOptimizationContext.)
Public propertyInitialArgument
The initial argument of the function which the search for the optimal one starts with.
Public propertyInitialParameter
Gets the parameter initially exploited to sample from the state-space of the system defined by this context.
(Inherited from CrossEntropyContext.)
Public propertyInitialStandardDeviation
Gets the value assigned to each standard deviation in the initial Cross-Entropy parameter exploited by this context.
Public propertyMaximumNumberOfIterations
Gets the maximum number of iterations allowed by this context.
(Inherited from SystemPerformanceOptimizationContext.)
Public propertyMeanSmoothingCoefficient
Gets the coefficient that defines the smoothing scheme for the means of the Cross-Entropy parameters exploited by this context.
Public propertyMinimumNumberOfIterations
Gets the minimum number of iterations required by this context.
(Inherited from SystemPerformanceOptimizationContext.)
Public propertyOptimizationGoal
Gets a constant specifying if the performance function in this context must be minimized or maximized.
(Inherited from SystemPerformanceOptimizationContext.)
Public propertyStandardDeviationSmoothingCoefficient
Gets the coefficient that defines the base smoothing scheme for the standard deviations of the Cross-Entropy parameters exploited by this context.
Public propertyStandardDeviationSmoothingExponent
Gets the exponent that defines the dynamic smoothing scheme for the standard deviations of the Cross-Entropy parameters exploited by this context.
Public propertyStateDimension
Gets or sets the dimension of a vector representing a system's state when a CrossEntropyProgram executes in this context.
(Inherited from CrossEntropyContext.)
Public propertyTerminationTolerance
Gets the minimal value which, if greater than all the standard deviations of a Cross-Entropy parameter, signals that the optimization must be considered as converged at intermediate iterations.
Public propertyTraceExecution
Gets or sets a value indicating whether the execution of this context must be traced.
(Inherited from CrossEntropyContext.)
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Protected methodGetOptimalState
Gets the argument that optimizes the objective function in this context, according to the specified Cross-Entropy sampling parameter.
(Overrides SystemPerformanceOptimizationContextGetOptimalState(DoubleMatrix).)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodCode exampleOnExecutedIteration
Called after completion of each iteration of a CrossEntropyProgram executing in this context.
(Inherited from SystemPerformanceOptimizationContext.)
Protected methodPartialSample
Draws the specified subset of a sample from a distribution characterized by the given parameter, using the stated random number generator. Used when executing the sampling step of a CrossEntropyProgram running in this context.
(Overrides CrossEntropyContextPartialSample(Double, TupleInt32, Int32, RandomNumberGenerator, DoubleMatrix, Int32).)
Protected methodPerformance
Computes the objective function at a specified argument as the performance defined in this context.
(Overrides CrossEntropyContextPerformance(DoubleMatrix).)
Protected methodSmoothParameter
Provides the smoothing of the updated sampling parameter of a SystemPerformanceOptimizer executing in this context.
(Overrides SystemPerformanceOptimizationContextSmoothParameter(LinkedListDoubleMatrix).)
Protected methodStopAtIntermediateIteration
Specifies conditions under which a SystemPerformanceOptimizer executing in this context should be considered as terminated after completing an intermediate iteration.
(Overrides SystemPerformanceOptimizationContextStopAtIntermediateIteration(Int32, LinkedListDouble, LinkedListDoubleMatrix).)
Protected methodStopExecution
Specifies conditions under which a CrossEntropyProgram executing in this context should be considered as terminated.
(Inherited from SystemPerformanceOptimizationContext.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Protected methodUpdateLevel
Updates the performance level for the current iteration of a CrossEntropyProgram executing in this context and determines the corresponding elite sample.
(Inherited from SystemPerformanceOptimizationContext.)
Protected methodUpdateParameter
Updates the sampling parameter attending the generation of the sample in the next iteration of a CrossEntropyProgram executing in this context.
(Overrides CrossEntropyContextUpdateParameter(LinkedListDoubleMatrix, DoubleMatrix).)
Top
Remarks

Class ContinuousOptimizationContext derives from SystemPerformanceOptimizationContext, and defines a Cross-Entropy context able to solve continuous optimization problems.

Such a context is exploited by the methods exposed by the static class ContinuousOptimization, where the parameters of the algorithm are set to default values. For advanced scenarios, a context with specialized values can be instantiated and hence passed as a parameter to method Optimize(SystemPerformanceOptimizationContext, Double, Int32).

Class SystemPerformanceOptimizationContext thoroughly defines a system whose performance must be optimized. Class ContinuousOptimizationContext specializes that system by assuming the its performance is a continuous function, say LaTeX equation.

A Cross-Entropy optimizer is designed to identify the optimal arguments at which the performance function of a complex system reaches its minimum or maximum value. To get the optimal state, the system's state-space LaTeX equation, i.e. the domain of LaTeX equation, is traversed iteratively by sampling, at each iteration, from a specific density function, member of a parametric family

LaTeX equation

where LaTeX equation is a vector representing a possible argument of LaTeX equation, and LaTeX equation is the set of allowable values for parameter LaTeX equation. The parameter exploited at a given iteration LaTeX equation is referred to as the reference parameter of such iteration and indicated as LaTeX equation. A minimum number of iterations, say LaTeX equation, must be executed, while a number of them up to a maximum, say LaTeX equation, is allowed.

Implementing a context for continuous optimization

The Cross-Entropy method provides an iterative multi step procedure. In the context of continuous optimization, at each iteration LaTeX equation a sampling step is executed in order to generate diverse candidate arguments of the objective function, sampled from a distribution characterized by the reference parameter of the iteration, say LaTeX equation. Such sample is thus exploited in the updating step in which a new reference parameter LaTeX equation is identified to modify the distribution from which the samples will be obtained in the next iteration: such modification is executed in order to improve the probability of sampling relevant arguments, i.e. those arguments corresponding to the function values of interest (See the documentation of class CrossEntropyProgram for a thorough discussion of the Cross-Entropy method).

When the Cross-Entropy method is applied in an optimization context, a final optimizing step is executed, in which the argument corresponding to the searched extremum is effectively identified.

These steps must be implemented as follows.

Sampling step

In a ContinuousOptimizationContext, the parametric family LaTeX equation is defined as follows. Each component LaTeX equation of an argument LaTeX equation of LaTeX equation is attached to a Gaussian distribution, say LaTeX equation, and the j-th entry of an argument is sampled from LaTeX equation, independently from the other, where LaTeX equation is the dimension of LaTeX equation. A Cross-Entropy sampling parameter can thus be represented as a LaTeX equation matrix LaTeX equation, whose first row stores the means of the Gaussian distributions, while the second row contains their standard deviations, so that LaTeX equation and LaTeX equation.

The parametric space LaTeX equation should include a parameter under which all possible states must have a real chance of being selected: this parameter must be specified as the initial reference parameter LaTeX equation. A ContinuousOptimizationContext defines LaTeX equation as follows. The first row of LaTeX equation is set to an initial guess argument of LaTeX equation, say LaTeX equation, so that

LaTeX equation

The entries in the second row of LaTeX equation are instead all set constantly equal to an initial, constant standard deviation, say LaTeX equation:

LaTeX equation

Updating step

At iteration LaTeX equation, let us represent the sample drawn as LaTeX equation, where LaTeX equation is the Cross-Entropy sample size, and the LaTeX equation-th sample point is the sequence LaTeX equation. The parameter's updating formula is,

LaTeX equation

where LaTeX equation is the elite sample in this context, i.e. the set of sample points having the lowest performances observed during the LaTeX equation-th iteration, if minimizing, the highest ones, otherwise, while LaTeX equation is its indicator function. The parameter's second row, that containing the standard deviations, is instead updated as follows:

LaTeX equation

Applying a smoothing scheme to updated parameters

In a ContinuousOptimizationContext, the sampling parameter is smoothed applying the following formulas (See Rubinstein and Kroese, Remark 5.2, p. 189[1] ):

LaTeX equation

where LaTeX equation, and

LaTeX equation

with LaTeX equation and LaTeX equation a positive integer.

Optimizing step

The optimizing step is executed after that the underlying Cross-Entropy program has converged. In a specified context, it is expected that, given a reference parameter LaTeX equation, a corresponding reasonable value could be guessed for the optimizing argument of LaTeX equation, say LaTeX equation, with LaTeX equation a function from LaTeX equation to LaTeX equation. Function LaTeX equation is defined by overriding method GetOptimalState(DoubleMatrix) that should return LaTeX equation given a specific reference parameter LaTeX equation.

Given the optimal parameter (the parameter corresponding to the last iteration LaTeX equation executed by the algorithm before stopping),

LaTeX equation

the argument at which the searched extremum is considered as reached according to the Cross-Entropy method will be returned as

LaTeX equation

Stopping criterion

A ContinuousOptimizationContext never stops before executing a number of iterations less than MinimumNumberOfIterations, and always stops if such number is greater than or equal to MaximumNumberOfIterations.

For intermediate iterations, method StopAtIntermediateIteration(Int32, LinkedListDouble, LinkedListDoubleMatrix) is called to check if a Cross-Entropy program executing in this context should stop or not.

In a ContinuousOptimizationContext, the method controls if, in the currently updated reference parameter, say LaTeX equation, all the standard deviations are less than a specified termination tolerance, say LaTeX equation, testing that

LaTeX equation

and, if so, return true; otherwise returns false.

Instantiating a context for continuous optimization

At instantiation, the constructor of a ContinuousOptimizationContext object will receive information about the optimization under study by means of parameters representing the objective function LaTeX equation, LaTeX equation, LaTeX equation, LaTeX equation, LaTeX equation, and a constant stating if the optimization goal is a maximization or a minimization. In addition, the smoothing parameters LaTeX equation, LaTeX equation, and LaTeX equation are also passed to the constructor.

After construction, property InitialArgument represents LaTeX equation, while LaTeX equation and LaTeX equation can be inspected, respectively, via properties MinimumNumberOfIterations and MaximumNumberOfIterations. The smoothing coefficients LaTeX equation, LaTeX equation, and the exponent LaTeX equation are also available via properties MeanSmoothingCoefficient, StandardDeviationSmoothingCoefficient and StandardDeviationSmoothingExponent, respectively. In addition, property OptimizationGoal signals that the performance function must be maximized if it evaluates to the constant Maximization, or that a minimization is requested if it evaluates to the constant Minimization.

To evaluate the objective function LaTeX equation at a specific argument, one can call method Performance(DoubleMatrix) passing the argument as a parameter. It is expected that the objective function will accept a row vector as a valid representation of an argument.

Examples

In the following example, function

LaTeX equation

is maximized.

The search for the maximizer starts from the initial argument LaTeX equation.

Maximizing a function on the real line.
using Novacta.Analytics.Advanced;
using System;

namespace Novacta.Analytics.CodeExamples.Advanced
{
    public class ContinuousOptimizationContextExample0  
    {
        public void Main()
        {
            // Define the objective function as follows: 
            // f(x) = exp(-(x-2)^2) + (.8) * exp(-(x+2)^2)).
            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));
                return y;
            }

            // Define the argument at which the method starts 
            // the search for optimality.
            var initialArgument = DoubleMatrix.Dense(1, 1, -6.0);

            // Create the context.
            var context = new ContinuousOptimizationContext(
                objectiveFunction: objectiveFunction,
                initialArgument: initialArgument,
                meanSmoothingCoefficient: .8,
                standardDeviationSmoothingCoefficient: .7,
                standardDeviationSmoothingExponent: 6,
                initialStandardDeviation: 100.0,
                optimizationGoal: OptimizationGoal.Maximization,
                terminationTolerance: 1.0e-3,
                minimumNumberOfIterations: 3,
                maximumNumberOfIterations: 1000);

            // Create the optimizer.
            var optimizer = new SystemPerformanceOptimizer()
            {
                PerformanceEvaluationParallelOptions = { MaxDegreeOfParallelism = -1 },
                SampleGenerationParallelOptions = { MaxDegreeOfParallelism = -1 }
            };

            // Set optimization parameters.
            double rarity = 0.01;
            int sampleSize = 1000;

            // Solve the problem.
            var results = optimizer.Optimize(
                context,
                rarity,
                sampleSize);

            var maximizer = results.OptimalState;

            // Show the results.
            Console.WriteLine(
                "The Cross-Entropy optimizer has converged: {0}.",
                results.HasConverged);

            Console.WriteLine();
            Console.WriteLine("Initial guess parameter:");
            Console.WriteLine(context.InitialParameter);

            Console.WriteLine();
            Console.WriteLine("The maximizer of the objective function is:");
            Console.WriteLine(maximizer);

            Console.WriteLine();

            Console.WriteLine("The maximum value of the objective function is:");
            Console.WriteLine(objectiveFunction(maximizer));
        }
    }
}

// Executing method Main() produces the following output:
// 
// The Cross-Entropy optimizer has converged: True.
// 
// Initial guess parameter:
// -6               
// 100              
// 
// 
// 
// The maximizer of the objective function is:
// 2.00000021       
// 
// 
// 
// The maximum value of the objective function is:
// 1.0000000900279427

Bibliography
[1] Rubinstein, R.Y. and Kroese, D.P., The Cross-Entropy Method, A unified Approach to Combinatorial Optimization, Monte-Carlo Simulation, and Machine Learning, Springer, New York. (2004)
See Also