fgoalattain以及为什么它在此简单功能上失败

时间:2019-08-21 03:18:34

标签: matlab optimization

我正在研究使用fgoalattain帮助我根据目标分布生成碎石碎片。在设置该功能之前,我认为最好在x ^ 2上对其进行测试。

这是代码

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

我希望答案是4

matlab R2017b会返回以下内容


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)'

这显然是错误的,我也不知道为什么。请帮忙。

1 个答案:

答案 0 :(得分:0)

negative value of attainfactor表示目标已实现。如果您希望准确实现目标,请使用EqualityGoalCount选项,如下所示:

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

还需要一个更改。 fgoalattain算法使用梯度来查找搜索方向。它的梯度为0,因此无法从0的起点移动; 0是函数x ^ 2的最小值。如果您将初始点设置为非零,例如1,则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