Problem regarding fmincon solver
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi,
I am trying to learn fmincon. I am trying to run the following example code for the first time. But, failed to run it. Getting error.
function [c,ceq] = nlcon (w)
c1 = 12*w(1) + 0*w(2) + w(5)*w(3) - 7*w(4) - 0.5*w(4);
c2 = - 0.5*w(4) - 12*w(1) - 0*w(2) - w(5)*w(3) + 7*w(4);
c = [c1;c2]
ceq = w(1) + w(2) + w(3) - w(4);
end
objective = @(w) w(1) + w(2) + 2*w(3) + 0*w(4) + 0*w(5);
x0 = [0.0,0.0,0.0,0.0,0.0];
disp(["Initial objective:" num2str(objective(x0))])
[c,ceq] = nlcon(x0)
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0.0;0.0;0.0;2.0;1.0];
ub = [4.0;4.0;3.0;4.0;12.0];
nonlincon = @nlcon;
[x,fval] = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,nonlincon)
But getting the error as follows:
܀error: Initial parameters violate constraints.
error: called from
__lm_feasible__ at line 92 column 5
fmincon at line 417 column 20
main at line 12 column 9
Could you please help me to solve this issue? Looking forward to your reply.
採用された回答
Walter Roberson
2020 年 8 月 14 日
lb = [0.0;0.0;0.0;2.0;1.0];
ub = [4.0;4.0;3.0;4.0;12.0];
x0 = [0.0,0.0,0.0,0.0,0.0];
lb is lower bounds. Your second-last entry in lb is 2.0, so the second last entry in x0 must be at least 2.0, as in
x0 = [0.0,0.0,0.0,2.0,1.0];
20 件のコメント
Sudip Poddar
2020 年 8 月 14 日
Thank you. I changed it. But, still getting same error:
error: Initial parameters violate constraints.
error: called from
__lm_feasible__ at line 92 column 5
fmincon at line 417 column 20
main at line 11 column 9
Walter Roberson
2020 年 8 月 14 日
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0.0;0.0;0.0;2.0;1.0];
ub = [4.0;4.0;3.0;4.0;12.0];
%x0 = [0.0,0.0,0.0,0.0,0.0];
x0 = lb;
objective = @(w) w(1) + w(2) + 2*w(3) + 0*w(4) + 0*w(5);
disp(["Initial objective:" num2str(objective(x0))])
[c,ceq] = nlcon(x0)
nonlincon = @nlcon;
[x,fval] = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,nonlincon)
function [c,ceq] = nlcon (w)
c1 = 12*w(1) + 0*w(2) + w(5)*w(3) - 7*w(4) - 0.5*w(4);
c2 = - 0.5*w(4) - 12*w(1) - 0*w(2) - w(5)*w(3) + 7*w(4);
c = [c1;c2]
ceq = w(1) + w(2) + w(3) - w(4);
end
Matt J
2020 年 8 月 14 日
Since the equality constraints are linear, I would recommend moving them to Aeq,beq.
A = [];
b = [];
Aeq = [ 1 1 1 -1 0];
beq = 0;
lb = [0.0;0.0;0.0;2.0;1.0];
ub = [4.0;4.0;3.0;4.0;12.0];
%x0 = [0.0,0.0,0.0,0.0,0.0];
x0 = lb;
objective = @(w) w(1) + w(2) + 2*w(3) + 0*w(4) + 0*w(5);
disp(["Initial objective:" num2str(objective(x0))])
[c,ceq] = nlcon(x0)
nonlincon = @nlcon;
[x,fval] = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,nonlincon)
function [c,ceq] = nlcon (w)
c1 = 12*w(1) + 0*w(2) + w(5)*w(3) - 7*w(4) - 0.5*w(4);
c2 = - 0.5*w(4) - 12*w(1) - 0*w(2) - w(5)*w(3) + 7*w(4);
c = [c1;c2];
ceq = [];
end
Sudip Poddar
2020 年 8 月 14 日
Many thanks. I changed again. But, receiving the same error:
ઑerror: Initial parameters violate constraints.
error: called from
__lm_feasible__ at line 92 column 5
fmincon at line 417 column 20
Walter Roberson
2020 年 8 月 14 日
What release are you using? These days, the checks on initial point are closer to line 490
Sudip Poddar
2020 年 8 月 14 日
編集済み: Walter Roberson
2020 年 8 月 14 日
Hi Matt,
I did it. But getting the following error:
error: linear equality constraints: wrong dimensions
error: called from
__linear_constraint_dimensions__ at line 34 column 5
fmincon at line 284 column 5
Hi Walter,
I am using MATLAB R2020a.
Walter Roberson
2020 年 8 月 14 日
In R2020a, line 284 of fmincon is
verbosity = 2;
and column numbers are not mentioned in error messages except for the case of invalid characters such as ` appearing in the code.
I believe you are using Octave, not MATLAB.
Sudip Poddar
2020 年 8 月 14 日
Hi Walter,
Sorry. I thoght that fmincon is same in both Matlab and Octave. Thats why I told you that I am using Matlab. My apologies. Is there any difference of fmincon solver in octave and Matlab? I dont know about it.
Walter Roberson
2020 年 8 月 14 日
I do not know how fmincon works in Octave.
Octave is not just a copy of MATLAB: Octave is a work-alike that copied many parts of the library but was written from scratch. It just has to more-or-less follow the documentation for the MATLAB version but can have very different algorithms and very different bugs.
Sudip Poddar
2020 年 8 月 14 日
But, is it also giving error in Matlab?
Walter Roberson
2020 年 8 月 14 日
For the code I posted, the result is
>> violate_constraints
"Initial objective:" "0"
c =
-15
13
ceq =
-2
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>
x =
1.15886428965752
0.841135710333056
2.0004820803248e-08
2.00000001999577
2.19458332033105
fval =
2.00000004000022
Sudip Poddar
2020 年 8 月 14 日
Thanks Water for showing my mistake. Could you please let me know which version of Matlab you are using?
Sudip Poddar
2020 年 8 月 14 日
Another thing, is there any way of forcing some variables as integer and few other vearibale as real?
Walter Roberson
2020 年 8 月 14 日
R2020a.
When you use fmincon() it is not possible to force integer constraints. To use integer constraints, you generally need ga() .
Your objective itself is fairly simple and could be handled by https://www.mathworks.com/help/optim/ug/intlinprog.html intlinprog -- but intlinprog does not handle nonlinear constraints, so you cannot use that.
ga() handles integer constraints and nonlinear constraints. However, when you use integer constraints, it can only handle nonlinear inequality constraints and not nonlinear equality constaints, which is a problem for you as you have nonlinear equality and nonlinear inequality. However, if Matt J is correct about rewriting your nonlinear equality constraints, you should be able to get rid of those, in which case you would be able to use ga()
Your objective itself is fairly simple and could be handled by intlinprog -- but intlinprog does not handle nonlinear constraints, so you cannot use that.
Except that notice that if w(3) or w(5) is fixed, the whole problem becomes a linear program. So, for example, if w(3) is supposed to be constrained to a finite set of integer values 1,...N, you could conceivably loop over N sub-problems corresponding to each possible value of w(3). Each sub-problem could be solved with intlinprog.
Sudip Poddar
2020 年 8 月 14 日
Hi Walter,
Many many thanks to you for your detailed explanation. I will try to use ga(). Could you please let me know the full name of ga. Is it genetic algorithm?
Hi Matt,
Many many thanks to you also. I agree with your views completely. I will check it also. Thanks.
Matt J
2020 年 8 月 14 日
But note that intlinprog, when you can apply it, is generally more reliable than ga. So, if w(3) or w(5) are finite, discrete variables, then you will more reliably find the global minimum using the deomposition strategy that I mentioned.
Walter Roberson
2020 年 8 月 14 日
ga() is not reliable for finding global minima.
Sudip Poddar
2020 年 8 月 14 日
Hi Walter,
Thanks a lot for providing the link. I will check ga. Thanks again.
Hi Matt,
I agree with you. I will check it also. Many thanks to you also.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Choose a Solver についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
