Use Problem-Based Optimize solve the "Or Constraints" problem,which showed in the chapter of "Solver-Based Optimization Problem Setup"
2 次查看(过去 30 天)
显示 更早的评论
How to solve the same problem with Problem-Based Optimize.
This example form https://ww2.mathworks.cn/help/releases/R2022b/optim/ug/or-instead-of-and-constraints.html shows with Solver-Based Optimization,how to How to solve the same problem with Problem-Based Optimize.
- The problem suppose your feasible region is the L-shaped region: x is in the rectangle –1 ≤ x(1) ≤ 1, 0 ≤ x(2) ≤ 1 OR x is in the rectangle 0 ≤ x(1) ≤ 1, –1 ≤ x(2) ≤ 1.
- Code for creating the figure
% Write the x and y coordinates of the figure, clockwise from (0,0)
x = [0,-1,-1,1,1,0,0];
y = [0,0,1,1,-1,-1,0];
plot(x,y)
xlim([-1.2 1.2])
ylim([-1.2 1.2])
axis equal
- To represent a rectangle as a nonlinear constraint, instead of as bound constraints, construct a function that is negative inside the rectangle a ≤ x(1) ≤ b, c ≤ x(2) ≤ d:
function cout = rectconstr(x,a,b,c,d)
% Negative when x is in the rectangle [a,b][c,d]
% First check that a,b,c,d are in the correct order
if (b <= a) || (d <= c)
error('Give a rectangle a < b, c < d')
end
cout = max([(x(1)-b),(x(2)-d),(a-x(1)),(c-x(2))]);
- Following the prescription of using the minimum of nonlinear constraint functions, for the L-shaped region, the nonlinear constraint function is:
function [c,ceq] = rectconstrfcn(x)
ceq = []; % no equality constraint
F(1) = rectconstr(x,-1,1,0,1); % one rectangle
F(2) = rectconstr(x,0,1,-1,1); % another rectangle
c = min(F); % for OR constraints
- Suppose your objective function is
fun = @(x)exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);
- Minimize fun over the L-shaped region:
opts = optimoptions(@fmincon,'Algorithm','interior-point','Display','off');
x0 = [-.5,.6]; % an arbitrary guess
[xsol,fval,eflag] = fmincon(fun,x0,[],[],[],[],[],[],@rectconstrfcn,opts)
- xsol = 0.4998 -0.9996
- fval = 2.4650e-07
- eflag = 1
- Clearly, the solution xsol is inside the L-shaped region. The exit flag is 1, indicating that xsol is a local minimum.
4 个评论
采纳的回答
Torsten
2023-3-2
编辑:Torsten
2023-3-2
clc,clear
prob=optimproblem;
x=optimvar('x',2);
x0.x=[-0.5,0.6];
fun = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);
prob.Objective=fun;
cons1=fcn2optimexpr(@optimer,x);
prob.Constraints=cons1<=0;
opts = optimoptions('fmincon','Algorithm','interior-point','Display','off');
[xsol,fval,eflag] = solve(prob,x0,'Options',opts)
xsol.x
nested function
function con=optimer(x)
con = rectconstrfcn(x);
function c= rectconstrfcn(x)
% ceq = []; % no equality constraint
F(1) = rectconstr(x,-1,1,0,1); % one rectangle
F(2) = rectconstr(x,0,1,-1,1); % another rectangle
c = min(F); % for OR constraints
end
function cout = rectconstr(x,a,b,c,d)
% Negative when x is in the rectangle [a,b][c,d]
% First check that a,b,c,d are in the correct order
if (b <= a) || (d <= c)
error('Give a rectangle a < b, c < d')
end
cout = max([(x(1)-b),(x(2)-d),(a-x(1)),(c-x(2))]);
end
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!