Problem regarding fmincon solver
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
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.
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?
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
