Exemplo n.º 1
0
        public void QuadraticConstraintConstructorTest()
        {
            IObjectiveFunction objective = null;

            double[,] quadraticTerms =
            {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 },
            };

            double[] linearTerms = { 1, 2, 3 };

            objective = new NonlinearObjectiveFunction(3, f => f[0] + f[1] + f[2]);

            QuadraticConstraint target = new QuadraticConstraint(objective,
                                                                 quadraticTerms, linearTerms,
                                                                 ConstraintType.LesserThanOrEqualTo, 0);

            var function = target.Function;
            var gradient = target.Gradient;

            FiniteDifferences fd = new FiniteDifferences(3, function);

            double[][] x =
            {
                new double[] {      1,  2,   3 },
                new double[] {      3,  1,   4 },
                new double[] {     -6,  5,   9 },
                new double[] {     31, 25, 246 },
                new double[] { -0.102,  0,  10 },
            };


            { // Function test
                for (int i = 0; i < x.Length; i++)
                {
                    double expected =
                        (x[i].Multiply(quadraticTerms)).InnerProduct(x[i])
                        + linearTerms.InnerProduct(x[i]);

                    double actual = function(x[i]);

                    Assert.AreEqual(expected, actual, 1e-8);
                }
            }

            { // Gradient test
                for (int i = 0; i < x.Length; i++)
                {
                    double[] expected = fd.Compute(x[i]);
                    double[] actual   = gradient(x[i]);

                    for (int j = 0; j < actual.Length; j++)
                    {
                        Assert.AreEqual(expected[j], actual[j], 1e-8);
                    }
                }
            }
        }
        public BallnBenchmark(ExperimentParameters experimentParameters, ITermsFactory termsFactory)
        {
            var numberOfDimensions = experimentParameters.NumberOfDimensions;
            var ballnBoundaryValue = experimentParameters.BallnBoundaryValue;
            var numberOfTerms      = numberOfDimensions * 2;
            var terms         = new Term[numberOfTerms];
            var limitingValue = ballnBoundaryValue * ballnBoundaryValue;

            Constraints = new Constraint[1];
            Domains     = new Domain[numberOfDimensions];

            for (var i = 0; i < numberOfDimensions; i++)
            {
                var value = i + 1;
                terms[i] = termsFactory.Create((int)TermType.Quadratic, 1);

                Domains[i] = new Domain(value - 2 * ballnBoundaryValue, value + 2 * ballnBoundaryValue);

                //HACK TODO
                //Domains[i] = new Domain(value - 2 * ballnBoundaryValue + 100, value + 2 * ballnBoundaryValue + 100);
                //

                limitingValue -= value * value;
            }

            for (var i = numberOfDimensions; i < numberOfTerms; i++)
            {
                var value = i + 1 - numberOfDimensions;
                terms[i] = termsFactory.Create((int)TermType.Linear, -2 * value);
            }

            Constraints[0] = new QuadraticConstraint(terms, limitingValue);
        }
Exemplo n.º 3
0
        public ConstraintsBuilder(Constraint[] referenceConstraints, ExperimentParameters experimentParameters)
        {
            var singleReferenceConstraint = referenceConstraints.First();

            _constraintsModel = new Constraint[experimentParameters.MaximumNumberOfConstraints];

            if (experimentParameters.TypeOfBenchmark == BenchmarkType.Balln && experimentParameters.AllowQuadraticTerms)
            {
                for (var i = 0; i < _constraintsModel.Length; i++)
                {
                    _constraintsModel[i] = new QuadraticConstraint(singleReferenceConstraint.Terms.DeepCopyByExpressionTree(), 0);
                }
            }
            else
            {
                var terms = singleReferenceConstraint.Terms.Where(term => term.Type == TermType.Linear).ToArray();

                for (var i = 0; i < _constraintsModel.Length; i++)
                {
                    _constraintsModel[i] = new LinearConstraint(terms.DeepCopyByExpressionTree(), 0);
                }
            }
        }