예제 #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);
            }
        }