示例#1
0
        public void RunTest()
        {
            // Valid input - Minimization
            {
                var optimizer = new SystemPerformanceOptimizer();

                // Create the context.
                var testableContext =
                    TestableSystemPerformanceOptimizationContext00.Get();

                var context = testableContext.Context;

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

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

                Assert.AreEqual(
                    expected: true,
                    actual: results.HasConverged);

                DoubleMatrixAssert.AreEqual(
                    expected: testableContext.OptimalState,
                    actual: results.OptimalState,
                    delta: .03);

                Assert.AreEqual(
                    expected: testableContext.OptimalPerformance,
                    actual: results.OptimalPerformance,
                    DoubleMatrixTest.Accuracy);
            }

            // Valid input - Maximization - with overrides
            {
                var optimizer = new SystemPerformanceOptimizer()
                {
                    PerformanceEvaluationParallelOptions = { MaxDegreeOfParallelism = 1 },
                    SampleGenerationParallelOptions      = { MaxDegreeOfParallelism = 1 }
                };

                // Create the context.
                var testableContext =
                    TestableSystemPerformanceOptimizationContext01.Get();

                var context = testableContext.Context;

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

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

                Assert.AreEqual(
                    expected: true,
                    actual: results.HasConverged);

                DoubleMatrixAssert.AreEqual(
                    expected: testableContext.OptimalState,
                    actual: results.OptimalState,
                    DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.OptimalPerformance,
                    actual: results.OptimalPerformance,
                    DoubleMatrixTest.Accuracy);
            }

            // Valid input - Maximization - without overrides
            {
                var optimizer = new SystemPerformanceOptimizer()
                {
                    PerformanceEvaluationParallelOptions = { MaxDegreeOfParallelism = 1 },
                    SampleGenerationParallelOptions      = { MaxDegreeOfParallelism = 1 }
                };

                // Create the context.
                var testableContext =
                    TestableSystemPerformanceOptimizationContext02.Get();

                var context = testableContext.Context;

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

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

                Assert.AreEqual(
                    expected: true,
                    actual: results.HasConverged);

                DoubleMatrixAssert.AreEqual(
                    expected: testableContext.OptimalState,
                    actual: results.OptimalState,
                    DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.OptimalPerformance,
                    actual: results.OptimalPerformance,
                    DoubleMatrixTest.Accuracy);
            }
        }
示例#2
0
        public void ConstructorTest()
        {
            // stateDimension is not positive
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "stateDimension";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new SystemPerformanceOptimizationContext00(
                        stateDimension: 0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        initialParameter: DoubleMatrix.Dense(2, 2,
                                                             new double[] { -1.0, 10000.0, -1.0, 10000.0 }),
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 10000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // optimizationGoal is not a field of OptimizationGoal
            {
                var STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL");

                string parameterName = "optimizationGoal";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new SystemPerformanceOptimizationContext00(
                        stateDimension: 2,
                        optimizationGoal: (OptimizationGoal)(-1),
                        initialParameter: DoubleMatrix.Dense(2, 2,
                                                             new double[] { -1.0, 10000.0, -1.0, 10000.0 }),
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 10000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL,
                    expectedParameterName: parameterName);
            }

            // initialParameter is null
            {
                string parameterName = "initialParameter";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new SystemPerformanceOptimizationContext00(
                        stateDimension: 2,
                        optimizationGoal: OptimizationGoal.Minimization,
                        initialParameter: null,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 10000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // minimumNumberOfIterations is not positive
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new SystemPerformanceOptimizationContext00(
                        stateDimension: 2,
                        optimizationGoal: OptimizationGoal.Minimization,
                        initialParameter: DoubleMatrix.Dense(2, 2,
                                                             new double[] { -1.0, 10000.0, -1.0, 10000.0 }),
                        minimumNumberOfIterations: 0,
                        maximumNumberOfIterations: 10000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // maximumNumberOfIterations is not positive
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new SystemPerformanceOptimizationContext00(
                        stateDimension: 2,
                        optimizationGoal: OptimizationGoal.Minimization,
                        initialParameter: DoubleMatrix.Dense(2, 2,
                                                             new double[] { -1.0, 10000.0, -1.0, 10000.0 }),
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // minimumNumberOfIterations is greater than maximumNumberOfIterations
            {
                var STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER"),
                        "maximumNumberOfIterations",
                        "minimumNumberOfIterations");

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new SystemPerformanceOptimizationContext00(
                        stateDimension: 2,
                        optimizationGoal: OptimizationGoal.Minimization,
                        initialParameter: DoubleMatrix.Dense(2, 2,
                                                             new double[] { -1.0, 10000.0, -1.0, 10000.0 }),
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 2);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER,
                    expectedParameterName: parameterName);
            }

            // valid input - LowerThanLevel
            {
                var testableContext =
                    TestableSystemPerformanceOptimizationContext00.Get();

                var context = testableContext.Context;

                Assert.AreEqual(
                    expected: testableContext.StateDimension,
                    actual: context.StateDimension);

                Assert.AreEqual(
                    expected: testableContext.TraceExecution,
                    actual: context.TraceExecution);

                Assert.AreEqual(
                    expected: testableContext.EliteSampleDefinition,
                    actual: context.EliteSampleDefinition);

                Assert.AreEqual(
                    expected: testableContext.OptimizationGoal,
                    actual: context.OptimizationGoal);

                DoubleMatrixAssert.AreEqual(
                    expected: testableContext.InitialParameter,
                    actual: context.InitialParameter,
                    DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.MinimumNumberOfIterations,
                    actual: context.MinimumNumberOfIterations);

                Assert.AreEqual(
                    expected: testableContext.MaximumNumberOfIterations,
                    actual: context.MaximumNumberOfIterations);
            }

            // valid input - HigherThanLevel
            {
                var testableContext = TestableSystemPerformanceOptimizationContext01.Get();

                var context = testableContext.Context;

                Assert.AreEqual(
                    expected: testableContext.StateDimension,
                    actual: context.StateDimension);

                Assert.AreEqual(
                    expected: testableContext.TraceExecution,
                    actual: context.TraceExecution);

                Assert.AreEqual(
                    expected: testableContext.EliteSampleDefinition,
                    actual: context.EliteSampleDefinition);

                Assert.AreEqual(
                    expected: testableContext.OptimizationGoal,
                    actual: context.OptimizationGoal);

                DoubleMatrixAssert.AreEqual(
                    expected: testableContext.InitialParameter,
                    actual: context.InitialParameter,
                    DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.MinimumNumberOfIterations,
                    actual: context.MinimumNumberOfIterations);

                Assert.AreEqual(
                    expected: testableContext.MaximumNumberOfIterations,
                    actual: context.MaximumNumberOfIterations);
            }
        }
        public void SampleTest()
        {
            // context is null
            {
                string parameterName = "context";
                string innerMessage  =
                    ArgumentExceptionAssert.NullPartialMessage +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var parameter = DoubleMatrix.Dense(1, 1);

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "Sample",
                        methodArgs: new object[] { null, 1, parameter });
                },
                    expectedInnerType: typeof(ArgumentNullException),
                    expectedInnerMessage: innerMessage);
            }

            // parameter is null
            {
                string parameterName = "parameter";
                string innerMessage  =
                    ArgumentExceptionAssert.NullPartialMessage +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var context = TestableSystemPerformanceOptimizationContext00.Get().Context;

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "Sample",
                        methodArgs: new object[] { context, 1, null });
                },
                    expectedInnerType: typeof(ArgumentNullException),
                    expectedInnerMessage: innerMessage);
            }

            // sampleSize is zero
            {
                string parameterName = "sampleSize";
                string innerMessage  =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE") +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var context = TestableSystemPerformanceOptimizationContext00.Get().Context;

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "Sample",
                        methodArgs: new object[] {
                        context,
                        0,
                        context.InitialParameter
                    });
                },
                    expectedInnerType: typeof(ArgumentOutOfRangeException),
                    expectedInnerMessage: innerMessage);
            }

            // sampleSize is negative
            {
                string parameterName = "sampleSize";
                string innerMessage  =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE") +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var context = TestableSystemPerformanceOptimizationContext00.Get().Context;

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "Sample",
                        methodArgs: new object[] {
                        context,
                        -1,
                        context.InitialParameter
                    });
                },
                    expectedInnerType: typeof(ArgumentOutOfRangeException),
                    expectedInnerMessage: innerMessage);
            }

            // parameter is not context compatible: wrong number of rows
            {
                string parameterName = "parameter";
                string innerMessage  =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CEP_PARAMETER_IS_CONTEXT_INCOMPATIBLE") +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var context = TestableSystemPerformanceOptimizationContext00.Get().Context;

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "Sample",
                        methodArgs: new object[] {
                        context,
                        1,
                        DoubleMatrix.Dense(
                            context.InitialParameter.NumberOfRows + 1,
                            context.InitialParameter.NumberOfColumns)
                    });
                },
                    expectedInnerType: typeof(ArgumentException),
                    expectedInnerMessage: innerMessage);
            }

            // parameter is not context compatible: wrong number of columns
            {
                string parameterName = "parameter";
                string innerMessage  =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CEP_PARAMETER_IS_CONTEXT_INCOMPATIBLE") +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var context = TestableSystemPerformanceOptimizationContext00.Get().Context;

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "Sample",
                        methodArgs: new object[] {
                        context,
                        1,
                        DoubleMatrix.Dense(
                            context.InitialParameter.NumberOfRows,
                            context.InitialParameter.NumberOfColumns + 1)
                    });
                },
                    expectedInnerType: typeof(ArgumentException),
                    expectedInnerMessage: innerMessage);
            }
        }
        public void EvauatePerformancesTest()
        {
            // context is null
            {
                string parameterName = "context";
                string innerMessage  =
                    ArgumentExceptionAssert.NullPartialMessage +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var sample = DoubleMatrix.Dense(1, 1);

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "EvaluatePerformances",
                        methodArgs: new object[] { null, sample });
                },
                    expectedInnerType: typeof(ArgumentNullException),
                    expectedInnerMessage: innerMessage);
            }

            // sample is null
            {
                string parameterName = "sample";
                string innerMessage  =
                    ArgumentExceptionAssert.NullPartialMessage +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var context = TestableSystemPerformanceOptimizationContext00.Get().Context;

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "EvaluatePerformances",
                        methodArgs: new object[] { context, null });
                },
                    expectedInnerType: typeof(ArgumentNullException),
                    expectedInnerMessage: innerMessage);
            }

            // sample is not context compatible
            {
                string parameterName = "sample";

                string innerMessage =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_CEP_SAMPLE_IS_CONTEXT_INCOMPATIBLE") +
                    " (Parameter '" + parameterName + "')";

                var optimizer = new SystemPerformanceOptimizer();

                var context = TestableSystemPerformanceOptimizationContext00.Get().Context;

                ExceptionAssert.InnerThrow(
                    () =>
                {
                    Reflector.ExecuteBaseMember(
                        obj: optimizer,
                        methodName: "EvaluatePerformances",
                        methodArgs: new object[] {
                        context,
                        DoubleMatrix.Dense(1, context.StateDimension + 1)
                    });
                },
                    expectedInnerType: typeof(ArgumentException),
                    expectedInnerMessage: innerMessage);
            }
        }
        public void OptimizeTest()
        {
            // context is null
            {
                string parameterName = "context";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.Optimize(
                        context: null,
                        rarity: 0.1,
                        sampleSize: 1000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // rarity is less than 0
            {
                var STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL =
                    String.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"), 0.0, 1.0);

                string parameterName = "rarity";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.Optimize(
                        context:
                        TestableSystemPerformanceOptimizationContext00
                        .Get().Context,
                        rarity: -0.1,
                        sampleSize: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // rarity is equal to 0
            {
                var STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL =
                    String.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"), 0.0, 1.0);

                string parameterName = "rarity";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.Optimize(
                        context:
                        TestableSystemPerformanceOptimizationContext00
                        .Get().Context,
                        rarity: 0.0,
                        sampleSize: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // rarity is greater than 1
            {
                var STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL =
                    String.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"), 0.0, 1.0);

                string parameterName = "rarity";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.Optimize(
                        context:
                        TestableSystemPerformanceOptimizationContext00
                        .Get().Context,
                        rarity: 1.1,
                        sampleSize: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // rarity is equal to 1
            {
                var STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL =
                    String.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL"), 0.0, 1.0);

                string parameterName = "rarity";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.Optimize(
                        context:
                        TestableSystemPerformanceOptimizationContext00
                        .Get().Context,
                        rarity: 1.0,
                        sampleSize: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // rarity is too low given context and sample size
            {
                var STR_EXCEPT_CEM_TOO_LOW_RARITY =
                    String.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_CEM_TOO_LOW_RARITY"), "sampleSize");

                string parameterName = "rarity";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.Optimize(
                        context:
                        TestableSystemPerformanceOptimizationContext02
                        .Get().Context,
                        rarity: .01,
                        sampleSize: 50);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_CEM_TOO_LOW_RARITY,
                    expectedParameterName: parameterName);
            }

            // rarity is too high given context and sample size
            {
                var STR_EXCEPT_CEM_TOO_HIGH_RARITY =
                    String.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_CEM_TOO_HIGH_RARITY"), "sampleSize");

                string parameterName = "rarity";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.Optimize(
                        context:
                        TestableSystemPerformanceOptimizationContext00
                        .Get().Context,
                        rarity: .99,
                        sampleSize: 50);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_CEM_TOO_HIGH_RARITY,
                    expectedParameterName: parameterName);
            }

            // sampleSize is not positive
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "sampleSize";

                var optimizer = new SystemPerformanceOptimizer();

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    optimizer.Optimize(
                        context:
                        TestableSystemPerformanceOptimizationContext00
                        .Get().Context,
                        rarity: 0.1,
                        sampleSize: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }
        }