When I use fmincon, the optimised result does not satisfy my non liner constraints
이전 댓글 표시
clear variables
close all
clc
fun = @(x)4*x(1)+x(2);
x0=[0.4,0.28]
lb = [0.01,0.01];
ub = [5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
options = optimoptions('fmincon','Algorithm','sqp');
c = @(x)(4+4*x(1)+9*x(1)^2)*(1+x(1))^2-(2*x(1)^3*x(2)+2+3*x(1)+3*x(1)^2)^2+0.001;
nonlcon = @(x)deal(c(x),[]);
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
exitflag
checkinitialpoint=(4+4*x0(1)+9*x0(1)^2)*(1+x0(1))^2-(2*x0(1)^3*x0(2)+2+3*x0(1)+3*x0(1)^2)^2+0.001;
checkconstraits=(4+4*x(1)+9*x(1)^2)*(1+x(1))^2-(2*x(1)^3*x(2)+2+3*x(1)+3*x(1)^2)^2+0.001;
The above is my matlab code, I input the nonlear constraints, but the results give to me is obviously not satisfy the constraints (you can see that checkconstraits is positive), my initial point is within the range.
Can anyone help me? Many thanks.
댓글 수: 1
Torsten
2023년 8월 29일
The solver converged to an infeasible point (see above). Your observation is the same as the exitflag from "fmincon" indicates.
답변 (2개)
You would do better to use the default 'interior-point' algorithm, which arrives at a feasible solution.
fun = @(x)4*x(1)+x(2);
x0=[0.4,0.28];
lb = [0.01,0.01];
ub = [5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
% options = optimoptions('fmincon','Algorithm','sqp');
c = @(x)(4+4*x(1)+9*x(1)^2)*(1+x(1))^2-(2*x(1)^3*x(2)+2+3*x(1)+3*x(1)^2)^2+0.001;
nonlcon = @(x)deal(c(x),[]);
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Alan Weiss
MATLAB mathematical toolbox documentation
Since it is a 2D problem, it practically begs you to pre-sweep for a good initial guess:
fun = @(x)4*x(1)+x(2);
lb = [0.01,0.01];
ub = [5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
c = @(x)(4+4*x(1)+9*x(1)^2)*(1+x(1))^2-(2*x(1)^3*x(2)+2+3*x(1)+3*x(1)^2)^2+0.001;
nonlcon = @(x)deal(c(x),[]);
[x0,fval0]=sweep(fun,c,lb,ub)
options = optimoptions('fmincon','Algorithm','sqp');
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
x,fval
function [x0,fval]=sweep(fun,c,lb,ub)
F=@(x1,x2) fun([x1,x2])+eps./(c([x1,x2])<=0);
[X1,X2]=ndgrid(linspace(lb(1),ub(1),30), linspace(lb(2),ub(2),30));
v=arrayfun(F,X1,X2);
[fval,i]=min(v(:));
if ~isfinite(fval)
disp 'No feasible point found'; x0=[];
else
x0=[X1(i),X2(i)];
end
end
댓글 수: 2
Tianshu Gao
2023년 8월 30일
Matt J
2023년 8월 30일
You're welcome, but please Accept-click whichever answer solved your issue.
카테고리
도움말 센터 및 File Exchange에서 Solver-Based Nonlinear Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!