Replace() public static method

public static Replace ( System.Linq.Expressions.ParameterExpression parameter, Expression expr, int>.IDictionary variables ) : Expression
parameter System.Linq.Expressions.ParameterExpression
expr System.Linq.Expressions.Expression
variables int>.IDictionary
return System.Linq.Expressions.Expression
Beispiel #1
0
        /// <summary>
        ///   Creates a new objective function specified through a string.
        /// </summary>
        ///
        /// <param name="function">A <see cref="Expression{T}"/> containing
        ///   the function in the form of a lambda expression.</param>
        /// <param name="gradient">A <see cref="Expression{T}"/> containing
        ///   the the gradient of the <paramref name="function">objective function</paramref>.</param>
        ///
        public NonlinearObjectiveFunction(
            Expression <Func <double> > function,
            Expression <Func <double[]> > gradient = null)
        {
            variables = new Dictionary <string, int>();
            indices   = new Dictionary <int, string>();

            var list = new SortedSet <string>();

            ExpressionParser.Parse(list, function.Body);

            int index = 0;

            foreach (string variable in list)
            {
                variables.Add(variable, index);
                indices.Add(index, variable);
                index++;
            }

            NumberOfVariables = index;

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

            this.Function = func.Compile();
            this.Gradient = grad.Compile();
        }
Beispiel #2
0
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        ///
        /// <param name="objective">The objective function to which this constraint refers.</param>
        ///
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        /// left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        /// <param name="withinTolerance">The tolerance for violations of the constraint. Equality
        ///   constraints should set this to a small positive value. Default is 0.</param>
        ///
        ///
        public NonlinearConstraint(IObjectiveFunction objective,
                                   Expression <Func <double> > function, ConstraintType shouldBe, double value,
                                   Expression <Func <double[]> > gradient = null, double withinTolerance = 0.0)
        {
            this.NumberOfVariables = objective.NumberOfVariables;
            this.ShouldBe          = shouldBe;

            // Generate lambda functions
            var func = ExpressionParser.Replace(function, objective.Variables);

            this.Function  = func.Compile();
            this.Value     = value;
            this.Tolerance = withinTolerance;

            if (gradient != null)
            {
                var grad = ExpressionParser.Replace(gradient, objective.Variables);
                this.Gradient = grad.Compile();

                int      n     = NumberOfVariables;
                double[] probe = new double[n];
                double[] g     = Gradient(probe);
                if (g.Length != n)
                {
                    throw new DimensionMismatchException("gradient",
                                                         "The length of the gradient vector must match the number of variables in the objective function.");
                }
            }
        }
        /// <summary>
        ///   Constructs a new nonlinear constraint.
        /// </summary>
        ///
        /// <param name="objective">The objective function to which this constraint refers.</param>
        ///
        /// <param name="function">A lambda expression defining the left hand side of the constraint equation.</param>
        /// <param name="gradient">A lambda expression defining the gradient of the <paramref name="function">
        /// left hand side of the constraint equation</paramref>.</param>
        /// <param name="shouldBe">How the left hand side of the constraint should be compared to the given <paramref name="value"/>.</param>
        /// <param name="value">The right hand side of the constraint equation.</param>
        ///
        ///
        public NonlinearConstraint(IObjectiveFunction objective,
                                   Expression <Func <double> > function,
                                   ConstraintType shouldBe, double value,
                                   Expression <Func <double[]> > gradient = null)
        {
            this.NumberOfVariables = objective.NumberOfVariables;
            this.ShouldBe          = shouldBe;

            // Generate lambda functions
            var func = ExpressionParser.Replace(function, objective.Variables);

            this.Function = func.Compile();
            this.Value    = value;

            if (gradient != null)
            {
                var grad = ExpressionParser.Replace(gradient, objective.Variables);
                this.Gradient = grad.Compile();
            }
        }