解决受约束的非线性优化,基于问题的优化
此示例显示了如何通过使用基于问题的方法找到具有非线性约束的非线性目标函数的最小值。有关显示类似问题解决方案的视频,请参阅Problem-Based Nonlinear Programming。
要使用基于问题的方法找到非线性目标函数的最小值,请首先将目标函数写为文件或匿名函数。此示例的目标函数是
类型objfunx
Function f = objfunx(x,y) f = exp(x).*(4*x.^2 + 2*y.^2 + 4*x.*y + 2*y - 1); end
创建优化问题变量X
andy
。
x = optimvar('X');y = optimvar('y');
在优化变量中创建目标函数作为表达式。
obj = objfunx(x,y);
与OBJ
作为目标函数。
prob = optimproblem('客观的',obj);
创建一个非线性约束,该解决方案位于倾斜椭圆中,指定为
在优化变量中创建约束作为不等式表达式。
tiltellipse = x。*y/2 +(x + 2)。^2 +(y-2)。^2/2 <= 2;
在问题中包括约束。
prob.constraints.constr = tiltellipse;
创建一个表示初始点的结构x = –3
,,,,y = 3
。
x0.x = -3;x0.y = 3;
Review the problem.
显示(概率)
优化问题:求解:x,y最小化:( exp(x)。* y)) +(2。* y)) - 1))约束:((((((x。* y)./ 2)) +(x + 2)。^2) +((y -2)。^2 ./ 2))<= 2
Solve the problem.
[sol,fval] = solve(prob,x0)
使用Fmincon解决问题。当地最低限度发现满足约束。之所以完成优化,是因为目标函数在可行的方向,最优性公差值之内不折叠,并且在约束公差的值之内满足了约束。
sol =带有字段的结构:X: -5.2813 y: 4.6815
Fval = 0.3299
尝试不同的起点。
x0.x = -1;x0.y = 1;[sol2,fval2] = solve(prob,x0)
使用Fmincon解决问题。Feasible point with lower objective function value found. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol2 =带有字段的结构:X:-0.8210 y:0.6696
FVAL2 = 0.7626
Plot the ellipse, the objective function contours, and the two solutions.
f = @objfunx;g = @(x,y)x。*y/2+(x+2)。^2+(y-2)。^2/2-2;rnge = [-5.5 -0.25 -0.25 7];fimplicit(g,'k-')轴(rnge);抓住onfcontour(f,rnge,“勒夫利主义者”,,,,logspace(-1,1)) plot(sol.x,sol.y,'ro',,,,'行宽',2)图(sol2.x,sol2.y,'ko',,,,'行宽',,,,2)legend('约束',,,,“ f轮廓”,,,,“全球解决方案”,,,,“本地解决方案”,,,,'Location',,,,'东北');抓住离开
解决方案万博 尤文图斯在非线性约束边界上。轮廓图表明,这些是唯一的局部最小值。该图还表明,在[–2,3/2]附近有一个固定点,以及[–2,0]和[–1,4]附近的局部最大值。
使用目标功能使用Fcn2optimexpr
对于某些目标功能或软件版本,您必须通过使用非线性功能将非线性功能转换为优化表达式Fcn2optimexpr
。See万博1manbetx优化变量和表达式的支持操作and将非线性函数转换为优化表达式。通过X
andy
变量Fcn2optimexpr
call to indicate which optimization variable corresponds to eachobjfunx
输入。
OBJ=Fcn2optimexpr(@objfunx,x,y);
与OBJ
就像以前一样作为目标函数。
prob = optimproblem('客观的',obj);
The remainder of the solution process is identical.
版权2018 - 2020年Mathworks,Inc。