이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
What happens if the termination condition is satisfied before the constraint in matlab's fmincon?
조회 수: 6 (최근 30일)
이전 댓글 표시
재현
2025년 2월 14일
I'm doing optimization using fmincon.
I used ObjectiveLimit to terminate the optimization when the size of the objective function is less than 0.2,
As I was writing the code, it seems that if the nonlinear constraints are satisfied, the termination condition is also satisfied.
However, when I run the code, the optimization is performed several times and then the code terminates.
I don't understand this part.
Doesn't fmincon perform optimization for cases that satisfy the constraints?
What happens if the termination condition is satisfied before the constraint in matlab's fmincon?
Below is the output when optimization is finished.
fmincon stopped because the objective function value 6.018404e-04 is less than options.ObjectiveLimit = 2.000000e-01 and the relative maximum constraint violation 2.039171e-13 is less than options.ConstraintTolerance = 1.000000e-06
Thank you.
댓글 수: 2
채택된 답변
Matt J
2025년 2월 15일
편집: Matt J
2025년 2월 15일
The only time fmincon will stop without satisfying the constraints (within ConstraintTolerance) is if the MaxItertions or MaxFunctionEvaluations limits are exceeded.
댓글 수: 27
Torsten
2025년 2월 15일
As I was writing the code, it seems that if the nonlinear constraints are satisfied, the termination condition is also satisfied.
However, when I run the code, the optimization is performed several times and then the code terminates.
As far as I understand, OP thinks that constraints and termination condition are satisfied right at the start, but nonetheless, "fmincon" starts iterating.
But the following "fmincon" behaviour may mean that constraints and objective function are only evaluated - I don't know:
However, when I run the code, the optimization is performed several times and then the code terminates.
재현
2025년 2월 16일
Thank you for your reply.
However, I made the other termination conditions except "ObjectiveLimit" very large so that they would not be triggered.
options = optimoptions('fmincon', 'Display', 'iter-detailed', 'Algorithm', 'sqp', ...
'ObjectiveLimit', (Tolerance^2)*2, 'MaxFunctionEvaluations', 1e10, 'MaxIterations', 1e5, 'OptimalityTolerance', 1e-100, 'FunctionTolerance', 1e-100, 'StepTolerance', 1e-100);
I still did not understand, so I sent a contact email to the developer. :*(
Torsten
2025년 2월 16일
This is not large, it is unreasonably small:
'OptimalityTolerance', 1e-100, 'FunctionTolerance', 1e-100, 'StepTolerance', 1e-100
재현
2025년 2월 16일
Sorry, I misspoke.
I made the conditions small so that the optimization would not terminate because of those conditions.
and made the optimization end with objectiveLimit
Matt J
2025년 2월 16일
편집: Matt J
2025년 2월 16일
However, I made the other termination conditions except "ObjectiveLimit" very large so that they would not be triggered.
And that is what the exit message you showed us confirms. The ObjectiveLimit criterion did stop the optimization. Soif that was what you were trying to do, you succeeded, and I don't know what you see as a problem.
Is it that you think the constraints are not satisified upon termination? They are satisfied, according to the exit message.
재현
2025년 2월 16일
Iterations end in about 20 times.
The time taken is about 1 second.
In my code, if the nonlinear constraint is satisfied, the termination condition (objectLimit) should be satisfied.
However, I am curious as to why several repetitions are performed because the termination condition is not satisfied in the early part of the iteration.
Thank you for your hard work.
재현
2025년 2월 16일
What I'm curious about is whether fmincon performs optimization at a solution that satisfies nonlinear constraints.
I guess that fmincon performs optimization at a solution that satisfies nonlinear constraints.
And in my code, if the nonlinear constraints are satisfied, the termination condition(objectiveLimit) is also satisfied.
However, when I run the code and look at the display, it does not satisfy the termination condition and after a few iterations, it satisfies the termination condition and terminates.
To summarize, I want to know whether fmincon performs optimization at a solution that satisfies nonlinear constraints.
Torsten
2025년 2월 16일
Before calling "fmincon", call your objective function and your constraint function with your initial guesses for the solution variables. Are the constraints satisfied ? Is your objective function below "objectiveLimit" ?
Matt J
2025년 2월 16일
편집: Matt J
2025년 2월 16일
And in my code, if the nonlinear constraints are satisfied, the termination condition(objectiveLimit) is also satisfied.
It is not enough that the nonlinear constraints be satisfied for the optimization to stop. The linear ones need to be satisfied as well.
But if you have an initial point that satisfies all the contraints and the objective limit, the optimization should not progress, as the following example shows:
nonlcon=@(x) deal(x.^2-1,[]);
opts = optimoptions('fmincon' , ObjectiveLimit=1,Display='iter');
x=fmincon(@(x)x,0.5, [],[],[],[],[],[],nonlcon,opts )
First-order Norm of
Iter F-count f(x) Feasibility optimality step
0 2 5.000000e-01 0.000e+00 1.000e+00
Problem appears unbounded.
fmincon stopped because the objective function value is less than
the value of the objective function limit and constraints
are satisfied to within the value of the constraint tolerance.
x = 0.5000
Conversely, here is a case where the nonlinear constraints are satisfied, but not the linear ones. So, the optimziation can continue,
x=fmincon(@(x)x,-0.5, -1,0,[],[],[],[],nonlcon,opts )
First-order Norm of
Iter F-count f(x) Feasibility optimality step
0 2 -5.000000e-01 5.000e-01 4.351e-01
1 5 -2.289291e-01 2.289e-01 1.992e-01 2.711e-01
2 7 9.409174e-02 0.000e+00 9.409e-02 3.230e-01
Problem appears unbounded.
fmincon stopped because the objective function value is less than
the value of the objective function limit and constraints
are satisfied to within the value of the constraint tolerance.
x = 0.0941
Walter Roberson
2025년 2월 16일
How many variables are you optimizing over? fmincon() typically starts by evaluating at a series of points, with there being (N+1) evaluations for N variables. (One evaluation is at the original point; the other evaluations are at the original point plus a delta at each component in turn.)
재현
2025년 2월 17일
이동: Stephen23
2025년 2월 17일
I can't check the results right now because I'm running other code.
However, I guess the initial conditions will not satisfy the constraints.
My code only has lb, ub, and nonlinear constraints.
[x_opt, fval] = fmincon(@(x) objectiveFunction(x, GN2_T_in, GW_T_in, GN2_MFR, GN2_Total_MFR, GW_MFR, GW_Total_MFR, GN2_D, GN2_Dh, GW_D, GW_Dh, GN2_Area, GW_Area, L_s, number_of_segments), ...
initial_guess, [], [], [], [], lb, ub, ...
@(x) constraintFunction(x, GN2_T_in, GW_T_in, GN2_MFR, GN2_Total_MFR, GW_MFR, GW_Total_MFR, GN2_D, GN2_Dh, GW_D, GW_Dh, GN2_Area, GW_Area, L_s, number_of_segments, Tolerance), options);
The number of variables in my code is not fixed.
I ran from as few as 2 to as many as 200. All of the ones I checked were like this.
Thank you all for your hard work. I sincerely appreciate it.
Additionally, I'm currently in conversation with the developer for a response.
Torsten
2025년 2월 17일
I can't check the results right now because I'm running other code.
However, I guess the initial conditions will not satisfy the constraints.
Then it will first iterate in order to satisfy the constaints.
재현
2025년 2월 17일
Here is a part of my code:
F_1, F_2, F_3 are energy balance equation, the true value should be 0.
But considering the tolerance, I allowed up to 1.
for i = 1:1:number_of_segments
F_1(i) = f_1(i) - f_3(i); % = 0
F_2(i) = f_2(i) - f_3(i); % = 0
F_3(i) = f_1(i) - f_2(i); % = 0
end
% Inequality constraint
c = [abs(F_1) - 1 ; abs(F_2) - 1; abs(F_3) - 1];
And here is objective function "F":
F = 0;
for i = 1:1:number_of_segments
F = F + F_1(i)^2 + F_2(i)^2 + F_3(i)^2;
end
So, F wil have a small value. but as I run my code, Fval has very large value.
and The termination condition is satisfied in the 8th iteration, but the iteration does not end.
(ObjectiveLimit is 2)

fmincon stopped because the objective function value 7.267704e-01 is less than options.ObjectiveLimit = 2.000000e+00 and the relative maximum constraint violation 0.000000e+00 is less than options.ConstraintTolerance = 1.000000e-06
Matt J
2025년 2월 17일
편집: Matt J
2025년 2월 17일
Fval < ObjectiveLimit = 2 is satisfied in iteration 11 - i.e. when "fmincon" terminates.
@재현 The output display also shows, in the Feasibility column, that your constraints aren't remotely satisfied, until about iteration 9 or 10.
My code only has lb, ub, and nonlinear constraints.
As I said before all constraints, including lb,ub must be satisfied in order for the ObjectiveLimit to terminate the iterations.
Also, keep in mind that if you are using fmincon's interior point algorithm, the initial x0 you give will not be used unless it satisfies the bounds. If x0 does not satisfy the bounds, fmincon will replace it with an alternative point.
Walter Roberson
2025년 2월 17일
Not sure why you did not code
F = 0;
for i = 1:1:number_of_segments
F = F + F_1(i)^2 + F_2(i)^2 + F_3(i)^2;
end
as simply
F = sum(F_1.^2 + F_2.^2 + F_3.^2);
?
재현
2025년 2월 18일
I should have sent you another example, but I was mistaken.
The picture above is when there is 1 segment, that is, 2 variables.
In this case, you can see that it ends normally.
However, when there are 3 segments, that is, 6 variables,
the iteration does not end even when Fval < objectiveLimit(iter 13), as shown in the picture below.

"As I said before all constraints, including lb,ub must be satisfied in order for the ObjectiveLimit to terminate the iterations. "
I understand that part. But I still don't know if fmincon optimizes on solutions that satisfy constraints.
(In the early part of the iteration, F values that cannot be obtained if the constraints are satisfied are obtained.)
Algorithm i used is sqp, not interior point.
I used that method to see it intuitively.
"F = sum(F_1.^2 + F_2.^2 + F_3.^2);"
Are there any benefits to doing this?
Matt J
2025년 2월 18일
편집: Matt J
2025년 2월 18일
I understand that part. But I still don't know if fmincon optimizes on solutions that satisfy constraints.(In the early part of the iteration, F values that cannot be obtained if the constraints are satisfied are obtained.)
No, the individual iterations are not required to satisfy any constraints except (by default) the bounds. Even if you initialize with a point that satisfies all nonlinear constraints, the iterations may transit into the space of nonfeasible solutions. Although, when this happens, fmincon tries to push them back toward the feasible set as the algorithm converges.
재현
2025년 2월 18일
편집: 재현
2025년 2월 18일
iterations may transit into the space of nonfeasible solutions.
Thank you so much, I really respect you very much. Another way to say the answer I want to know.
I wanted to know whether fmincon only optimizes on feasible solutions (If this were not, fmincon would optimize even on nonfeasible solutions).
We've come a long way because of my poor explanation.
But there's one thing I'm curious about your comment.
the individual iterations are not required to satisfy any constraints except (by default) the bounds.
Do bounds refer to lb, ub?
I ran the code, and the optimization went well even when I gave the initial value outside the range of lb, ub.
So is it okay if bounds are not satisfied?
Thank you.
Matt J
2025년 2월 18일
Thank you so much
You are quite welcome, but please Accept-click the answer if you consider the question resolved.
Do bounds refer to lb, ub? I ran the code, and the optimization went well even when I gave the initial value outside the range of lb, ub.
Yes, certain fmincon algorithms (interior-point and sqp) are guaranteed to satisfy the lb,ub bounds at all iterations, which you can read about here,
Also, in the case of the interior-point algorithm, you can turn off this behavior with the HonorBounds optimoption,
However, none of the fmincon algorithms can guarantee if/when the nonlinear or the general linear constraints (A,b,Aeq,beq) will be satisfied.
재현
2025년 2월 19일
Even if I give an initial value that is outside the boundary lb, ub, it will immediately fall within lb, ub when the first iteration is executed, right?
Nonlinear constraints may not be like that.
Walter Roberson
2025년 2월 19일
Are there any benefits to doing this?
Vectorized code is typically faster.
F_1 = rand(1,1e6);
F_2 = rand(1,1e6);
F_3 = rand(1,1e6);
t1 = timeit(@()ByLoop(F_1, F_2, F_3), 1)
t1 = 0.0037
t2 = timeit(@()ByVector(F_1, F_2, F_3), 1)
t2 = 2.6420e-04
t1/t2
ans = 13.9356
function F = ByLoop(F_1, F_2, F_3)
number_of_segments = numel(F_1);
F = 0;
for i = 1:1:number_of_segments
F = F + F_1(i)^2 + F_2(i)^2 + F_3(i)^2;
end
end
function F = ByVector(F_1, F_2, F_3)
F = sum(F_1.^2 + F_2.^2 + F_3.^2);
end
Walter Roberson
2025년 2월 19일
Even if I give an initial value that is outside the boundary lb, ub, it will immediately fall within lb, ub when the first iteration is executed, right?
If you give initial values outside of lb, ub, then those values will be immediately moved inside the bounds (well, except for the case where the bounds are equal, in which case they will be moved to be the bounds and the corresponding variable will be internally eliminated from movement.)
If you give initial values that are exactly equal to (non-equal) bounds then the exact behaviour depends on which algorithm is active. Some of the algorithms (including the default interior-point) move values that are exactly on the bounds to be interior to the bounds, whereas some of the algorithms leave the values at the bounds.
Matt J
2025년 2월 19일
편집: Matt J
2025년 2월 19일
Even if I give an initial value that is outside the boundary lb, ub, it will immediately fall within lb, ub when the first iteration is executed, right?
When using sqp or interior-point, yes.
Nonlinear constraints may not be like that.
Nonlinear constraints are never like that, nor are linear ones (that aren't bounds).
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- 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
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)