Ejemplo n.º 1
0
        public void ToDoubleMatrixTest()
        {
            // value is null
            {
                DoubleMatrix actual = (DoubleMatrixRowCollection)(null);

                Assert.IsNull(actual);

                actual = DoubleMatrixRowCollection.ToDoubleMatrix(null);

                Assert.IsNull(actual);
            }

            // value is not null
            {
                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                var expected = matrix;

                var actual = (DoubleMatrix)rows;
                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);

                actual = DoubleMatrixRowCollection.ToDoubleMatrix(rows);
                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 2
0
        public void StandardTest()
        {
            // dimension is not positive
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var basis = Basis.Standard(0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE"),
                    expectedParameterName: "dimension");
            }

            // dimension is valid
            {
                var basis    = Basis.Standard(2);
                var actual   = basis.GetBasisMatrix();
                var expected = DoubleMatrix.Identity(2);

                DoubleMatrixAssert.AreEqual(
                    expected,
                    actual,
                    DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 3
0
        public void ToDoubleMatrixTest()
        {
            // value is null
            {
                DoubleMatrix actual = (DoubleMatrixRow)(null);

                Assert.IsNull(actual);

                actual = DoubleMatrixRow.ToDoubleMatrix(null);

                Assert.IsNull(actual);
            }

            // value is not null
            {
                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });
                var rows = matrix.AsRowCollection();

                for (int i = 0; i < matrix.NumberOfRows; i++)
                {
                    var          expected = matrix[i, ":"];
                    DoubleMatrix actual   = rows[i];
                    DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);

                    actual = DoubleMatrixRow.ToDoubleMatrix(rows[i]);
                    DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
                }
            }
        }
Ejemplo n.º 4
0
        public void ToDoubleMatrixTest()
        {
            // This code example produces the following output:
            //
            //
            // Part identifier: 0
            //      indexes: 1
            //
            // Part identifier: 1
            //      indexes: 0, 5
            //
            // Part identifier: 2
            //      indexes: 2, 4
            //
            // Part identifier: 3
            //      indexes: 3
            //

            var target = new IndexPartition <double>
            {
                partIndetifiers = new List <double>(3)
                {
                    0.0,
                    1.0,
                    2.0,
                    3.0
                },

                parts = new Dictionary <double, IndexCollection>(3)
                {
                    { 0.0, IndexCollection.FromArray(new int[] { 1 }) },
                    { 1.0, IndexCollection.FromArray(new int[] { 0, 5 }) },
                    { 2.0, IndexCollection.FromArray(new int[] { 2, 4 }) },
                    { 3.0, IndexCollection.FromArray(new int[] { 3 }) }
                }
            };

            // Convert the partition to a matrix.
            var actual = (DoubleMatrix)target;

            // Conversion of a partition to a matrix:
            // 1
            // 0
            // 2
            // 3
            // 2
            // 1
            //
            var expected = DoubleMatrix.Dense(6, 1, new double[] {
                1,
                0,
                2,
                3,
                2,
                1
            });

            DoubleMatrixAssert.AreEqual(expected, actual, 1e-2);
        }
Ejemplo n.º 5
0
        public void ParametricHessianTest()
        {
            // function is null
            {
                string parameterName = "function";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: null,
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: EngineFailureModel.LogLikelihood,
                        argument: null,
                        parameter: EngineFailureModel.Data);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // valid input
            {
                var expected =
                    EngineFailureModel.LogLikelihoodHessian(
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);

                var actual =
                    NumericalDifferentiation.Hessian(
                        function: EngineFailureModel.LogLikelihood,
                        argument: EngineFailureModel.EstimatedParameters,
                        parameter: EngineFailureModel.Data);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 6
0
#pragma warning disable CS0618 // Type or member is obsolete

        private static void TestSerializationToPath(
            TestableDoubleMatrix testableMatrix,
            string partialPath)
        {
            // dense
            {
                var expected = testableMatrix.AsDense;

                var path = "dense-" + partialPath;


                CsvMatrixSerializer.Serialize(path, expected);

                var actual = CsvMatrixSerializer.Deserialize(path);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }

            // sparse
            {
                var expected = testableMatrix.AsSparse;

                var path = "sparse-" + partialPath;

                CsvMatrixSerializer.Serialize(path, expected);

                var actual = CsvMatrixSerializer.Deserialize(path);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }

            // read-only dense
            {
                var expected = testableMatrix.AsDense;

                var path = "read-only-dense-" + partialPath;

                CsvMatrixSerializer.Serialize(path, expected.AsReadOnly());

                var actual = CsvMatrixSerializer.Deserialize(path);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }

            // read-only sparse
            {
                var expected = testableMatrix.AsSparse;

                var path = "read-only-sparse-" + partialPath;

                CsvMatrixSerializer.Serialize(path, expected.AsReadOnly());

                var actual = CsvMatrixSerializer.Deserialize(path);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 7
0
        public void UniformTest()
        {
            var values = DoubleMatrix.Dense(3, 2,
                                            new double[6] {
                1, 2, 3, 4, 5, 6
            });

            var distribution = FiniteDiscreteDistribution.Uniform(values);

            DoubleMatrixAssert.AreEqual(
                expected: values,
                actual: (DoubleMatrix)distribution.Values,
                delta: DoubleMatrixTest.Accuracy);
            DoubleMatrixAssert.AreEqual(
                expected: DoubleMatrix.Dense(3, 2, 1.0 / 6.0),
                actual: (DoubleMatrix)distribution.Masses,
                delta: DoubleMatrixTest.Accuracy);
        }
Ejemplo n.º 8
0
        public void AsDoubleMatrixTest()
        {
            DoubleMatrix expected = DoubleMatrix.Dense(2, 3,
                                                       new double[6] {
                1, 2, 3, 4, 5, 6
            });

            var doubleMatrixRowCollection = expected.AsRowCollection();

            var actual = doubleMatrixRowCollection;

            DoubleMatrixAssert.IsStateAsExpected(
                expectedState: new DoubleMatrixState(
                    asColumnMajorDenseArray: new double[6] {
                1, 2, 3, 4, 5, 6
            },
                    numberOfRows: 2,
                    numberOfColumns: 3),
                actualMatrix: actual,
                delta: DoubleMatrixTest.Accuracy);
        }
Ejemplo n.º 9
0
        public void GetPrincipalProjectionsTest()
        {
            var cloud = TestableCloud00.Get().Cloud;

            var principalProjections = cloud.GetPrincipalProjections();

            DoubleMatrixAssert.AreEqual(
                expected: cloud.Basis.GetBasisMatrix(),
                actual: principalProjections.ActiveCloud.Basis.GetBasisMatrix(),
                delta: CloudTest.Accuracy);

            DoubleMatrixAssert.AreEqual(
                expected: cloud.Coordinates,
                actual: principalProjections.ActiveCloud.Coordinates,
                delta: CloudTest.Accuracy);

            DoubleMatrixAssert.AreEqual(
                expected: cloud.Weights,
                actual: principalProjections.ActiveCloud.Weights,
                delta: CloudTest.Accuracy);
        }
Ejemplo n.º 10
0
        public void ToDoubleMatrixByMethodTest()
        {
            // value is null
            {
                CategoricalVariable target = null;

                ArgumentExceptionAssert.Throw(
                    () => { var result = CategoricalVariable.ToDoubleMatrix(target); },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "value");
            }

            // Valid input
            {
                string name   = "The name";
                var    target = new CategoricalVariable(name)
                {
                    { 0.0, "Zero" },
                    { 1.0, "One" },
                    { 2.0, "Two" }
                };

                var expected = DoubleMatrix.Dense(3, 1,
                                                  new double[3] {
                    0.0, 1.0, 2.0
                });
                expected.Name = target.Name;
                expected.SetRowName(0, "Zero");
                expected.SetRowName(1, "One");
                expected.SetRowName(2, "Two");

                DoubleMatrix actual = CategoricalVariable.ToDoubleMatrix(target);

                DoubleMatrixAssert.AreEqual(expected, actual, 1e-2);
            }
        }
Ejemplo n.º 11
0
        public void EuclideanTest()
        {
            // cluster is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var partition = Distance.Euclidean((DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "cluster");
            }

            // cluster is valid
            {
                var items         = IndexCollection.Range(0, 3);
                int numberOfItems = items.Count;
                var attributes    = IrisDataSet.GetAttributesAsDoubleMatrix()[items, ":"];

                var actual = Distance.Euclidean(attributes);

                var expected = DoubleMatrix.Dense(
                    numberOfItems,
                    numberOfItems,
                    new double[] {
                    0.0000000, 0.5385165, 0.5099020, 0.6480741,
                    0.5385165, 0.0000000, 0.3000000, 0.3316625,
                    0.5099020, 0.3000000, 0.0000000, 0.2449490,
                    0.6480741, 0.3316625, 0.2449490, 0.0000000
                },
                    StorageOrder.RowMajor);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }
        private static void TestSerializationToStream(
            TestableDoubleMatrix testableMatrix)
        {
            // dense
            {
                var expected = testableMatrix.AsDense;

                MemoryStream stream = new();

                var textWriter = new StreamWriter(stream);

                CsvDoubleMatrixSerializer.Serialize(textWriter, expected);

                stream.Position = 0;

                var textReader = new StreamReader(stream);

                var actual = CsvDoubleMatrixSerializer.Deserialize(textReader);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }

            // sparse
            {
                var expected = testableMatrix.AsSparse;

                MemoryStream stream = new();

                var textWriter = new StreamWriter(stream);

                CsvDoubleMatrixSerializer.Serialize(textWriter, expected);

                stream.Position = 0;

                var textReader = new StreamReader(stream);

                var actual = CsvDoubleMatrixSerializer.Deserialize(textReader);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }

            // read-only dense
            {
                var expected = testableMatrix.AsDense;

                MemoryStream stream = new();

                var textWriter = new StreamWriter(stream);

                CsvDoubleMatrixSerializer.Serialize(textWriter, expected.AsReadOnly());

                stream.Position = 0;

                var textReader = new StreamReader(stream);

                var actual = CsvDoubleMatrixSerializer.Deserialize(textReader);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }

            // read-only sparse
            {
                var expected = testableMatrix.AsSparse;

                MemoryStream stream = new();

                var textWriter = new StreamWriter(stream);

                CsvDoubleMatrixSerializer.Serialize(textWriter, expected.AsReadOnly());

                stream.Position = 0;

                var textReader = new StreamReader(stream);

                var actual = CsvDoubleMatrixSerializer.Deserialize(textReader);

                DoubleMatrixAssert.AreEqual(expected, actual, DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 13
0
        public void IndexSetTest()
        {
            // value is less than 0
            {
                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var rows = matrix.AsRowCollection();

                var target = rows[0];

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    target.Index = -1;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_TAB_INDEX_EXCEEDS_DIMS"),
                    expectedParameterName: "value");
            }

            // value is greater than NumberOfColumns - 1
            {
                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var rows = matrix.AsRowCollection();

                var target = rows[0];

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    target.Index = matrix.NumberOfColumns;
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_TAB_INDEX_EXCEEDS_DIMS"),
                    expectedParameterName: "value");
            }

            // Setting Index without changing its value
            {
                var subscriber = new PropertyChangedSubscriber();

                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var rows = matrix.AsRowCollection();

                for (int i = 0; i < matrix.NumberOfRows; i++)
                {
                    var target = rows[i];

                    target.PropertyChanged += subscriber.PropertyChangedEventHandler;

                    target.Index = i;

                    Assert.AreEqual(expected: i, actual: target.Index);
                }
            }

            // Setting Index changing its value
            {
                var subscriber = new PropertyChangedSubscriber();

                DoubleMatrix matrix = DoubleMatrix.Dense(2, 3,
                                                         new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var rows = matrix.AsRowCollection();

                var target = rows[0];

                target.PropertyChanged += subscriber.PropertyChangedEventHandler;

                int expectedIndex = 1;
                target.Index = expectedIndex;

                Assert.AreEqual(expectedIndex, target.Index);

                DoubleMatrixAssert.AreEqual(
                    matrix[expectedIndex, ":"], target, DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 14
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);
            }
        }
Ejemplo n.º 15
0
        public void ChangeCoordinatesTest()
        {
            // newBasis is null
            {
                Basis newBasis           = null;
                var   currentCoordinates = DoubleMatrix.Dense(1, 2,
                                                              new double[2] {
                    2.0 / 3.0, -1.0 / 3.0
                });
                var currentBasisMatrix = DoubleMatrix.Dense(2, 2,
                                                            new double[4] {
                    1, 1, -1, 2
                });
                Basis currentBasis = new(currentBasisMatrix);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var newCoordinates = Basis.ChangeCoordinates(
                        newBasis,
                        currentCoordinates,
                        currentBasis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "newBasis");
            }

            // currentCoordinates is null
            {
                Basis        newBasis           = Basis.Standard(2);
                DoubleMatrix currentCoordinates = null;
                var          currentBasisMatrix = DoubleMatrix.Dense(2, 2,
                                                                     new double[4] {
                    1, 1, -1, 2
                });
                Basis currentBasis = new(currentBasisMatrix);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var newCoordinates = Basis.ChangeCoordinates(
                        newBasis,
                        currentCoordinates,
                        currentBasis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "currentCoordinates");
            }

            // currentBasis is null
            {
                Basis newBasis           = Basis.Standard(2);
                var   currentCoordinates = DoubleMatrix.Dense(1, 2,
                                                              new double[2] {
                    2.0 / 3.0, -1.0 / 3.0
                });
                Basis currentBasis = null;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var newCoordinates = Basis.ChangeCoordinates(
                        newBasis,
                        currentCoordinates,
                        currentBasis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "currentBasis");
            }

            // newBasis and currentBasis do not share the same dimension
            {
                Basis newBasis           = Basis.Standard(1);
                var   currentCoordinates = DoubleMatrix.Dense(1, 2,
                                                              new double[2] {
                    2.0 / 3.0, -1.0 / 3.0
                });
                var currentBasisMatrix = DoubleMatrix.Dense(2, 2,
                                                            new double[4] {
                    1, 1, -1, 2
                });
                Basis currentBasis = new(currentBasisMatrix);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var newCoordinates = Basis.ChangeCoordinates(
                        newBasis,
                        currentCoordinates,
                        currentBasis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_BASES_MUST_SHARE_DIMENSION"),
                    expectedParameterName: "newBasis");
            }

            // currentCoordinates has count unequal to basis dimension
            {
                Basis newBasis           = Basis.Standard(2);
                var   currentCoordinates = DoubleMatrix.Dense(1, 3);
                var   currentBasisMatrix = DoubleMatrix.Dense(2, 2,
                                                              new double[4] {
                    1, 1, -1, 2
                });
                Basis currentBasis = new(currentBasisMatrix);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var newCoordinates = Basis.ChangeCoordinates(
                        newBasis,
                        currentCoordinates,
                        currentBasis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_BASIS_COMPLIANT_MATRIX"),
                    expectedParameterName: "currentCoordinates");
            }

            // all parameters are valid
            {
                // See example 4, p. 44, Linear Algebra, Lang.

                var newBasis = Basis.Standard(2);

                var currentCoordinates = DoubleMatrix.Dense(1, 2,
                                                            new double[2] {
                    2.0 / 3.0, -1.0 / 3.0
                });

                var currentBasisMatrix = DoubleMatrix.Dense(2, 2,
                                                            new double[4] {
                    1, 1, -1, 2
                });
                var currentBasis = new Basis(currentBasisMatrix);

                var newCoordinates = Basis.ChangeCoordinates(
                    newBasis,
                    currentCoordinates,
                    currentBasis);

                var expected = DoubleMatrix.Dense(1, 2, new double[2] {
                    1, 0
                });
                var actual = newCoordinates;

                DoubleMatrixAssert.AreEqual(
                    expected,
                    actual,
                    DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 16
0
        public void GetVectorsTest()
        {
            // coordinates is null
            {
                var basisMatrix = DoubleMatrix.Dense(2, 2,
                                                     new double[4] {
                    1, 1, -1, 2
                });
                var basis = new Basis(basisMatrix);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var vectors = basis.GetVectors(null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "coordinates");
            }

            // coordinates is not a basis compliant matrix
            {
                var basisMatrix = DoubleMatrix.Dense(2, 2,
                                                     new double[4] {
                    1, 1, -1, 2
                });
                var basis = new Basis(basisMatrix);

                var coordinates = DoubleMatrix.Dense(2, 3);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var vectors = basis.GetVectors(coordinates);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_BASIS_COMPLIANT_MATRIX"),
                    expectedParameterName: "coordinates");
            }

            // coordinates is valid
            {
                var basisMatrix = DoubleMatrix.Dense(2, 2,
                                                     new double[4] {
                    1, 1, -1, 2
                });
                var basis = new Basis(basisMatrix);

                var coordinates = DoubleMatrix.Dense(1, 2,
                                                     new double[2] {
                    2.0 / 3.0, -1.0 / 3.0
                });

                var vectors = basis.GetVectors(coordinates);

                var expected = DoubleMatrix.Dense(1, 2,
                                                  new double[2] {
                    1, 0
                });
                var actual = vectors;

                DoubleMatrixAssert.AreEqual(
                    expected,
                    actual,
                    DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 17
0
        public void ConstructorTest()
        {
            // 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 CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(2)
                    {
                        5, 6
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: (OptimizationGoal)(-1),
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL,
                    expectedParameterName: parameterName);
            }

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

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: null,
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: null,
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // featureCategoryCounts is empty
            {
                var STR_EXCEPT_PAR_MUST_BE_NON_EMPTY =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_NON_EMPTY");

                string parameterName = "featureCategoryCounts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(),
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_NON_EMPTY,
                    expectedParameterName: parameterName);
            }

            // featureCategoryCounts has zero entries
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE");

                string parameterName = "featureCategoryCounts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 0, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // featureCategoryCounts has negative entries
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE");

                string parameterName = "featureCategoryCounts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, -1, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfResponseCategories is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfResponseCategories";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 0,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfResponseCategories is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfResponseCategories";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: -1,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfCategoricalEntailments is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfCategoricalEntailments";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 0,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfCategoricalEntailments is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfCategoricalEntailments";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: -1,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 0,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: -1,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: -1);
                },
                    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 CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 2);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is zero
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: .0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is negative
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: -.1,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is one
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: 1.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is greater than one
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new CategoricalEntailmentEnsembleOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        featureCategoryCounts: new List <int>(3)
                    {
                        5, 6, 7
                    },
                        numberOfResponseCategories: 3,
                        numberOfCategoricalEntailments: 4,
                        allowEntailmentPartialTruthValues: true,
                        probabilitySmoothingCoefficient: 1.1,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // valid input - HigherThanLevel
            {
                var testableContext = TestableCategoricalEntailmentEnsembleOptimizationContext00.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);

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

                for (int i = 0; i < context.FeatureCategoryCounts.Count; i++)
                {
                    Assert.AreEqual(
                        expected: testableContext.FeatureCategoryCounts[i],
                        actual: context.FeatureCategoryCounts[i]);
                }

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

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

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

                Assert.AreEqual(
                    expected: testableContext.ProbabilitySmoothingCoefficient,
                    actual: context.ProbabilitySmoothingCoefficient,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 18
0
        public void NonparametricHessianTest()
        {
            // function is null
            {
                string parameterName = "function";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: null,
                        argument: DoubleMatrix.Dense(2, 1));
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Hessian(
                        function: function,
                        argument: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // valid input
            {
                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                DoubleMatrix hessian(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    DoubleMatrix h = DoubleMatrix.Dense(2, 2);

                    h[0, 0] = 2.0 * y;
                    h[0, 1] = 2.0 * x;
                    h[1, 0] = h[0, 1];
                    h[1, 1] = Math.Exp(y);

                    return(h);
                }

                var arg = DoubleMatrix.Dense(2, 1, new double[2]
                {
                    9.0, -2.1
                });

                var expected =
                    hessian(
                        argument: arg);

                var actual =
                    NumericalDifferentiation.Hessian(
                        function: function,
                        argument: arg);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 19
0
        public void NonparametricGradientTest()
        {
            // function is null
            {
                string parameterName = "function";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Gradient(
                        function: null,
                        argument: DoubleMatrix.Dense(2, 1));
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    NumericalDifferentiation.Gradient(
                        function: function,
                        argument: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // valid input - Non zero arguments
            {
                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                DoubleMatrix gradient(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    DoubleMatrix g = DoubleMatrix.Dense(2, 1);

                    g[0] = 2.0 * x * y;
                    g[1] = x * x + Math.Exp(y);

                    return(g);
                };

                var arg = DoubleMatrix.Dense(2, 1, new double[2]
                {
                    9.0, -2.1
                });

                var expected =
                    gradient(
                        argument: arg);

                var actual =
                    NumericalDifferentiation.Gradient(
                        function: function,
                        argument: arg);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // valid input - Some zero arguments
            {
                double function(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    double f = x * x * y + Math.Exp(y);

                    return(f);
                };

                DoubleMatrix gradient(DoubleMatrix argument)
                {
                    double x, y;

                    x = argument[0];
                    y = argument[1];

                    DoubleMatrix g = DoubleMatrix.Dense(2, 1);

                    g[0] = 2.0 * x * y;
                    g[1] = x * x + Math.Exp(y);

                    return(g);
                };

                var arg = DoubleMatrix.Dense(2, 1, new double[2]
                {
                    0.0, -2.1
                });

                var expected =
                    gradient(
                        argument: arg);

                var actual =
                    NumericalDifferentiation.Gradient(
                        function: function,
                        argument: arg);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);

                arg = DoubleMatrix.Dense(2, 1, new double[2]
                {
                    9.0, 0.0
                });

                expected =
                    gradient(
                        argument: arg);

                actual =
                    NumericalDifferentiation.Gradient(
                        function: function,
                        argument: arg);

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
        public void RunTest()
        {
            // Valid input - Minimization
            {
                var optimizer = new SystemPerformanceOptimizer();

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

                var context = testableContext.Context;

                // Set optimization parameters.
                int    sampleSize = 100;
                double rarity     = 0.09;

                // 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
            {
                var optimizer = new SystemPerformanceOptimizer();

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

                var context = testableContext.Context;

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

                // 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 - not converging
            {
                var optimizer = new SystemPerformanceOptimizer();

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

                var context = new ContinuousOptimizationContext(
                    objectiveFunction: testableContext.ObjectiveFunction,
                    initialArgument: testableContext.InitialArgument,
                    meanSmoothingCoefficient: testableContext.MeanSmoothingCoefficient,
                    standardDeviationSmoothingCoefficient: testableContext.StandardDeviationSmoothingCoefficient,
                    standardDeviationSmoothingExponent: testableContext.StandardDeviationSmoothingExponent,
                    initialStandardDeviation: testableContext.InitialStandardDeviation,
                    terminationTolerance: testableContext.TerminationTolerance,
                    optimizationGoal: testableContext.OptimizationGoal,
                    minimumNumberOfIterations: testableContext.MinimumNumberOfIterations,
                    maximumNumberOfIterations: testableContext.MinimumNumberOfIterations + 1);

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

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

                Assert.AreEqual(
                    expected: false,
                    actual: results.HasConverged);
            }
        }
        public void ConstructorTest()
        {
            // 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 ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: (OptimizationGoal)(-1),
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL,
                    expectedParameterName: parameterName);
            }

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

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: null,
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: null,
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // initialArgument is not a row vector
            {
                var STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR");

                string parameterName = "initialArgument";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(3, 1),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR,
                    expectedParameterName: parameterName);
            }

            // initialStandardDeviation is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "initialStandardDeviation";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 0.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // initialStandardDeviation is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "initialStandardDeviation";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: -100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 0,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: -1,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: -1000);
                },
                    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 ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 2);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER,
                    expectedParameterName: parameterName);
            }

            // meanSmoothingCoefficient is zero
            {
                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 = "meanSmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: 0.0,
                        standardDeviationSmoothingCoefficient: 0.7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // meanSmoothingCoefficient is negative
            {
                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 = "meanSmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: -.8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // meanSmoothingCoefficient is one
            {
                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 = "meanSmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: 1.0,
                        standardDeviationSmoothingCoefficient: 0.7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // meanSmoothingCoefficient is greater than one
            {
                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 = "meanSmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: 1.1,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // terminationTolerance is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "terminationTolerance";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 0.0,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // terminationTolerance is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "terminationTolerance";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: -1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // standardDeviationSmoothingCoefficient is zero
            {
                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 = "standardDeviationSmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: 0.0,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // standardDeviationSmoothingCoefficient is negative
            {
                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 = "standardDeviationSmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: -.7,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // standardDeviationSmoothingCoefficient is one
            {
                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 = "standardDeviationSmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: 1.0,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // standardDeviationSmoothingCoefficient is greater than one
            {
                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 = "standardDeviationSmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: 1.1,
                        standardDeviationSmoothingExponent: 6,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // standardDeviationSmoothingExponent is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "standardDeviationSmoothingExponent";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: 0,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // standardDeviationSmoothingExponent is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "standardDeviationSmoothingExponent";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new ContinuousOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(1, 3),
                        meanSmoothingCoefficient: .8,
                        standardDeviationSmoothingCoefficient: .7,
                        standardDeviationSmoothingExponent: -1,
                        initialStandardDeviation: 100.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        terminationTolerance: 1.0e-3,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // valid input - LowerThanLevel
            {
                var testableContext =
                    TestableContinuousOptimizationContext00.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);

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

                Assert.AreEqual(
                    expected: testableContext.InitialStandardDeviation,
                    actual: context.InitialStandardDeviation,
                    delta: DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.MeanSmoothingCoefficient,
                    actual: context.MeanSmoothingCoefficient,
                    delta: DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.StandardDeviationSmoothingCoefficient,
                    actual: context.StandardDeviationSmoothingCoefficient,
                    delta: DoubleMatrixTest.Accuracy);

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

                Assert.AreEqual(
                    expected: testableContext.TerminationTolerance,
                    actual: context.TerminationTolerance,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // valid input - HigherThanLevel
            {
                var testableContext = TestableContinuousOptimizationContext01.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);

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

                Assert.AreEqual(
                    expected: testableContext.InitialStandardDeviation,
                    actual: context.InitialStandardDeviation,
                    delta: DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.MeanSmoothingCoefficient,
                    actual: context.MeanSmoothingCoefficient,
                    delta: DoubleMatrixTest.Accuracy);

                Assert.AreEqual(
                    expected: testableContext.StandardDeviationSmoothingCoefficient,
                    actual: context.StandardDeviationSmoothingCoefficient,
                    delta: DoubleMatrixTest.Accuracy);

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

                Assert.AreEqual(
                    expected: testableContext.TerminationTolerance,
                    actual: context.TerminationTolerance,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 22
0
        public void BasisTest()
        {
            // basisMatrix is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var basis = new Basis(null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "basisMatrix");
            }

            // basisMatrix is not square
            {
                var basisMatrix = DoubleMatrix.Dense(2, 3);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var basis = new Basis(basisMatrix);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_SQUARE"),
                    expectedParameterName: "basisMatrix");
            }

            // basisMatrix is singular
            {
                var basisMatrix = DoubleMatrix.Dense(2, 2,
                                                     new double[4] {
                    1, 2, 2, 4
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var basis = new Basis(basisMatrix);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_NON_SINGULAR"),
                    expectedParameterName: "basisMatrix");
            }

            // basisMatrix is valid (dense)
            {
                var basisMatrix = DoubleMatrix.Dense(2, 2,
                                                     new double[4] {
                    1, 1, -1, 2
                });

                var basis = new Basis(basisMatrix);

                var actual   = basis.GetBasisMatrix();
                var expected = basisMatrix;

                DoubleMatrixAssert.AreEqual(
                    expected,
                    actual,
                    DoubleMatrixTest.Accuracy);

                Assert.AreEqual(2, basis.Dimension);
            }

            // basisMatrix is valid (sparse)
            {
                var basisMatrix = DoubleMatrix.Identity(2);

                var basis = new Basis(basisMatrix);

                var actual   = basis.GetBasisMatrix();
                var expected = basisMatrix;

                DoubleMatrixAssert.AreEqual(
                    expected,
                    actual,
                    DoubleMatrixTest.Accuracy);

                Assert.AreEqual(2, basis.Dimension);
            }
        }
Ejemplo n.º 23
0
        public void AnalyzeTest()
        {
            // data is null
            {
                string parameterName = "data";

                DoubleMatrix data = null;
                DoubleMatrix individualWeights    = DoubleMatrix.Dense(1, 1, 1.0 / 4.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    9, 4
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights    = null;
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    9, 4
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights    = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);;
                DoubleMatrix variableCoefficients = null;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // weights is not a column vector
            {
                var STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR");

                string parameterName = "individualWeights";

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights    = DoubleMatrix.Dense(1, 4, 1.0 / 4.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    9, 4
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);
            }

            // individualWeights must have the number of rows of data
            {
                var STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS =
                    string.Format(CultureInfo.InvariantCulture,
                                  ImplementationServices.GetResourceString(
                                      "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS"),
                                  "data");

                string parameterName = "individualWeights";

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights    = DoubleMatrix.Dense(5, 1, 1.0 / 4.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    9, 4
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);
            }

            // individualWeights must have non negative entries
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE");

                string parameterName = "individualWeights";

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights    = DoubleMatrix.Dense(4, 1, -1.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    9, 4
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);
            }

            // individualWeights must have entries summing up to 1
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1 =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1");

                string parameterName = "individualWeights";

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights = DoubleMatrix.Dense(4, 1,
                                                                    new double[4] {
                    .3, .6, .2, .1
                });
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    9, 4
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);
            }

            // variableCoefficients must be a row vector
            {
                var STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR");

                string parameterName = "variableCoefficients";

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights = DoubleMatrix.Dense(4, 1,
                                                                    1.0 / 4.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    2, 1, new double[2] {
                    9, 4
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR,
                    expectedParameterName: parameterName);
            }

            // variableCoefficients must have the number of columns of data
            {
                var STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS =
                    string.Format(CultureInfo.InvariantCulture,
                                  ImplementationServices.GetResourceString(
                                      "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS"),
                                  "data");

                string parameterName = "variableCoefficients";

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights = DoubleMatrix.Dense(4, 1,
                                                                    1.0 / 4.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 3, new double[3] {
                    9, 4, 5
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS,
                    expectedParameterName: parameterName);
            }

            // variableCoefficients must have positive entries
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE");

                string parameterName = "variableCoefficients";

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights = DoubleMatrix.Dense(4, 1,
                                                                    1.0 / 4.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    9, 0
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // SVD cannot be executed or does not converge
            {
                var STR_EXCEPT_SVD_ERRORS =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_SVD_ERRORS");

                DoubleMatrix data = DoubleMatrix.Dense(2, 2,
                                                       new double[4] {
                    Double.NegativeInfinity, 0, 0, 1
                });
                DoubleMatrix individualWeights = DoubleMatrix.Dense(2, 1,
                                                                    1.0 / 2.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    1, 1
                });

                ExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage:
                    STR_EXCEPT_SVD_ERRORS);
            }

            // No positive principal variances
            {
                var STR_EXCEPT_GDA_NON_POSITIVE_PRINCIPAL_VARIANCES =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_GDA_NON_POSITIVE_PRINCIPAL_VARIANCES");

                DoubleMatrix data = DoubleMatrix.Dense(4, 2);
                DoubleMatrix individualWeights = DoubleMatrix.Dense(4, 1,
                                                                    1.0 / 4.0);
                DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                    1, 2, new double[2] {
                    1, 1
                });

                ExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights,
                        variableCoefficients);
                },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage:
                    STR_EXCEPT_GDA_NON_POSITIVE_PRINCIPAL_VARIANCES);

                ExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data,
                        individualWeights);
                },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage:
                    STR_EXCEPT_GDA_NON_POSITIVE_PRINCIPAL_VARIANCES);

                ExceptionAssert.Throw(
                    () =>
                {
                    PrincipalComponents.Analyze(
                        data);
                },
                    expectedType: typeof(InvalidOperationException),
                    expectedMessage:
                    STR_EXCEPT_GDA_NON_POSITIVE_PRINCIPAL_VARIANCES);
            }

            // Valid input
            {
                {
                    DoubleMatrix data = DoubleMatrix.Dense(4, 2,
                                                           new double[8] {
                        1, 2, 3, 4, 5, 6, 7, 0
                    });
                    DoubleMatrix individualWeights = DoubleMatrix.Dense(4, 1,
                                                                        1.0 / 4.0);
                    DoubleMatrix variableCoefficients = DoubleMatrix.Dense(
                        1, 2, new double[2] {
                        9, 4
                    });

                    var principalComponents =
                        PrincipalComponents.Analyze(
                            data,
                            individualWeights,
                            variableCoefficients);

                    var cloud = principalComponents.ActiveCloud;

                    DoubleMatrixAssert.AreEqual(
                        expected: new Basis(
                            DoubleMatrix.Dense(2, 2, new double[4] {
                        3, 0, 0, 2
                    }))
                        .GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: data,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: individualWeights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);

                    data[0, 0] = Double.PositiveInfinity;

                    Assert.AreNotEqual(
                        notExpected: data[0, 0],
                        actual: cloud.Coordinates[0, 0],
                        delta: CloudTest.Accuracy);
                }

                {
                    DoubleMatrix data = DoubleMatrix.Dense(4, 2,
                                                           new double[8] {
                        1, 2, 3, 4, 5, 6, 7, 0
                    });
                    DoubleMatrix individualWeights = DoubleMatrix.Dense(4, 1,
                                                                        1.0 / 4.0);

                    var principalComponents =
                        PrincipalComponents.Analyze(
                            data,
                            individualWeights);

                    var cloud = principalComponents.ActiveCloud;

                    DoubleMatrixAssert.AreEqual(
                        expected: Basis.Standard(2)
                        .GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: data,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: individualWeights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);

                    data[0, 0] = Double.PositiveInfinity;

                    Assert.AreNotEqual(
                        notExpected: data[0, 0],
                        actual: cloud.Coordinates[0, 0],
                        delta: CloudTest.Accuracy);
                }

                {
                    DoubleMatrix data = DoubleMatrix.Dense(4, 2,
                                                           new double[8] {
                        1, 2, 3, 4, 5, 6, 7, 0
                    });

                    var principalComponents =
                        PrincipalComponents.Analyze(
                            data);

                    var cloud = principalComponents.ActiveCloud;

                    DoubleMatrixAssert.AreEqual(
                        expected: Basis.Standard(2)
                        .GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: data,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: DoubleMatrix.Dense(4, 1,
                                                     1.0 / 4.0),
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);

                    data[0, 0] = Double.PositiveInfinity;

                    Assert.AreNotEqual(
                        notExpected: data[0, 0],
                        actual: cloud.Coordinates[0, 0],
                        delta: CloudTest.Accuracy);
                }
            }
        }
Ejemplo n.º 24
0
        public void RunTest()
        {
            // Valid input - Minimization
            {
                var optimizer = new SystemPerformanceOptimizer();

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

                var context = testableContext.Context;

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

                // 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
            {
                var optimizer = new SystemPerformanceOptimizer();

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

                var context = testableContext.Context;

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

                // 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);
            }
        }
Ejemplo n.º 25
0
        public void ConstructorTest()
        {
            // coordinates is null
            {
                string parameterName = "coordinates";

                DoubleMatrix coordinates = null;
                DoubleMatrix weights     = DoubleMatrix.Dense(1, 1, 1.0 / 4.0);
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = null;
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);
                Basis        basis       = null;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // weights is not a column vector
            {
                var STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR");

                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(1, 4);
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_COLUMN_VECTOR,
                    expectedParameterName: parameterName);
            }

            // weights must have the number of rows of coordinates
            {
                var STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS =
                    string.Format(CultureInfo.InvariantCulture,
                                  ImplementationServices.GetResourceString(
                                      "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS"),
                                  "coordinates");

                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(5, 1);
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);
            }

            // weights must have non negative entries
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE");

                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, -1.0);
                Basis        basis       = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_BE_NON_NEGATIVE,
                    expectedParameterName: parameterName);
            }

            // weights must have entries summing up to 1
            {
                var STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1 =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1");

                string parameterName = "weights";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1,
                                                              new double[4] {
                    .3, .6, .2, .1
                });
                Basis basis = Basis.Standard(2);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: parameterName);
            }

            // basis must have dimension equal to the coordinates number of columns
            {
                var STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS =
                    string.Format(CultureInfo.InvariantCulture,
                                  ImplementationServices.GetResourceString(
                                      "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS"),
                                  "coordinates");

                string parameterName = "basis";

                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);
                Basis        basis       = Basis.Standard(5);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new Cloud(
                        coordinates,
                        weights,
                        basis);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: parameterName);
            }

            // Valid input and copyData is true
            {
                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);
                Basis        basis       = Basis.Standard(2);

                {
                    var cloud = new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: true);

                    DoubleMatrixAssert.AreEqual(
                        expected: basis.GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: coordinates,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: weights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);

                    coordinates[0, 0] = Double.PositiveInfinity;

                    Assert.AreNotEqual(
                        notExpected: coordinates[0, 0],
                        actual: cloud.Coordinates[0, 0],
                        delta: CloudTest.Accuracy);
                }

                {
                    var cloud = new Cloud(
                        coordinates,
                        weights);

                    DoubleMatrixAssert.AreEqual(
                        expected: basis.GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: coordinates,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: weights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);
                }

                {
                    var cloud = new Cloud(
                        coordinates);

                    DoubleMatrixAssert.AreEqual(
                        expected: basis.GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: coordinates,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: weights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);
                }
            }

            // Valid input and copyData is false
            {
                DoubleMatrix coordinates = DoubleMatrix.Dense(4, 2);
                DoubleMatrix weights     = DoubleMatrix.Dense(4, 1, 1.0 / 4.0);
                Basis        basis       = Basis.Standard(2);

                {
                    var cloud = new Cloud(
                        coordinates,
                        weights,
                        basis,
                        copyData: false);

                    DoubleMatrixAssert.AreEqual(
                        expected: basis.GetBasisMatrix(),
                        actual: cloud.Basis.GetBasisMatrix(),
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: coordinates,
                        actual: cloud.Coordinates,
                        delta: CloudTest.Accuracy);

                    DoubleMatrixAssert.AreEqual(
                        expected: weights,
                        actual: cloud.Weights,
                        delta: CloudTest.Accuracy);

                    coordinates[0, 0] = Double.PositiveInfinity;

                    Assert.AreEqual(
                        expected: coordinates[0, 0],
                        actual: cloud.Coordinates[0, 0],
                        delta: CloudTest.Accuracy);
                }
            }
        }
        public void ParametricMinimizeTest()
        {
            // objectiveFunction is null
            {
                string parameterName = "objectiveFunction";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    ContinuousOptimization.Minimize(
                        objectiveFunction: (Func <DoubleMatrix, DoubleMatrix, double>)null,
                        initialArgument: DoubleMatrix.Dense(1, 1, -6.0),
                        functionParameter: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

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

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    ContinuousOptimization.Minimize <DoubleMatrix>(
                        objectiveFunction: (x, p) => Stat.Mean(x),
                        initialArgument: null,
                        functionParameter: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // initialArgument is not a row vector
            {
                var STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR");

                string parameterName = "initialArgument";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    ContinuousOptimization.Minimize <DoubleMatrix>(
                        objectiveFunction: (x, p) => Stat.Mean(x),
                        initialArgument: DoubleMatrix.Dense(2, 1, -6.0),
                        functionParameter: null);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_ROW_VECTOR,
                    expectedParameterName: parameterName);
            }

            // Valid input
            {
                var testableContext =
                    TestableContinuousOptimizationContext00
                    .Get();

                double objectiveFunction(DoubleMatrix argument, double parameter)
                {
                    return(parameter * testableContext.Context.Performance(argument));
                };

                var actual = ContinuousOptimization.Minimize(
                    objectiveFunction: objectiveFunction,
                    initialArgument: DoubleMatrix.Dense(1, 1, -3.0),
                    functionParameter: 2.0);

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

            // Valid input
            {
                int numberOfArguments = 10;

                var testableContext =
                    TestableContinuousOptimizationContext03
                    .Get(numberOfArguments);

                double objectiveFunction(DoubleMatrix argument, double parameter)
                {
                    return(parameter * testableContext.Context.Performance(argument));
                };

                var actual = ContinuousOptimization.Minimize(
                    objectiveFunction: objectiveFunction,
                    initialArgument: DoubleMatrix.Dense(1, numberOfArguments, 5.0),
                    functionParameter: 2.0);

                DoubleMatrixAssert.AreEqual(
                    expected: testableContext.OptimalState,
                    actual: actual,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 27
0
        public void GetVariancesTest()
        {
            // supplementaryVariables is null - DoubleMatrix
            {
                string parameterName = "supplementaryVariables";

                var cloud = TestableCloud00.Get().Cloud;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    cloud.GetVariances((DoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // supplementaryVariables is null - ReadOnlyDoubleMatrix
            {
                string parameterName = "supplementaryVariables";

                var cloud = TestableCloud00.Get().Cloud;

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    cloud.GetVariances((ReadOnlyDoubleMatrix)null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // supplementaryVariables must have as number of rows the number of cloud points
            {
                var STR_EXCEPT_PAR_ROW_DIM_MUST_MATCH_INDIVIDUALS_COUNT =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ROW_DIM_MUST_MATCH_INDIVIDUALS_COUNT");

                string parameterName = "supplementaryVariables";

                var cloud = TestableCloud00.Get().Cloud;

                var supplementaryVariables =
                    DoubleMatrix.Dense(
                        cloud.Coordinates.NumberOfRows + 1,
                        10);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    cloud.GetVariances(supplementaryVariables);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_ROW_DIM_MUST_MATCH_INDIVIDUALS_COUNT,
                    expectedParameterName: parameterName);
            }

            // Valid input - DoubleMatrix
            {
                var cloud = TestableCloud00.Get().Cloud;

                var actual = cloud.GetVariances((DoubleMatrix)cloud.Coordinates);

                var cov_sa   = cloud.Covariance;
                var expected = DoubleMatrix.Dense(1, cloud.Coordinates.NumberOfColumns);
                for (int i = 0; i < expected.Count; i++)
                {
                    expected[i] = cov_sa[i, i];
                }

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: CloudTest.Accuracy);
            }

            // Valid input - ReadOnlyDoubleMatrix
            {
                var cloud = TestableCloud00.Get().Cloud;

                var actual = cloud.GetVariances(cloud.Coordinates);

                var cov_sa   = cloud.Covariance;
                var expected = DoubleMatrix.Dense(1, cloud.Coordinates.NumberOfColumns);
                for (int i = 0; i < expected.Count; i++)
                {
                    expected[i] = cov_sa[i, i];
                }

                DoubleMatrixAssert.AreEqual(
                    expected: expected,
                    actual: actual,
                    delta: CloudTest.Accuracy);
            }
        }
        public void ConstructorTest()
        {
            // 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 PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: (OptimizationGoal)(-1),
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_NOT_FIELD_OF_OPTIMIZATION_GOAL,
                    expectedParameterName: parameterName);
            }

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

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: null,
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // stateDimension is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "stateDimension";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 0,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // stateDimension is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "stateDimension";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: -1,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // partitionDimension is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE"),
                        "1");

                string parameterName = "partitionDimension";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 0,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE,
                    expectedParameterName: parameterName);
            }

            // partitionDimension is one
            {
                var STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE"),
                        "1");

                string parameterName = "partitionDimension";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 1,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE,
                    expectedParameterName: parameterName);
            }

            // partitionDimension is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE"),
                        "1");

                string parameterName = "partitionDimension";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: -1,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE,
                    expectedParameterName: parameterName);
            }

            // partitionDimension is equal to stateDimension
            {
                var STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER"),
                        "partitionDimension",
                        "stateDimension");

                string parameterName = "partitionDimension";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 7,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER,
                    expectedParameterName: parameterName);
            }

            // partitionDimension is greater than stateDimension
            {
                var STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER"),
                        "partitionDimension",
                        "stateDimension");

                string parameterName = "partitionDimension";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 8,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 0,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "minimumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: -1,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

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

                string parameterName = "maximumNumberOfIterations";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: -1);
                },
                    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 PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .8,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 2);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_OTHER,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is zero
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: .0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is negative
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: -.1,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is one
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: 1.0,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // probabilitySmoothingCoefficient is greater than one
            {
                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 = "probabilitySmoothingCoefficient";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    new PartitionOptimizationContext(
                        objectiveFunction: (x) => Stat.Mean(x),
                        stateDimension: 7,
                        partitionDimension: 2,
                        probabilitySmoothingCoefficient: 1.1,
                        optimizationGoal: OptimizationGoal.Minimization,
                        minimumNumberOfIterations: 3,
                        maximumNumberOfIterations: 1000);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_NOT_IN_OPEN_INTERVAL,
                    expectedParameterName: parameterName);
            }

            // valid input - LowerThanLevel
            {
                var testableContext =
                    TestablePartitionOptimizationContext00.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);

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

                Assert.AreEqual(
                    expected: testableContext.ProbabilitySmoothingCoefficient,
                    actual: context.ProbabilitySmoothingCoefficient,
                    delta: DoubleMatrixTest.Accuracy);
            }
        }
Ejemplo n.º 29
0
        public void SetMassesTest()
        {
            // Valid input
            {
                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .1, .2, .3, .2, .1, .1
                });

                distribution.SetMasses(masses: masses);

                DoubleMatrixAssert.AreEqual(
                    expected: masses,
                    actual: (DoubleMatrix)distribution.Masses,
                    delta: DoubleMatrixTest.Accuracy);
            }

            // masses is null
            {
                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: null);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "masses");
            }

            // The number of rows in masses is not equal to that of values
            {
                string STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS"),
                        "values");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(1, 2,
                                                new double[2] {
                    .5, .5
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_ROWS,
                    expectedParameterName: "masses");
            }

            // The number of columns in masses is not equal to that of values
            {
                string STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS"),
                        "values");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 1,
                                                new double[3] {
                    .2, .5, .3
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: STR_EXCEPT_PAR_MUST_HAVE_SAME_NUM_OF_COLUMNS,
                    expectedParameterName: "masses");
            }

            // At least an entry in masses is negative
            {
                string STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL"),
                        "0", "1");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .2, .5, .3, -.1, .1, 0
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_ENTRIES_NOT_IN_CLOSED_INTERVAL,
                    expectedParameterName: "masses");
            }

            // The sum of the masses is not 1
            {
                string STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1 =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1");

                var values = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    1, 2, 3, 4, 5, 6
                });

                var distribution = FiniteDiscreteDistribution.Uniform(
                    values: values);

                var masses = DoubleMatrix.Dense(3, 2,
                                                new double[6] {
                    .2, .5, .3, .1, .1, 0
                });

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    distribution.SetMasses(masses: masses);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage: STR_EXCEPT_PAR_ENTRIES_MUST_SUM_TO_1,
                    expectedParameterName: "masses");
            }
        }
Ejemplo n.º 30
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);
            }
        }