# When I use fmincon, the optimised result does not satisfy my non liner constraints

6 views (last 30 days)
Tianshu Gao on 29 Aug 2023
Commented: Matt J on 30 Aug 2023
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 than the value of the step size tolerance but constraints are not satisfied 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.
Torsten on 29 Aug 2023
The solver converged to an infeasible point (see above). Your observation is the same as the exitflag from "fmincon" indicates.

Alan Weiss on 29 Aug 2023
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]
Alan Weiss
MATLAB mathematical toolbox documentation
Tianshu Gao on 30 Aug 2023
Thank you

Matt J on 29 Aug 2023
Edited: Matt J on 29 Aug 2023
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 than the 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
Tianshu Gao on 30 Aug 2023
Thank you.
Matt J on 30 Aug 2023