public void RunTest()
        {
            // Valid input - Minimization
            {
                var optimizer = new SystemPerformanceOptimizer();

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

                var context = testableContext.Context;

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

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

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

                var expectedPartition = IndexPartition.Create(
                    testableContext.OptimalState);

                var actualPartition = IndexPartition.Create(
                    results.OptimalState);

                IndexPartitionAssert.HaveEqualIdentifiers(
                    expected: expectedPartition,
                    actual: actualPartition);

                IndexPartitionAssert.HaveEqualParts(
                    expected: expectedPartition,
                    actual: actualPartition);

                Assert.AreEqual(
                    expected: testableContext.OptimalPerformance,
                    actual: results.OptimalPerformance,
                    DoubleMatrixTest.Accuracy);
            }
        }
Example #2
0
        public void DiscoverTest()
        {
            // data is null
            {
                string parameterName = "data";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Discover(
                        data: null,
                        maximumNumberOfParts: 2);
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // maximumNumberOfParts 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 = "maximumNumberOfParts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Discover(
                        data: DoubleMatrix.Dense(10, 5),
                        maximumNumberOfParts: 1);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE,
                    expectedParameterName: parameterName);
            }

            // maximumNumberOfParts 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 = "maximumNumberOfParts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Discover(
                        data: DoubleMatrix.Dense(10, 5),
                        maximumNumberOfParts: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE,
                    expectedParameterName: parameterName);
            }

            // maximumNumberOfParts 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 = "maximumNumberOfParts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Discover(
                        data: DoubleMatrix.Dense(10, 5),
                        maximumNumberOfParts: -1);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_GREATER_THAN_VALUE,
                    expectedParameterName: parameterName);
            }

            // maximumNumberOfParts is equal to the number of rows in data
            {
                var STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_ROWS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_ROWS"),
                        "maximumNumberOfParts",
                        "data");

                string parameterName = "maximumNumberOfParts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Discover(
                        data: DoubleMatrix.Dense(10, 5),
                        maximumNumberOfParts: 10);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_ROWS,
                    expectedParameterName: parameterName);
            }

            // maximumNumberOfParts is greater than the number of rows in data
            {
                var STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_ROWS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_ROWS"),
                        "maximumNumberOfParts",
                        "data");

                string parameterName = "maximumNumberOfParts";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Discover(
                        data: DoubleMatrix.Dense(10, 5),
                        maximumNumberOfParts: 11);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_ROWS,
                    expectedParameterName: parameterName);
            }

            // Valid input
            {
                const int numberOfItems    = 12;
                const int numberOfFeatures = 7;

                var source = DoubleMatrix.Dense(numberOfItems, 1,
                                                new double[numberOfItems]
                {
                    0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
                });

                var expectedPartition = IndexPartition.Create(source);
                var data = DoubleMatrix.Dense(numberOfItems, numberOfFeatures);

                double mu = 1.0;

                var partIdentifiers = expectedPartition.Identifiers;

                for (int i = 0; i < partIdentifiers.Count; i++)
                {
                    var part     = expectedPartition[partIdentifiers[i]];
                    int partSize = part.Count;
                    for (int j = 0; j < partSize; j++)
                    {
                        data[part[j], ":"] += mu;
                    }
                    mu += 5.0;
                }

                var actualPartition = Clusters.Discover(
                    data: data,
                    maximumNumberOfParts: 3);

                IndexPartitionAssert.HaveEqualIdentifiers(
                    expected: expectedPartition,
                    actual: actualPartition);

                IndexPartitionAssert.HaveEqualParts(
                    expected: expectedPartition,
                    actual: actualPartition);
            }
        }