/// <summary>Initializes a new instance of the
 /// <see cref="TestablePartitionOptimizationContext"/>
 /// class.</summary>
 /// <param name="context">The context to test.</param>
 /// <param name="stateDimension">The expected state dimension.</param>
 /// <param name="eliteSampleDefinition">The expected elite sample definition.</param>
 /// <param name="traceExecution">The expected value about tracing context execution.</param>
 /// <param name="optimizationGoal">The expected optimization goal.</param>
 /// <param name="initialParameter">The expected initial parameter.</param>
 /// <param name="minimumNumberOfIterations">
 /// The expected minimum number of iterations.</param>
 /// <param name="maximumNumberOfIterations">
 /// The expected maximum number of iterations.</param>
 /// <param name="optimalState">The expected optimal state.</param>
 /// <param name="optimalPerformance">The expected optimal performance.</param>
 /// <param name="objectiveFunction">The expected objective function.</param>
 /// <param name="partitionDimension">The expected partition dimension.</param>
 /// <param name="probabilitySmoothingCoefficient">The expected probability smoothing coefficient.</param>
 public TestablePartitionOptimizationContext(
     PartitionOptimizationContext context,
     int stateDimension,
     EliteSampleDefinition eliteSampleDefinition,
     bool traceExecution,
     OptimizationGoal optimizationGoal,
     DoubleMatrix initialParameter,
     int minimumNumberOfIterations,
     int maximumNumberOfIterations,
     DoubleMatrix optimalState,
     double optimalPerformance,
     Func <DoubleMatrix, double> objectiveFunction,
     int partitionDimension,
     double probabilitySmoothingCoefficient
     ) : base(
         context,
         stateDimension,
         eliteSampleDefinition,
         traceExecution,
         optimizationGoal,
         initialParameter,
         minimumNumberOfIterations,
         maximumNumberOfIterations,
         optimalState,
         optimalPerformance)
 {
     this.ObjectiveFunction  = objectiveFunction;
     this.PartitionDimension = partitionDimension;
     this.ProbabilitySmoothingCoefficient = probabilitySmoothingCoefficient;
 }
Example #2
0
 public SystemPerformanceOptimizationContext01(
     int stateDimension,
     OptimizationGoal optimizationGoal,
     DoubleMatrix initialParameter,
     int minimumNumberOfIterations,
     int maximumNumberOfIterations)
     : base(
         // The function to be optimized has one argument.
         // Thus it can be interpreted as the performance of
         // a system whose state can be represented as a vector
         // of length 1.
         stateDimension: stateDimension,
         // Set the optimization goal to maximization.
         optimizationGoal: optimizationGoal,
         // Define the initial parameter of the distribution
         // from which samples are drawn while searching
         // for the optimizer (sampling the state-space
         // of the system, i.e. the domain of the function).
         // The parameter is a column of two rows.
         // Its first row represents the mean, its second one
         // the standard deviation of the Gaussian distribution
         // from which the argument of the optimizing function,
         // is sampled while searching
         // for the optimizer.
         initialParameter: initialParameter,
         minimumNumberOfIterations: minimumNumberOfIterations,
         maximumNumberOfIterations: maximumNumberOfIterations)
 {
 }
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="SystemPerformanceOptimizationContext" /> class
        /// having the specified state dimension, initial parameter,
        /// optimization goal, and range of iterations.
        /// </summary>
        /// <param name="stateDimension">
        /// The dimension of a vector representing the state
        /// of the system whose performance must be optimized.
        /// </param>
        /// <param name="optimizationGoal">
        /// A constant to specify if the performance function
        /// must be minimized or maximized.
        /// </param>
        /// <param name="initialParameter">
        /// The parameter initially exploited to sample arguments
        /// of the performance function while searching
        /// for the optimal one.
        /// </param>
        /// <param name="minimumNumberOfIterations">
        /// The minimum number of iterations
        /// required by this context.
        /// </param>
        /// <param name="maximumNumberOfIterations">
        /// The maximum number of iterations
        /// allowed by this context.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="initialParameter"/> is <b>null</b>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="optimizationGoal"/> is not a field of
        /// <see cref="Advanced.OptimizationGoal"/>.<br/>
        /// -or-<br/>
        /// <paramref name="minimumNumberOfIterations"/> is greater than
        /// <paramref name="maximumNumberOfIterations"/>.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="minimumNumberOfIterations"/> is
        /// not positive.<br/>
        /// -or-<br/>
        /// <paramref name="maximumNumberOfIterations"/> is
        /// not positive.
        /// </exception>
        protected SystemPerformanceOptimizationContext(
            int stateDimension,
            DoubleMatrix initialParameter,
            OptimizationGoal optimizationGoal,
            int minimumNumberOfIterations,
            int maximumNumberOfIterations)
            : base(stateDimension, initialParameter)
        {
            if ((optimizationGoal != OptimizationGoal.Maximization)
                &&
                (optimizationGoal != OptimizationGoal.Minimization)
                )
            {
                throw new ArgumentException(
                          ImplementationServices.GetResourceString(
                              "STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL"),
                          nameof(optimizationGoal));
            }

            if (minimumNumberOfIterations < 1)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(minimumNumberOfIterations),
                          ImplementationServices.GetResourceString(
                              "STR_EXCEPT_PAR_MUST_BE_POSITIVE"));
            }

            if (maximumNumberOfIterations < 1)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(maximumNumberOfIterations),
                          ImplementationServices.GetResourceString(
                              "STR_EXCEPT_PAR_MUST_BE_POSITIVE"));
            }

            if (maximumNumberOfIterations < minimumNumberOfIterations)
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              ImplementationServices.GetResourceString(
                                  "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER"),
                              nameof(maximumNumberOfIterations),
                              nameof(minimumNumberOfIterations)),
                          nameof(maximumNumberOfIterations));
            }

            this.OptimizationGoal          = optimizationGoal;
            this.MinimumNumberOfIterations = minimumNumberOfIterations;
            this.MaximumNumberOfIterations = maximumNumberOfIterations;
        }
Example #4
0
        /// <summary>Initializes a new instance of the
        /// <see cref="TestablePartitionOptimizationContext"/>
        /// class.</summary>
        /// <param name="context">The context to test.</param>
        /// <param name="stateDimension">The expected state dimension.</param>
        /// <param name="eliteSampleDefinition">The expected elite sample definition.</param>
        /// <param name="traceExecution">The expected value about tracing context execution.</param>
        /// <param name="optimizationGoal">The expected optimization goal.</param>
        /// <param name="initialParameter">The expected initial parameter.</param>
        /// <param name="minimumNumberOfIterations">
        /// The expected minimum number of iterations.</param>
        /// <param name="maximumNumberOfIterations">
        /// The expected maximum number of iterations.</param>
        /// <param name="optimalState">The expected optimal state.</param>
        /// <param name="optimalPerformance">The expected optimal performance.</param>
        /// <param name="objectiveFunction">The expected objective function.</param>
        /// <param name="featureVariables">
        /// The expected feature variables.</param>
        /// <param name="responseVariable">
        /// The expected response variable.</param>
        /// <param name="numberOfResponseCategories">
        /// The expected number of response categories</param>
        /// <param name="numberOfCategoricalEntailments">
        /// The expected number of categorical entailments.
        /// </param>
        /// <param name="allowEntailmentPartialTruthValues">
        /// The expected allowance for entailment partial truth values.
        /// </param>
        /// <param name="probabilitySmoothingCoefficient">
        /// The expected probability smoothing coefficient.</param>
        public TestableCategoricalEntailmentEnsembleOptimizationContext(
            CategoricalEntailmentEnsembleOptimizationContext context,
            int stateDimension,
            EliteSampleDefinition eliteSampleDefinition,
            bool traceExecution,
            OptimizationGoal optimizationGoal,
            DoubleMatrix initialParameter,
            int minimumNumberOfIterations,
            int maximumNumberOfIterations,
            DoubleMatrix optimalState,
            double optimalPerformance,
            Func <DoubleMatrix, double> objectiveFunction,
            List <CategoricalVariable> featureVariables,
            CategoricalVariable responseVariable,
            int numberOfResponseCategories,
            int numberOfCategoricalEntailments,
            bool allowEntailmentPartialTruthValues,
            double probabilitySmoothingCoefficient
            ) : base(
                context,
                stateDimension,
                eliteSampleDefinition,
                traceExecution,
                optimizationGoal,
                initialParameter,
                minimumNumberOfIterations,
                maximumNumberOfIterations,
                optimalState,
                optimalPerformance)
        {
            this.ObjectiveFunction = objectiveFunction;
            this.FeatureVariables  = featureVariables;
            this.ResponseVariable  = responseVariable;

            var featureCategoryCounts = new List <int>(featureVariables.Count);

            for (int i = 0; i < featureVariables.Count; i++)
            {
                featureCategoryCounts.Add(featureVariables[i].NumberOfCategories);
            }
            this.FeatureCategoryCounts = featureCategoryCounts;

            this.NumberOfResponseCategories        = numberOfResponseCategories;
            this.NumberOfCategoricalEntailments    = numberOfCategoricalEntailments;
            this.AllowEntailmentPartialTruthValues = allowEntailmentPartialTruthValues;
            this.ProbabilitySmoothingCoefficient   = probabilitySmoothingCoefficient;
        }
Example #5
0
 /// <summary>Initializes a new instance of the
 /// <see cref="TestableContinuousOptimizationContext"/>
 /// class.</summary>
 /// <param name="context">The context to test.</param>
 /// <param name="stateDimension">The expected state dimension.</param>
 /// <param name="eliteSampleDefinition">The expected elite sample definition.</param>
 /// <param name="traceExecution">The expected value about tracing context execution.</param>
 /// <param name="optimizationGoal">The expected optimization goal.</param>
 /// <param name="initialParameter">The expected initial parameter.</param>
 /// <param name="minimumNumberOfIterations">
 /// The expected minimum number of iterations.</param>
 /// <param name="maximumNumberOfIterations">
 /// The expected maximum number of iterations.</param>
 /// <param name="optimalState">The expected optimal state.</param>
 /// <param name="optimalPerformance">The expected optimal performance.</param>
 /// <param name="objectiveFunction">The expected objective function.</param>
 /// <param name="initialArgument">The expected initial argument.</param>
 /// <param name="meanSmoothingCoefficient">The expected mean smoothing coefficient.</param>
 /// <param name="standardDeviationSmoothingCoefficient">The expected standard deviation smoothing coefficient.</param>
 /// <param name="standardDeviationSmoothingExponent">The expected standard deviation smoothing exponent.</param>
 /// <param name="initialStandardDeviation">The expected initial standard deviation.</param>
 /// <param name="terminationTolerance">The expected termination tolerance.</param>
 public TestableContinuousOptimizationContext(
     ContinuousOptimizationContext context,
     int stateDimension,
     EliteSampleDefinition eliteSampleDefinition,
     bool traceExecution,
     OptimizationGoal optimizationGoal,
     DoubleMatrix initialParameter,
     int minimumNumberOfIterations,
     int maximumNumberOfIterations,
     DoubleMatrix optimalState,
     double optimalPerformance,
     Func <DoubleMatrix, double> objectiveFunction,
     DoubleMatrix initialArgument,
     double meanSmoothingCoefficient,
     double standardDeviationSmoothingCoefficient,
     int standardDeviationSmoothingExponent,
     double initialStandardDeviation,
     double terminationTolerance
     ) : base(
         context,
         stateDimension,
         eliteSampleDefinition,
         traceExecution,
         optimizationGoal,
         initialParameter,
         minimumNumberOfIterations,
         maximumNumberOfIterations,
         optimalState,
         optimalPerformance)
 {
     this.ObjectiveFunction        = objectiveFunction;
     this.InitialArgument          = initialArgument;
     this.MeanSmoothingCoefficient = meanSmoothingCoefficient;
     this.StandardDeviationSmoothingCoefficient = standardDeviationSmoothingCoefficient;
     this.StandardDeviationSmoothingExponent    = standardDeviationSmoothingExponent;
     this.InitialStandardDeviation = initialStandardDeviation;
     this.TerminationTolerance     = terminationTolerance;
 }
 /// <summary>Initializes a new instance of the
 /// <see cref="TestableSystemPerformanceOptimizationContext"/>
 /// class.</summary>
 /// <param name="context">The context to test.</param>
 /// <param name="stateDimension">The expected state dimension.</param>
 /// <param name="eliteSampleDefinition">The expected elite sample definition.</param>
 /// <param name="traceExecution">The expected value about tracing context execution.</param>
 /// <param name="optimizationGoal">The expected optimization goal.</param>
 /// <param name="initialParameter">The expected initial parameter.</param>
 /// <param name="minimumNumberOfIterations">
 /// The expected minimum number of iterations.</param>
 /// <param name="maximumNumberOfIterations">
 /// The expected maximum number of iterations.</param>
 /// <param name="optimalState">The expected optimal state.</param>
 /// <param name="optimalPerformance">The expected optimal performance.</param>
 public TestableSystemPerformanceOptimizationContext(
     TCrossEntropyContext context,
     int stateDimension,
     EliteSampleDefinition eliteSampleDefinition,
     bool traceExecution,
     OptimizationGoal optimizationGoal,
     DoubleMatrix initialParameter,
     int minimumNumberOfIterations,
     int maximumNumberOfIterations,
     DoubleMatrix optimalState,
     double optimalPerformance
     ) : base(
         context,
         stateDimension,
         eliteSampleDefinition,
         traceExecution)
 {
     this.OptimizationGoal          = optimizationGoal;
     this.InitialParameter          = initialParameter;
     this.MinimumNumberOfIterations = minimumNumberOfIterations;
     this.MaximumNumberOfIterations = maximumNumberOfIterations;
     this.OptimalState       = optimalState;
     this.OptimalPerformance = optimalPerformance;
 }
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="ContinuousOptimizationContext" /> class
        /// aimed to optimize the specified
        /// continuous function,
        /// with the given initial
        /// guess argument, optimization goal,
        /// range of iterations, and smoothing coefficients.
        /// </summary>
        /// <param name="objectiveFunction">
        /// The function to be optimized.
        /// </param>
        /// <param name="optimizationGoal">
        /// A constant to specify if the function
        /// must be minimized or maximized.
        /// </param>
        /// <param name="initialArgument">
        /// The means of the Cross-Entropy parameter
        /// initially exploited to sample arguments
        /// of the function while searching
        /// for the optimal one.
        /// </param>
        /// <param name="meanSmoothingCoefficient">
        /// A coefficient to define the smoothing scheme for the
        /// means of the Cross-Entropy parameters
        /// exploited by this context.
        /// </param>
        /// <param name="standardDeviationSmoothingCoefficient">
        /// The coefficient that defines the base smoothing scheme for the
        /// standard deviations of the Cross-Entropy parameters
        /// exploited by this context.
        /// </param>
        /// <param name="standardDeviationSmoothingExponent">
        /// The exponent that defines the dynamic smoothing scheme
        /// for the standard deviations of the Cross-Entropy
        /// parameters exploited by this context.
        /// </param>
        /// <param name="initialStandardDeviation">
        /// The value assigned to each standard deviation in
        /// the initial Cross-Entropy parameter
        /// exploited by this context.
        /// </param>
        /// <param name="terminationTolerance">
        /// 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.
        /// </param>
        /// <param name="minimumNumberOfIterations">
        /// The minimum number of iterations
        /// required by this context.
        /// </param>
        /// <param name="maximumNumberOfIterations">
        /// The maximum number of iterations
        /// allowed by this context.
        /// </param>
        /// <remarks>
        /// <para>
        /// It is assumed that the <paramref name="objectiveFunction"/>
        /// will accept row
        /// vectors as valid representations of an argument.
        /// As a consequence, <paramref name="initialArgument"/> is
        /// expected to be a row vector.
        /// </para>
        /// <para>
        /// As discussed by Rubinstein and Kroese,
        /// Remark 5.2, p. 189<cite>rubinstein-kroese-2004</cite>,
        /// typical values for <paramref name="meanSmoothingCoefficient"/>
        /// are between .7 and 1 (excluded), while
        /// <paramref name="standardDeviationSmoothingCoefficient"/> should
        /// be between .8 and 1 (excluded), with
        /// <paramref name="standardDeviationSmoothingExponent"/>
        /// between 5 and 10.
        /// </para>
        /// <para>
        /// Also, it is expected that
        /// <paramref name="initialStandardDeviation"/> is a big enough
        /// number, such as 100.0.
        /// In this way, during the first execution of the sampling step,
        /// each argument will have a good likelihood of being drawn.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="objectiveFunction"/> is <b>null</b>.<br/>
        /// -or-<br/>
        /// <paramref name="initialArgument"/> is <b>null</b>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="optimizationGoal"/> is not a field of
        /// <see cref="OptimizationGoal"/>.<br/>
        /// -or-<br/>
        /// <paramref name="initialArgument"/> is not a row
        /// vector.<br/>
        /// -or-<br/>
        /// <paramref name="minimumNumberOfIterations"/> is greater than
        /// <paramref name="maximumNumberOfIterations"/>.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="meanSmoothingCoefficient"/> is not
        /// in the open interval between 0 and 1.<br/>
        /// -or-<br/>
        /// <paramref name="standardDeviationSmoothingCoefficient"/> is
        /// not in the open interval between 0 and 1.<br/>
        /// -or-<br/>
        /// <paramref name="standardDeviationSmoothingExponent"/> is
        /// not positive.<br/>
        /// -or-<br/>
        /// <paramref name="initialStandardDeviation"/> is not
        /// positive.<br/>
        /// -or-<br/>
        /// <paramref name="terminationTolerance"/> is not
        /// positive.<br/>
        /// -or-<br/>
        /// <paramref name="minimumNumberOfIterations"/> is
        /// not positive.<br/>
        /// -or-<br/>
        /// <paramref name="maximumNumberOfIterations"/> is
        /// not positive.
        /// </exception>
        public ContinuousOptimizationContext(
            Func <DoubleMatrix, double> objectiveFunction,
            DoubleMatrix initialArgument,
            double meanSmoothingCoefficient,
            double standardDeviationSmoothingCoefficient,
            int standardDeviationSmoothingExponent,
            double initialStandardDeviation,
            double terminationTolerance,
            OptimizationGoal optimizationGoal,
            int minimumNumberOfIterations,
            int maximumNumberOfIterations)
            : base(
                stateDimension: GetStateDimension(initialArgument),
                optimizationGoal: optimizationGoal,
                initialParameter: GetInitialParameter(
                    initialArgument,
                    initialStandardDeviation),
                minimumNumberOfIterations: minimumNumberOfIterations,
                maximumNumberOfIterations: maximumNumberOfIterations)
        {
            #region Input validation

            if (objectiveFunction is null)
            {
                throw new ArgumentNullException(nameof(objectiveFunction));
            }

            if (terminationTolerance <= 0)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(terminationTolerance),
                          ImplementationServices.GetResourceString(
                              "STR_EXCEPT_PAR_MUST_BE_POSITIVE"));
            }

            if (
                (meanSmoothingCoefficient <= 0.0)
                ||
                (1.0 <= meanSmoothingCoefficient)
                )
            {
                throw new ArgumentOutOfRangeException(
                          nameof(meanSmoothingCoefficient),
                          string.Format(
                              CultureInfo.InvariantCulture,
                              ImplementationServices.GetResourceString(
                                  "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"),
                              "0.0",
                              "1.0"));
            }

            if (
                (standardDeviationSmoothingCoefficient <= 0.0)
                ||
                (1.0 <= standardDeviationSmoothingCoefficient)
                )
            {
                throw new ArgumentOutOfRangeException(
                          nameof(standardDeviationSmoothingCoefficient),
                          string.Format(
                              CultureInfo.InvariantCulture,
                              ImplementationServices.GetResourceString(
                                  "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"),
                              "0.0",
                              "1.0"));
            }

            if (standardDeviationSmoothingExponent < 1)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(standardDeviationSmoothingExponent),
                          ImplementationServices.GetResourceString(
                              "STR_EXCEPT_PAR_MUST_BE_POSITIVE"));
            }

            #endregion

            this.InitialArgument          = initialArgument;
            this.MeanSmoothingCoefficient = meanSmoothingCoefficient;
            this.StandardDeviationSmoothingCoefficient =
                standardDeviationSmoothingCoefficient;
            this.StandardDeviationSmoothingExponent =
                standardDeviationSmoothingExponent;
            this.InitialStandardDeviation = initialStandardDeviation;

            this.objectiveFunction    = objectiveFunction;
            this.TerminationTolerance = terminationTolerance;
        }
Example #8
0
        public static CPInstance load(DataGridView grid)
        {
            OpenFileDialog file = new OpenFileDialog();

            if (file.ShowDialog() == DialogResult.OK)
            {
                String[] data  = System.IO.File.ReadAllLines(file.FileName);
                int      index = 2;


                CPInstance i = new CPInstance(200, 200);


                // Vars

                while (index < data.Length && !data[index].StartsWith("--CONSTRAINTS--"))
                {
                    i.addVariable(Variable.loadVar(data[index], grid));


                    index++;
                }

                index++;



                // Constraints

                while (index < data.Length && !data[index].StartsWith("--OPTIMIZATION GOALS--"))
                {
                    try
                    {
                        String[]   values      = data[index].Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                        Type       elementType = Type.GetType(values[0]);
                        MethodInfo f           = elementType.GetMethod("fromSerialization");
                        Constraint c           = (Constraint)f.Invoke(null, new Object[] { data[index], i });

                        if (c != null)
                        {
                            i.addConstraint(c);
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }



                    index++;
                }

                index++;



                // Opt Goals

                while (index < data.Length)
                {
                    Variable v     = OptimizationGoal.loadVar(i, data[index]);
                    String   value = OptimizationGoal.loadValue(data[index]);

                    i.setOptTo(v, value);


                    index++;
                }

                return(i);
            }

            return(null);
        }