Use Problem-Based Optimize solve the "Or Constraints" problem,which showed in the chapter of "Solver-Based Optimization Problem Setup"

4 次查看(过去 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 ax(1) ≤ b, cx(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
Torsten 2023-3-2
编辑:Torsten 2023-3-2
Put the two functions as nested functions into a third function which you define as the function to be called by the optimizer.
jin yong
jin yong 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
opts = optimoptions('fmincon','Algorithm','interior-point','Display','off');
[xsol,fval,eflag] = solve(prob,x0,'Options',opts)
nested function
function con=optimer(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
Output argument "con" (and possibly others) not assigned a value in the execution with "untitled>optimer" function.
error: optim.problemdef.fcn2optimexpr
error optim.problemdef.fcn2optimexpr
error fcn2optimexpr
reason:
Function evaluation failed while attempting to determine output size. The function might contain an error, or might not be well-defined at the
automatically-chosen point. To specify output size without function evaluation, use 'OutputSize'.

请先登录,再进行评论。

采纳的回答

Torsten
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 = struct with fields:
x: [2×1 double]
fval = 2.4649e-07
eflag =
OptimalSolution
xsol.x
ans = 2×1
0.4998 -0.9996
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 个)

类别

Help CenterFile Exchange 中查找有关 Linear Least Squares 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by