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

24 ビュー (過去 30 日間)
Tianshu Gao
Tianshu Gao 2023 年 8 月 29 日
コメント済み: Matt J 2023 年 8 月 30 日
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.
  1 件のコメント
Torsten
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 件)

Alan Weiss
Alan Weiss 2023 年 8 月 29 日
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

Matt J
Matt J 2023 年 8 月 29 日
編集済み: Matt J 2023 年 8 月 29 日
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
  2 件のコメント
Tianshu Gao
Tianshu Gao 2023 年 8 月 30 日
Thank you.
Matt J
Matt J 2023 年 8 月 30 日
You're welcome, but please Accept-click whichever answer solved your issue.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by