Ejemplo n.º 1
0
        /// <summary>
        ///     Creates a new objective function specified through a lambda expression.
        /// </summary>
        /// <param name="function">
        ///     A <see cref="Expression{TDelegate}" /> containing
        ///     the function in the form of a lambda expression.
        /// </param>
        /// <param name="gradient">
        ///     A <see cref="Expression{T}" /> containing
        ///     the gradient of the <paramref name="function">objective function</paramref>.
        /// </param>
        public NonlinearObjectiveFunction(Expression <Func <double> > function, Expression <Func <double[]> > gradient = null)
            : this()
        {
            var list = new SortedSet <string>();

            ExpressionParser.Parse(list, function.Body);

            var index = 0;

            foreach (var variable in list)
            {
                InnerVariables.Add(variable, index);
                InnerIndices.Add(index, variable);
                index++;
            }

            NumberOfVariables = index;

            // Generate lambda functions
            var func = ExpressionParser.Replace(function, InnerVariables);
            var grad = ExpressionParser.Replace(gradient, InnerVariables);

            Function = func.Compile();
            Gradient = grad.Compile();
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Creates a new objective function specified through a string.
        /// </summary>
        /// <param name="numberOfVariables">The number of parameters in the <paramref name="function" />.</param>
        /// <param name="function">
        ///     A lambda expression defining the objective
        ///     function.
        /// </param>
        public NonlinearObjectiveFunction(int numberOfVariables, Func <double[], double> function)
            : this()
        {
            NumberOfVariables = numberOfVariables;
            Function          = function;

            for (var i = 0; i < numberOfVariables; i++)
            {
                var name = "x" + i;
                InnerVariables.Add(name, i);
                InnerIndices.Add(i, name);
            }
        }
Ejemplo n.º 3
0
        private void initialize(Dictionary <Tuple <string, string>, double> terms)
        {
            linear    = new Dictionary <string, double>();
            quadratic = new Dictionary <Tuple <string, string>, double>();

            var list = new SortedSet <string>();

            foreach (var term in terms)
            {
                if (term.Key.Item2 != null)
                {
                    list.Add(term.Key.Item1);
                    list.Add(term.Key.Item2);

                    quadratic.Add(term.Key, term.Value);
                }
                else if (term.Key.Item1 != null)
                {
                    list.Add(term.Key.Item1);

                    linear.Add(term.Key.Item1, term.Value);
                }
                else
                {
                    c = term.Value;
                }
            }

            int i = 0;

            foreach (var variable in list)
            {
                InnerVariables.Add(variable, i);
                InnerIndices.Add(i, variable);
                i++;
            }

            NumberOfVariables = Variables.Count;
            this.Q            = createQuadraticTermsMatrix();
            this.d            = createLinearTermsVector();

            this.Function = function;
            this.Gradient = gradient;
        }
Ejemplo n.º 4
0
        private void initialize(Dictionary <Tuple <string, string>, double> terms)
        {
            var linear    = new Dictionary <string, double>();
            var quadratic = new Dictionary <Tuple <string, string>, double>();

            var list = new SortedSet <string>();

            foreach (var term in terms)
            {
                if (term.Key.Item2 is not null)
                {
                    list.Add(term.Key.Item1);
                    list.Add(term.Key.Item2);

                    quadratic.Add(term.Key, term.Value);
                }
                else if (term.Key.Item1 is not null)
                {
                    list.Add(term.Key.Item1);

                    linear.Add(term.Key.Item1, term.Value);
                }
                else
                {
                    ConstantTerm = term.Value;
                }
            }

            var i = 0;

            foreach (var variable in list)
            {
                InnerVariables.Add(variable, i);
                InnerIndices.Add(i, variable);
                i++;
            }

            NumberOfVariables = Variables.Count;
            QuadraticTerms    = createQuadraticTermsMatrix(quadratic);
            LinearTerms       = createLinearTermsVector(linear);

            Function = function;
            Gradient = gradient;
        }