Issues in the constraint functions of the Optimization Problem solving
Show older comments
I want to add more than two constraints which are linear, nonlinear and with both equality and inequality signs. As per the MATLAB documentation the examples shown are mostly of same variables which are written as array Examples.
I am using the following shown constraint for the optimisation. The constraint function is named as "area", this same function is called for the 'nonlcon' (default function for constraints).
#1 The equations commented are also to be considered as Constraints for the Optimisation. Do MATLAB optimisation considers this kind of constraint equations?
#2 How to check whether all constraints are satisfied.
function [c,ceq]= area(x0)
M=5;
x0=[5,8];
M2=m(M,x0(1));
M3=m(M2,x0(2));
M4=m(M3,(x0(1)+x0(2)));
beta1=b(M,(x0(1)));
beta2=b(M2,(x0(2)));
beta3=b(M3,((x0(1)+x0(2))));
% M*sin(beta1)==M2*sin(beta2) & M2*sin(beta2)==M3*sin(beta3)
% (M4/M) <= 0.38
% beta1,beta2,beta3 < 62 degrees (This can be written in terms of Thetas)
% Some more constraints will be added
c=[];
ceq = log(2.4/((2.8*M*(sin(beta1)^2))-0.4)) + log(2.4/((2.8*M2*(sin(beta2)^2))-0.4)) + log(P(x0))
end
Below code shows the use of constraint function 'area' in the optimisation algorithm as "nonlcon".
%Initial Condition
% x0=[5,5] ;
% Lower bounds
lb=[1,1];
% Upper Bounds
ub=[30,40];
%constraint
nonlcon=@area
opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter')
opts1 = optimoptions(opts,'MaxIterations',50,'StepTolerance',1e-9,'ConstraintTolerance',1e-9)
[x,fval,exitflag,output,lambda,grad,hessian]= fmincon(@f,x0,[],[],[],[],lb,ub,nonlcon,opts1)
The optimisation is stopped between saying the below shown statement and then after enabling Feasibility Mode also the error is shown which is also mentioned in the end.
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.
<stopping criteria details>
Consider enabling the interior point method feasibility mode.
%Initial Condition
x0=[5,5] ;
% Lower bounds
lb=[1,1];
% Upper Bounds
ub=[30,40];
%constraint
nonlcon=@area
opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter', ...
'Algorithm','interior-point','EnableFeasibilityMode',true)
opts1 = optimoptions(opts,'MaxIterations',50,'StepTolerance',1e-9,'ConstraintTolerance',1e-9); % Recommended
[x,fval,exitflag,output,lambda,grad,hessian]= fmincon(@f,x0,[],[],[],[],lb,ub,nonlcon,opts1);
Converged to an infeasible point.
fmincon stopped because it is unable to find a point locally that satisfies
the constraints within the value of the constraint tolerance.
<stopping criteria details>
Even after changing the Constraint Tolerance it is not helping. Tried in the range 1e-9 to 1e-3
Thank you!
4 Comments
Torsten
on 31 Jan 2023
What are m and b in the lines
M2=m(M,x0(1));
M3=m(M2,x0(2));
M4=m(M3,(x0(1)+x0(2)));
beta1=b(M,(x0(1)));
beta2=b(M2,(x0(2)));
beta3=b(M3,((x0(1)+x0(2))));
?
Walter Roberson
on 31 Jan 2023
beta1,beta2,beta3 < 62 degrees
do you need to be using sind()?
Vivek
on 1 Feb 2023
Vivek
on 1 Feb 2023
Answers (2)
Sulaymon Eshkabilov
on 31 Jan 2023
Edited: Sulaymon Eshkabilov
on 31 Jan 2023
0 votes
There should be elementwise operations in ceq formulation if M2, beta1, beta2 and P are vectors.
function [c,ceq]= area(x0)
M=5;
% x0=[5,8];
M2=m(M,x0(1));
M3=m(M2,x0(2));
M4=m(M3,(x0(1)+x0(2)));
beta1=b(M,(x0(1)))*180/pi;
beta2=b(M2,(x0(2)))*180/pi;
beta3=b(M3,((x0(1)+x0(2))))*180/pi;
c(1)=M4-0.38*M;
c(2:4)=[beta1,beta2,beta3]-62;
ceq(1) = log(2.4/((2.8*M*(sin(beta1)^2))-0.4)) + log(2.4/((2.8*M2*(sin(beta2)^2))-0.4)) + log(P(x0));
ceq(2) = M*sin(beta1)-M2*sin(beta2) ;
ceq(3) = M2*sin(beta2)-M3*sin(beta3) ;
end
10 Comments
Vivek
on 1 Feb 2023
Vivek
on 1 Feb 2023
Torsten
on 1 Feb 2023
An inequality constraint
f(x) <= 0
is specified as
c = f
not as
c = f<=0
An equality constraint
g(x)=0
is specified as
ceq = g
not as
ceq = g==0
Vivek
on 1 Feb 2023
Torsten
on 1 Feb 2023
If you convert beta1, beta2 and beta3 in your code to degrees, I'm sure you had had to use
ceq(1) = log(2.4/((2.8*M*(sind(beta1)^2))-0.4)) + log(2.4/((2.8*M2*(sind(beta2)^2))-0.4)) + log(P(x0));
ceq(2) = M*sind(beta1)-M2*sind(beta2) ;
ceq(3) = M2*sind(beta2)-M3*sind(beta3) ;
Vivek
on 1 Feb 2023
Torsten
on 1 Feb 2023
Maybe you could try to take exp of your first equality constraint to avoid taking log of possibly negative expressions.
Vivek
on 2 Feb 2023
Matt J
on 2 Feb 2023
@Vivek I think you need to consider the possibility that your constraints might truly be infeasible. First, however, you should plot the constraint functions over the bounded region lb<=x<=ub and see if they are simultaneously satisfied anywhere. Because you only have two unknowns, this should be tractable. If you discover a region where they are satisfied, you should choose your initial guess x0 to lie in/near that region.
Using exponential function will change the range of all numbers and it cant be used in further calculations.
Taking the exponential of a constraint does not change anything in the range of the parameters.
It doesn't matter whether you set
log(x)-2 = 0
or
exp(log(x)-2) = exp(0)
as a constraint.
Categories
Find more on Choose a Solver in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!