Jaye Foster
Jaye Foster

Reputation: 13

fgoalattain and why it's failing at this simple function

I'm looking at using fgoalattain to help me generate fragments of smashed rock based on target distributions. Before setting the function to work on that, I thought it prudent to test it on x^2.

Here's the code

 fun = @(x) x^2;
 goal = [16];
 weight = [1];
 x0 = 0;
 [x,fval,attainfactor,exitflag,output] = fgoalattain(fun,x0,goal,weight)

I expect the answer to be 4

matlab R2017b instead returns the following


Local minimum possible. Constraints satisfied.

fgoalattain stopped because the size of the current search direction is less than
twice the default value of the step size tolerance and constraints are 
satisfied to within the default value of the constraint tolerance.

<stopping criteria details>


x =

     0


fval =

     0


attainfactor =

   -16


exitflag =

     4


output = 

  struct with fields:

         iterations: 2
          funcCount: 7
       lssteplength: 1
           stepsize: 0
          algorithm: 'active-set'
      firstorderopt: []
    constrviolation: 0
            message: 'Local minimum possible. Constraints satisfied.↵↵fgoalattain stopped because the size of the current search direction is less than↵twice the default value of the step size tolerance and constraints are ↵satisfied to within the default value of the constraint tolerance.↵↵Stopping criteria details:↵↵Optimization stopped because the norm of the current search direction, 0.000000e+00,↵is less than 2*options.StepTolerance = 1.000000e-06, and the maximum constraint ↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-06.↵↵Optimization Metric                                              Options↵norm(search direction) =   0.00e+00                        StepTolerance =   1e-06 (default)↵max(constraint violation)  =   0.00e+00              ConstraintTolerance =   1e-06 (default)'

This is clearly wrong and I have no idea why. Please help.

Upvotes: 1

Views: 544

Answers (1)

mcfenelon
mcfenelon

Reputation: 106

The negative value of attainfactor means the goal has been overachieved. If you want the goal to be achieved exactly, use the EqualityGoalCount option, like this:

o = optimoptions('fgoalattain','EqualityGoalCount',1);
[x,fval,attainfactor,exitflag,output] = fgoalattain(fun,x0,goal,weight,[],[],[],[],[],[],[],o)

One more change is needed. The fgoalattain algorithm uses gradients to find the search direction. It can't move from the starting point of 0 because its gradient is 0; 0 is the minimum of the function x^2. If you set a non-zero initial point, say 1, then the goal is achieved exactly with x = 4.

Local minimum possible. Constraints satisfied.

fgoalattain stopped because the size of the current search direction is less than
twice the value of the step size tolerance and constraints are 
satisfied to within the value of the constraint tolerance.

<stopping criteria details>

x =

    4.0000


fval =

   16.0000


attainfactor =

  -1.6941e-21


exitflag =

     4

Upvotes: 1

Related Questions