Matlab - 当客观和非线性约束需要参数时正确设置 fmincon

发布于 2024-11-01 01:51:32 字数 1781 浏览 0 评论 0原文

我正在尝试遵循此处给出的建议

http://www. mathworks.com/help/toolbox/optim/ug/brhkghv-7.html

但我遇到了麻烦。我有一个复杂的目标,要最大限度地减少复杂非线性约束的影响,所有这些约束都是我选择的 3 个变量和各种参数的函数。我启动一切的脚本如下所示:

% define a bunch of parameters
gamma= .2;
beta = .3; %
x0=[.5 .5 .5];
%etc
solution = nested_minimization_program(x0,gamma,beta)

Nested_minimization_program 如下所示:

function out = nest_minimization-program(x0,gamma,beta)
options = optimset('GradObj','on');
out = fmincon(@objective,x0,[],[],[],[],[0 0 0],[1 1 1],@nonlin,options)
 function [obj obj_gradient] = objective(x)
   [obj obj_gradient] = complicated_objective(x,gamma,beta);
 end
 function [ineq_constriant eq_constraint] = nonlin(x)
  [ineq_constriant eq_constraint] = complicated_constaints(x,beta,gamma)
 end
end

Complicated_objective 是一个文件,返回其第一个参数的目标值以及第二个参数的分析梯度值。 Complicated_constaints 返回其第一个参数的非线性不等式约束向量,以及第二个参数的非线性等式约束向量。

这样做的原因是我可以对 fmincon 使用 @objective 和 @nonlin 语法; Objective 和 nonlin 只是 x 的函数,而不是参数的函数,因为它们是已传递参数的函数的子函数。我相信这是我应该使用的形式,以便将梯度和非线性约束传递给 fmincon。我的问题是,当我运行此代码时,出现以下错误

警告:信任区域反射算法不能解决此类问题, 使用活动集算法。您也可以尝试内点或 sqp 算法:将算法选项设置为“interior-point”或“sqp”并重新运行。为了 更多帮助,请参阅文档中的选择算法。

IE,出于某种原因,fmincon 正在离开信任区域反射算法并转到活动集,这不利用我的分析梯度。 fmincon 使用解析梯度的要求是,根据 http:// /www.mathworks.com/help/toolbox/optim/ug/brhkghv-3.html

编写返回的代码: 目标函数(标量)作为第一个输出

梯度(向量)作为第二个输出

使用 optimset 将 GradObj 选项设置为“on”。

Objective 返回目标的标量值和所需的梯度,Gradobj 已打开,所以我没有看到我的问题。

I am trying to follow the advice given here

http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-7.html

but I am having trouble. I have a complicated objective to minimize subject to complicated nonlinear constraints, all of which are functions of my 3 choice variables, and a wide variety of parameters. My script that starts everything looks like this:

% define a bunch of parameters
gamma= .2;
beta = .3; %
x0=[.5 .5 .5];
%etc
solution = nested_minimization_program(x0,gamma,beta)

Nested_minimization_program looks like this:

function out = nest_minimization-program(x0,gamma,beta)
options = optimset('GradObj','on');
out = fmincon(@objective,x0,[],[],[],[],[0 0 0],[1 1 1],@nonlin,options)
 function [obj obj_gradient] = objective(x)
   [obj obj_gradient] = complicated_objective(x,gamma,beta);
 end
 function [ineq_constriant eq_constraint] = nonlin(x)
  [ineq_constriant eq_constraint] = complicated_constaints(x,beta,gamma)
 end
end

Complicated_objective is a file that returns the value of the objective for its first argument, and the value of anlaytical gradient for its second. Complicated_constaints returns a vector of nonlinear inequality constraints for its first argument, and a vector of nonlinear equality constraints for its second.

The reason to do this is so that I can use the @objective and @nonlin syntax for fmincon; objective and nonlin are only functions of x, not of the parameters, because they are subfunctions of a function that has been passed the parameters already. I believe this is the form I should use in order to pass the gradient and the nonlinear constraints on to fmincon. My problem is that when I run this code, I get the following error

Warning: Trust-region-reflective algorithm does not solve this type of problem,
using active-set algorithm. You could also try the interior-point or sqp
algorithms: set the Algorithm option to 'interior-point' or 'sqp' and rerun. For
more help, see Choosing the Algorithm in the documentation.

IE, for some reason fmincon is leaving the Trust-region-reflective algorithm and going to active set, which does not make use of my analytical gradient. The requirements for fmincon to use analytical gradients is, according to http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-3.html,

Write code that returns:
The objective function (scalar) as the first output

The gradient (vector) as the second output

Set the GradObj option to 'on' with optimset.

objective returns a scalar value of the objective and a gradient as required, Gradobj is turned on, so I don't see my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房的律动 2024-11-08 01:51:32

我从 Matlab Usenet 小组得到了一些帮助,并向我透露信任区域方法实际上并不支持非线性约束,因此这根本不是我的代码中的错误。他们建议重写使用“内点”算法,该算法有其自身的问题,但至少这个问题已经解决了。

I got some help from the Matlab Usenet group, and it was revealed to me that the trust-region methods don't actually support non-linear constraints, so it is not an error in my code at all. They reccomend rewriting to use the 'interior-point' algorithm, which has its own issues, but at least this problem has been solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文