17 views (last 30 days)
Show older comments
Tianshu Gao on 29 Aug 2023
-
-
Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/2014216-when-i-use-fmincon-the-optimised-result-does-not-satisfy-my-non-liner-constraints
Commented: Matt J on 30 Aug 2023
Open in MATLAB Online
clear variables
close all
clc
fun = @(x)4*x(1)+x(2);
x0=[0.4,0.28]
x0 = 1×2
0.4000 0.2800
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);
Feasible point with lower objective function value found.Converged to an infeasible point.fmincon stopped because the size of the current step is less thanthe value of the step size tolerance but constraints are notsatisfied to within the value of the constraint tolerance.
exitflag
exitflag = -2
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Torsten on 29 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2014216-when-i-use-fmincon-the-optimised-result-does-not-satisfy-my-non-liner-constraints#comment_2864801
⋮
The solver converged to an infeasible point (see above). Your observation is the same as the exitflag from "fmincon" indicates.
Sign in to comment.
Sign in to answer this question.
Answers (2)
Alan Weiss on 29 Aug 2023
Open in MATLAB Online
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)
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.
x = 1×2
0.1004 0.5349
fval = 0.9363
exitflag = 1
output = struct with fields:
iterations: 51 funcCount: 199 constrviolation: 0 stepsize: 2.4326e-07 algorithm: 'interior-point' firstorderopt: 5.5041e-07 cgiterations: 4 message: '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.↵↵<stopping criteria details>↵↵Optimization completed: The relative first-order optimality measure, 1.376030e-07,↵is less than options.OptimalityTolerance = 1.000000e-06, and the relative maximum constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-06.' bestfeasible: [1×1 struct]
For more information about dealing with infeasible solutions, see Solver Lost Feasibility.
Alan Weiss
MATLAB mathematical toolbox documentation
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Tianshu Gao on 30 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2014216-when-i-use-fmincon-the-optimised-result-does-not-satisfy-my-non-liner-constraints#comment_2865941
Thank you
Sign in to comment.
Matt J on 29 Aug 2023
Edited: Matt J on 29 Aug 2023
Open in MATLAB Online
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)
x0 = 1×2
0.1821 0.4186
fval0 = 1.1469
options = optimoptions('fmincon','Algorithm','sqp');
[x,fval,exitflag,output] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
Local minimum possible. Constraints satisfied.fmincon stopped because the size of the current step is less thanthe value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
x,fval
x = 1×2
0.1004 0.5349
fval = 0.9363
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 Comments Show NoneHide None
Show NoneHide None
Tianshu Gao on 30 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2014216-when-i-use-fmincon-the-optimised-result-does-not-satisfy-my-non-liner-constraints#comment_2865946
Thank you.
Matt J on 30 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2014216-when-i-use-fmincon-the-optimised-result-does-not-satisfy-my-non-liner-constraints#comment_2866401
You're welcome, but please Accept-click whichever answer solved your issue.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationOptimization ToolboxOptimization ResultsSolver Outputs and Iterative Display
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Tags
- fmincon nonlcon
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office