accord-net / framework

Machine learning, computer vision, statistics and general scientific computing for .NET
http://accord-framework.net
GNU Lesser General Public License v2.1
4.48k stars 1.99k forks source link

Add an Example for NonlinearConstraint Constructor (IObjectiveFunction, Expression(Func(Double[], Boolean)), Func(Double[], Double[])) #2222

Open mahui-cn opened 3 years ago

mahui-cn commented 3 years ago

hi,

When I call new NonlinearConstraint like below:

   var cc = new NonlinearConstraint(objFun, initialGuess => initialGuess.All(x => x <= 1));

Complier throw an error like: System.NullReferenceException

I check the source code of constructor of NonlinearConstraint class:

  public NonlinearConstraint(IObjectiveFunction objective,
            Expression<Func<double[], bool>> constraint,
            Func<double[], double[]> gradient = null)
        {
            Func<double[], double> function;
            ConstraintType shouldBe;
            double value;

            parse(constraint, out function, out shouldBe, out value);

            this.Create(objective.NumberOfVariables, function, shouldBe, value, gradient, DEFAULT_TOL);
        }

the constraint which I put in should return a result of boolean, but why the constructor call parse to get value of double type?

Please add an example for NonlinearConstraint Constructor (IObjectiveFunction, Expression(Func(Double[], Boolean)), Func(Double[], Double[])).

TODO (optional): Describe a specific scenario you would like to see addressed.

Help Topic: http://accord-framework.net/docs/html/M_Accord_Math_Optimization_NonlinearConstraint__ctor_3.htm